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