correct watchdog_timeout value note added
[brisk.git] / web / xynt-http-streaming.js
1 // old targetpage == page and moved into start method
2
3 function http_streaming(win, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
4 {
5     this.win = win;
6     this.console = console;
7     this.gst = gst;
8     this.from = from;
9     this.cookiename = cookiename;
10     this.sess = sess;
11     this.sandbox = sandbox;
12     this.page = page;
13     this.cmdproc = cmdproc;
14     // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
15
16     this.doc = win.document;
17     this.keepalive_old = -1;
18     this.keepalive_new = -1;
19 }
20
21 http_streaming.prototype = {
22     win:               null,
23     console:           null,
24     gst:               null,
25     from:              null,
26     cookiename:        null,
27     sess:              null,
28     sandbox:           null,
29     page:              null,
30     cmdproc:           null,
31
32     doc:               null,
33     ifra:              null,
34     cookiepath: "/brisk/",
35     watchdog_hdl:      null,
36     hbit:              null,
37     keepalive_old:    -1,
38     keepalive_new:    -1,
39     keepalives_equal:  0,
40     keepalives_eq_max: 6,
41     /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
42     watchdog_timeout:  100,
43     watchdog_ct:       0,
44     watchdog_checktm:  20,
45     watchable:         false,
46     restart_n:         0,
47     comm_match:        /_*@BEGIN@(.*?)@END@/g, 
48     comm_clean:        /_*@BEGIN@(.*?)@END@/,
49     stream:            "",
50     the_end:           false,
51
52     start: function() { /* public */
53         if (this.the_end) 
54             return;
55
56         createCookie(this.cookiename, sess, 24*365, this.cookiepath);
57         // alert("start");
58         this.log("http_streaming:start restart: "+this.restart_n);
59         this.keepalives_equal = 0;
60         this.ifra = this.doc.createElement("iframe");
61         this.ifra.style.visibility = "hidden";
62         this.doc.body.appendChild(this.ifra);
63         this.page = url_complete(this.win.location.href, this.page);
64         // stat, subst, this.gst.st
65
66         this.page = url_append_args(this.page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
67         // alert(this.page);
68         this.log(this.page);
69
70         // this.log(this.ifra);
71         this.ifra.contentWindow.location.href = this.page;
72         this.watchdog_ct  = 0;
73         if (!this.the_end) {
74             this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, 0, this);
75         }
76     },
77
78     stop: function() {
79         this.the_end = true;
80         this.abort();
81     },
82
83     hbit_set: function (hbit) {
84         this.hbit = hbit;
85     },
86
87     watchdog: function () {
88         // alert("watchdog");
89         var i, again;
90         var comm_newpart, comm_len, comm_arr;
91         var ctx_new_len;
92
93         if (this.sandbox != null) {
94             // 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;
95             var zug = "WATCHDOG step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;          
96             if (zug != this.sandbox.innerHTML)
97                 this.sandbox.innerHTML = zug;
98         }
99
100         // WATCHDOGING THE CONNECTION
101         this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
102         if ( (this.watchdog_ct % this.watchdog_checktm) == 0 || !this.watchable) {
103             if (!this.watchable) {
104                 do {
105                     try{
106                         if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
107                             break;
108                     }
109                     catch(b) {
110                         break;
111                     }
112
113                     /*
114                       on IE7 the the window frame scope is cleaned after the href is set, so we wait 
115                       for a well know variable value before assign this object value to it (OO is a passion)
116                     */
117                     if (this.ifra.contentWindow.http_streaming == "ready") {
118                         this.ifra.contentWindow.http_streaming = this;
119                         this.watchable = true;
120                         this.log("hs::watchdog: watchable = yes");
121                     }
122                 } while (false);
123             }
124             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
125             if (this.keepalive_old == this.keepalive_new) {
126                 this.keepalives_equal++;
127             }
128             else {
129                 this.keepalive_old = this.keepalive_new;
130                 this.keepalives_equal = 0;
131             }
132             
133             if (this.keepalives_equal > this.keepalives_eq_max) {
134                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
135                 this.reload();
136                 // alert("watchdog return reload");
137                 return;
138             }
139         }
140
141         // PICK COMMANDS FROM STREAM
142         do {
143             // alert("do--while begin ["+again+"]");
144             // CHECK: maybe again here isn't needed 
145             again = 0;
146             try {
147                 if (typeof(this.ifra.contentWindow.ctx_new)     == 'undefined' ||
148                     typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined')
149                     break;
150             }
151             catch(b) {
152                 break;
153             }
154             
155             ctx_new_len = this.ifra.contentWindow.ctx_new.length;
156             if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
157                 break;
158             }
159             this.log("new: "+ ctx_new_len + "  old: "+this.ifra.contentWindow.ctx_old_len);
160             this.keepalive_new++;
161             // alert("pre-loop 1");
162             for (i = this.ifra.contentWindow.ctx_old_len ; i < ctx_new_len ; i++) {
163                 if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
164                     // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
165                     break;
166                 }
167                 // else {
168                 //     this.log("ctx_new.charAt(i) == '_'");
169                 // }
170             }
171             this.ifra.contentWindow.ctx_old_len = i;
172             if (i == ctx_new_len) {
173                 this.log("old_len == i");
174                 break;
175             }
176             else {
177                 this.log("old_len != i: "+i);
178             }
179             // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
180
181             comm_newpart = this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len);    
182             this.log("COM_NEWPART: ["+comm_newpart+"]");
183             comm_len = 0;
184             comm_arr = comm_newpart.match(this.comm_match);
185
186             // alert("do--while middle2 ["+again+"]");
187             if (comm_arr) {
188                 var comm_arr_len = comm_arr.length;
189                 for (i = 0 ; i < comm_arr_len ; i++) {
190                     var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
191                     this.gst.comms = this.gst.comms.concat(temp);
192                     comm_len += comm_arr[i].length;
193                 }
194                 again = 1;
195             }
196             this.ifra.contentWindow.ctx_old_len += comm_len;
197             this.ifra.contentWindow.script_clean = this.gst.st;
198             // alert("do--while end ["+again+"]");
199         } while (again);
200
201         // alert("post while");
202         // EXECUTION OF STREAM COMMANDS
203         do {
204             again = 0;
205             //MOP ?? xhrrestart = 0;
206             if (this.gst.st_loc < this.gst.st_loc_new) {
207                 // there is some slow actions running
208                 break;
209             }
210             else if (this.gst.comms.length > 0) {
211                 var singlecomm;
212                 
213                 singlecomm = this.gst.comms.shift();
214                 // alert("EXE"+gugu);
215                 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
216                 //xx this.hbit("+");
217
218                 // alert("SINGLE: ["+singlecomm+"]");
219                 this.cmdproc(singlecomm);
220                 again = 1;
221             }
222         } while (again);
223         this.watchdog_ct++;
224         if (!this.the_end) {
225             this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
226         }
227         // alert("watchdog return normal");
228
229         return;
230     },
231
232     //
233     // moved to xynt-http-streaming-ifra as push()
234     //
235     // keepalive: function (s) {
236     //     this.log("hs::keepalive");
237     //     if (s != null) {
238     //         this.log(s);
239     //         this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
240     //     }
241     //     else {
242     //         this.ifra.contentWindow.ctx_new += "_";
243     //     }
244     //     // this.keepalive_new++;
245     // },
246
247     abort: function () { /* public */
248         // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
249
250         this.gst.abort();
251         if (this.watchdog_hdl != null) {
252             clearTimeout(this.watchdog_hdl);
253             this.watchdog_hdl = null;
254         }
255
256         this.restart_n++;
257         this.log("hs::reload");
258         this.watchable = false;
259         if (this.ifra != null) {
260             this.doc.body.removeChild(this.ifra);
261             delete this.ifra;
262             this.ifra = null;
263         }
264     },
265
266     reload: function () {
267         this.abort();
268         this.start(null);
269     },
270
271     log: function (s) {
272         if (this.console != null) {
273             return (this.console.log(s));
274         }
275     }
276 }