add session management and step passed to the streamer
[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:           "",
64     ctx_old_len:       0,
65     // ctx_new:           "",
66     ctx_new_len:       0,
67     stream:            "",
68
69     start: function(page) {
70         this.log("http_streaming:start restart: "+this.restart_n);
71         this.keepalives_equal = 0;
72         this.ifra = this.doc.createElement("iframe");
73         this.ifra.style.visibility = "hidden";
74         this.doc.body.appendChild(this.ifra);
75         if (page != null)
76             this.page = page;
77         this.page = url_append_args(this.page, "sess", this.sess, "st", gst.st);
78         this.log(this.page);
79         this.ifra.contentWindow.location.href = this.page;
80         this.watchdog_ct  = 0;
81         this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
82     },
83
84     watchdog: function () {
85         var i, again;
86         var comm_newpart, comm_len, comm_arr;
87
88         // WATCHDOGING THE CONNECTION
89         if ( (this.watchdog_ct % this.watchdog_checktm) == 0 || !this.watchable) {
90             this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
91             if (!this.watchable) {
92                 do {
93                     if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
94                         break;
95                     /*
96                       on IE7 the the window frame scope is cleaned after the href is set, so we wait 
97                       for a well know variable value before assign this object value to it (OO is a passion)
98                     */
99                     if (this.ifra.contentWindow.http_streaming == "ready") {
100                         this.ifra.contentWindow.http_streaming = this;
101                         this.watchable = true;
102                         this.log("hs::watchdog: watchable = yes");
103                     }
104                 } while (false);
105             }
106             this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
107             if (this.keepalive_old == this.keepalive_new) {
108                 this.keepalives_equal++;
109             }
110             else {
111                 this.keepalive_old = this.keepalive_new;
112                 this.keepalives_equal = 0;
113             }
114             
115             if (this.keepalives_equal > this.keepalives_eq_max) {
116                 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
117                 this.reload();
118                 return;
119             }
120         }
121
122         // PICK COMMANDS FROM STREAM
123         do {
124             // CHECK: maybe again here isn't needed 
125             again = 0;
126             if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined')
127                 break;
128             if (this.ifra.contentWindow.ctx_new.length == this.ctx_old_len) {
129                 // this.ctx_new = this.ctx_old = "";
130                 // FIXME find a more robust clean method
131                 break;
132             }
133             this.log("new: "+ this.ifra.contentWindow.ctx_new.length + "  old: "+this.ctx_old_len);
134             this.keepalive_new++;            
135             for (i = this.ctx_old_len ; i < this.ifra.contentWindow.ctx_new.length ; i++) {
136                 if (this.ifra.contentWindow.ctx_new[i] != '_') 
137                     break;
138             }
139             if (i == this.ifra.contentWindow.ctx_new.length) {
140                 this.ctx_old_len = i;
141                 break;
142             }
143             comm_newpart = this.ifra.contentWindow.ctx_new.substr(this.ctx_old_len);    
144             comm_len = 0;
145             comm_arr = comm_newpart.match(this.comm_match);
146
147             if (comm_arr) {
148                 for (i = 0 ; i < comm_arr.length ; i++) {
149                     var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
150
151                     this.gst.comms = this.gst.comms.concat(temp);
152                     // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+"  LEN:"+comm_arr[i].length);
153                     comm_len += comm_arr[i].length;
154                 }
155                 this.ctx_old += comm_newpart.substr(0,comm_len);
156                 again = 1;
157             }
158             this.ctx_old_len = this.ctx_old.length;
159         } while (again);
160         
161
162
163         // EXECUTION OF STREAM COMMANDS
164         do {
165             again = 0;
166             //MOP ?? xhrrestart = 0;
167             if (this.gst.st_loc < this.gst.st_loc_new) {
168                 // there is some slow actions running
169                 break;
170             }
171             else if (this.gst.comms.length > 0) {
172                 var singlecomm;
173                 
174                 singlecomm = this.gst.comms.shift();
175                 // alert("EXE"+gugu);
176                 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
177                 //xx this.hbit("+");
178                 
179                 this.cmdproc(singlecomm);
180                 again = 1;
181             }
182         } while (again);
183
184
185         this.watchdog_ct++;
186         this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
187         return;
188     },
189     
190     keepalive: function (s) {
191         this.log("hs::keepalive");
192         if (s != null) {
193             this.log(s);
194             this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
195         }
196         else {
197             this.ifra.contentWindow.ctx_new += "_";
198         }
199         // this.keepalive_new++;
200     },
201
202     abort: function () {
203         // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
204
205         this.gst.abort();
206         if (this.watchdog_hdl != null) {
207             clearTimeout(this.watchdog_hdl);
208             this.watchdog_hdl = null;
209         }
210
211         this.restart_n++;
212         this.log("hs::reload");
213         this.watchable = false;
214         if (this.ifra != null) {
215             this.doc.body.removeChild(this.ifra);
216             delete this.ifra;
217             this.ifra = null;
218         }
219         this.ctx_old = "";
220         this.ctx_old_len = 0;
221     },
222
223     reload: function () {
224         this.abort();
225         this.start(null);
226     },
227
228     log: function (s) {
229         if (this.console != null) {
230             return (this.console.log(s));
231         }
232     }
233 }