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