ctx_new moved inside iframe to avoid inconsistencies, stream with commands added
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Wed, 11 Apr 2012 06:16:39 +0000 (08:16 +0200)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Wed, 11 Apr 2012 06:16:39 +0000 (08:16 +0200)
web/test-hs-rd.php
web/test-hs.php
web/xynt/xynt-http-streaming-ifra.js [new file with mode: 0644]
web/xynt/xynt-http-streaming.js

index bfa7544..9edd50b 100644 (file)
@@ -1,8 +1,11 @@
 <html>
 <head>
 <script type="text/javascript" src="xynt/xynt-console.js"></script>
+<script type="text/javascript" src="xynt/xynt-http-streaming-ifra.js"></script>
 <script type="text/javascript">
 var http_streaming = "ready";
+var ctx_new = "";
+
 window.onload = function () { if (http_streaming != "ready") http_streaming.reload(); };
 </script> 
 <?php
@@ -16,20 +19,20 @@ function main()
 {
     $s = "";
     for ($i = 0 ; $i < 4096 ; $i++) {
-        $s .= rand(0,255);
+        $s .= rand(65,90);
     }
-    for ($i = 0 ; $i < 20 ; $i++) {
-        
-        if ($i != 18) {
+    for ($i = 0 ; $i < 15 ; $i++) {
+        // if ($i == 0)
+        //    sleep(1);
+     
+        if ($i != 8) {
             printf("<script id='hs%d' type='text/javascript'>
-if (http_streaming != 'ready') {
-    http_streaming.keepalive();
-}
+push(\"$('cont').innerHTML = '%d';\");
 </script>", $i, $i);
         }
         else {
             // exit();
-            sleep(30);
+            // sleep(30);
         }
         if ($i == 0)
             printf("<!-- %s -->", $s);
index 8d9082d..6804a99 100644 (file)
@@ -8,9 +8,9 @@ var allmythings="BEGIN";
 
 window.onload = function () {
     var hs;
-    console.log("zugu");
     hs = new http_streaming(window, console);
-    hs.start('test-hs-rd.php');
+    // FIXME: with relative path not work!
+    hs.start('http://dodo.birds.lan/xynt/test-hs-rd.php');
 }
     
 //-->
diff --git a/web/xynt/xynt-http-streaming-ifra.js b/web/xynt/xynt-http-streaming-ifra.js
new file mode 100644 (file)
index 0000000..a854c05
--- /dev/null
@@ -0,0 +1,8 @@
+function push(s) {
+    if (s != null) {
+        ctx_new += "@BEGIN@"+s+"@END@";
+    }
+    else {
+        ctx_new += "_";
+    }
+}
index 3988d1d..d671d8f 100644 (file)
+function globst() {
+    this.st = -1;
+    this.st_loc = -1;
+    this.st_loc_new = -1;
+    this.comms  = new Array;
+}
+
+globst.prototype = {
+    st: -1,
+    st_loc: -1,
+    st_loc_new: -1,
+    comms: null,
+    sleep_hd: null,
+
+    sleep: function(delay) {
+        st.st_loc_new++;
+
+        this.sleep_hdl = setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; obj.sleep_hdl = null; }},
+                                  delay, this);
+    },
+
+    abort: function() {
+        if (this.sleep_hdl != null) {
+            clearTimeout(this.sleep_hdl);
+            this.sleep_hdl = null;
+        }
+    }
+}
+
 function http_streaming(win, cons)
 {
     this.console = cons;
+    this.win = win;
     this.doc = win.document;
     this.keepalive_old = -1;
     this.keepalive_new = -1;
-    this.keepalives_equal = 0;
-    this.ifra = this.doc.createElement("iframe");
-    this.ifra.style.visibility = "hidden";
-    this.doc.body.appendChild(this.ifra);
+    this.gst = new globst();
+    this.cmdproc = function(com){eval(com);}
 }
 
 http_streaming.prototype = {
     console:           null,
+    win:               null,
     doc:               null,
     ifra:              null,
     page:              null,
-    wd_handle:         null,
+    watchdog_hdl:      null,
     keepalive_old:    -1,
     keepalive_new:    -1,
     keepalives_equal:  0,
     keepalives_eq_max: 6,
-    watchdog_timeout: 2000,
-    watchable: false,
-    restart_n: 0,
+    watchdog_timeout:  100,
+    watchdog_ct:       0,
+    watchdog_checktm:  20,
+    watchable:         false,
+    gst:               null,
+    restart_n:         0,
+    cmdproc:           null,
+    comm_match:        /_*@BEGIN@(.*?)@END@/g, 
+    comm_clean:        /_*@BEGIN@(.*?)@END@/,
+    ctx_old:           "",
+    ctx_old_len:       0,
+    // ctx_new:           "",
+    ctx_new_len:       0,
+    stream:            "",
 
     start: function(page) {
         this.log("http_streaming:start restart: "+this.restart_n);
-        this.page = page;
-        this.ifra.contentWindow.location.href = page;
-        this.wd_handle = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
+        this.keepalives_equal = 0;
+        this.ifra = this.doc.createElement("iframe");
+        this.ifra.style.visibility = "hidden";
+        this.doc.body.appendChild(this.ifra);
+        if (page != null)
+            this.page = page;
+        // this.log(this.ifra);
+        this.ifra.contentWindow.location.href = this.page;
+        this.watchdog_ct  = 0;
+        this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
     },
 
     watchdog: function () {
-        /* +this.keepalives_equal */
-        this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
-        if (!this.watchable) {
-            do {
-                if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
-                    break;
-                /*
-                  on IE7 the the window frame scope is cleaned after the href is set, so we wait 
-                  for a well know variable value before assign this object value to it (OO is a passion)
-                */
-                if (this.ifra.contentWindow.http_streaming == "ready") {
-                    this.ifra.contentWindow.http_streaming = this;
-                    this.watchable = true;
-                }
-            } while (false);
-        }
-        this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
-        if (this.keepalive_old == this.keepalive_new) {
-            this.keepalives_equal++;
-        }
-        else {
-            this.keepalive_old = this.keepalive_new;
-            this.keepalives_equal = 0;
+        var i, again;
+        var comm_newpart, comm_len, comm_arr;
+
+        // WATCHDOGING THE CONNECTION
+        if ( (this.watchdog_ct % this.watchdog_checktm) == 0 || !this.watchable) {
+            this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
+            if (!this.watchable) {
+                do {
+                    if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
+                        break;
+                    /*
+                      on IE7 the the window frame scope is cleaned after the href is set, so we wait 
+                      for a well know variable value before assign this object value to it (OO is a passion)
+                    */
+                    if (this.ifra.contentWindow.http_streaming == "ready") {
+                        this.ifra.contentWindow.http_streaming = this;
+                        this.watchable = true;
+                        this.log("hs::watchdog: watchable = yes");
+                    }
+                } while (false);
+            }
+            this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
+            if (this.keepalive_old == this.keepalive_new) {
+                this.keepalives_equal++;
+            }
+            else {
+                this.keepalive_old = this.keepalive_new;
+                this.keepalives_equal = 0;
+            }
+            
+            if (this.keepalives_equal > this.keepalives_eq_max) {
+                this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
+                this.reload();
+                return;
+            }
         }
+
+        // PICK COMMANDS FROM STREAM
+        do {
+           // CHECK: maybe again here isn't needed 
+            again = 0;
+            if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined')
+                break;
+            if (this.ifra.contentWindow.ctx_new.length == this.ctx_old_len) {
+                // this.ctx_new = this.ctx_old = "";
+                // FIXME find a more robust clean method
+                break;
+            }
+            this.log("new: "+ this.ifra.contentWindow.ctx_new.length + "  old: "+this.ctx_old_len);
+            this.keepalive_new++;            
+            for (i = this.ctx_old_len ; i < this.ifra.contentWindow.ctx_new.length ; i++) {
+               if (this.ifra.contentWindow.ctx_new[i] != '_') 
+                   break;
+           }
+           if (i == this.ifra.contentWindow.ctx_new.length) {
+               this.ctx_old_len = i;
+               break;
+           }
+            comm_newpart = this.ifra.contentWindow.ctx_new.substr(this.ctx_old_len);    
+            comm_len = 0;
+           comm_arr = comm_newpart.match(this.comm_match);
+
+           if (comm_arr) {
+               for (i = 0 ; i < comm_arr.length ; i++) {
+                   var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
+
+                   this.gst.comms = this.gst.comms.concat(temp);
+                   // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+"  LEN:"+comm_arr[i].length);
+                   comm_len += comm_arr[i].length;
+               }
+               this.ctx_old += comm_newpart.substr(0,comm_len);
+               again = 1;
+           }
+           this.ctx_old_len = this.ctx_old.length;
+        } while (again);
         
-        if (this.keepalives_equal > this.keepalives_eq_max) {
-            this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
-            this.reload();
-            return;
-        }
-        else {
-            this.wd_handle = setTimeout(function(obj) { obj.log("tout2"); obj.watchdog(); }, this.watchdog_timeout, this);
-        }
+
+
+        // EXECUTION OF STREAM COMMANDS
+        do {
+           again = 0;
+           //MOP ?? xhrrestart = 0;
+           if (this.gst.st_loc < this.gst.st_loc_new) {
+               // there is some slow actions running
+               break;
+           }
+           else if (this.gst.comms.length > 0) {
+               var singlecomm;
+                
+               singlecomm = this.gst.comms.shift();
+               // alert("EXE"+gugu);
+               // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
+               //xx this.hbit("+");
+                
+               this.cmdproc(singlecomm);
+               again = 1;
+           }
+        } while (again);
+
+
+        this.watchdog_ct++;
+        this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
+        return;
     },
     
-    keepalive: function () {
+    keepalive: function (s) {
         this.log("hs::keepalive");
-        this.keepalive_new++;
+        if (s != null) {
+            this.log(s);
+            this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
+        }
+        else {
+            this.ifra.contentWindow.ctx_new += "_";
+        }
+        // this.keepalive_new++;
     },
 
     abort: function () {
-        clearTimeout(this.wd_handle);
+        this.gst.abort();
+        if (this.watchdog_hdl != null) {
+            clearTimeout(this.watchdog_hdl);
+            this.watchdog_hdl = null;
+        }
+
         this.restart_n++;
         this.log("hs::reload");
         this.watchable = false;
-        delete this.ifra;
+        if (this.ifra != null) {
+            this.doc.body.removeChild(this.ifra);
+            delete this.ifra;
+            this.ifra = null;
+        }
+        this.ctx_old = "";
+        this.ctx_old_len = 0;
     },
 
     reload: function () {
         this.abort();
-        this.keepalives_equal = 0;
-        this.ifra = this.doc.createElement("iframe");
-        this.ifra.style.visibility = "hidden";
-        this.doc.body.appendChild(this.ifra);
-        this.start(this.page);
+        this.start(null);
     },
 
     log: function (s) {