visual heartbit added and reload fixed in explorer transport
[brisk.git] / web / xynt-streaming.js
1 // old targetpage == page and moved into start method
2
3 //
4 // CLASS transport_xhr
5 //
6 function transport_xhr(doc, xynt_streaming, page)
7 {
8     this.doc = doc;
9     this.xynt_streaming = xynt_streaming;
10     this.xhr = createXMLHttpRequest();
11     this.xhr.open('GET', page);
12
13     var self = this;
14     this.xhr.onreadystatechange = function () { self.xhr_cb(); };
15     this.xhr.send(null);
16
17     this.stopped = false;
18 }
19
20 transport_xhr.prototype = {
21     doc: null,
22     xynt_streaming: "ready",
23     xhr: null,
24     stopped: true,
25
26     ctx_old: "",
27     ctx_old_len: 0,
28     ctx_new: null,
29
30     // script_clean: 0,
31
32     destroy: function () { /* public */
33         if (this.xhr != null) {
34             this.xhr_abort();
35         }
36         delete this.xhr;
37     },
38
39     xhr_cb: function () {
40         var ret;
41
42         if (this.xhr.readyState == 4) {
43             // console.log("SS: "+safestatus(xhr));
44
45             // NOTE: delay management later
46             // try {
47             //     if ((ret = safestatus(this.xhr)) == 200) {
48             //         this.delay = 0;
49             //         // console.log("del a null "+this.delayed);
50             //     } else if (ret != -1) {
51             //         this.delay = 5000;
52             //         this.hbit('X');
53             //         // alert('There was a problem with the request.' + ret);
54             //     }
55             // } catch(b) {};
56
57             // this.delayed = null;
58             this.stopped = true;
59         }
60     },
61
62     xhr_abort: function() {
63         if (this.xhr != null) {
64             this.xhr.abort();
65         }
66     },
67
68     xstr_is_init: function () { /* public */
69         try {
70             if (this.xhr.responseText != null) {
71                 this.ctx_new = this.xhr.responseText;
72             }
73         }
74         catch (e) {
75         }
76
77         return (this.ctx_new != null);
78     },
79
80     /* only after a successfull is_initialized call */
81     xstr_is_ready: function () { /* public */
82         return (this.xynt_streaming == "ready");
83     },
84
85     xstr_set: function () { /* public */
86         // already set
87     },
88
89     ctx_new_is_set: function () { /* public */
90         return (this.ctx_new != null);
91     },
92
93     ctx_new_curlen_get: function () { /* public */
94         return (this.ctx_new.length);
95     },
96
97     ctx_new_getchar: function(idx) { /* public */
98         return (this.ctx_new[idx]);
99     },
100
101     ctx_old_len_is_set: function () { /* public */
102         return (true);
103     },
104
105     ctx_old_len_get: function () { /* public */
106         return (this.ctx_old_len);
107     },
108
109     ctx_old_len_set: function (len) { /* public */
110         this.ctx_old_len = len;
111     },
112
113     ctx_old_len_add: function (len) { /* public */
114         this.ctx_old_len += len;
115     },
116
117     new_part: function () { /* public */
118         return (this.ctx_new.substr(this.ctx_old_len));
119     },
120
121     scrcls_set: function (step) { /* public */
122         // this.script_clean = step;
123     },
124
125     postproc: function () {
126         if (this.stopped && !this.xstr_is_ready()) {
127             this.xynt_streaming.reload();
128         }
129     }
130 }
131
132 //
133 // CLASS transport_htmlfile
134 //
135 function transport_htmlfile(doc, xynt_streaming, page)
136 {
137     this.doc = doc;
138     this.xynt_streaming = xynt_streaming;
139     this.transfdoc = new ActiveXObject("htmlfile");
140     this.transfdoc.open();
141     this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
142     this.transfdoc.close();
143
144     this.ifra = this.transfdoc.getElementById("iframe");
145     this.ifra.contentWindow.location.href = page;
146     this.stopped = false;
147 }
148
149 transport_htmlfile.prototype = {
150     doc: null,
151     xynt_streaming: null,
152     stopped: true,
153     ifra: null,
154     tradoc: null,
155
156     destroy: function () { /* public */
157         if (this.ifra != null) {
158         //     this.doc.body.removeChild(this.ifra);
159         //     delete this.ifra;
160              this.ifra = null;
161         }
162
163         if (this.transfdoc) {
164             delete this.transfdoc;
165             this.transfdoc = null;
166         }
167     },
168
169     xstr_is_init: function () { /* public */
170         return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
171     },
172
173     /* only after a successfull is_initialized call */
174     xstr_is_ready: function () { /* public */
175         return (this.ifra.contentWindow.xynt_streaming == "ready");
176     },
177
178     /* only after a successfull is_initialized call */
179     xstr_set: function () { /* public */
180         if (this.ifra.contentWindow.xynt_streaming == "ready") {
181             this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
182             return (true);
183         }
184         else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
185             return (true);
186         }
187         else {
188             return (false);
189         }
190     },
191
192     ctx_new_is_set: function () { /* public */
193         return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
194     },
195
196     ctx_new_curlen_get: function () { /* public */
197         return (this.ifra.contentWindow.ctx_new.length);
198     },
199
200     ctx_new_getchar: function(idx) { /* public */
201         return (this.ifra.contentWindow.ctx_new.charAt(idx));
202
203     },
204
205     ctx_old_len_is_set: function () { /* public */
206         return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
207     },
208
209     ctx_old_len_get: function () { /* public */
210         return (this.ifra.contentWindow.ctx_old_len);
211     },
212
213     ctx_old_len_set: function (len) { /* public */
214         this.ifra.contentWindow.ctx_old_len = len;
215     },
216
217     ctx_old_len_add: function (len) { /* public */
218         this.ifra.contentWindow.ctx_old_len += len;
219     },
220
221     new_part: function () { /* public */
222         return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
223     },
224
225     scrcls_set: function (step) { /* public */
226         this.ifra.contentWindow.script_clean = step;
227     },
228
229     postproc: function () { /* public */
230         if (this.stopped && !this.xstr_is_ready()) {
231             this.xynt_streaming.reload();
232         }
233     }
234 }
235
236
237
238 //
239 // CLASS transport_iframe
240 //
241 function transport_iframe(doc, xynt_streaming, page)
242 {
243     this.doc = doc;
244     this.xynt_streaming = xynt_streaming;
245     this.ifra = doc.createElement("iframe");
246     this.ifra.style.visibility = "hidden";
247     doc.body.appendChild(this.ifra);
248     this.ifra.contentWindow.location.href = page;
249     this.stopped = false;
250 }
251
252 transport_iframe.prototype = {
253     doc: null,
254     xynt_streaming: null,
255     stopped: true,
256     ifra: null,
257
258     destroy: function () { /* public */
259         try {
260             if (this.ifra != null) {
261                 // NOTE:  on Opera this remove child crash js if called from
262                 //        inside of the iframe, on IE on Windows without
263                 //        it stream abort fails.
264                 //        the problem is fixed setting into the iframe's onload
265                 //        function the stopped attribute to true and delegate
266                 //        postproc() fired by xynt_streaming watchdog()
267                 this.doc.body.removeChild(this.ifra);
268                 delete this.ifra;
269                 this.ifra = null;
270             }
271         } catch (b) {
272             alert("destroy exception catched");
273         }
274     },
275
276     xstr_is_init: function () { /* public */
277         return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
278     },
279
280     /* only after a successfull is_initialized call */
281     xstr_is_ready: function () { /* public */
282         return (this.ifra.contentWindow.xynt_streaming == "ready");
283     },
284
285     /* only after a successfull is_initialized call */
286     xstr_set: function () { /* public */
287         if (this.ifra.contentWindow.xynt_streaming == "ready") {
288             this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
289             return (true);
290         }
291         else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
292             return (true);
293         }
294         else {
295             return (false);
296         }
297     },
298
299
300     /* only after a successfull is_ready call to be sure the accessibility of the var */
301     xstr_set_old: function (xynt_streaming) { /* public */
302         this.ifra.contentWindow.xynt_streaming = xynt_streaming;
303     },
304
305     ctx_new_is_set: function () { /* public */
306         return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
307     },
308
309     ctx_new_curlen_get: function () { /* public */
310         return (this.ifra.contentWindow.ctx_new.length);
311     },
312
313     ctx_new_getchar: function(idx) { /* public */
314         return (this.ifra.contentWindow.ctx_new.charAt(idx));
315     },
316
317     ctx_old_len_is_set: function () { /* public */
318         return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
319     },
320
321     ctx_old_len_get: function () { /* public */
322         return (this.ifra.contentWindow.ctx_old_len);
323     },
324
325     ctx_old_len_set: function (len) { /* public */
326         this.ifra.contentWindow.ctx_old_len = len;
327     },
328
329     ctx_old_len_add: function (len) { /* public */
330         this.ifra.contentWindow.ctx_old_len += len;
331     },
332
333     new_part: function () { /* public */
334         return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
335     },
336
337     scrcls_set: function (step) { /* public */
338         this.ifra.contentWindow.script_clean = step;
339     },
340
341     postproc: function () { /* public */
342         if (this.stopped && !this.xstr_is_ready()) {
343             this.xynt_streaming.reload();
344         }
345     }
346 }
347
348 function xynt_streaming(win, transp_type, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
349 {
350     this.win = win;
351     this.transp_type = transp_type;
352     this.console = console;
353     this.gst = gst;
354     this.from = from;
355     this.cookiename = cookiename;
356     this.sess = sess;
357     this.sandbox = sandbox;
358     this.page = page;
359     this.cmdproc = cmdproc;
360     // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
361     this.doc = win.document;
362     this.keepalive_old = -1;
363     this.keepalive_new = -1;
364 }
365
366 xynt_streaming.prototype = {
367     win:               null,
368     transp_type:       null,
369     transp:            null,
370     console:           null,
371     gst:               null,
372     from:              null,
373     cookiename:        null,
374     sess:              null,
375     sandbox:           null,
376     page:              null,
377     cmdproc:           null,
378
379     start_time:        0,
380     restart_wait:      5000, // wait restart_wait millisec before begin to check if restart is needed
381
382     doc:               null,
383     cookiepath: "/brisk/",
384     watchdog_hdl:      null,
385     hbit:              function () {},
386     keepalive_old:    -1,
387     keepalive_new:    -1,
388     keepalives_equal:  0,
389     /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
390     /* restart after  4 * 40 * 100 millisec if server ping is missing => 16secs */
391     keepalives_eq_max: 4,
392     watchdog_checktm:  40,
393     watchdog_timeout:  100,
394     watchdog_ct:       0,
395     watchable:         false,
396     restart_n:         0,
397     comm_match:        /_*@BEGIN@(.*?)@END@/g, 
398     comm_clean:        /_*@BEGIN@(.*?)@END@/,
399     stream:            "",
400     the_end:           false,
401
402     w_stat_old:        "",
403     s_stat_old:        "",
404
405     start: function() { /* public */
406         if (this.the_end) 
407             return;
408
409         createCookie(this.cookiename, sess, 24*365, this.cookiepath);
410         // alert("start");
411         this.log("xynt_streaming:start restart: "+this.restart_n);
412         this.keepalives_equal = 0;
413
414         // page arrangement
415         this.page = url_complete(this.win.location.href, this.page);
416         // stat, subst, this.gst.st
417
418         this.page = url_append_args(this.page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
419         this.log(this.page);
420
421         // transport instantiation
422         if (this.transp_type == "xhr") {
423             this.page = url_append_args(this.page, "transp", "xhr");
424             this.transp = new transport_xhr(this.doc, this, this.page);
425         }
426         else if (this.transp_type == "iframe") {
427             this.page = url_append_args(this.page, "transp", "iframe");
428             this.transp = new transport_iframe(this.doc, this, this.page);
429         }
430         else if (this.transp_type == "htmlfile") {
431             this.page = url_append_args(this.page, "transp", "htmlfile");
432             this.transp = new transport_htmlfile(this.doc, this, this.page);
433         }
434         else
435             return;
436
437         // watchdog setting
438         this.watchdog_ct  = 0;
439         if (!this.the_end) {
440             this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
441         }
442
443         var date = new Date();
444         this.start_time = date.getTime();
445     },
446
447     stop: function() {
448         this.the_end = true;
449         this.abort();
450     },
451
452     hbit_set: function (hbit) {
453         this.hbit = hbit;
454     },
455
456     hbit_status: function () {
457         if (this.watchdog_hdl)
458             w_stat = "g";
459         else
460             w_stat = "r";
461
462         if (this.transp.ctx_new_is_set() &&
463             this.transp.ctx_new_curlen_get() > 0) {
464             if (this.keepalives_equal == 0) {
465                 s_stat = "g";
466             }
467             else if (this.keepalives_equal < this.keepalives_eq_max) {
468                 s_stat = "y";
469             }
470             else {
471                 s_stat = "r";
472             }
473         }
474         else {
475             s_stat = "r";
476         }
477
478         if (this.s_stat_old != s_stat ||
479             this.w_stat_old != w_stat)
480             this.hbit(s_stat, w_stat);
481         this.s_stat_old = s_stat;
482         this.w_stat_old = w_stat;
483     },
484
485     watchdog: function () {
486         // alert("watchdog");
487         var i, again;
488         var comm_newpart, comm_len, comm_arr;
489         var ctx_new_len;
490
491         this.watchdog_hdl = null;
492
493         this.hbit_status();
494         if (this.sandbox != null) {
495             // 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;
496             var zug = "WATCHDOG  sess = ["+this.sess+"]  step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;          
497             if (zug != this.sandbox.innerHTML)
498                 this.sandbox.innerHTML = zug;
499         }
500
501         // WATCHDOGING THE CONNECTION
502         this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
503         if (!this.watchable) {
504             do {
505                 try{
506                     // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
507                     if (!this.transp.xstr_is_init()) {
508                         this.log("hs::watchdog: xstr_is_init = false");
509                         break;
510                     }
511                 }
512                 catch(b) {
513                     this.log("hs::watchdog: exception");
514                     break;
515                 }
516
517                 /*
518                   on IE7 the the window frame scope is cleaned after the href is set, so we wait 
519                   for a well know variable value before assign this object value to it (OO is a passion)
520                 */
521                 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
522                 if (this.transp.xstr_set()) {
523                     // this.ifra.contentWindow.xynt_streaming = this;
524                     this.watchable = true;
525                     this.watchdog_ct = 0;
526                     this.log("hs::watchdog: watchable = yes");
527                 }
528             } while (false);
529         }
530         if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
531             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
532
533             // alert("qui "+this.transp.ctx_new_curlen_get()+" "+this.transp.ctx_old_len_get();
534             if (this.keepalive_old == this.keepalive_new) {
535                 this.keepalives_equal++;
536             }
537             else {
538                 this.keepalive_old = this.keepalive_new;
539                 this.keepalives_equal = 0;
540             }
541             
542             if (this.keepalives_equal >= this.keepalives_eq_max) {
543                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
544                 this.reload();
545                 // alert("watchdog return reload");
546                 return;
547             }
548         }
549
550         // PICK COMMANDS FROM STREAM
551         do {
552             // alert("do--while begin ["+again+"]");
553             // CHECK: maybe again here isn't needed 
554             again = 0;
555             try {
556                 /* if (typeof(this.ifra.contentWindow.ctx_new)     == 'undefined' ||
557                    typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
558                 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
559                     break;
560             }
561             catch(b) {
562                 break;
563             }
564             // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
565             ctx_new_len = this.transp.ctx_new_curlen_get();
566             this.log("new_len: "+ ctx_new_len);
567             // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
568             if (ctx_new_len <= this.transp.ctx_old_len_get()) {
569                 break;
570             }
571             this.log("new: "+ ctx_new_len + "  old: "+this.transp.ctx_old_len_get());
572             this.keepalive_new++;
573             // alert("pre-loop 1");
574             for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
575                 // alert("ctx_new_getchar: "+this.transp.ctx_new_getchar(i));
576
577                 if (this.transp.ctx_new_getchar(i) != '_') {
578                     // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
579                     break;
580                 }
581                 // else {
582                 //     this.log("ctx_new.charAt(i) == '_'");
583                 // }
584             }
585             // this.ifra.contentWindow.ctx_old_len = i;
586             this.transp.ctx_old_len_set(i);
587             if (i == ctx_new_len) {
588                 this.log("old_len == i");
589                 break;
590             }
591             else {
592                 this.log("old_len != i: "+i);
593             }
594             // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
595
596             comm_newpart = this.transp.new_part();
597             this.log("COM_NEWPART: ["+comm_newpart+"]");
598             comm_len = 0;
599             comm_arr = comm_newpart.match(this.comm_match);
600
601             // alert("do--while middle2 ["+again+"]");
602             if (comm_arr) {
603                 var comm_arr_len = comm_arr.length;
604                 for (i = 0 ; i < comm_arr_len ; i++) {
605                     var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
606                     this.gst.comms = this.gst.comms.concat(temp);
607                     comm_len += comm_arr[i].length;
608                 }
609                 again = 1;
610             }
611             // this.ifra.contentWindow.ctx_old_len += comm_len;
612             this.transp.ctx_old_len_add(comm_len);
613             // this.ifra.contentWindow.script_clean = this.gst.st;
614             this.transp.scrcls_set(this.gst.st);
615             // alert("do--while end ["+again+"]");
616         } while (again);
617
618         // alert("post while");
619         // EXECUTION OF STREAM COMMANDS
620         do {
621             again = 0;
622             //MOP ?? xhrrestart = 0;
623             if (this.gst.st_loc < this.gst.st_loc_new) {
624                 // there is some slow actions running
625                 break;
626             }
627             else if (this.gst.comms.length > 0) {
628                 var singlecomm;
629                 
630                 singlecomm = this.gst.comms.shift();
631                 // alert("EXE"+gugu);
632                 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
633                 //xx this.hbit("+");
634
635                 // alert("SINGLE: ["+singlecomm+"]");
636                 this.cmdproc(singlecomm);
637                 again = 1;
638             }
639         } while (again);
640         this.watchdog_ct++;
641         if (!this.the_end) {
642             var date = new Date();
643             if (date.getTime() > (this.start_time + this.restart_wait)) {
644                 this.transp.postproc();
645             }
646             this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
647             this.hbit_status();
648         }
649         // alert("watchdog return normal");
650
651         return;
652     },
653
654     //
655     // moved to xynt-streaming-ifra as push()
656     //
657     // keepalive: function (s) {
658     //     this.log("hs::keepalive");
659     //     if (s != null) {
660     //         this.log(s);
661     //         this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
662     //     }
663     //     else {
664     //         this.ifra.contentWindow.ctx_new += "_";
665     //     }
666     //     // this.keepalive_new++;
667     // },
668
669     abort: function () { /* public */
670         // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
671
672         this.gst.abort();
673         if (this.watchdog_hdl != null) {
674             clearTimeout(this.watchdog_hdl);
675             this.watchdog_hdl = null;
676         }
677
678         this.restart_n++;
679         this.log("hs::reload");
680         this.watchable = false;
681         if (this.transp != null) {
682             this.transp.destroy();
683             delete this.transp;
684             this.transp = null;
685         }
686     },
687
688     reload: function () {
689         this.abort();
690         this.start(null);
691     },
692
693     log: function (s) {
694         if (this.console != null) {
695             return (this.console.log(s));
696         }
697     }
698 }