hs attribute ctx_new_len removed and typo fixed ctx_new_length to ctx_new_len for...
[xynt.git] / web / xynt / xynt-http-streaming.js
1 function globst() {
2     this.st = -1;
3     this.st_loc = -1;
4     this.st_loc_new = -1;
5     this.comms  = new Array;
6 }
7
8 globst.prototype = {
9     st: -1,
10     st_loc: -1,
11     st_loc_new: -1,
12     comms: null,
13     sleep_hd: null,
14
15     sleep: function(delay) {
16         st.st_loc_new++;
17
18         this.sleep_hdl = setTimeout(function(obj){ if (obj.st_loc_new > obj.st_loc) { obj.st_loc++; obj.sleep_hdl = null; }},
19                                    delay, this);
20     },
21
22     abort: function() {
23         if (this.sleep_hdl != null) {
24             clearTimeout(this.sleep_hdl);
25             this.sleep_hdl = null;
26         }
27     }
28 }
29
30 function http_streaming(win, gst, sess, cons)
31 {
32     this.console = cons;
33     this.win = win;
34     this.sess = sess;
35     this.doc = win.document;
36     this.keepalive_old = -1;
37     this.keepalive_new = -1;
38     this.gst = gst;
39     this.cmdproc = function(com){eval(com);}
40 }
41
42 http_streaming.prototype = {
43     console:           null,
44     win:               null,
45     doc:               null,
46     sess:              null,
47     ifra:              null,
48     page:              null,
49     watchdog_hdl:      null,
50     keepalive_old:    -1,
51     keepalive_new:    -1,
52     keepalives_equal:  0,
53     keepalives_eq_max: 6,
54     watchdog_timeout:  100,
55     watchdog_ct:       0,
56     watchdog_checktm:  20,
57     watchable:         false,
58     gst:               null,
59     restart_n:         0,
60     cmdproc:           null,
61     comm_match:        /_*@BEGIN@(.*?)@END@/g, 
62     comm_clean:        /_*@BEGIN@(.*?)@END@/,
63     // ctx_old_len:       0,
64     // ctx_new:           "",
65     // ctx_new_len:       0,
66     stream:            "",
67
68     start: function(page) {
69         this.log("http_streaming:start restart: "+this.restart_n);
70         this.keepalives_equal = 0;
71         this.ifra = this.doc.createElement("iframe");
72         this.ifra.style.visibility = "hidden";
73         this.doc.body.appendChild(this.ifra);
74         if (page != null)
75             this.page = url_complete(this.win.location.href, page);
76         this.page = url_append_args(this.page, "sess", this.sess, "st", this.gst.st);
77         this.log(this.page);
78
79         // this.log(this.ifra);
80         this.ifra.contentWindow.location.href = this.page;
81         this.watchdog_ct  = 0;
82         this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
83     },
84
85     watchdog: function () {
86         var i, again;
87         var comm_newpart, comm_len, comm_arr;
88         var ctx_new_len;
89
90         // WATCHDOGING THE CONNECTION
91         this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
92         if ( (this.watchdog_ct % this.watchdog_checktm) == 0 || !this.watchable) {
93             if (!this.watchable) {
94                 do {
95                     if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
96                         break;
97                     /*
98                       on IE7 the the window frame scope is cleaned after the href is set, so we wait 
99                       for a well know variable value before assign this object value to it (OO is a passion)
100                     */
101                     if (this.ifra.contentWindow.http_streaming == "ready") {
102                         this.ifra.contentWindow.http_streaming = this;
103                         this.watchable = true;
104                         this.log("hs::watchdog: watchable = yes");
105                     }
106                 } while (false);
107             }
108             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
109             if (this.keepalive_old == this.keepalive_new) {
110                 this.keepalives_equal++;
111             }
112             else {
113                 this.keepalive_old = this.keepalive_new;
114                 this.keepalives_equal = 0;
115             }
116             
117             if (this.keepalives_equal > this.keepalives_eq_max) {
118                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
119                 this.reload();
120                 return;
121             }
122         }
123
124         // PICK COMMANDS FROM STREAM
125         do {
126             // CHECK: maybe again here isn't needed 
127             again = 0;
128             if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined')
129                 break;
130             
131             ctx_new_len = this.ifra.contentWindow.ctx_new.length;
132             if (ctx_new_len == this.ctx_old_len) {
133                 break;
134             }
135             this.log("new: "+ ctx_new_len + "  old: "+this.ctx_old_len);
136             this.keepalive_new++;            
137             for (i = this.ctx_old_len ; i < ctx_new_len ; i++) {
138                 if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
139                     this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
140                     break;
141                 }
142                 else {
143                     this.log("ctx_new.charAt(i) == '_'");
144                 }
145             }
146             this.ctx_old_len = i;
147             if (i == ctx_new_len) {
148                 this.log("old_len == i");
149                 break;
150             }
151             else {
152                 this.log("old_len != i: "+i);
153             }
154             comm_newpart = this.ifra.contentWindow.ctx_new.substr(this.ctx_old_len);    
155             comm_len = 0;
156             comm_arr = comm_newpart.match(this.comm_match);
157
158             if (comm_arr) {
159                 for (i = 0 ; i < comm_arr.length ; i++) {
160                     var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
161
162                     this.gst.comms = this.gst.comms.concat(temp);
163                     // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+"  LEN:"+comm_arr[i].length);
164                     comm_len += comm_arr[i].length;
165                 }
166                 again = 1;
167             }
168             this.ctx_old_len += comm_len;
169         } while (again);
170         
171
172
173         // EXECUTION OF STREAM COMMANDS
174         do {
175             again = 0;
176             //MOP ?? xhrrestart = 0;
177             if (this.gst.st_loc < this.gst.st_loc_new) {
178                 // there is some slow actions running
179                 break;
180             }
181             else if (this.gst.comms.length > 0) {
182                 var singlecomm;
183                 
184                 singlecomm = this.gst.comms.shift();
185                 // alert("EXE"+gugu);
186                 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
187                 //xx this.hbit("+");
188                 
189                 this.cmdproc(singlecomm);
190                 again = 1;
191             }
192         } while (again);
193
194
195         this.watchdog_ct++;
196         this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
197         return;
198     },
199     
200     keepalive: function (s) {
201         this.log("hs::keepalive");
202         if (s != null) {
203             this.log(s);
204             this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
205         }
206         else {
207             this.ifra.contentWindow.ctx_new += "_";
208         }
209         // this.keepalive_new++;
210     },
211
212     abort: function () {
213         // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
214
215         this.gst.abort();
216         if (this.watchdog_hdl != null) {
217             clearTimeout(this.watchdog_hdl);
218             this.watchdog_hdl = null;
219         }
220
221         this.restart_n++;
222         this.log("hs::reload");
223         this.watchable = false;
224         if (this.ifra != null) {
225             this.doc.body.removeChild(this.ifra);
226             delete this.ifra;
227             this.ifra = null;
228         }
229         this.ctx_old_len = 0;
230     },
231
232     reload: function () {
233         this.abort();
234         this.start(null);
235     },
236
237     log: function (s) {
238         if (this.console != null) {
239             return (this.console.log(s));
240         }
241     }
242 }