xhr transport added
[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     },
202
203     ctx_old_len_is_set: function () { /* public */
204         return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
205     },
206
207     ctx_old_len_get: function () { /* public */
208         return (this.ifra.contentWindow.ctx_old_len);
209     },
210
211     ctx_old_len_set: function (len) { /* public */
212         this.ifra.contentWindow.ctx_old_len = len;
213     },
214
215     ctx_old_len_add: function (len) { /* public */
216         this.ifra.contentWindow.ctx_old_len += len;
217     },
218
219     new_part: function () { /* public */
220         return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
221     },
222
223     scrcls_set: function (step) { /* public */
224         this.ifra.contentWindow.script_clean = step;
225     },
226
227     postproc: function () { /* public */
228         if (this.stopped && !this.xstr_is_ready()) {
229             this.xynt_streaming.reload();
230         }
231     }
232 }
233
234
235
236 //
237 // CLASS transport_iframe
238 //
239 function transport_iframe(doc, xynt_streaming, page)
240 {
241     this.doc = doc;
242     this.xynt_streaming = xynt_streaming;
243     this.ifra = doc.createElement("iframe");
244     this.ifra.style.visibility = "hidden";
245     doc.body.appendChild(this.ifra);
246     this.ifra.contentWindow.location.href = page;
247     this.stopped = false;
248 }
249
250 transport_iframe.prototype = {
251     doc: null,
252     xynt_streaming: null,
253     stopped: true,
254     ifra: null,
255
256     destroy: function () { /* public */
257         try {
258             if (this.ifra != null) {
259                 // NOTE:  on Opera this remove child crash js if called from
260                 //        inside of the iframe, on IE on Windows without
261                 //        it stream abort fails.
262                 //        the problem is fixed setting into the iframe's onload
263                 //        function the stopped attribute to true and delegate
264                 //        postproc() fired by xynt_streaming watchdog()
265                 this.doc.body.removeChild(this.ifra);
266                 delete this.ifra;
267                 this.ifra = null;
268             }
269         } catch (b) {
270             alert("destroy exception catched");
271         }
272     },
273
274     xstr_is_init: function () { /* public */
275         return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
276     },
277
278     /* only after a successfull is_initialized call */
279     xstr_is_ready: function () { /* public */
280         return (this.ifra.contentWindow.xynt_streaming == "ready");
281     },
282
283     /* only after a successfull is_initialized call */
284     xstr_set: function () { /* public */
285         if (this.ifra.contentWindow.xynt_streaming == "ready") {
286             this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
287             return (true);
288         }
289         else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
290             return (true);
291         }
292         else {
293             return (false);
294         }
295     },
296
297
298     /* only after a successfull is_ready call to be sure the accessibility of the var */
299     xstr_set_old: function (xynt_streaming) { /* public */
300         this.ifra.contentWindow.xynt_streaming = xynt_streaming;
301     },
302
303     ctx_new_is_set: function () { /* public */
304         return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
305     },
306
307     ctx_new_curlen_get: function () { /* public */
308         return (this.ifra.contentWindow.ctx_new.length);
309     },
310
311     ctx_new_getchar: function(idx) { /* public */
312     },
313
314     ctx_old_len_is_set: function () { /* public */
315         return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
316     },
317
318     ctx_old_len_get: function () { /* public */
319         return (this.ifra.contentWindow.ctx_old_len);
320     },
321
322     ctx_old_len_set: function (len) { /* public */
323         this.ifra.contentWindow.ctx_old_len = len;
324     },
325
326     ctx_old_len_add: function (len) { /* public */
327         this.ifra.contentWindow.ctx_old_len += len;
328     },
329
330     new_part: function () { /* public */
331         return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
332     },
333
334     scrcls_set: function (step) { /* public */
335         this.ifra.contentWindow.script_clean = step;
336     },
337
338     postproc: function () { /* public */
339         if (this.stopped && !this.xstr_is_ready()) {
340             this.xynt_streaming.reload();
341         }
342     }
343 }
344
345 function xynt_streaming(win, transp_type, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
346 {
347     this.win = win;
348     this.transp_type = transp_type;
349     this.console = console;
350     this.gst = gst;
351     this.from = from;
352     this.cookiename = cookiename;
353     this.sess = sess;
354     this.sandbox = sandbox;
355     this.page = page;
356     this.cmdproc = cmdproc;
357     // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
358     this.doc = win.document;
359     this.keepalive_old = -1;
360     this.keepalive_new = -1;
361 }
362
363 xynt_streaming.prototype = {
364     win:               null,
365     transp_type:       null,
366     transp:            null,
367     console:           null,
368     gst:               null,
369     from:              null,
370     cookiename:        null,
371     sess:              null,
372     sandbox:           null,
373     page:              null,
374     cmdproc:           null,
375
376     start_time:        0,
377     restart_wait:      5000, // wait restart_wait millisec before begin to check if restart is needed
378
379     doc:               null,
380     cookiepath: "/brisk/",
381     watchdog_hdl:      null,
382     hbit:              null,
383     keepalive_old:    -1,
384     keepalive_new:    -1,
385     keepalives_equal:  0,
386     /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
387     /* restart after  4 * 40 * 100 millisec if server ping is missing => 16secs */
388     keepalives_eq_max: 4,
389     watchdog_checktm:  40,
390     watchdog_timeout:  100,
391     watchdog_ct:       0,
392     watchable:         false,
393     restart_n:         0,
394     comm_match:        /_*@BEGIN@(.*?)@END@/g, 
395     comm_clean:        /_*@BEGIN@(.*?)@END@/,
396     stream:            "",
397     the_end:           false,
398
399     start: function() { /* public */
400         if (this.the_end) 
401             return;
402
403         createCookie(this.cookiename, sess, 24*365, this.cookiepath);
404         // alert("start");
405         this.log("xynt_streaming:start restart: "+this.restart_n);
406         this.keepalives_equal = 0;
407
408         // page arrangement
409         this.page = url_complete(this.win.location.href, this.page);
410         // stat, subst, this.gst.st
411
412         this.page = url_append_args(this.page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
413         this.log(this.page);
414
415         // transport instantiation
416         if (this.transp_type == "xhr") {
417             this.page = url_append_args(this.page, "transp", "xhr");
418             this.transp = new transport_xhr(this.doc, this, this.page);
419         }
420         else if (this.transp_type == "iframe") {
421             this.page = url_append_args(this.page, "transp", "iframe");
422             this.transp = new transport_iframe(this.doc, this, this.page);
423         }
424         else if (this.transp_type == "htmlfile") {
425             this.page = url_append_args(this.page, "transp", "htmlfile");
426             this.transp = new transport_htmlfile(this.doc, this, this.page);
427         }
428         else
429             return;
430
431         // watchdog setting
432         this.watchdog_ct  = 0;
433         if (!this.the_end) {
434             this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
435         }
436
437         var date = new Date();
438         this.start_time = date.getTime();
439     },
440
441     stop: function() {
442         this.the_end = true;
443         this.abort();
444     },
445
446     hbit_set: function (hbit) {
447         this.hbit = hbit;
448     },
449
450     watchdog: function () {
451         // alert("watchdog");
452         var i, again;
453         var comm_newpart, comm_len, comm_arr;
454         var ctx_new_len;
455
456         if (this.sandbox != null) {
457             // 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;
458             var zug = "WATCHDOG  sess = ["+this.sess+"]  step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;          
459             if (zug != this.sandbox.innerHTML)
460                 this.sandbox.innerHTML = zug;
461         }
462
463         // WATCHDOGING THE CONNECTION
464         this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
465         if (!this.watchable) {
466             do {
467                 try{
468                     // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
469                     if (!this.transp.xstr_is_init()) {
470                         this.log("hs::watchdog: xstr_is_init = false");
471                         break;
472                     }
473                 }
474                 catch(b) {
475                     this.log("hs::watchdog: exception");
476                     break;
477                 }
478
479                 /*
480                   on IE7 the the window frame scope is cleaned after the href is set, so we wait 
481                   for a well know variable value before assign this object value to it (OO is a passion)
482                 */
483                 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
484                 if (this.transp.xstr_set()) {
485                     // this.ifra.contentWindow.xynt_streaming = this;
486                     this.watchable = true;
487                     this.watchdog_ct = 0;
488                     this.log("hs::watchdog: watchable = yes");
489                 }
490             } while (false);
491         }
492         if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
493             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
494             if (this.keepalive_old == this.keepalive_new) {
495                 this.keepalives_equal++;
496             }
497             else {
498                 this.keepalive_old = this.keepalive_new;
499                 this.keepalives_equal = 0;
500             }
501             
502             if (this.keepalives_equal >= this.keepalives_eq_max) {
503                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
504                 this.reload();
505                 // alert("watchdog return reload");
506                 return;
507             }
508         }
509
510         // PICK COMMANDS FROM STREAM
511         do {
512             // alert("do--while begin ["+again+"]");
513             // CHECK: maybe again here isn't needed 
514             again = 0;
515             try {
516                 /* if (typeof(this.ifra.contentWindow.ctx_new)     == 'undefined' ||
517                    typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
518                 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
519                     break;
520             }
521             catch(b) {
522                 break;
523             }
524
525             // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
526             ctx_new_len = this.transp.ctx_new_curlen_get();
527             // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
528             if (ctx_new_len <= this.transp.ctx_old_len_get()) {
529                 break;
530             }
531             this.log("new: "+ ctx_new_len + "  old: "+this.transp.ctx_old_len_get());
532             this.keepalive_new++;
533             // alert("pre-loop 1");
534             for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
535                 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
536                 if (this.transp.ctx_new_getchar(i) != '_') {
537                     // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
538                     break;
539                 }
540                 // else {
541                 //     this.log("ctx_new.charAt(i) == '_'");
542                 // }
543             }
544             // this.ifra.contentWindow.ctx_old_len = i;
545             this.transp.ctx_old_len_set(i);
546             if (i == ctx_new_len) {
547                 this.log("old_len == i");
548                 break;
549             }
550             else {
551                 this.log("old_len != i: "+i);
552             }
553             // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
554
555             comm_newpart = this.transp.new_part();
556             this.log("COM_NEWPART: ["+comm_newpart+"]");
557             comm_len = 0;
558             comm_arr = comm_newpart.match(this.comm_match);
559
560             // alert("do--while middle2 ["+again+"]");
561             if (comm_arr) {
562                 var comm_arr_len = comm_arr.length;
563                 for (i = 0 ; i < comm_arr_len ; i++) {
564                     var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
565                     this.gst.comms = this.gst.comms.concat(temp);
566                     comm_len += comm_arr[i].length;
567                 }
568                 again = 1;
569             }
570             // this.ifra.contentWindow.ctx_old_len += comm_len;
571             this.transp.ctx_old_len_add(comm_len);
572             // this.ifra.contentWindow.script_clean = this.gst.st;
573             this.transp.scrcls_set(this.gst.st);
574             // alert("do--while end ["+again+"]");
575         } while (again);
576
577         // alert("post while");
578         // EXECUTION OF STREAM COMMANDS
579         do {
580             again = 0;
581             //MOP ?? xhrrestart = 0;
582             if (this.gst.st_loc < this.gst.st_loc_new) {
583                 // there is some slow actions running
584                 break;
585             }
586             else if (this.gst.comms.length > 0) {
587                 var singlecomm;
588                 
589                 singlecomm = this.gst.comms.shift();
590                 // alert("EXE"+gugu);
591                 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
592                 //xx this.hbit("+");
593
594                 // alert("SINGLE: ["+singlecomm+"]");
595                 this.cmdproc(singlecomm);
596                 again = 1;
597             }
598         } while (again);
599         this.watchdog_ct++;
600         if (!this.the_end) {
601             var date = new Date();
602             if (date.getTime() > (this.start_time + this.restart_wait)) {
603                 this.transp.postproc();
604             }
605             this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
606         }
607         // alert("watchdog return normal");
608
609         return;
610     },
611
612     //
613     // moved to xynt-streaming-ifra as push()
614     //
615     // keepalive: function (s) {
616     //     this.log("hs::keepalive");
617     //     if (s != null) {
618     //         this.log(s);
619     //         this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
620     //     }
621     //     else {
622     //         this.ifra.contentWindow.ctx_new += "_";
623     //     }
624     //     // this.keepalive_new++;
625     // },
626
627     abort: function () { /* public */
628         // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
629
630         this.gst.abort();
631         if (this.watchdog_hdl != null) {
632             clearTimeout(this.watchdog_hdl);
633             this.watchdog_hdl = null;
634         }
635
636         this.restart_n++;
637         this.log("hs::reload");
638         this.watchable = false;
639         if (this.transp != null) {
640             this.transp.destroy();
641             delete this.transp;
642             this.transp = null;
643         }
644     },
645
646     reload: function () {
647         this.abort();
648         this.start(null);
649     },
650
651     log: function (s) {
652         if (this.console != null) {
653             return (this.console.log(s));
654         }
655     }
656 }