fixed missing fallback if serverside websocket fails
[brisk.git] / web / xynt-streaming.js
1 // old targetpage == page and moved into start method
2
3 //
4 // CLASS transport_ws
5 //
6 function transport_ws(doc, xynt_streaming, page)
7 {
8     this.ctx_new = "";
9     var self = this;
10
11     this.doc = doc;
12     this.failed = false;
13     this.xynt_streaming = xynt_streaming;
14     try {
15         this.ws = new WebSocket(page);
16         this.ws.onopen = function () {
17             self.xynt_streaming.log("onopen");
18             if (this.readyState == 1) {
19                 // connected
20                 self.ws_cb("open");
21                 self.init_steps = 1;
22             }
23         };
24         this.ws.onmessage = function (msg) {
25             self.xynt_streaming.log("onmessage");
26             // new data in msg.data
27             self.ctx_new += msg.data;
28         };
29         this.ws.onclose = function (msg) {
30             self.xynt_streaming.log("onclose"+self.init_steps);
31             if (self.init_steps == 0)
32                 self.ws_cb("error");
33             else
34                 self.ws_cb("close");
35         };
36         this.ws.onerror = function () {
37             // on error
38             self.xynt_streaming.log("onerror");
39             self.ws_cb("error");
40         };
41     }
42     catch (ex) {
43         throw "websocket creation failed";
44     }
45
46     this.stopped = false;
47 }
48
49 transport_ws.prototype = {
50     doc: null,
51     xynt_streaming: "ready",
52     ws: null,
53     stopped: true,
54     failed: false,
55
56     init_steps: 0,
57
58     ctx_old: "",
59     ctx_old_len: 0,
60     ctx_new: "",
61
62     // script_clean: 0,
63
64     destroy: function () { /* public */
65         if (this.ws != null) {
66             this.ws_abort();
67         }
68         delete this.ws;
69     },
70
71     ws_cb: function (from) {
72         var ret;
73
74         if (from == "error") {
75             if (this.xynt_streaming != "ready") {
76                 if (this.xynt_streaming.transp_fback > 0) {
77                     this.xynt_streaming.transp_fback--;
78                     this.stopped = true;
79                     this.xynt_streaming.reload();
80                 }
81             }
82         }
83         if (this.ws != null && this.ws.readyState > 1) {
84             this.stopped = true;
85         }
86     },
87
88     ws_abort: function() {
89         if (this.ws != null) {
90             this.ws.close();
91         }
92     },
93
94     xstr_is_init: function () { /* public */
95         return (true);
96     },
97
98     /* only after a successfull is_initialized call */
99     xstr_is_ready: function () { /* public */
100         return (this.ws.readyState == 1);
101     },
102
103     xstr_set: function () { /* public */
104         // already set
105     },
106
107     ctx_new_is_set: function () { /* public */
108         return (this.ctx_new != null);
109     },
110
111     ctx_new_curlen_get: function () { /* public */
112         return (this.ctx_new.length);
113     },
114
115     ctx_new_getchar: function(idx) { /* public */
116         return (this.ctx_new[idx]);
117     },
118
119     ctx_old_len_is_set: function () { /* public */
120         return (true);
121     },
122
123     ctx_old_len_get: function () { /* public */
124         return (this.ctx_old_len);
125     },
126
127     ctx_old_len_set: function (len) { /* public */
128         this.ctx_old_len = len;
129     },
130
131     ctx_old_len_add: function (len) { /* public */
132         this.ctx_old_len += len;
133     },
134
135     new_part: function () { /* public */
136         return (this.ctx_new.substr(this.ctx_old_len));
137     },
138
139     scrcls_set: function (step) { /* public */
140         // this.script_clean = step;
141     },
142
143     postproc: function () {
144         if (this.stopped && !this.xstr_is_ready()) {
145             this.xynt_streaming.reload();
146         }
147     }
148 }
149
150 //
151 // CLASS transport_xhr
152 //
153 function transport_xhr(doc, xynt_streaming, page)
154 {
155     this.doc = doc;
156     this.xynt_streaming = xynt_streaming;
157     this.xhr = createXMLHttpRequest();
158     this.xhr.open('GET', page);
159
160     var self = this;
161     this.xhr.onreadystatechange = function () { self.xhr_cb(); };
162     this.xhr.send(null);
163
164     this.stopped = false;
165 }
166
167 transport_xhr.prototype = {
168     doc: null,
169     xynt_streaming: "ready",
170     xhr: null,
171     stopped: true,
172
173     ctx_old: "",
174     ctx_old_len: 0,
175     ctx_new: null,
176
177     // script_clean: 0,
178
179     destroy: function () { /* public */
180         if (this.xhr != null) {
181             this.xhr_abort();
182         }
183         delete this.xhr;
184     },
185
186     xhr_cb: function () {
187         var ret;
188
189         if (this.xhr.readyState == 4) {
190             // console.log("SS: "+safestatus(xhr));
191
192             // NOTE: delay management later
193             // try {
194             //     if ((ret = safestatus(this.xhr)) == 200) {
195             //         this.delay = 0;
196             //         // console.log("del a null "+this.delayed);
197             //     } else if (ret != -1) {
198             //         this.delay = 5000;
199             //         this.hbit('X');
200             //         // alert('There was a problem with the request.' + ret);
201             //     }
202             // } catch(b) {};
203
204             // this.delayed = null;
205             this.stopped = true;
206         }
207     },
208
209     xhr_abort: function() {
210         if (this.xhr != null) {
211             this.xhr.abort();
212         }
213     },
214
215     xstr_is_init: function () { /* public */
216         try {
217             if (this.xhr.responseText != null) {
218                 this.ctx_new = this.xhr.responseText;
219             }
220         }
221         catch (e) {
222         }
223
224         return (this.ctx_new != null);
225     },
226
227     /* only after a successfull is_initialized call */
228     xstr_is_ready: function () { /* public */
229         return (this.xynt_streaming == "ready");
230     },
231
232     xstr_set: function () { /* public */
233         // already set
234     },
235
236     ctx_new_is_set: function () { /* public */
237         return (this.ctx_new != null);
238     },
239
240     ctx_new_curlen_get: function () { /* public */
241         return (this.ctx_new.length);
242     },
243
244     ctx_new_getchar: function(idx) { /* public */
245         return (this.ctx_new[idx]);
246     },
247
248     ctx_old_len_is_set: function () { /* public */
249         return (true);
250     },
251
252     ctx_old_len_get: function () { /* public */
253         return (this.ctx_old_len);
254     },
255
256     ctx_old_len_set: function (len) { /* public */
257         this.ctx_old_len = len;
258     },
259
260     ctx_old_len_add: function (len) { /* public */
261         this.ctx_old_len += len;
262     },
263
264     new_part: function () { /* public */
265         return (this.ctx_new.substr(this.ctx_old_len));
266     },
267
268     scrcls_set: function (step) { /* public */
269         // this.script_clean = step;
270     },
271
272     postproc: function () {
273         if (this.stopped && !this.xstr_is_ready()) {
274             this.xynt_streaming.reload();
275         }
276     }
277 }
278
279 //
280 // CLASS transport_htmlfile
281 //
282 function transport_htmlfile(doc, xynt_streaming, page)
283 {
284     this.doc = doc;
285     this.xynt_streaming = xynt_streaming;
286     this.transfdoc = new ActiveXObject("htmlfile");
287     this.transfdoc.open();
288     this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
289     this.transfdoc.close();
290
291     this.ifra = this.transfdoc.getElementById("iframe");
292     this.ifra.contentWindow.location.href = page;
293     this.stopped = false;
294 }
295
296 transport_htmlfile.prototype = {
297     doc: null,
298     xynt_streaming: null,
299     stopped: true,
300     ifra: null,
301     tradoc: null,
302
303     destroy: function () { /* public */
304         if (this.ifra != null) {
305         //     this.doc.body.removeChild(this.ifra);
306         //     delete this.ifra;
307              this.ifra = null;
308         }
309
310         if (this.transfdoc) {
311             delete this.transfdoc;
312             this.transfdoc = null;
313         }
314     },
315
316     xstr_is_init: function () { /* public */
317         return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
318     },
319
320     /* only after a successfull is_initialized call */
321     xstr_is_ready: function () { /* public */
322         return (this.ifra.contentWindow.xynt_streaming == "ready");
323     },
324
325     /* only after a successfull is_initialized call */
326     xstr_set: function () { /* public */
327         if (this.ifra.contentWindow.xynt_streaming == "ready") {
328             this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
329             return (true);
330         }
331         else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
332             return (true);
333         }
334         else {
335             return (false);
336         }
337     },
338
339     ctx_new_is_set: function () { /* public */
340         return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
341     },
342
343     ctx_new_curlen_get: function () { /* public */
344         return (this.ifra.contentWindow.ctx_new.length);
345     },
346
347     ctx_new_getchar: function(idx) { /* public */
348         return (this.ifra.contentWindow.ctx_new.charAt(idx));
349     },
350
351     ctx_old_len_is_set: function () { /* public */
352         return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
353     },
354
355     ctx_old_len_get: function () { /* public */
356         return (this.ifra.contentWindow.ctx_old_len);
357     },
358
359     ctx_old_len_set: function (len) { /* public */
360         this.ifra.contentWindow.ctx_old_len = len;
361     },
362
363     ctx_old_len_add: function (len) { /* public */
364         this.ifra.contentWindow.ctx_old_len += len;
365     },
366
367     new_part: function () { /* public */
368         return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
369     },
370
371     scrcls_set: function (step) { /* public */
372         this.ifra.contentWindow.script_clean = step;
373     },
374
375     postproc: function () { /* public */
376         if (this.stopped && !this.xstr_is_ready()) {
377             this.xynt_streaming.reload();
378         }
379     }
380 }
381
382 //
383 // CLASS transport_iframe
384 //
385 function transport_iframe(doc, xynt_streaming, page)
386 {
387     this.doc = doc;
388     this.xynt_streaming = xynt_streaming;
389     this.ifra = doc.createElement("iframe");
390     this.ifra.style.visibility = "hidden";
391     doc.body.appendChild(this.ifra);
392     this.ifra.contentWindow.location.href = page;
393     this.stopped = false;
394 }
395
396 transport_iframe.prototype = {
397     doc: null,
398     xynt_streaming: null,
399     stopped: true,
400     ifra: null,
401
402     destroy: function () { /* public */
403         try {
404             if (this.ifra != null) {
405                 // NOTE:  on Opera this remove child crash js if called from
406                 //        inside of the iframe, on IE on Windows without
407                 //        it stream abort fails.
408                 //        the problem is fixed setting into the iframe's onload
409                 //        function the stopped attribute to true and delegate
410                 //        postproc() fired by xynt_streaming watchdog()
411                 this.doc.body.removeChild(this.ifra);
412                 delete this.ifra;
413                 this.ifra = null;
414             }
415         } catch (b) {
416             alert("destroy exception catched");
417         }
418     },
419
420     xstr_is_init: function () { /* public */
421         return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
422     },
423
424     /* only after a successfull is_initialized call */
425     xstr_is_ready: function () { /* public */
426         return (this.ifra.contentWindow.xynt_streaming == "ready");
427     },
428
429     /* only after a successfull is_initialized call */
430     xstr_set: function () { /* public */
431         if (this.ifra.contentWindow.xynt_streaming == "ready") {
432             this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
433             return (true);
434         }
435         else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
436             return (true);
437         }
438         else {
439             return (false);
440         }
441     },
442
443
444     /* only after a successfull is_ready call to be sure the accessibility of the var */
445     xstr_set_old: function (xynt_streaming) { /* public */
446         this.ifra.contentWindow.xynt_streaming = xynt_streaming;
447     },
448
449     ctx_new_is_set: function () { /* public */
450         return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
451     },
452
453     ctx_new_curlen_get: function () { /* public */
454         return (this.ifra.contentWindow.ctx_new.length);
455     },
456
457     ctx_new_getchar: function(idx) { /* public */
458         return (this.ifra.contentWindow.ctx_new.charAt(idx));
459     },
460
461     ctx_old_len_is_set: function () { /* public */
462         return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
463     },
464
465     ctx_old_len_get: function () { /* public */
466         return (this.ifra.contentWindow.ctx_old_len);
467     },
468
469     ctx_old_len_set: function (len) { /* public */
470         this.ifra.contentWindow.ctx_old_len = len;
471     },
472
473     ctx_old_len_add: function (len) { /* public */
474         this.ifra.contentWindow.ctx_old_len += len;
475     },
476
477     new_part: function () { /* public */
478         return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
479     },
480
481     scrcls_set: function (step) { /* public */
482         this.ifra.contentWindow.script_clean = step;
483     },
484
485     postproc: function () { /* public */
486         if (this.stopped && !this.xstr_is_ready()) {
487             this.xynt_streaming.reload();
488         }
489     }
490 }
491
492 function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
493 {
494     this.win = win;
495     this.transp_type = transp_type;
496     this.transp_port = transp_port;
497     this.transp_fback = transp_fback;
498     this.console = console;
499     this.gst = gst;
500     this.from = from;
501     this.cookiename = cookiename;
502     this.sess = sess;
503     this.sandbox = sandbox;
504     this.page = page;
505     this.cmdproc = cmdproc;
506     // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
507     this.doc = win.document;
508     this.keepalive_old = -1;
509     this.keepalive_new = -1;
510
511     this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
512     this.mon_wrntime = this.mon_errtime / 2;
513
514     this.mon_update();
515 }
516
517 xynt_streaming.prototype = {
518     win:               null,
519     transp_type:       null,
520     transp_port:         80,
521     transp_fback:         0,
522     transp:            null,
523     console:           null,
524     gst:               null,
525     from:              null,
526     cookiename:        null,
527     sess:              null,
528     sandbox:           null,
529     page:              null,
530     cmdproc:           null,
531
532     start_time:        0,
533     restart_wait:      5000, // wait restart_wait millisec before begin to check if restart is needed
534
535     doc:               null,
536     cookiepath: "/brisk/",
537     watchdog_hdl:      null,
538     hbit:              function () {},
539     keepalive_old:    -1,
540     keepalive_new:    -1,
541     keepalives_equal:  0,
542     /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
543     /* restart after  4 * 40 * 100 millisec if server ping is missing => 16secs */
544     keepalives_eq_max: 4,
545     watchdog_checktm:  40,
546     watchdog_timeout:  100,
547     watchdog_ct:       0,
548     watchable:         false,
549     restart_n:         0,
550     comm_match:        /_*@BEGIN@(.*?)@END@/g,
551     comm_clean:        /_*@BEGIN@(.*?)@END@/,
552     stream:            "",
553     the_end:           false,
554
555     mon_time:         -1,
556     mon_wrntime:       0,
557     mon_errtime:       0,
558
559     mon_stat_old:      "",
560
561     mon_update: function()
562     {
563         var date = new Date();
564
565         this.mon_time = date.getTime();
566     },
567
568     /*
569       ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
570
571       return values: 0 unknown
572                      1 ok
573                      2 warning
574                      3 error
575      */
576     mon_status: function()
577     {
578         var delta, date;
579
580         var date = new Date();
581
582         delta = date.getTime() - this.mon_time;
583
584         if (delta < this.mon_wrntime)
585             return 1;
586         else if (delta < this.mon_errtime)
587             return 2;
588         else
589             return 3;
590     },
591
592     start: function() { /* public */
593         var transp_type;
594         var page;
595
596         // this.log("start "+this.transp_type+" "+this.transp_fback);
597         if (this.the_end)
598             return;
599
600         createCookie(this.cookiename, sess, 24*365, this.cookiepath);
601         // alert("start");
602         this.log("xynt_streaming:start restart: "+this.restart_n);
603         this.keepalives_equal = 0;
604
605         // page arrangement
606         this.page = url_complete(this.win.location.href, this.page);
607
608         if (this.transp_fback > 0) {
609             transp_type = "websocket";
610             transp_port = (this.transp_fback == 2 ? 80 : 8080);
611         }
612         else {
613             transp_type = this.transp_type;
614             transp_port = this.transp_port;
615         }
616
617         if (transp_type == "websocket") {
618             var end_proto, first_slash;
619
620             // change protocol
621             this.log("precha ["+this.page+"]");
622             end_proto = this.page.indexOf("://");
623             first_slash = this.page.substring(end_proto+3).indexOf("/");
624
625             page = "ws://" + this.page.substring(end_proto+3, end_proto+3+first_slash) + ":" + transp_port + this.page.substring(end_proto+3 + first_slash);
626         }
627         else {
628             page = this.page;
629         }
630         // stat, subst, this.gst.st
631
632         page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
633         // this.log("the page:");
634         // this.log(page);
635
636         try {
637             // transport instantiation
638             if (transp_type == "websocket") {
639                 page = url_append_args(page, "transp", "websocket");
640                 this.transp = new transport_ws(this.doc, this, page);
641             }
642             else if (transp_type == "xhr") {
643                 page = url_append_args(page, "transp", "xhr");
644                 this.transp = new transport_xhr(this.doc, this, page);
645             }
646             else if (transp_type == "iframe") {
647                 page = url_append_args(page, "transp", "iframe");
648                 this.transp = new transport_iframe(this.doc, this, page);
649             }
650             else if (transp_type == "htmlfile") {
651                 page = url_append_args(page, "transp", "htmlfile");
652                 this.transp = new transport_htmlfile(this.doc, this, page);
653             }
654             else
655                 return;
656         }
657         catch (err) {
658             if (this.transp_fback > 0) {
659                 this.transp_fback--;
660                 this.start();
661                 return;
662             }
663         }
664
665         // watchdog setting
666         this.watchdog_ct  = 0;
667         if (!this.the_end) {
668             this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
669         }
670
671         var date = new Date();
672         this.start_time = date.getTime();
673     },
674
675     stop: function() {
676         this.the_end = true;
677         this.abort();
678     },
679
680     hbit_set: function (hbit) {
681         this.hbit = hbit;
682     },
683
684
685     hbit_status: function () {
686         var ret;
687
688         ret = this.mon_status();
689         // console.log("mon_status: "+ret+" 0: "+this.mon_time);
690         switch (ret) {
691         case 0:
692             mon_stat = "b";
693             break;
694         case 1:
695             mon_stat = "g";
696             break;
697         case 2:
698             mon_stat = "y";
699             break;
700         case 3:
701             mon_stat = "r";
702             break;
703         }
704
705         if (this.mon_stat_old != mon_stat) {
706             this.hbit(mon_stat);
707             this.mon_stat_old = mon_stat;
708         }
709     },
710
711
712     watchdog: function () {
713         // alert("watchdog");
714         var i, again;
715         var comm_newpart, comm_len, comm_arr;
716         var ctx_new_len;
717
718         if (this.sandbox != null) {
719             // 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;
720             var zug = "WATCHDOG  sess = ["+this.sess+"]  step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;
721             if (zug != this.sandbox.innerHTML)
722                 this.sandbox.innerHTML = zug;
723         }
724
725         // WATCHDOGING THE CONNECTION
726         this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
727         if (!this.watchable) {
728             do {
729                 try{
730                     // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
731                     if (!this.transp.xstr_is_init()) {
732                         this.log("hs::watchdog: xstr_is_init = false");
733                         break;
734                     }
735                 }
736                 catch(b) {
737                     this.log("hs::watchdog: exception");
738                     break;
739                 }
740
741                 /*
742                   on IE7 the the window frame scope is cleaned after the href is set, so we wait
743                   for a well know variable value before assign this object value to it (OO is a passion)
744                 */
745                 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
746                 if (this.transp.xstr_set()) {
747                     // this.ifra.contentWindow.xynt_streaming = this;
748                     this.watchable = true;
749                     this.watchdog_ct = 0;
750                     this.log("hs::watchdog: watchable = yes");
751                 }
752             } while (false);
753         }
754         if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
755             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
756             if (this.keepalive_old == this.keepalive_new) {
757                 this.keepalives_equal++;
758             }
759             else {
760                 this.keepalive_old = this.keepalive_new;
761                 this.keepalives_equal = 0;
762             }
763
764             if (this.keepalives_equal >= this.keepalives_eq_max) {
765                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
766                 this.reload();
767                 // alert("watchdog return reload");
768                 this.hbit_status();
769                 return;
770             }
771         }
772
773         // PICK COMMANDS FROM STREAM
774         do {
775             // alert("do--while begin ["+again+"]");
776             // CHECK: maybe again here isn't needed
777             again = 0;
778             try {
779                 /* if (typeof(this.ifra.contentWindow.ctx_new)     == 'undefined' ||
780                    typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
781                 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
782                     break;
783             }
784             catch(b) {
785                 break;
786             }
787
788             // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
789             ctx_new_len = this.transp.ctx_new_curlen_get();
790             // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
791             if (ctx_new_len <= this.transp.ctx_old_len_get()) {
792                 break;
793             }
794             this.log("new: "+ ctx_new_len + "  old: "+this.transp.ctx_old_len_get());
795             this.keepalive_new++;
796             // alert("pre-loop 1");
797             for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
798                 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
799                 if (this.transp.ctx_new_getchar(i) != '_') {
800                     // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
801                     break;
802                 }
803                 this.mon_update();
804                 this.hbit_status();
805
806                 // else {
807                 //     this.log("ctx_new.charAt(i) == '_'");
808                 // }
809             }
810             // this.ifra.contentWindow.ctx_old_len = i;
811             this.transp.ctx_old_len_set(i);
812             if (i == ctx_new_len) {
813                 this.log("old_len == i");
814                 break;
815             }
816             else {
817                 this.log("old_len != i: "+i);
818             }
819             // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
820
821             comm_newpart = this.transp.new_part();
822             this.log("COM_NEWPART: ["+comm_newpart+"]");
823             comm_len = 0;
824             comm_arr = comm_newpart.match(this.comm_match);
825
826             // alert("do--while middle2 ["+again+"]");
827             if (comm_arr) {
828                 var comm_arr_len = comm_arr.length;
829                 for (i = 0 ; i < comm_arr_len ; i++) {
830                     var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
831                     this.gst.comms = this.gst.comms.concat(temp);
832                     comm_len += comm_arr[i].length;
833                 }
834                 again = 1;
835                 this.mon_update();
836                 this.hbit_status();
837             }
838             // this.ifra.contentWindow.ctx_old_len += comm_len;
839             this.transp.ctx_old_len_add(comm_len);
840             // this.ifra.contentWindow.script_clean = this.gst.st;
841             this.transp.scrcls_set(this.gst.st);
842             // alert("do--while end ["+again+"]");
843         } while (again);
844
845         // alert("post while");
846         // EXECUTION OF STREAM COMMANDS
847         do {
848             again = 0;
849             //MOP ?? xhrrestart = 0;
850             if (this.gst.st_loc < this.gst.st_loc_new) {
851                 // there is some slow actions running
852                 break;
853             }
854             else if (this.gst.comms.length > 0) {
855                 var singlecomm;
856
857                 singlecomm = this.gst.comms.shift();
858                 // alert("EXE"+gugu);
859                 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
860                 //xx this.hbit("+");
861
862                 // alert("SINGLE: ["+singlecomm+"]");
863                 this.cmdproc(singlecomm);
864                 again = 1;
865             }
866         } while (again);
867         this.watchdog_ct++;
868         if (!this.the_end) {
869             var date = new Date();
870             if (date.getTime() > (this.start_time + this.restart_wait)) {
871                 this.transp.postproc();
872             }
873             this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
874             this.hbit_status();
875         }
876         // alert("watchdog return normal");
877
878         return;
879     },
880
881     //
882     // moved to xynt-streaming-ifra as push()
883     //
884     // keepalive: function (s) {
885     //     this.log("hs::keepalive");
886     //     if (s != null) {
887     //         this.log(s);
888     //         this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
889     //     }
890     //     else {
891     //         this.ifra.contentWindow.ctx_new += "_";
892     //     }
893     //     // this.keepalive_new++;
894     // },
895
896     abort: function () { /* public */
897         // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
898
899         this.gst.abort();
900         if (this.watchdog_hdl != null) {
901             clearTimeout(this.watchdog_hdl);
902             this.watchdog_hdl = null;
903         }
904
905         this.restart_n++;
906         this.log("hs::reload");
907         this.watchable = false;
908         if (this.transp != null) {
909             this.transp.destroy();
910             delete this.transp;
911             this.transp = null;
912         }
913     },
914
915     reload: function () {
916         this.abort();
917         this.start(null);
918     },
919
920     log: function (s) {
921         if (this.console != null) {
922             return (this.console.log(s));
923         }
924     }
925 }