X-Git-Url: https://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=web%2Fxynt-streaming.js;h=11f4f229d24199910b0c9df4a86d9ee6276c754a;hb=509c28d644b55837dcddb5cf4610dba58446a451;hp=f716d8cd9f2d40b5bef97d424e7d7641b4d7f122;hpb=0b1e5521bd8d1c7b6f349038fe4daa819da11d0e;p=brisk.git diff --git a/web/xynt-streaming.js b/web/xynt-streaming.js index f716d8c..11f4f22 100644 --- a/web/xynt-streaming.js +++ b/web/xynt-streaming.js @@ -1,8 +1,353 @@ // old targetpage == page and moved into start method -function xynt_streaming(win, console, gst, from, cookiename, sess, sandbox, page, cmdproc) +// +// CLASS transport_xhr +// +function transport_xhr(doc, xynt_streaming, page) +{ + this.doc = doc; + this.xynt_streaming = xynt_streaming; + this.xhr = createXMLHttpRequest(); + this.xhr.open('GET', page); + + var self = this; + this.xhr.onreadystatechange = function () { self.xhr_cb(); }; + this.xhr.send(null); + + this.stopped = false; +} + +transport_xhr.prototype = { + doc: null, + xynt_streaming: "ready", + xhr: null, + stopped: true, + + ctx_old: "", + ctx_old_len: 0, + ctx_new: null, + + // script_clean: 0, + + destroy: function () { /* public */ + if (this.xhr != null) { + this.xhr_abort(); + } + delete this.xhr; + }, + + xhr_cb: function () { + var ret; + + if (this.xhr.readyState == 4) { + // console.log("SS: "+safestatus(xhr)); + + // NOTE: delay management later + // try { + // if ((ret = safestatus(this.xhr)) == 200) { + // this.delay = 0; + // // console.log("del a null "+this.delayed); + // } else if (ret != -1) { + // this.delay = 5000; + // this.hbit('X'); + // // alert('There was a problem with the request.' + ret); + // } + // } catch(b) {}; + + // this.delayed = null; + this.stopped = true; + } + }, + + xhr_abort: function() { + if (this.xhr != null) { + this.xhr.abort(); + } + }, + + xstr_is_init: function () { /* public */ + try { + if (this.xhr.responseText != null) { + this.ctx_new = this.xhr.responseText; + } + } + catch (e) { + } + + return (this.ctx_new != null); + }, + + /* only after a successfull is_initialized call */ + xstr_is_ready: function () { /* public */ + return (this.xynt_streaming == "ready"); + }, + + 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_htmlfile +// +function transport_htmlfile(doc, xynt_streaming, page) +{ + this.doc = doc; + this.xynt_streaming = xynt_streaming; + this.transfdoc = new ActiveXObject("htmlfile"); + this.transfdoc.open(); + this.transfdoc.write(""); + 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, + + destroy: function () { /* public */ + if (this.ifra != null) { + // this.doc.body.removeChild(this.ifra); + // delete this.ifra; + this.ifra = null; + } + + if (this.transfdoc) { + delete this.transfdoc; + this.transfdoc = null; + } + }, + + xstr_is_init: function () { /* public */ + 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") { + this.ifra.contentWindow.xynt_streaming = this.xynt_streaming; + return (true); + } + else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) { + return (true); + } + else { + return (false); + } + }, + + ctx_new_is_set: function () { /* public */ + return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined'); + }, + + ctx_new_curlen_get: function () { /* public */ + return (this.ifra.contentWindow.ctx_new.length); + }, + + ctx_new_getchar: function(idx) { /* public */ + return (this.ifra.contentWindow.ctx_new.charAt(idx)); + }, + + ctx_old_len_is_set: function () { /* public */ + return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined'); + }, + + ctx_old_len_get: function () { /* public */ + return (this.ifra.contentWindow.ctx_old_len); + }, + + ctx_old_len_set: function (len) { /* public */ + this.ifra.contentWindow.ctx_old_len = len; + }, + + ctx_old_len_add: function (len) { /* public */ + this.ifra.contentWindow.ctx_old_len += len; + }, + + new_part: function () { /* public */ + return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len)); + }, + + scrcls_set: function (step) { /* public */ + this.ifra.contentWindow.script_clean = step; + }, + + 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.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, + xynt_streaming: null, + stopped: true, + ifra: null, + + destroy: function () { /* public */ + try { + if (this.ifra != null) { + // 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("destroy exception catched"); + } + }, + + xstr_is_init: function () { /* public */ + 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") { + this.ifra.contentWindow.xynt_streaming = this.xynt_streaming; + return (true); + } + else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) { + return (true); + } + else { + return (false); + } + }, + + + /* only after a successfull is_ready call to be sure the accessibility of the var */ + xstr_set_old: function (xynt_streaming) { /* public */ + this.ifra.contentWindow.xynt_streaming = xynt_streaming; + }, + + ctx_new_is_set: function () { /* public */ + return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined'); + }, + + ctx_new_curlen_get: function () { /* public */ + return (this.ifra.contentWindow.ctx_new.length); + }, + + ctx_new_getchar: function(idx) { /* public */ + return (this.ifra.contentWindow.ctx_new.charAt(idx)); + }, + + ctx_old_len_is_set: function () { /* public */ + return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined'); + }, + + ctx_old_len_get: function () { /* public */ + return (this.ifra.contentWindow.ctx_old_len); + }, + + ctx_old_len_set: function (len) { /* public */ + this.ifra.contentWindow.ctx_old_len = len; + }, + + ctx_old_len_add: function (len) { /* public */ + this.ifra.contentWindow.ctx_old_len += len; + }, + + new_part: function () { /* public */ + return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len)); + }, + + scrcls_set: function (step) { /* public */ + this.ifra.contentWindow.script_clean = step; + }, + + 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) { this.win = win; + this.transp_type = transp_type; this.console = console; this.gst = gst; this.from = from; @@ -12,14 +357,21 @@ function xynt_streaming(win, console, gst, from, cookiename, sess, sandbox, page 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 = { win: null, + transp_type: null, + transp: null, console: null, gst: null, from: null, @@ -29,18 +381,19 @@ 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, - ifra: 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 */ - // FIXME watchdog_timeout: 200, - /* 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, watchdog_timeout: 100, watchdog_ct: 0, @@ -51,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; @@ -59,23 +449,38 @@ xynt_streaming.prototype = { // alert("start"); this.log("xynt_streaming:start restart: "+this.restart_n); this.keepalives_equal = 0; - this.ifra = this.doc.createElement("iframe"); - this.ifra.style.visibility = "hidden"; - this.doc.body.appendChild(this.ifra); + + // 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); - // alert(this.page); this.log(this.page); - // this.log(this.ifra); - this.ifra.contentWindow.location.href = this.page; - // this.ifra.src = this.page; + // 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 if (this.transp_type == "htmlfile") { + this.page = url_append_args(this.page, "transp", "htmlfile"); + this.transp = new transport_htmlfile(this.doc, this, this.page); + } + 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() { @@ -87,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; @@ -105,10 +538,14 @@ xynt_streaming.prototype = { if (!this.watchable) { do { try{ - if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined') + // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined') + if (!this.transp.xstr_is_init()) { + this.log("hs::watchdog: xstr_is_init = false"); break; + } } catch(b) { + this.log("hs::watchdog: exception"); break; } @@ -116,8 +553,9 @@ xynt_streaming.prototype = { 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") { - this.ifra.contentWindow.xynt_streaming = this; + // if (this.ifra.contentWindow.xynt_streaming == "ready") { + if (this.transp.xstr_set()) { + // this.ifra.contentWindow.xynt_streaming = this; this.watchable = true; this.watchdog_ct = 0; this.log("hs::watchdog: watchable = yes"); @@ -134,10 +572,11 @@ 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"); + this.hbit_status(); return; } } @@ -148,31 +587,39 @@ xynt_streaming.prototype = { // CHECK: maybe again here isn't needed again = 0; try { - if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' || - typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') + /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' || + typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */ + if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set()) break; } catch(b) { break; } - - ctx_new_len = this.ifra.contentWindow.ctx_new.length; - if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) { + + // ctx_new_len = this.ifra.contentWindow.ctx_new.length; + ctx_new_len = this.transp.ctx_new_curlen_get(); + // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) { + if (ctx_new_len <= this.transp.ctx_old_len_get()) { break; } - this.log("new: "+ ctx_new_len + " old: "+this.ifra.contentWindow.ctx_old_len); + this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get()); this.keepalive_new++; // alert("pre-loop 1"); - for (i = this.ifra.contentWindow.ctx_old_len ; i < ctx_new_len ; i++) { - if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') { + for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) { + // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') { + if (this.transp.ctx_new_getchar(i) != '_') { // 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) == '_'"); // } } - this.ifra.contentWindow.ctx_old_len = i; + // this.ifra.contentWindow.ctx_old_len = i; + this.transp.ctx_old_len_set(i); if (i == ctx_new_len) { this.log("old_len == i"); break; @@ -182,7 +629,7 @@ xynt_streaming.prototype = { } // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]"); - comm_newpart = this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len); + comm_newpart = this.transp.new_part(); this.log("COM_NEWPART: ["+comm_newpart+"]"); comm_len = 0; comm_arr = comm_newpart.match(this.comm_match); @@ -196,9 +643,13 @@ 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.ifra.contentWindow.script_clean = this.gst.st; + // this.ifra.contentWindow.ctx_old_len += comm_len; + this.transp.ctx_old_len_add(comm_len); + // this.ifra.contentWindow.script_clean = this.gst.st; + this.transp.scrcls_set(this.gst.st); // alert("do--while end ["+again+"]"); } while (again); @@ -226,7 +677,12 @@ xynt_streaming.prototype = { } while (again); this.watchdog_ct++; if (!this.the_end) { + 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"); @@ -260,10 +716,10 @@ xynt_streaming.prototype = { this.restart_n++; this.log("hs::reload"); this.watchable = false; - if (this.ifra != null) { - this.doc.body.removeChild(this.ifra); - delete this.ifra; - this.ifra = null; + if (this.transp != null) { + this.transp.destroy(); + delete this.transp; + this.transp = null; } },