visual heartbit added and reload fixed in explorer transport
[brisk.git] / web / xynt-streaming.js
index 7e61638..dc0043e 100644 (file)
@@ -95,6 +95,7 @@ transport_xhr.prototype = {
     },
 
     ctx_new_getchar: function(idx) { /* public */
+        return (this.ctx_new[idx]);
     },
 
     ctx_old_len_is_set: function () { /* public */
@@ -137,19 +138,18 @@ function transport_htmlfile(doc, xynt_streaming, page)
     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,
 
@@ -170,6 +170,11 @@ transport_htmlfile.prototype = {
         return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
     },
 
+    /* only after a successfull is_initialized call */
+    xstr_is_ready: function () { /* public */
+        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") {
@@ -193,6 +198,8 @@ transport_htmlfile.prototype = {
     },
 
     ctx_new_getchar: function(idx) { /* public */
+       return (this.ifra.contentWindow.ctx_new.charAt(idx));
+
     },
 
     ctx_old_len_is_set: function () { /* public */
@@ -219,7 +226,10 @@ transport_htmlfile.prototype = {
         this.ifra.contentWindow.script_clean = step;
     },
 
-    postproc: function () {
+    postproc: function () { /* public */
+        if (this.stopped && !this.xstr_is_ready()) {
+            this.xynt_streaming.reload();
+        }
     }
 }
 
@@ -236,24 +246,30 @@ function transport_iframe(doc, xynt_streaming, page)
     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 */
         try {
             if (this.ifra != null) {
-                // FIXME: with opera on win this remove child crash js so is
-                //        commented
-                // this.doc.body.removeChild(this.ifra);
+                // 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("catched");
+            alert("destroy exception catched");
         }
     },
 
@@ -295,6 +311,7 @@ transport_iframe.prototype = {
     },
 
     ctx_new_getchar: function(idx) { /* public */
+       return (this.ifra.contentWindow.ctx_new.charAt(idx));
     },
 
     ctx_old_len_is_set: function () { /* public */
@@ -321,7 +338,10 @@ transport_iframe.prototype = {
         this.ifra.contentWindow.script_clean = step;
     },
 
-    postproc: function () {
+    postproc: function () { /* public */
+        if (this.stopped && !this.xstr_is_ready()) {
+            this.xynt_streaming.reload();
+        }
     }
 }
 
@@ -338,7 +358,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;
@@ -357,18 +376,20 @@ 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,
-    hbit:              null,
+    hbit:              function () {},
     keepalive_old:    -1,
     keepalive_new:    -1,
     keepalives_equal:  0,
     /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
-    /* 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,
-    // FIXME watchdog_timeout:  100,
     watchdog_timeout:  100,
     watchdog_ct:       0,
     watchable:         false,
@@ -378,6 +399,9 @@ xynt_streaming.prototype = {
     stream:            "",
     the_end:           false,
 
+    w_stat_old:        "",
+    s_stat_old:        "",
+
     start: function() { /* public */
         if (this.the_end) 
             return;
@@ -415,6 +439,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() {
@@ -426,12 +453,44 @@ xynt_streaming.prototype = {
         this.hbit = hbit;
     },
 
+    hbit_status: function () {
+        if (this.watchdog_hdl)
+            w_stat = "g";
+        else
+            w_stat = "r";
+
+        if (this.transp.ctx_new_is_set() &&
+            this.transp.ctx_new_curlen_get() > 0) {
+            if (this.keepalives_equal == 0) {
+                s_stat = "g";
+            }
+            else if (this.keepalives_equal < this.keepalives_eq_max) {
+                s_stat = "y";
+            }
+            else {
+                s_stat = "r";
+            }
+        }
+        else {
+            s_stat = "r";
+        }
+
+        if (this.s_stat_old != s_stat ||
+            this.w_stat_old != w_stat)
+            this.hbit(s_stat, w_stat);
+        this.s_stat_old = s_stat;
+        this.w_stat_old = w_stat;
+    },
+
     watchdog: function () {
         // alert("watchdog");
         var i, again;
         var comm_newpart, comm_len, comm_arr;
         var ctx_new_len;
 
+        this.watchdog_hdl = null;
+
+        this.hbit_status();
         if (this.sandbox != null) {
             // from old: var zug = "POLL sess = "+sess+" stat = "+stat+" subst = "+subst+" step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new+" STOP: "+this.stopped;
             var zug = "WATCHDOG  sess = ["+this.sess+"]  step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;          
@@ -470,6 +529,8 @@ xynt_streaming.prototype = {
         }
         if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
+
+            // alert("qui "+this.transp.ctx_new_curlen_get()+" "+this.transp.ctx_old_len_get();
             if (this.keepalive_old == this.keepalive_new) {
                 this.keepalives_equal++;
             }
@@ -478,7 +539,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");
@@ -500,9 +561,9 @@ xynt_streaming.prototype = {
             catch(b) {
                break;
             }
-
             // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
             ctx_new_len = this.transp.ctx_new_curlen_get();
+            this.log("new_len: "+ ctx_new_len);
             // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
             if (ctx_new_len <= this.transp.ctx_old_len_get()) {
                 break;
@@ -511,7 +572,8 @@ xynt_streaming.prototype = {
             this.keepalive_new++;
             // alert("pre-loop 1");
             for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
-               // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
+                // alert("ctx_new_getchar: "+this.transp.ctx_new_getchar(i));
+
                if (this.transp.ctx_new_getchar(i) != '_') {
                     // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
                    break;
@@ -577,8 +639,12 @@ xynt_streaming.prototype = {
         } while (again);
         this.watchdog_ct++;
         if (!this.the_end) {
-            this.transp.postproc();
+            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);
+            this.hbit_status();
         }
         // alert("watchdog return normal");