0cbf36b7852cca1a363707801e531af73120a1c9
[brisk.git] / 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:  200,
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.ifra.src = this.page;
73         this.watchdog_ct  = 0;
74         if (!this.the_end) {
75             this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, 0, this);
76         }
77     },
78
79     stop: function() {
80         this.the_end = true;
81         this.abort();
82     },
83
84     hbit_set: function (hbit) {
85         this.hbit = hbit;
86     },
87
88     watchdog: function () {
89         // alert("watchdog");
90         var i, again;
91         var comm_newpart, comm_len, comm_arr;
92         var ctx_new_len;
93
94         if (this.sandbox != null) {
95             // 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;
96             var zug = "WATCHDOG  sess = ["+this.sess+"]  step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;          
97             if (zug != this.sandbox.innerHTML)
98                 this.sandbox.innerHTML = zug;
99         }
100
101         // WATCHDOGING THE CONNECTION
102         this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
103         if ( (this.watchdog_ct % this.watchdog_checktm) == 0 || !this.watchable) {
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.log("hs::watchdog: watchable = yes");
122                     }
123                 } while (false);
124             }
125             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
126             if (this.keepalive_old == this.keepalive_new) {
127                 this.keepalives_equal++;
128             }
129             else {
130                 this.keepalive_old = this.keepalive_new;
131                 this.keepalives_equal = 0;
132             }
133             
134             if (this.keepalives_equal > this.keepalives_eq_max) {
135                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
136                 this.reload();
137                 // alert("watchdog return reload");
138                 return;
139             }
140         }
141
142         // PICK COMMANDS FROM STREAM
143         do {
144             // alert("do--while begin ["+again+"]");
145             // CHECK: maybe again here isn't needed 
146             again = 0;
147             try {
148                 if (typeof(this.ifra.contentWindow.ctx_new)     == 'undefined' ||
149                     typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined')
150                     break;
151             }
152             catch(b) {
153                 break;
154             }
155             
156             ctx_new_len = this.ifra.contentWindow.ctx_new.length;
157             if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
158                 break;
159             }
160             this.log("new: "+ ctx_new_len + "  old: "+this.ifra.contentWindow.ctx_old_len);
161             this.keepalive_new++;
162             // alert("pre-loop 1");
163             for (i = this.ifra.contentWindow.ctx_old_len ; i < ctx_new_len ; i++) {
164                 if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
165                     // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
166                     break;
167                 }
168                 // else {
169                 //     this.log("ctx_new.charAt(i) == '_'");
170                 // }
171             }
172             this.ifra.contentWindow.ctx_old_len = i;
173             if (i == ctx_new_len) {
174                 this.log("old_len == i");
175                 break;
176             }
177             else {
178                 this.log("old_len != i: "+i);
179             }
180             // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
181
182             comm_newpart = this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len);    
183             this.log("COM_NEWPART: ["+comm_newpart+"]");
184             comm_len = 0;
185             comm_arr = comm_newpart.match(this.comm_match);
186
187             // alert("do--while middle2 ["+again+"]");
188             if (comm_arr) {
189                 var comm_arr_len = comm_arr.length;
190                 for (i = 0 ; i < comm_arr_len ; i++) {
191                     var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
192                     this.gst.comms = this.gst.comms.concat(temp);
193                     comm_len += comm_arr[i].length;
194                 }
195                 again = 1;
196             }
197             this.ifra.contentWindow.ctx_old_len += comm_len;
198             this.ifra.contentWindow.script_clean = this.gst.st;
199             // alert("do--while end ["+again+"]");
200         } while (again);
201
202         // alert("post while");
203         // EXECUTION OF STREAM COMMANDS
204         do {
205             again = 0;
206             //MOP ?? xhrrestart = 0;
207             if (this.gst.st_loc < this.gst.st_loc_new) {
208                 // there is some slow actions running
209                 break;
210             }
211             else if (this.gst.comms.length > 0) {
212                 var singlecomm;
213                 
214                 singlecomm = this.gst.comms.shift();
215                 // alert("EXE"+gugu);
216                 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
217                 //xx this.hbit("+");
218
219                 // alert("SINGLE: ["+singlecomm+"]");
220                 this.cmdproc(singlecomm);
221                 again = 1;
222             }
223         } while (again);
224         this.watchdog_ct++;
225         if (!this.the_end) {
226             this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
227         }
228         // alert("watchdog return normal");
229
230         return;
231     },
232
233     //
234     // moved to xynt-http-streaming-ifra as push()
235     //
236     // keepalive: function (s) {
237     //     this.log("hs::keepalive");
238     //     if (s != null) {
239     //         this.log(s);
240     //         this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
241     //     }
242     //     else {
243     //         this.ifra.contentWindow.ctx_new += "_";
244     //     }
245     //     // this.keepalive_new++;
246     // },
247
248     abort: function () { /* public */
249         // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
250
251         this.gst.abort();
252         if (this.watchdog_hdl != null) {
253             clearTimeout(this.watchdog_hdl);
254             this.watchdog_hdl = null;
255         }
256
257         this.restart_n++;
258         this.log("hs::reload");
259         this.watchable = false;
260         if (this.ifra != null) {
261             this.doc.body.removeChild(this.ifra);
262             delete this.ifra;
263             this.ifra = null;
264         }
265     },
266
267     reload: function () {
268         this.abort();
269         this.start(null);
270     },
271
272     log: function (s) {
273         if (this.console != null) {
274             return (this.console.log(s));
275         }
276     }
277 }