xhr transport added v4.4.0
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Mon, 10 Dec 2012 09:25:41 +0000 (10:25 +0100)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Mon, 10 Dec 2012 09:26:54 +0000 (10:26 +0100)
TODO.txt
web/Obj/brisk.phh
web/Obj/transports.phh
web/Obj/user.phh
web/briskin5/Obj/briskin5.phh
web/commons.js
web/xynt-streaming.js
web/xynt_test01.php

index aed18eb..c475eff 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
    - try to be more antivirus-friendly
    - packetize and automatize php-ancillary and apache module
    - into the room local step remains -1
-   - add xhr as transport layer and manage different streams better
    - DISABLED_TO_FIX: DNS access (perform blocking dns requests, must be fixed)
 
 
   DONE |
  ------+
+   DONE - manage too fast reload in xynt-streaming
+   DONE - add xhr as transport layer and manage different streams better
    TEST - BUG: pari + 72 non fa 4x ma attualmente fa 3x, da correggere
    TEST - BUG: lurker are logged out (and remain a pending socket opened (look the spush log)
    DONE - save/restore database
index f38b504..f16ba02 100644 (file)
@@ -140,10 +140,10 @@ $mlang_brisk = array( 'btn_backstand'=> array( 'it' => 'torna in piedi',
 $G_lng = langtolng($G_lang);
 
 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
-$G_brisk_version = "4.3.0";
+$G_brisk_version = "4.4.0";
 
 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
-$root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: adottato sac-a-push come motore per l\'invio dei dati in tempo reale, nuovo trasporto httpfile per explorer, tanti bug fixati, freeze su disco, fix del calcolo dei punti, nuovo stream dati xhr.',
+$root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo stream dati xhr, prima versione completa.',
                                        'Se vuoi iscriverti alla <a target="_blank" href="mailto:ml-briscola+subscribe@milug.org">Mailing List</a>, cliccala!' ),
                        'en' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NEWS</b>: usage of reader/writer locking instead of generic exclusive locking.',
                                        'If you want to subscribe our <a target="_blank" href="ml-briscola+subscribe@milug.org">Mailing List</a>, click it!' ) );
@@ -2137,8 +2137,13 @@ class Room
       printf("NEW_SOCKET (root): %d\n", intval($new_socket));
 
       $enc = get_encoding($header);
-      if (isset($header['User-Agent']) && strstr($header['User-Agent'], "MSIE")) {
-          $transp_type = "htmlfile";
+      if (isset($header['User-Agent'])) {
+          if (strstr($header['User-Agent'], "MSIE")) {
+              $transp_type = "htmlfile";
+          }
+          else {
+              $transp_type = "xhr";
+          }
       }
       else {
           $transp_type = "iframe";
index 3de2197..0f0e4a7 100644 (file)
  *
  */
 
+/*
+ *  test: SO x Browser
+ *  Values: Y: works, N: not works, @: continuous download,
+ *          D: continuous download after first reload
+ *
+ *  Stream IFRAME:
+ *
+ * Iframe| IW | FF | Ch | Op | Ko | IE
+ * ------+----+----+----+----+----+----
+ *   Lnx | D  |    | @  |    | @  | x
+ *   Win | x  | D  | @  | @  |    | D
+ *   Mac | x  |    |    |    |    |
+ *
+ *
+ *   XHR | IW | FF | Ch | Op | Ko | IE
+ * ------+----+----+----+----+----+----
+ *   Lnx | Y  |    | ^D |    | Y  | x
+ *   Win | x  | Y  | Y  |    |    | N
+ *   Mac | x  |    |    |    |    |
+ *
+ *
+ * HtmlFl| IW | FF | Ch | Op | Ko | IE
+ * ------+----+----+----+----+----+----
+ *   Lnx | N  |    |    |    | N  |
+ *   Win | x  | N  | N  |    |    | Y* (* seems delay between click and load of a new page)
+ *   Mac | x  |    |    |    |    |
+ *
+ *
+ */
+
+
 class Transport_template {
 
     function Transport_template() {
@@ -139,4 +170,22 @@ push(\"%s\");\n// -->\n</script>", $step, escpush($cont) );
 class Transport_htmlfile extends Transport_iframe {
 }
 
+class Transport {
+    function Transport()
+    {
+    }
+
+    static function create($transp)
+    {
+        if ($transp == 'xhr') {
+            return new Transport_xhr();
+        }
+        else if ($transp == 'htmlfile') {
+            return new Transport_htmlfile();
+        }
+        else  {
+            return new Transport_iframe();
+        }
+    }
+}
 ?>
\ No newline at end of file
index a770964..ad63154 100644 (file)
@@ -294,7 +294,7 @@ class User {
     return ($thiz);
   }
 
-  function rd_data_set($curtime, $enc, $stat, $subst, $step, $from)
+  function rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from)
   {
       $this->rd_endtime = $curtime + RD_ENDTIME_DELTA;
       $this->rd_stat    = $stat;
@@ -304,7 +304,7 @@ class User {
       $this->rd_scristp = 0;
       $this->rd_kalive  = $curtime + RD_KEEPALIVE_TOUT;
       $this->rd_zls     = ZLibStream::create($enc);
-      $this->rd_transp  = new Transport_iframe();
+      $this->rd_transp  = Transport::create($transp);
   }
 
   function rd_socket_get() {
@@ -688,8 +688,10 @@ function stream_init($init_string, $enc, &$header_out, &$body, $get, $post, $coo
         $subst = "";
     if (($step  = gpcs_var('step', $get, $post, $cookie)) === FALSE) 
         unset($step);
+    if (($transp  = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
+        $transp = "iframe";
     
-    $this->rd_data_set($curtime, $enc, $stat, $subst, $step, $from);
+    $this->rd_data_set($curtime, $transp, $enc, $stat, $subst, $step, $from);
     
     $body .= $this->rd_transp->init($enc, &$header_out, $init_string, self::base_get(), $this->rd_scristp);
 
index 8c51f0b..bd358a0 100644 (file)
@@ -1171,8 +1171,13 @@ class Bin5 {
         printf("NEW_SOCKET (root): %d\n", intval($new_socket));
         
         $enc = get_encoding($header);
-        if (isset($header['User-Agent']) && strstr($header['User-Agent'], "MSIE")) {
-            $transp_type = "htmlfile";
+        if (isset($header['User-Agent'])) {
+            if (strstr($header['User-Agent'], "MSIE")) {
+                $transp_type = "htmlfile";
+            }
+            else {
+                $transp_type = "xhr";
+            }
         }
         else {
             $transp_type = "iframe";
index 9f70f65..75cabf4 100644 (file)
@@ -235,8 +235,10 @@ function safestatus(a)
 }
 
 function createXMLHttpRequest() {
-    try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
-    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
+    if (typeof(ActiveXObject) != 'undefined') { // Konqueror complain as unknown object
+        try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
+        try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
+    }
     try { return new XMLHttpRequest();                   } catch(e) {}
     alert("XMLHttpRequest not supported");
     return null;
index 4ebeb1a..50cea6d 100644 (file)
@@ -138,9 +138,6 @@ 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();
 
@@ -173,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") {
@@ -353,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;
@@ -372,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,
@@ -383,7 +387,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,
@@ -430,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() {
@@ -592,7 +598,10 @@ 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);
         }
         // alert("watchdog return normal");
index 78aeca0..bfeb59c 100644 (file)
@@ -1,5 +1,13 @@
 <?php
 
+$desc = array( "Semplice: da 1 a 9 ogni secondo, poi ricomincia.",
+               "Continuo: da 1 a N ogni secondo, ricomincia ogni 9.",
+               "Restart: da 1 a 8 ogni secondo, pausa 16 secondi, poi ricomincia.",
+               "Pausa: da 1 a 5 ogni secondo, pausa 3 secondi, e poi 8 e 9 ogni secondo, e poi ricomincia.",
+               "Keyword: da 1 a 5 ogni secondo, @BEGIN@, @END@, @BEGIN@ xxx yyy @END@, 9",
+               "Reload limiter: da 1 a 8 ogni secondo e chiude, 9 setta e chiude subito,<br>il client aspetta 3 secondi, e poi da 10 a N ogni secondo");
+
+
 $transs = array( "iframe", "xhr", "htmlfile" );
 if (!isset($f_trans))
     $f_trans = $transs[0];
@@ -25,6 +33,14 @@ function escpush($s)
     return str_replace($escpush_from, $escpush_to, $s);
 }
 
+function xcape($s)
+{
+  $from = array (   '\\',     '@',        '|' );
+  $to   = array ( '\\\\', '&#64;', '&brvbar;' );
+
+  return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
+}
+
 if ($isstream == "true") {
 
     require_once("Obj/transports.phh");
@@ -68,6 +84,15 @@ if ($isstream == "true") {
         }
         break;
     case 2:
+        // from 1 to 9 into the innerHTML and than close
+        for ($i = 1 ; $i < 10 ; $i++) {
+            $chunk = $transp->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
+            print($chunk);
+            mop_flush();
+            sleep(1);
+        }
+        break;
+    case 3:
         // from 1 to 9 with 60 secs after 8, the client js api must restart stream after 12 secs
         for ($i = 1 ; $i < 10 ; $i++) {
             $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
@@ -78,7 +103,7 @@ if ($isstream == "true") {
                 sleep(60);
         }
         break;
-    case 3:
+    case 4:
         // from 1 to 9 into the innerHTML and than close
         for ($i = 1 ; $i < 10 ; $i++) {
             if ($i != 5) {
@@ -92,6 +117,46 @@ if ($isstream == "true") {
             sleep(1);
         }
         break;
+    case 5:
+        // from 1 to 9 into the innerHTML and than close
+        $cont = array('@BEGIN@', '@END@', '@BEGIN@ sleep(1); @END@');
+        for ($i = 1 ; $i < 10 ; $i++) {
+            switch($i) {
+            case 6:
+            case 7:
+            case 8:
+                $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%s';", xcape($cont[$i - 6])));
+                break;
+            default:
+                $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
+                break;
+            }
+            print($chunk);
+            mop_flush();
+            if ($i < 9)
+                sleep(1);
+        }
+        break;
+    case 6:
+        // from 1 to 9 into the innerHTML and than close
+        if ($step == 8) {
+            $chunk = $transp->chunk(1, sprintf("gst.st++; \$('container').innerHTML = gst.st+\" x_x \"+(%d)", $step));
+            print($chunk);
+            // without this usleep the delay is doubled in iframe stream because 
+            // no transp.xynt_streaming back-set is performed
+            usleep(250000);
+            mop_flush();
+        }
+        else {
+            for ($i = 1 ; $i < 10 ; $i++) {
+                $chunk = $transp->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st+\" _ \"+(%d)", $step));
+                print($chunk);
+                mop_flush();
+                if ($i < 9)
+                    sleep(1);
+            }
+        }
+        break;
     }
     exit;
 }
@@ -123,13 +188,10 @@ if ($isstream == "true") {
 <div>
 <?php
 
-$desc = array( "Semplice: da 1 a 9 ogni secondo, poi ricomincia.",
-               "Restart: da 1 a 8 ogni secondo, pausa 16 secondi, poi ricomincia.",
-               "Pausa: da 1 a 5 ogni secondo, pausa 3 secondi, e poi 8 e 9 ogni secondo, e poi ricomincia.");
 
 
 printf("<table>");
-for ($test = 1 ; $test <= 3 ; $test++) {
+for ($test = 1 ; $test <= count($desc) ; $test++) {
     printf("<tr>");
     foreach ($transs as $trans) {
         printf("<td style=\"padding: 8px; border: 1px solid black;\"><a href=\"?f_trans=%s&f_test=%d\">Test %s %02d</a></td>", $trans, $test, $trans, $test);