d023eb39e6b20789951147b6f1bb59622167ada6
[brisk.git] / web / xhr.js
1 /*
2  *  brisk - xhr.js
3  *
4  *  Copyright (C) 2006-2011 Matteo Nastasi
5  *                          mailto: nastasi@alternativeoutput.it 
6  *                                  matteo.nastasi@milug.org
7  *                          web: http://www.alternativeoutput.it
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details. You should have received a
18  * copy of the GNU General Public License along with this program; if
19  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
20  * Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23
24 var xhr_rd_cookiepath = "/brisk/";
25 var xhr_rd = null;
26 var xhr_rd_stopped = true;
27 var xhr_rd_oldctx = "";
28 var xhr_rd_newctx = "";
29 var xhr_rd_delay = 0;
30 var xhr_rd_delayed = null;
31 var xhr_rd_cur_n = -1;
32 var xhr_rd_old_n = -1;
33 var xhr_rd_checkedlen = 0;
34 var xhr_rd_watchdog = null;
35 var the_end = false;
36 var ct = 0;
37 var watchdog = 0;
38
39 function hbit(symb)
40 {
41     if ($("heartbit").innerHTML.length >= 120) {
42         $("heartbit").innerHTML = $("heartbit").innerHTML.substring(10);
43         $("heartbit").innerHTML += symb;
44     }
45     else {
46         $("heartbit").innerHTML += symb;
47     }
48     // $("heartbit").innerHTML = $("heartbit").innerHTML.substring(20,20); // DA METTERE APPOSTO!!!!
49         
50 }
51
52 function xhr_rd_cb(xhr_rd) 
53 {
54     var ret;
55
56     // console.log(xhr_rd.readyState);
57
58     if (xhr_rd.readyState == 4) {
59         if (typeof xhr_rd_watchdog != 'undefined' && xhr_rd_watchdog != null) {
60             hbit('C');
61             clearTimeout(xhr_rd_watchdog);
62             xhr_rd_watchdog = null;
63         }
64
65         // console.log("SS: "+safestatus(xhr_rd));
66
67         try {
68             if ((ret = safestatus(xhr_rd)) == 200) {
69                 xhr_rd_delay = 0;
70                 // console.log("del a null "+xhr_rd_delayed);
71             } else if (ret != -1) {
72                 xhr_rd_delay = 5000;
73                 hbit('X');
74                 // alert('There was a problem with the request.' + ret);
75             }
76         } catch(b) {};
77
78         xhr_rd_delayed = null;
79         xhr_rd_stopped = true;
80     }
81 };
82
83 function xhr_rd_abort(xhr)
84 {
85     hbit('A');
86     if (xhr != null)
87         xhr.abort();
88     // alert("de che");
89 }
90
91 function xhr_rd_start(sess,stat,subst,step) 
92 {
93     if (the_end) {
94         //x alert("the_end1");
95         if (typeof xhr_rd_watchdog != 'undefined' && xhr_rd_watchdog != null) {
96             hbit('C');
97             clearTimeout(xhr_rd_watchdog);
98             xhr_rd_watchdog = null;
99         }
100         return;
101     }
102     createCookie("sess", sess, 24*365, xhr_rd_cookiepath);
103
104     // NOTE: *ctx = "" to prevent konqueror stream commands duplication.
105     xhr_rd_oldctx = "";
106     xhr_rd_newctx = "";
107
108     /* NOTE document.uniqueID exists only under IE  */
109     // if (g_is_spawn == 1)
110     // alert("di qui3: "+(g_is_spawn == 1 ? "&table_idx="+g_table_idx : ""));
111     xhr_rd.open('GET', 'index_rd.php?sess='+sess+"&stat="+stat+"&subst="+subst+"&step="+step+"&onlyone="+(document.uniqueID ? "TRUE" : "FALSE")+"&myfrom="+myfrom, true);
112     //    try { 
113     xhr_rd.onreadystatechange = function() { xhr_rd_cb(xhr_rd); }
114     xhr_rd.send(null);
115     // 
116     // TODO: qui avvio del timer per riavviare xhr
117     // 
118     xhr_rd_watchdog = setTimeout(xhr_rd_abort, 60000, xhr_rd);
119     xhr_rd_cur_n++;
120     xhr_rd_stopped = false;
121     // } catch (e) {}
122 };
123
124 function xhr_rd_poll(sess)
125 {
126     var tout = 100;
127     var again;
128     var xhrrestart;
129     ct++;
130
131     /*
132     if (watchdog >= 50) {
133         watchdog = 0;
134         // alert("ABORT XHR_RD");
135         xhr_rd_stopped = true;
136         xhr_rd.abort(); 
137     }
138     */
139     var zug = "XHR_RD_POLL sess = "+sess+" stat = "+stat+" subst = "+subst+" step = "+gst.st+" step_loc = "+gst.st_loc+" step_loc_new = "+gst.st_loc_new+" STOP: "+xhr_rd_stopped;
140
141     if (zug != $("sandbox").innerHTML)
142         $("sandbox").innerHTML = zug;
143
144     /* heartbit log */
145     hbit("_");
146     
147     do {
148         again = 0;
149         xhrrestart = 0;
150         if (gst.st_loc < gst.st_loc_new) {
151             // there is some slow actions running
152             break;
153         }
154         else if (gst.comms.length > 0) {
155             var singlecomm;
156
157             singlecomm = gst.comms.shift();
158             // alert("EXE"+gugu);
159             // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
160             hbit("+");
161
162             eval(singlecomm);
163             again = 1;
164         }
165         else {
166             xhrrestart = 1;
167             try { 
168                 if (xhr_rd == null)
169                     throw "restart";
170                 if (xhr_rd.responseText != null)
171                     xhr_rd_newctx = xhr_rd.responseText;
172             }
173             catch (e) {
174                 if (xhr_rd_stopped == true) {
175                     xhr_rd_stopped = false;
176                     // XX $("xhrstart").innerHTML += "XHRSTART: da catch<br>";
177                     if (xhr_rd_delay > 0) {
178                         if (xhr_rd_delayed == null) {
179                             // console.log("XXX DI QUI "+xhr_rd_delay);
180                             xhr_rd_delayed = setTimeout(xhr_rd_start, xhr_rd_delay, sess, stat, subst, gst.st);
181                             // console.log("XXX DI QUI post"+xhr_rd_delayed);
182                         }
183                     }
184                     else {
185                         // console.log("yyy DI QUI "+xhr_rd_delay);
186                         xhr_rd_start(sess, stat, subst, gst.st);
187                     }
188                 }
189                 
190                 
191                 // $("sandbox").innerHTML += "return 1<br>";
192                 if (the_end != true) {
193                     watchdog = 0;
194                     setTimeout(xhr_rd_poll, tout, sess);
195                     
196                     // hbit(".");
197                     
198                 }
199                 else {
200                     //x alert("the_end2");
201                     if (typeof xhr_rd_watchdog != 'undefined' && xhr_rd_watchdog != null) {
202                         clearTimeout(xhr_rd_watchdog);
203                         xhr_rd_watchdog = null;
204                     }
205                 }    
206                 return;
207             }
208                 
209             
210             // no new char from the last loop, break
211             if (xhr_rd_old_n == xhr_rd_cur_n && 
212                 xhr_rd_newctx.length == xhr_rd_checkedlen) {
213                 watchdog++;
214                 break;
215             }
216             else {
217                 watchdog = 0;
218                 // $("sandbox").innerHTML += "BIG IF<br>";
219                 var comm_match;
220                 var comm_clean;
221                 var comm_len;
222                 var comm_newpart;
223                 var comm_arr;
224                 var i;
225                 var delta = 0;
226                 var match_lines = /^_*$/;
227
228                 hbit("/\\");
229
230                 // check for the same command group
231                 if (xhr_rd_old_n != xhr_rd_cur_n) {
232                     xhr_rd_old_n = xhr_rd_cur_n;
233                     xhr_rd_checkedlen = 0;
234                     xhr_rd_oldctx = "";
235                 }
236                 else
237                     delta = xhr_rd_oldctx.length;
238
239                 // $("xhrlog").innerHTML += "EVERY SEC<br>";            
240                 for (i = delta ; i < xhr_rd_newctx.length ; i++) {
241                     if (xhr_rd_newctx[i] != '_') 
242                         break;
243                 }
244                 if (i == xhr_rd_newctx.length) {
245                     xhr_rd_checkedlen = i;
246                     break;
247                 }
248
249                 // $("xhrlog").innerHTML += "CHECK COM<br>";            
250                 // extracts the new part of the command string
251                 comm_newpart = xhr_rd_newctx.substr(delta);
252                 
253                 // XX $("xhrlog").innerHTML = xhr_rd_newctx.replace("<", "&lt;", "g");
254
255                 // $("response").innerHTML = comm_newpart;
256                 comm_match = /_*@BEGIN@(.*?)@END@/g;
257                 comm_clean = /_*@BEGIN@(.*?)@END@/;
258                 comm_len = 0;
259                 comm_arr = comm_newpart.match(comm_match);
260                 
261                 // $("sandbox").innerHTML += "PRE COMMARR<br>";
262                 if (comm_arr) {
263                     // XX $("xhrdeltalog").innerHTML += "DELTA: "+delta +"<br>";
264                     // XX alert("xhr_rd_newctx: "+xhr_rd_newctx);
265                     // $("sandbox").innerHTML += "POST COMMARR<br>";
266                     for (i = 0 ; i < comm_arr.length ; i++) {
267                         var temp = comm_arr[i].replace(comm_clean,"$1").split("|");
268                         gst.comms = gst.comms.concat(temp);
269                         // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+"  LEN:"+comm_arr[i].length);
270                         comm_len += comm_arr[i].length;
271                     }
272                     tout = 0;
273                     xhr_rd_oldctx += comm_newpart.substr(0,comm_len);
274                     // XX alert("XHR_RD_OLDCTX: "+xhr_rd_oldctx);
275                     again = 1;
276                 }
277                 xhr_rd_checkedlen = xhr_rd_oldctx.length;
278             }
279         }
280     } while (again);
281
282     if (xhrrestart == 1 && xhr_rd_stopped == true) {
283         // $("sandbox").innerHTML += "LITTLE IF<br>";
284         // alert("di qui");
285         // XX $("xhrstart").innerHTML += "XHRSTART: da end poll<br>";
286
287         if (xhr_rd_delay > 0) {
288             if (xhr_rd_delayed == null) {
289                 // console.log("XXX DI QUO "+xhr_rd_delay);
290                 xhr_rd_delayed = setTimeout(xhr_rd_start, xhr_rd_delay, sess, stat, subst, gst.st);
291                 // console.log("XXX DI QUO post"+xhr_rd_delayed);
292             }
293         }
294         else {
295             // console.log("yyy DI QUO "+xhr_rd_delay);
296             xhr_rd_start(sess, stat, subst, gst.st);
297         }
298         
299     }
300     
301     if (the_end != true) {
302         setTimeout(xhr_rd_poll, tout, sess);
303     }
304     else {
305         //x alert("the_end3");
306         if (typeof(xhr_rd_watchdog) != 'undefined') {
307             if (xhr_rd_watchdog != null) {
308                 clearTimeout(xhr_rd_watchdog);
309                 xhr_rd_watchdog = null;
310             }
311         }
312     }
313     return;
314 };
315
316 /*
317   window.onload = function () {
318   xhr_rd = createXMLHttpRequest();
319
320   sess = $("user").value;
321   window.setTimeout(xhr_rd_poll, 0, sess);
322   };
323 */