xhr transport added
[brisk.git] / web / xynt-streaming.js
index f703aff..50cea6d 100644 (file)
 // old targetpage == page and moved into start method
 
+//
+// CLASS transport_xhr
+//
+function transport_xhr(doc, xynt_streaming, page)
+{
+    this.doc = doc;
+    this.xynt_streaming = xynt_streaming;
+    this.xhr = createXMLHttpRequest();
+    this.xhr.open('GET', page);
+
+    var self = this;
+    this.xhr.onreadystatechange = function () { self.xhr_cb(); };
+    this.xhr.send(null);
+
+    this.stopped = false;
+}
+
+transport_xhr.prototype = {
+    doc: null,
+    xynt_streaming: "ready",
+    xhr: null,
+    stopped: true,
+
+    ctx_old: "",
+    ctx_old_len: 0,
+    ctx_new: null,
+
+    // script_clean: 0,
+
+    destroy: function () { /* public */
+        if (this.xhr != null) {
+            this.xhr_abort();
+        }
+        delete this.xhr;
+    },
+
+    xhr_cb: function () {
+        var ret;
+
+        if (this.xhr.readyState == 4) {
+            // console.log("SS: "+safestatus(xhr));
+
+            // NOTE: delay management later
+           // try {
+           //     if ((ret = safestatus(this.xhr)) == 200) {
+            //         this.delay = 0;
+            //         // console.log("del a null "+this.delayed);
+           //     } else if (ret != -1) {
+            //         this.delay = 5000;
+            //         this.hbit('X');
+           //         // alert('There was a problem with the request.' + ret);
+           //     }
+           // } catch(b) {};
+
+            // this.delayed = null;
+           this.stopped = true;
+        }
+    },
+
+    xhr_abort: function() {
+        if (this.xhr != null) {
+            this.xhr.abort();
+        }
+    },
+
+    xstr_is_init: function () { /* public */
+       try {
+           if (this.xhr.responseText != null) {
+                this.ctx_new = this.xhr.responseText;
+            }
+       }
+       catch (e) {
+        }
+
+        return (this.ctx_new != null);
+    },
+
+    /* only after a successfull is_initialized call */
+    xstr_is_ready: function () { /* public */
+        return (this.xynt_streaming == "ready");
+    },
+
+    xstr_set: function () { /* public */
+        // already set
+    },
+
+    ctx_new_is_set: function () { /* public */
+        return (this.ctx_new != null);
+    },
+
+    ctx_new_curlen_get: function () { /* public */
+        return (this.ctx_new.length);
+    },
+
+    ctx_new_getchar: function(idx) { /* public */
+        return (this.ctx_new[idx]);
+    },
+
+    ctx_old_len_is_set: function () { /* public */
+        return (true);
+    },
+
+    ctx_old_len_get: function () { /* public */
+        return (this.ctx_old_len);
+    },
+
+    ctx_old_len_set: function (len) { /* public */
+        this.ctx_old_len = len;
+    },
+
+    ctx_old_len_add: function (len) { /* public */
+        this.ctx_old_len += len;
+    },
+
+    new_part: function () { /* public */
+        return (this.ctx_new.substr(this.ctx_old_len));
+    },
+
+    scrcls_set: function (step) { /* public */
+        // this.script_clean = step;
+    },
+
+    postproc: function () {
+        if (this.stopped && !this.xstr_is_ready()) {
+            this.xynt_streaming.reload();
+        }
+    }
+}
+
 //
 // CLASS transport_htmlfile
 //
-function transport_htmlfile(doc, page)
+function transport_htmlfile(doc, xynt_streaming, page)
 {
     this.doc = doc;
+    this.xynt_streaming = xynt_streaming;
     this.transfdoc = new ActiveXObject("htmlfile");
     this.transfdoc.open();
-    /*this.transfdoc.write("<html><head><script>");
-    this.transfdoc.write("document.domain=\""+(doc.domain)+"\";");
-    this.transfdoc.write("</"+"script></"+"head>"); */
     this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
     this.transfdoc.close();
 
     this.ifra = this.transfdoc.getElementById("iframe");
     this.ifra.contentWindow.location.href = page;
+    this.stopped = false;
 }
 
 transport_htmlfile.prototype = {
     doc: null,
+    xynt_streaming: null,
+    stopped: true,
     ifra: null,
     tradoc: null,
 
@@ -45,9 +175,18 @@ transport_htmlfile.prototype = {
         return (this.ifra.contentWindow.xynt_streaming == "ready");
     },
 
-    /* only after a successfull is_ready call to be sure the accessibility of the var */
-    xstr_set: function (xynt_streaming) { /* public */
-        this.ifra.contentWindow.xynt_streaming = xynt_streaming;
+    /* only after a successfull is_initialized call */
+    xstr_set: function () { /* public */
+        if (this.ifra.contentWindow.xynt_streaming == "ready") {
+            this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
+            return (true);
+        }
+        else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
+            return (true);
+        }
+        else {
+            return (false);
+        }
     },
 
     ctx_new_is_set: function () { /* public */
@@ -83,6 +222,12 @@ transport_htmlfile.prototype = {
 
     scrcls_set: function (step) { /* public */
         this.ifra.contentWindow.script_clean = step;
+    },
+
+    postproc: function () { /* public */
+        if (this.stopped && !this.xstr_is_ready()) {
+            this.xynt_streaming.reload();
+        }
     }
 }
 
@@ -91,24 +236,38 @@ transport_htmlfile.prototype = {
 //
 // CLASS transport_iframe
 //
-function transport_iframe(doc, page)
+function transport_iframe(doc, xynt_streaming, page)
 {
     this.doc = doc;
+    this.xynt_streaming = xynt_streaming;
     this.ifra = doc.createElement("iframe");
     this.ifra.style.visibility = "hidden";
     doc.body.appendChild(this.ifra);
     this.ifra.contentWindow.location.href = page;
+    this.stopped = false;
 }
 
 transport_iframe.prototype = {
     doc: null,
+    xynt_streaming: null,
+    stopped: true,
     ifra: null,
 
     destroy: function () { /* public */
-        if (this.ifra != null) {
-            this.doc.body.removeChild(this.ifra);
-            delete this.ifra;
-            this.ifra = null;
+        try {
+            if (this.ifra != null) {
+                // NOTE:  on Opera this remove child crash js if called from
+                //        inside of the iframe, on IE on Windows without
+                //        it stream abort fails.
+                //        the problem is fixed setting into the iframe's onload
+                //        function the stopped attribute to true and delegate
+                //        postproc() fired by xynt_streaming watchdog()
+                this.doc.body.removeChild(this.ifra);
+                delete this.ifra;
+                this.ifra = null;
+            }
+        } catch (b) {
+            alert("destroy exception catched");
         }
     },
 
@@ -121,8 +280,23 @@ transport_iframe.prototype = {
         return (this.ifra.contentWindow.xynt_streaming == "ready");
     },
 
+    /* only after a successfull is_initialized call */
+    xstr_set: function () { /* public */
+        if (this.ifra.contentWindow.xynt_streaming == "ready") {
+            this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
+            return (true);
+        }
+        else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
+            return (true);
+        }
+        else {
+            return (false);
+        }
+    },
+
+
     /* only after a successfull is_ready call to be sure the accessibility of the var */
-    xstr_set: function (xynt_streaming) { /* public */
+    xstr_set_old: function (xynt_streaming) { /* public */
         this.ifra.contentWindow.xynt_streaming = xynt_streaming;
     },
 
@@ -159,6 +333,12 @@ transport_iframe.prototype = {
 
     scrcls_set: function (step) { /* public */
         this.ifra.contentWindow.script_clean = step;
+    },
+
+    postproc: function () { /* public */
+        if (this.stopped && !this.xstr_is_ready()) {
+            this.xynt_streaming.reload();
+        }
     }
 }
 
@@ -175,7 +355,6 @@ function xynt_streaming(win, transp_type, console, gst, from, cookiename, sess,
     this.page = page;
     this.cmdproc = cmdproc;
     // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
-
     this.doc = win.document;
     this.keepalive_old = -1;
     this.keepalive_new = -1;
@@ -194,6 +373,9 @@ xynt_streaming.prototype = {
     page:              null,
     cmdproc:           null,
 
+    start_time:        0,
+    restart_wait:      5000, // wait restart_wait millisec before begin to check if restart is needed
+
     doc:               null,
     cookiepath: "/brisk/",
     watchdog_hdl:      null,
@@ -202,9 +384,8 @@ xynt_streaming.prototype = {
     keepalive_new:    -1,
     keepalives_equal:  0,
     /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
-    // FIXME watchdog_timeout:  200,
-    /* restart after  3 * 40 * 100 millisec if server ping is missing => 12secs */
-    keepalives_eq_max: 3,
+    /* restart after  4 * 40 * 100 millisec if server ping is missing => 16secs */
+    keepalives_eq_max: 4,
     watchdog_checktm:  40,
     watchdog_timeout:  100,
     watchdog_ct:       0,
@@ -232,11 +413,17 @@ xynt_streaming.prototype = {
         this.log(this.page);
 
         // transport instantiation
-        if (this.transp_type == "iframe") {
-            this.transp = new transport_iframe(this.doc, this.page);
+        if (this.transp_type == "xhr") {
+            this.page = url_append_args(this.page, "transp", "xhr");
+            this.transp = new transport_xhr(this.doc, this, this.page);
+        }
+        else if (this.transp_type == "iframe") {
+            this.page = url_append_args(this.page, "transp", "iframe");
+            this.transp = new transport_iframe(this.doc, this, this.page);
         }
         else if (this.transp_type == "htmlfile") {
-            this.transp = new transport_htmlfile(this.doc, this.page);
+            this.page = url_append_args(this.page, "transp", "htmlfile");
+            this.transp = new transport_htmlfile(this.doc, this, this.page);
         }
         else
             return;
@@ -246,6 +433,9 @@ xynt_streaming.prototype = {
         if (!this.the_end) {
             this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
         }
+
+        var date = new Date();
+        this.start_time = date.getTime();
     },
 
     stop: function() {
@@ -291,9 +481,8 @@ xynt_streaming.prototype = {
                   for a well know variable value before assign this object value to it (OO is a passion)
                 */
                 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
-                if (this.transp.xstr_is_ready()) {
+                if (this.transp.xstr_set()) {
                     // this.ifra.contentWindow.xynt_streaming = this;
-                    this.transp.xstr_set(this);
                     this.watchable = true;
                     this.watchdog_ct = 0;
                     this.log("hs::watchdog: watchable = yes");
@@ -310,7 +499,7 @@ xynt_streaming.prototype = {
                 this.keepalives_equal = 0;
             }
             
-            if (this.keepalives_equal > this.keepalives_eq_max) {
+            if (this.keepalives_equal >= this.keepalives_eq_max) {
                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
                 this.reload();
                 // alert("watchdog return reload");
@@ -332,7 +521,7 @@ xynt_streaming.prototype = {
             catch(b) {
                break;
             }
-            
+
             // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
             ctx_new_len = this.transp.ctx_new_curlen_get();
             // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
@@ -409,6 +598,10 @@ xynt_streaming.prototype = {
         } while (again);
         this.watchdog_ct++;
         if (!this.the_end) {
+            var date = new Date();
+            if (date.getTime() > (this.start_time + this.restart_wait)) {
+                this.transp.postproc();
+            }
             this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
         }
         // alert("watchdog return normal");