fix missing bold for your account item
[brisk.git] / web / xynt-streaming.js
index fe14f24..b9ef124 100644 (file)
 // old targetpage == page and moved into start method
 
+//
+// CLASS transport_ws
+//
+function transport_ws(doc, xynt_streaming, page)
+{
+    // if four arguments manage if WS or WSS connection
+    if (arguments.length > 3)
+        this.is_secure = arguments[3];
+    else
+        this.is_secure = false;
+
+    if (this.is_secure)
+        this.name = "WebSocketSecure";
+    else
+        this.name = "WebSocket";
+    this.ctx_new = "";
+    var self = this;
+
+    this.doc = doc;
+    this.failed = false;
+    this.xynt_streaming = xynt_streaming;
+    try {
+        this.xynt_streaming.log("PAGE: "+page);
+        this.ws = new WebSocket(page);
+        this.ws.onopen = function () {
+            console.log('WS On open');
+
+            self.xynt_streaming.log("onopen");
+            if (this.readyState == 1) {
+                // connected
+                self.ws_cb("open");
+                self.init_steps = 1;
+            }
+        };
+        this.ws.onmessage = function (msg) {
+            console.log('WS On message');
+            self.xynt_streaming.log("onmessage");
+            // new data in msg.data
+            self.ctx_new += msg.data;
+        };
+        this.ws.onclose = function (msg) {
+            console.log('WS On close');
+            self.onopen  = null;
+            self.onclose = null;
+            self.onerror = null;
+            self.xynt_streaming.log("onclose"+self.init_steps);
+            if (self.init_steps == 0)
+                self.ws_cb("error");
+            else
+                self.ws_cb("close");
+        };
+        this.ws.onerror = function () {
+            // on error
+            this.onopen  = null;
+            this.onclose = null;
+            this.onerror = null;
+            self.xynt_streaming.log("onerror");
+            self.ws_cb("error");
+        };
+    }
+    catch (ex) {
+        throw "websocket creation failed";
+    }
+
+    this.stopped = false;
+}
+
+transport_ws.prototype = {
+    doc: null,
+    name: null,
+    xynt_streaming: "ready",
+    ws: null,
+    stopped: true,
+    failed: false,
+
+    init_steps: 0,
+
+    ctx_old: "",
+    ctx_old_len: 0,
+    ctx_new: "",
+
+    // script_clean: 0,
+
+    destroy: function () { /* public */
+        if (this.ws != null) {
+            this.ws_abort();
+        }
+        delete this.ws;
+    },
+
+    ws_cb: function (from) {
+        var ret;
+
+        if (from == "error") {
+            if (this.xynt_streaming != "ready") {
+                if (this.xynt_streaming.transp_fback > 0) {
+this.xynt_streaming.log("DEC: "+this.xynt_streaming.transp_fback);
+                    this.xynt_streaming.transp_fback--;
+                   this.stopped = true;
+                    this.xynt_streaming.reload();
+                }
+            }
+        }
+        else if (from == "open") {
+            this.flush_out_queue();
+        }
+
+        if (this.ws != null && this.ws.readyState > 1) {
+           this.stopped = true;
+        }
+    },
+
+    flush_out_queue: function() {
+        var l_out = this.xynt_streaming.out_queue.length;
+
+        if (l_out == 0)
+            return;
+
+        console.log('flush_out_queue: ' + l_out);
+        for (var i = 0 ; i < l_out ; i++) {
+            if (this.ws.readyState != 1) {
+                break;
+            }
+            var item = this.xynt_streaming.out_queue.shift();
+            var sent = true;
+            try {
+                this.ws.send(item);
+            }
+            catch (ex) {
+                this.xynt_streaming.out_queue.unshift(item);
+                break;
+            }
+        }
+    },
+
+    send: function(msg) {
+        console.log('new send');
+        if (this.ws && this.ws.readyState == 1) {
+            try {
+                console.log('Try send ... ');
+                this.flush_out_queue();
+                this.ws.send(msg);
+                console.log(' ... done');
+            }
+            catch (ex) {
+                console.log(' ... catched exception');
+                this.xynt_streaming.out_queue.push(msg);
+            }
+        }
+        else {
+            console.log('ws not ready: push into out_queue');
+            this.xynt_streaming.out_queue.push(msg);
+        }
+    },
+
+    ws_abort: function() {
+        if (this.ws != null) {
+this.xynt_streaming.log("WSCLOSE");
+            this.ws.close();
+        }
+    },
+
+    xstr_is_init: function () { /* public */
+        return (true);
+    },
+
+    /* only after a successfull is_initialized call */
+    xstr_is_ready: function () { /* public */
+        return (this.ws.readyState == 1);
+    },
+
+    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_xhr
 //
 function transport_xhr(doc, xynt_streaming, page)
 {
+    this.name = "XHR";
     this.doc = doc;
     this.xynt_streaming = xynt_streaming;
     this.xhr = createXMLHttpRequest();
@@ -19,6 +238,7 @@ function transport_xhr(doc, xynt_streaming, page)
 
 transport_xhr.prototype = {
     doc: null,
+    name: null,
     xynt_streaming: "ready",
     xhr: null,
     stopped: true,
@@ -134,23 +354,24 @@ transport_xhr.prototype = {
 //
 function transport_htmlfile(doc, xynt_streaming, page)
 {
+    this.name = "HTMLFile";
     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,
+    name: null,
     xynt_streaming: null,
+    stopped: true,
     ifra: null,
     tradoc: null,
 
@@ -171,6 +392,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 +420,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,35 +447,44 @@ 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();
+        }
     }
 }
 
-
-
 //
 // CLASS transport_iframe
 //
 function transport_iframe(doc, xynt_streaming, page)
 {
+    this.name = "IFRAME";
     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,
+    name: 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 +532,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,14 +559,19 @@ 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();
+        }
     }
 }
 
-function xynt_streaming(win, transp_type, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
+function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
 {
     this.win = win;
     this.transp_type = transp_type;
+    this.transp_port = transp_port;
+    this.transp_fback = transp_fback;
     this.console = console;
     this.gst = gst;
     this.from = from;
@@ -339,15 +581,24 @@ 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.out_queue = [];
+
+    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 = {
     win:               null,
     transp_type:       null,
+    transp_port:         80,
+    transp_type_cur:   null,
+    transp_port_cur:     80,
+    transp_fback:         0,
     transp:            null,
     console:           null,
     gst:               null,
@@ -358,10 +609,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,18 +623,59 @@ 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,
     restart_n:         0,
-    comm_match:        /_*@BEGIN@(.*?)@END@/g, 
+    comm_match:        /_*@BEGIN@(.*?)@END@/g,
     comm_clean:        /_*@BEGIN@(.*?)@END@/,
     stream:            "",
+    out_queue:         null,
     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) 
+        var transp_type;
+        var page;
+
+        // this.log("start "+this.transp_type+" "+this.transp_fback);
+        if (this.the_end)
             return;
 
         createCookie(this.cookiename, sess, 24*365, this.cookiepath);
@@ -390,32 +685,96 @@ xynt_streaming.prototype = {
 
         // page arrangement
         this.page = url_complete(this.win.location.href, this.page);
-        // stat, subst, this.gst.st
 
-        this.page = url_append_args(this.page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
-        this.log(this.page);
+        // DEFAULT TRANSPORT PROTOCOL HERE websocketsec, websocket
+        if (this.transp_fback > 0) {
+            if (location.protocol == 'https:') {
+                transp_type = "websocketsec";
+                transp_port = 443;
+            }
+            else {
+                transp_type = "websocket";
+                transp_port = (this.transp_fback == 2 ? 80 : 8080);
+            }
 
-        // transport instantiation
-        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 {
+            transp_type = this.transp_type;
+            transp_port = this.transp_port;
         }
-        else if (this.transp_type == "htmlfile") {
-            this.page = url_append_args(this.page, "transp", "htmlfile");
-            this.transp = new transport_htmlfile(this.doc, this, this.page);
+
+        this.transp_type_cur = transp_type;
+        this.transp_port_cur = transp_port;
+
+        if (transp_type == "websocket" || transp_type == "websocketsec") {
+            var end_proto, first_slash, newpage;
+
+            // change protocol
+            this.log("precha ["+this.page+"]");
+            if (transp_type == "websocketsec") {
+                newpage = this.page.replace(/\.php$/g, "_wss.php").replace(/\.php\?/g, "_wss.php?");
+                }
+            else {
+                newpage = this.page;
+                }
+            end_proto = newpage.indexOf("://");
+            first_slash = newpage.substring(end_proto+3).indexOf("/");
+
+            page = (transp_type == "websocketsec" ? "wss://" : "ws://")
+                + newpage.substring(end_proto+3, end_proto+3 + first_slash) + ":"
+                + transp_port + newpage.substring(end_proto+3 + first_slash);
+            // this.log("MOP WS: " + page);
+        }
+        else {
+            page = this.page;
+        }
+        // stat, subst, this.gst.st
+
+        page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
+        // this.log("the page:");
+        // this.log(page);
+
+        try {
+            // transport instantiation
+            if (transp_type == "websocketsec") {
+                page = url_append_args(page, "transp", "websocketsec");
+                this.transp = new transport_ws(this.doc, this, page, true);
+            }
+            else if (transp_type == "websocket") {
+                page = url_append_args(page, "transp", "websocket");
+                this.transp = new transport_ws(this.doc, this, page);
+            }
+            else if (transp_type == "xhr") {
+                page = url_append_args(page, "transp", "xhr");
+                this.transp = new transport_xhr(this.doc, this, page);
+            }
+            else if (transp_type == "iframe") {
+                page = url_append_args(page, "transp", "iframe");
+                this.transp = new transport_iframe(this.doc, this, page);
+            }
+            else if (transp_type == "htmlfile") {
+                page = url_append_args(page, "transp", "htmlfile");
+                this.transp = new transport_htmlfile(this.doc, this, page);
+            }
+            else
+                return;
+        }
+        catch (err) {
+            if (this.transp_fback > 0) {
+                this.transp_fback--;
+                this.start();
+                return;
+            }
         }
-        else
-            return;
 
         // watchdog setting
         this.watchdog_ct  = 0;
         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 +786,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;
@@ -434,8 +821,7 @@ xynt_streaming.prototype = {
         var ctx_new_len;
 
         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;          
+            var zug = "WATCHDOG  sess = ["+this.sess+"]  step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new+"Transport: "+this.transp.name;
             if (zug != this.sandbox.innerHTML)
                this.sandbox.innerHTML = zug;
         }
@@ -457,7 +843,7 @@ xynt_streaming.prototype = {
                 }
 
                 /*
-                  on IE7 the the window frame scope is cleaned after the href is set, so we wait 
+                  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.xynt_streaming == "ready") {
@@ -478,11 +864,12 @@ xynt_streaming.prototype = {
                 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();
                 // alert("watchdog return reload");
+                this.hbit_status();
                 return;
             }
         }
@@ -490,7 +877,7 @@ xynt_streaming.prototype = {
         // PICK COMMANDS FROM STREAM
         do {
             // alert("do--while begin ["+again+"]");
-           // CHECK: maybe again here isn't needed 
+           // CHECK: maybe again here isn't needed
             again = 0;
             try {
                 /* if (typeof(this.ifra.contentWindow.ctx_new)     == 'undefined' ||
@@ -517,6 +904,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 +936,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);
@@ -565,27 +957,45 @@ xynt_streaming.prototype = {
            }
            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("+");
 
                 // alert("SINGLE: ["+singlecomm+"]");
+                // window.console.log("["+singlecomm+"]");
                this.cmdproc(singlecomm);
+                if (this.transp_type_cur) {
+                    this.transp_type = this.transp_type_cur;
+                    this.transp_port = this.transp_port_cur;
+                }
                again = 1;
            }
         } 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");
 
         return;
     },
 
+    send: function(msg) {
+        if (typeof(this.transp.send) == 'undefined') {
+            this.log('send not implemented for ' + this.transp_type);
+            return;
+        }
+
+        return this.transp.send(msg);
+    },
+
     //
     // moved to xynt-streaming-ifra as push()
     //