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