modification to fix explorer bug.
[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     // 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(); }, 0, 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.watchdog_ct % this.watchdog_checktm) == 0 || !this.watchable) {
105             if (!this.watchable) {
106                 do {
107                     try{
108                         if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
109                             break;
110                     }
111                     catch(b) {
112                         break;
113                     }
114
115                     /*
116                       on IE7 the the window frame scope is cleaned after the href is set, so we wait 
117                       for a well know variable value before assign this object value to it (OO is a passion)
118                     */
119                     if (this.ifra.contentWindow.http_streaming == "ready") {
120                         this.ifra.contentWindow.http_streaming = this;
121                         this.watchable = true;
122                         this.log("hs::watchdog: watchable = yes");
123                     }
124                 } while (false);
125             }
126             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
127             if (this.keepalive_old == this.keepalive_new) {
128                 this.keepalives_equal++;
129             }
130             else {
131                 this.keepalive_old = this.keepalive_new;
132                 this.keepalives_equal = 0;
133             }
134             
135             if (this.keepalives_equal > this.keepalives_eq_max) {
136                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
137                 this.reload();
138                 // alert("watchdog return reload");
139                 return;
140             }
141         }
142
143         // PICK COMMANDS FROM STREAM
144         do {
145             // alert("do--while begin ["+again+"]");
146             // CHECK: maybe again here isn't needed 
147             again = 0;
148             try {
149                 if (typeof(this.ifra.contentWindow.ctx_new)     == 'undefined' ||
150                     typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined')
151                     break;
152             }
153             catch(b) {
154                 break;
155             }
156             
157             ctx_new_len = this.ifra.contentWindow.ctx_new.length;
158             if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
159                 break;
160             }
161             this.log("new: "+ ctx_new_len + "  old: "+this.ifra.contentWindow.ctx_old_len);
162             this.keepalive_new++;
163             // alert("pre-loop 1");
164             for (i = this.ifra.contentWindow.ctx_old_len ; i < ctx_new_len ; i++) {
165                 if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
166                     // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
167                     break;
168                 }
169                 // else {
170                 //     this.log("ctx_new.charAt(i) == '_'");
171                 // }
172             }
173             this.ifra.contentWindow.ctx_old_len = i;
174             if (i == ctx_new_len) {
175                 this.log("old_len == i");
176                 break;
177             }
178             else {
179                 this.log("old_len != i: "+i);
180             }
181             // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
182
183             comm_newpart = this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len);    
184             this.log("COM_NEWPART: ["+comm_newpart+"]");
185             comm_len = 0;
186             comm_arr = comm_newpart.match(this.comm_match);
187
188             // alert("do--while middle2 ["+again+"]");
189             if (comm_arr) {
190                 var comm_arr_len = comm_arr.length;
191                 for (i = 0 ; i < comm_arr_len ; i++) {
192                     var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
193                     this.gst.comms = this.gst.comms.concat(temp);
194                     comm_len += comm_arr[i].length;
195                 }
196                 again = 1;
197             }
198             this.ifra.contentWindow.ctx_old_len += comm_len;
199             this.ifra.contentWindow.script_clean = this.gst.st;
200             // alert("do--while end ["+again+"]");
201         } while (again);
202
203         // alert("post while");
204         // EXECUTION OF STREAM COMMANDS
205         do {
206             again = 0;
207             //MOP ?? xhrrestart = 0;
208             if (this.gst.st_loc < this.gst.st_loc_new) {
209                 // there is some slow actions running
210                 break;
211             }
212             else if (this.gst.comms.length > 0) {
213                 var singlecomm;
214                 
215                 singlecomm = this.gst.comms.shift();
216                 // alert("EXE"+gugu);
217                 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
218                 //xx this.hbit("+");
219
220                 // alert("SINGLE: ["+singlecomm+"]");
221                 this.cmdproc(singlecomm);
222                 again = 1;
223             }
224         } while (again);
225         this.watchdog_ct++;
226         if (!this.the_end) {
227             this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
228         }
229         // alert("watchdog return normal");
230
231         return;
232     },
233
234     //
235     // moved to xynt-http-streaming-ifra as push()
236     //
237     // keepalive: function (s) {
238     //     this.log("hs::keepalive");
239     //     if (s != null) {
240     //         this.log(s);
241     //         this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
242     //     }
243     //     else {
244     //         this.ifra.contentWindow.ctx_new += "_";
245     //     }
246     //     // this.keepalive_new++;
247     // },
248
249     abort: function () { /* public */
250         // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
251
252         this.gst.abort();
253         if (this.watchdog_hdl != null) {
254             clearTimeout(this.watchdog_hdl);
255             this.watchdog_hdl = null;
256         }
257
258         this.restart_n++;
259         this.log("hs::reload");
260         this.watchable = false;
261         if (this.ifra != null) {
262             this.doc.body.removeChild(this.ifra);
263             delete this.ifra;
264             this.ifra = null;
265         }
266     },
267
268     reload: function () {
269         this.abort();
270         this.start(null);
271     },
272
273     log: function (s) {
274         if (this.console != null) {
275             return (this.console.log(s));
276         }
277     }
278 }