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