heartbit graphical feedback added
[brisk.git] / web / xynt-streaming.js
index fe14f24..11f4f22 100644 (file)
@@ -138,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,
 
@@ -171,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") {
@@ -194,6 +198,7 @@ transport_htmlfile.prototype = {
     },
 
     ctx_new_getchar: function(idx) { /* public */
+        return (this.ifra.contentWindow.ctx_new.charAt(idx));
     },
 
     ctx_old_len_is_set: function () { /* public */
@@ -220,7 +225,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();
+        }
     }
 }
 
@@ -237,18 +245,24 @@ 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 AND NOWWW ????
+                // 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;
@@ -296,6 +310,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 */
@@ -322,7 +337,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();
+        }
     }
 }
 
@@ -339,10 +357,15 @@ 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;
+
+    this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
+    this.mon_wrntime = this.mon_errtime / 2;
+
+    this.mon_update();
+
 }
 
 xynt_streaming.prototype = {
@@ -358,10 +381,13 @@ 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,
@@ -369,7 +395,6 @@ xynt_streaming.prototype = {
     /* 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,
@@ -379,6 +404,43 @@ xynt_streaming.prototype = {
     stream:            "",
     the_end:           false,
 
+    mon_time:         -1,
+    mon_wrntime:       0,
+    mon_errtime:       0,
+
+    mon_stat_old:      "",
+
+    mon_update: function()
+    {
+        var date = new Date();
+
+        this.mon_time = date.getTime();
+    },
+
+    /*
+      ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
+
+      return values: 0 unknown
+                     1 ok
+                     2 warning
+                     3 error
+     */
+    mon_status: function()
+    {
+        var delta, date;
+
+        var date = new Date();
+
+        delta = date.getTime() - this.mon_time;
+
+        if (delta < this.mon_wrntime)
+            return 1;
+        else if (delta < this.mon_errtime)
+            return 2;
+        else
+            return 3;
+    },
+
     start: function() { /* public */
         if (this.the_end) 
             return;
@@ -416,6 +478,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() {
@@ -427,6 +492,34 @@ xynt_streaming.prototype = {
         this.hbit = hbit;
     },
 
+
+    hbit_status: function () {
+        var ret;
+
+        ret = this.mon_status();
+        // console.log("mon_status: "+ret+" 0: "+this.mon_time);
+        switch (ret) {
+        case 0:
+            mon_stat = "b";
+            break;
+        case 1:
+            mon_stat = "g";
+            break;
+        case 2:
+            mon_stat = "y";
+            break;
+        case 3:
+            mon_stat = "r";
+            break;
+        }
+
+        if (this.mon_stat_old != mon_stat) {
+            this.hbit(mon_stat);
+            this.mon_stat_old = mon_stat;
+        }
+    },
+
+
     watchdog: function () {
         // alert("watchdog");
         var i, again;
@@ -483,6 +576,7 @@ xynt_streaming.prototype = {
                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
                 this.reload();
                 // alert("watchdog return reload");
+                this.hbit_status();
                 return;
             }
         }
@@ -517,6 +611,9 @@ xynt_streaming.prototype = {
                     // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
                    break;
                 }
+                this.mon_update();
+                this.hbit_status();
+
                 // else {
                 //     this.log("ctx_new.charAt(i) == '_'");
                 // }
@@ -546,6 +643,8 @@ xynt_streaming.prototype = {
                    comm_len += comm_arr[i].length;
                }
                again = 1;
+                this.mon_update();
+                this.hbit_status();
            }
             // this.ifra.contentWindow.ctx_old_len += comm_len;
             this.transp.ctx_old_len_add(comm_len);
@@ -578,8 +677,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");