1 // old targetpage == page and moved into start method
6 function transport_ws(doc, xynt_streaming, page)
8 this.name = "WebSocket";
14 this.xynt_streaming = xynt_streaming;
16 this.xynt_streaming.log("PAGE: "+page);
17 this.ws = new WebSocket(page);
18 this.ws.onopen = function () {
19 self.xynt_streaming.log("onopen");
20 if (this.readyState == 1) {
26 this.ws.onmessage = function (msg) {
27 self.xynt_streaming.log("onmessage");
28 // new data in msg.data
29 self.ctx_new += msg.data;
31 this.ws.onclose = function (msg) {
35 self.xynt_streaming.log("onclose"+self.init_steps);
36 if (self.init_steps == 0)
41 this.ws.onerror = function () {
46 self.xynt_streaming.log("onerror");
51 throw "websocket creation failed";
57 transport_ws.prototype = {
60 xynt_streaming: "ready",
73 destroy: function () { /* public */
74 if (this.ws != null) {
80 ws_cb: function (from) {
83 if (from == "error") {
84 if (this.xynt_streaming != "ready") {
85 if (this.xynt_streaming.transp_fback > 0) {
86 this.xynt_streaming.log("DEC: "+this.xynt_streaming.transp_fback);
87 this.xynt_streaming.transp_fback--;
89 this.xynt_streaming.reload();
93 if (this.ws != null && this.ws.readyState > 1) {
98 ws_abort: function() {
99 if (this.ws != null) {
100 this.xynt_streaming.log("WSCLOSE");
105 xstr_is_init: function () { /* public */
109 /* only after a successfull is_initialized call */
110 xstr_is_ready: function () { /* public */
111 return (this.ws.readyState == 1);
114 xstr_set: function () { /* public */
118 ctx_new_is_set: function () { /* public */
119 return (this.ctx_new != null);
122 ctx_new_curlen_get: function () { /* public */
123 return (this.ctx_new.length);
126 ctx_new_getchar: function(idx) { /* public */
127 return (this.ctx_new[idx]);
130 ctx_old_len_is_set: function () { /* public */
134 ctx_old_len_get: function () { /* public */
135 return (this.ctx_old_len);
138 ctx_old_len_set: function (len) { /* public */
139 this.ctx_old_len = len;
142 ctx_old_len_add: function (len) { /* public */
143 this.ctx_old_len += len;
146 new_part: function () { /* public */
147 return (this.ctx_new.substr(this.ctx_old_len));
150 scrcls_set: function (step) { /* public */
151 // this.script_clean = step;
154 postproc: function () {
155 if (this.stopped && !this.xstr_is_ready()) {
156 this.xynt_streaming.reload();
162 // CLASS transport_xhr
164 function transport_xhr(doc, xynt_streaming, page)
168 this.xynt_streaming = xynt_streaming;
169 this.xhr = createXMLHttpRequest();
170 this.xhr.open('GET', page);
173 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
176 this.stopped = false;
179 transport_xhr.prototype = {
182 xynt_streaming: "ready",
192 destroy: function () { /* public */
193 if (this.xhr != null) {
199 xhr_cb: function () {
202 if (this.xhr.readyState == 4) {
203 // console.log("SS: "+safestatus(xhr));
205 // NOTE: delay management later
207 // if ((ret = safestatus(this.xhr)) == 200) {
209 // // console.log("del a null "+this.delayed);
210 // } else if (ret != -1) {
211 // this.delay = 5000;
213 // // alert('There was a problem with the request.' + ret);
217 // this.delayed = null;
222 xhr_abort: function() {
223 if (this.xhr != null) {
228 xstr_is_init: function () { /* public */
230 if (this.xhr.responseText != null) {
231 this.ctx_new = this.xhr.responseText;
237 return (this.ctx_new != null);
240 /* only after a successfull is_initialized call */
241 xstr_is_ready: function () { /* public */
242 return (this.xynt_streaming == "ready");
245 xstr_set: function () { /* public */
249 ctx_new_is_set: function () { /* public */
250 return (this.ctx_new != null);
253 ctx_new_curlen_get: function () { /* public */
254 return (this.ctx_new.length);
257 ctx_new_getchar: function(idx) { /* public */
258 return (this.ctx_new[idx]);
261 ctx_old_len_is_set: function () { /* public */
265 ctx_old_len_get: function () { /* public */
266 return (this.ctx_old_len);
269 ctx_old_len_set: function (len) { /* public */
270 this.ctx_old_len = len;
273 ctx_old_len_add: function (len) { /* public */
274 this.ctx_old_len += len;
277 new_part: function () { /* public */
278 return (this.ctx_new.substr(this.ctx_old_len));
281 scrcls_set: function (step) { /* public */
282 // this.script_clean = step;
285 postproc: function () {
286 if (this.stopped && !this.xstr_is_ready()) {
287 this.xynt_streaming.reload();
293 // CLASS transport_htmlfile
295 function transport_htmlfile(doc, xynt_streaming, page)
297 this.name = "HTMLFile";
299 this.xynt_streaming = xynt_streaming;
300 this.transfdoc = new ActiveXObject("htmlfile");
301 this.transfdoc.open();
302 this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
303 this.transfdoc.close();
305 this.ifra = this.transfdoc.getElementById("iframe");
306 this.ifra.contentWindow.location.href = page;
307 this.stopped = false;
310 transport_htmlfile.prototype = {
313 xynt_streaming: null,
318 destroy: function () { /* public */
319 if (this.ifra != null) {
320 // this.doc.body.removeChild(this.ifra);
325 if (this.transfdoc) {
326 delete this.transfdoc;
327 this.transfdoc = null;
331 xstr_is_init: function () { /* public */
332 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
335 /* only after a successfull is_initialized call */
336 xstr_is_ready: function () { /* public */
337 return (this.ifra.contentWindow.xynt_streaming == "ready");
340 /* only after a successfull is_initialized call */
341 xstr_set: function () { /* public */
342 if (this.ifra.contentWindow.xynt_streaming == "ready") {
343 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
346 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
354 ctx_new_is_set: function () { /* public */
355 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
358 ctx_new_curlen_get: function () { /* public */
359 return (this.ifra.contentWindow.ctx_new.length);
362 ctx_new_getchar: function(idx) { /* public */
363 return (this.ifra.contentWindow.ctx_new.charAt(idx));
366 ctx_old_len_is_set: function () { /* public */
367 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
370 ctx_old_len_get: function () { /* public */
371 return (this.ifra.contentWindow.ctx_old_len);
374 ctx_old_len_set: function (len) { /* public */
375 this.ifra.contentWindow.ctx_old_len = len;
378 ctx_old_len_add: function (len) { /* public */
379 this.ifra.contentWindow.ctx_old_len += len;
382 new_part: function () { /* public */
383 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
386 scrcls_set: function (step) { /* public */
387 this.ifra.contentWindow.script_clean = step;
390 postproc: function () { /* public */
391 if (this.stopped && !this.xstr_is_ready()) {
392 this.xynt_streaming.reload();
398 // CLASS transport_iframe
400 function transport_iframe(doc, xynt_streaming, page)
402 this.name = "IFRAME";
404 this.xynt_streaming = xynt_streaming;
405 this.ifra = doc.createElement("iframe");
406 this.ifra.style.visibility = "hidden";
407 doc.body.appendChild(this.ifra);
408 this.ifra.contentWindow.location.href = page;
409 this.stopped = false;
412 transport_iframe.prototype = {
415 xynt_streaming: null,
419 destroy: function () { /* public */
421 if (this.ifra != null) {
422 // NOTE: on Opera this remove child crash js if called from
423 // inside of the iframe, on IE on Windows without
424 // it stream abort fails.
425 // the problem is fixed setting into the iframe's onload
426 // function the stopped attribute to true and delegate
427 // postproc() fired by xynt_streaming watchdog()
428 this.doc.body.removeChild(this.ifra);
433 alert("destroy exception catched");
437 xstr_is_init: function () { /* public */
438 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
441 /* only after a successfull is_initialized call */
442 xstr_is_ready: function () { /* public */
443 return (this.ifra.contentWindow.xynt_streaming == "ready");
446 /* only after a successfull is_initialized call */
447 xstr_set: function () { /* public */
448 if (this.ifra.contentWindow.xynt_streaming == "ready") {
449 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
452 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
461 /* only after a successfull is_ready call to be sure the accessibility of the var */
462 xstr_set_old: function (xynt_streaming) { /* public */
463 this.ifra.contentWindow.xynt_streaming = xynt_streaming;
466 ctx_new_is_set: function () { /* public */
467 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
470 ctx_new_curlen_get: function () { /* public */
471 return (this.ifra.contentWindow.ctx_new.length);
474 ctx_new_getchar: function(idx) { /* public */
475 return (this.ifra.contentWindow.ctx_new.charAt(idx));
478 ctx_old_len_is_set: function () { /* public */
479 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
482 ctx_old_len_get: function () { /* public */
483 return (this.ifra.contentWindow.ctx_old_len);
486 ctx_old_len_set: function (len) { /* public */
487 this.ifra.contentWindow.ctx_old_len = len;
490 ctx_old_len_add: function (len) { /* public */
491 this.ifra.contentWindow.ctx_old_len += len;
494 new_part: function () { /* public */
495 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
498 scrcls_set: function (step) { /* public */
499 this.ifra.contentWindow.script_clean = step;
502 postproc: function () { /* public */
503 if (this.stopped && !this.xstr_is_ready()) {
504 this.xynt_streaming.reload();
509 function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
512 this.transp_type = transp_type;
513 this.transp_port = transp_port;
514 this.transp_fback = transp_fback;
515 this.console = console;
518 this.cookiename = cookiename;
520 this.sandbox = sandbox;
522 this.cmdproc = cmdproc;
523 // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
524 this.doc = win.document;
525 this.keepalive_old = -1;
526 this.keepalive_new = -1;
528 this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
529 this.mon_wrntime = this.mon_errtime / 2;
534 xynt_streaming.prototype = {
550 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
553 cookiepath: "/brisk/",
555 hbit: function () {},
559 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
560 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
561 keepalives_eq_max: 4,
562 watchdog_checktm: 40,
563 watchdog_timeout: 100,
567 comm_match: /_*@BEGIN@(.*?)@END@/g,
568 comm_clean: /_*@BEGIN@(.*?)@END@/,
578 mon_update: function()
580 var date = new Date();
582 this.mon_time = date.getTime();
586 ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
588 return values: 0 unknown
593 mon_status: function()
597 var date = new Date();
599 delta = date.getTime() - this.mon_time;
601 if (delta < this.mon_wrntime)
603 else if (delta < this.mon_errtime)
609 start: function() { /* public */
613 // this.log("start "+this.transp_type+" "+this.transp_fback);
617 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
619 this.log("xynt_streaming:start restart: "+this.restart_n);
620 this.keepalives_equal = 0;
623 this.page = url_complete(this.win.location.href, this.page);
625 if (this.transp_fback > 0) {
626 transp_type = "websocket";
627 transp_port = (this.transp_fback == 2 ? 80 : 8080);
630 transp_type = this.transp_type;
631 transp_port = this.transp_port;
634 if (transp_type == "websocket") {
635 var end_proto, first_slash;
638 this.log("precha ["+this.page+"]");
639 end_proto = this.page.indexOf("://");
640 first_slash = this.page.substring(end_proto+3).indexOf("/");
642 page = "ws://" + this.page.substring(end_proto+3, end_proto+3+first_slash) + ":" + transp_port + this.page.substring(end_proto+3 + first_slash);
647 // stat, subst, this.gst.st
649 page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
650 // this.log("the page:");
654 // transport instantiation
655 if (transp_type == "websocket") {
656 page = url_append_args(page, "transp", "websocket");
657 this.transp = new transport_ws(this.doc, this, page);
659 else if (transp_type == "xhr") {
660 page = url_append_args(page, "transp", "xhr");
661 this.transp = new transport_xhr(this.doc, this, page);
663 else if (transp_type == "iframe") {
664 page = url_append_args(page, "transp", "iframe");
665 this.transp = new transport_iframe(this.doc, this, page);
667 else if (transp_type == "htmlfile") {
668 page = url_append_args(page, "transp", "htmlfile");
669 this.transp = new transport_htmlfile(this.doc, this, page);
675 if (this.transp_fback > 0) {
683 this.watchdog_ct = 0;
685 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
688 var date = new Date();
689 this.start_time = date.getTime();
697 hbit_set: function (hbit) {
702 hbit_status: function () {
705 ret = this.mon_status();
706 // console.log("mon_status: "+ret+" 0: "+this.mon_time);
722 if (this.mon_stat_old != mon_stat) {
724 this.mon_stat_old = mon_stat;
729 watchdog: function () {
730 // alert("watchdog");
732 var comm_newpart, comm_len, comm_arr;
735 if (this.sandbox != null) {
736 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;
737 if (zug != this.sandbox.innerHTML)
738 this.sandbox.innerHTML = zug;
741 // WATCHDOGING THE CONNECTION
742 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
743 if (!this.watchable) {
746 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
747 if (!this.transp.xstr_is_init()) {
748 this.log("hs::watchdog: xstr_is_init = false");
753 this.log("hs::watchdog: exception");
758 on IE7 the the window frame scope is cleaned after the href is set, so we wait
759 for a well know variable value before assign this object value to it (OO is a passion)
761 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
762 if (this.transp.xstr_set()) {
763 // this.ifra.contentWindow.xynt_streaming = this;
764 this.watchable = true;
765 this.watchdog_ct = 0;
766 this.log("hs::watchdog: watchable = yes");
770 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
771 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
772 if (this.keepalive_old == this.keepalive_new) {
773 this.keepalives_equal++;
776 this.keepalive_old = this.keepalive_new;
777 this.keepalives_equal = 0;
780 if (this.keepalives_equal >= this.keepalives_eq_max) {
781 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
783 // alert("watchdog return reload");
789 // PICK COMMANDS FROM STREAM
791 // alert("do--while begin ["+again+"]");
792 // CHECK: maybe again here isn't needed
795 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
796 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
797 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
804 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
805 ctx_new_len = this.transp.ctx_new_curlen_get();
806 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
807 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
810 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
811 this.keepalive_new++;
812 // alert("pre-loop 1");
813 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
814 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
815 if (this.transp.ctx_new_getchar(i) != '_') {
816 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
823 // this.log("ctx_new.charAt(i) == '_'");
826 // this.ifra.contentWindow.ctx_old_len = i;
827 this.transp.ctx_old_len_set(i);
828 if (i == ctx_new_len) {
829 this.log("old_len == i");
833 this.log("old_len != i: "+i);
835 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
837 comm_newpart = this.transp.new_part();
838 this.log("COM_NEWPART: ["+comm_newpart+"]");
840 comm_arr = comm_newpart.match(this.comm_match);
842 // alert("do--while middle2 ["+again+"]");
844 var comm_arr_len = comm_arr.length;
845 for (i = 0 ; i < comm_arr_len ; i++) {
846 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
847 this.gst.comms = this.gst.comms.concat(temp);
848 comm_len += comm_arr[i].length;
854 // this.ifra.contentWindow.ctx_old_len += comm_len;
855 this.transp.ctx_old_len_add(comm_len);
856 // this.ifra.contentWindow.script_clean = this.gst.st;
857 this.transp.scrcls_set(this.gst.st);
858 // alert("do--while end ["+again+"]");
861 // alert("post while");
862 // EXECUTION OF STREAM COMMANDS
865 //MOP ?? xhrrestart = 0;
866 if (this.gst.st_loc < this.gst.st_loc_new) {
867 // there is some slow actions running
870 else if (this.gst.comms.length > 0) {
873 singlecomm = this.gst.comms.shift();
874 // alert("EXE"+gugu);
875 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
878 // alert("SINGLE: ["+singlecomm+"]");
879 // window.console.log("["+singlecomm+"]");
880 this.cmdproc(singlecomm);
886 var date = new Date();
887 if (date.getTime() > (this.start_time + this.restart_wait)) {
888 this.transp.postproc();
890 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
893 // alert("watchdog return normal");
899 // moved to xynt-streaming-ifra as push()
901 // keepalive: function (s) {
902 // this.log("hs::keepalive");
905 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
908 // this.ifra.contentWindow.ctx_new += "_";
910 // // this.keepalive_new++;
913 abort: function () { /* public */
914 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
917 if (this.watchdog_hdl != null) {
918 clearTimeout(this.watchdog_hdl);
919 this.watchdog_hdl = null;
923 this.log("hs::reload");
924 this.watchable = false;
925 if (this.transp != null) {
926 this.transp.destroy();
932 reload: function () {
938 if (this.console != null) {
939 return (this.console.log(s));