assegnation split to avoid php warning
[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 (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         if (xhr_rd_watchdog != null) {
95             hbit('C');
96             clearTimeout(xhr_rd_watchdog);
97             xhr_rd_watchdog = null;
98         }
99         return;
100     }
101     createCookie("sess", sess, 24*365, xhr_rd_cookiepath);
102
103     // NOTE: *ctx = "" to prevent konqueror stream commands duplication.
104     xhr_rd_oldctx = "";
105     xhr_rd_newctx = "";
106
107     /* NOTE document.uniqueID exists only under IE  */
108     // if (g_is_spawn == 1)
109     // alert("di qui3: "+(g_is_spawn == 1 ? "&table_idx="+g_table_idx : ""));
110     xhr_rd.open('GET', 'index_rd.php?sess='+sess+"&stat="+stat+"&subst="+subst+"&step="+step+"&onlyone="+(document.uniqueID ? "TRUE" : "FALSE")+"&myfrom="+myfrom, true);
111     //    try { 
112     xhr_rd.onreadystatechange = function() { xhr_rd_cb(xhr_rd); }
113     xhr_rd.send(null);
114     // 
115     // TODO: qui avvio del timer per riavviare xhr
116     // 
117     xhr_rd_watchdog = setTimeout(xhr_rd_abort, 60000, xhr_rd);
118     xhr_rd_cur_n++;
119     xhr_rd_stopped = false;
120     // } catch (e) {}
121 };
122
123 function xhr_rd_poll(sess)
124 {
125     var tout = 100;
126     var again;
127     var xhrrestart;
128     ct++;
129
130     /*
131     if (watchdog >= 50) {
132         watchdog = 0;
133         // alert("ABORT XHR_RD");
134         xhr_rd_stopped = true;
135         xhr_rd.abort(); 
136     }
137     */
138     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;
139
140     if (zug != $("sandbox").innerHTML)
141         $("sandbox").innerHTML = zug;
142
143     /* heartbit log */
144     hbit("_");
145     
146     do {
147         again = 0;
148         xhrrestart = 0;
149         if (gst.st_loc < gst.st_loc_new) {
150             // there is some slow actions running
151             break;
152         }
153         else if (gst.comms.length > 0) {
154             var singlecomm;
155
156             singlecomm = gst.comms.shift();
157             // alert("EXE"+gugu);
158             // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
159             hbit("+");
160
161             eval(singlecomm);
162             again = 1;
163         }
164         else {
165             xhrrestart = 1;
166             try { 
167                 if (xhr_rd == null)
168                     throw "restart";
169                 if (xhr_rd.responseText != null)
170                     xhr_rd_newctx = xhr_rd.responseText;
171             }
172             catch (e) {
173                 if (xhr_rd_stopped == true) {
174                     xhr_rd_stopped = false;
175                     // XX $("xhrstart").innerHTML += "XHRSTART: da catch<br>";
176                     if (xhr_rd_delay > 0) {
177                         if (xhr_rd_delayed == null) {
178                             // console.log("XXX DI QUI "+xhr_rd_delay);
179                             xhr_rd_delayed = setTimeout(xhr_rd_start, xhr_rd_delay, sess, stat, subst, gst.st);
180                             // console.log("XXX DI QUI post"+xhr_rd_delayed);
181                         }
182                     }
183                     else {
184                         // console.log("yyy DI QUI "+xhr_rd_delay);
185                         xhr_rd_start(sess, stat, subst, gst.st);
186                     }
187                 }
188                 
189                 
190                 // $("sandbox").innerHTML += "return 1<br>";
191                 if (the_end != true) {
192                     watchdog = 0;
193                     setTimeout(xhr_rd_poll, tout, sess);
194                     
195                     // hbit(".");
196                     
197                 }
198                 else {
199                     if (xhr_rd_watchdog != null) {
200                         clearTimeout(xhr_rd_watchdog);
201                         xhr_rd_watchdog = null;
202                     }
203                 }    
204                 return;
205             }
206                 
207             
208             // no new char from the last loop, break
209             if (xhr_rd_old_n == xhr_rd_cur_n && 
210                 xhr_rd_newctx.length == xhr_rd_checkedlen) {
211                 watchdog++;
212                 break;
213             }
214             else {
215                 watchdog = 0;
216                 // $("sandbox").innerHTML += "BIG IF<br>";
217                 var comm_match;
218                 var comm_clean;
219                 var comm_len;
220                 var comm_newpart;
221                 var comm_arr;
222                 var i;
223                 var delta = 0;
224                 var match_lines = /^_*$/;
225
226                 hbit("/\\");
227
228                 // check for the same command group
229                 if (xhr_rd_old_n != xhr_rd_cur_n) {
230                     xhr_rd_old_n = xhr_rd_cur_n;
231                     xhr_rd_checkedlen = 0;
232                     xhr_rd_oldctx = "";
233                 }
234                 else
235                     delta = xhr_rd_oldctx.length;
236
237                 // $("xhrlog").innerHTML += "EVERY SEC<br>";            
238                 for (i = delta ; i < xhr_rd_newctx.length ; i++) {
239                     if (xhr_rd_newctx[i] != '_') 
240                         break;
241                 }
242                 if (i == xhr_rd_newctx.length) {
243                     xhr_rd_checkedlen = i;
244                     break;
245                 }
246
247                 // $("xhrlog").innerHTML += "CHECK COM<br>";            
248                 // extracts the new part of the command string
249                 comm_newpart = xhr_rd_newctx.substr(delta);
250                 
251                 // XX $("xhrlog").innerHTML = xhr_rd_newctx.replace("<", "&lt;", "g");
252
253                 // $("response").innerHTML = comm_newpart;
254                 comm_match = /_*@BEGIN@(.*?)@END@/g;
255                 comm_clean = /_*@BEGIN@(.*?)@END@/;
256                 comm_len = 0;
257                 comm_arr = comm_newpart.match(comm_match);
258                 
259                 // $("sandbox").innerHTML += "PRE COMMARR<br>";
260                 if (comm_arr) {
261                     // XX $("xhrdeltalog").innerHTML += "DELTA: "+delta +"<br>";
262                     // XX alert("xhr_rd_newctx: "+xhr_rd_newctx);
263                     // $("sandbox").innerHTML += "POST COMMARR<br>";
264                     for (i = 0 ; i < comm_arr.length ; i++) {
265                         var temp = comm_arr[i].replace(comm_clean,"$1").split("|");
266                         gst.comms = gst.comms.concat(temp);
267                         // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+"  LEN:"+comm_arr[i].length);
268                         comm_len += comm_arr[i].length;
269                     }
270                     tout = 0;
271                     xhr_rd_oldctx += comm_newpart.substr(0,comm_len);
272                     // XX alert("XHR_RD_OLDCTX: "+xhr_rd_oldctx);
273                     again = 1;
274                 }
275                 xhr_rd_checkedlen = xhr_rd_oldctx.length;
276             }
277         }
278     } while (again);
279
280     if (xhrrestart == 1 && xhr_rd_stopped == true) {
281         // $("sandbox").innerHTML += "LITTLE IF<br>";
282         // alert("di qui");
283         // XX $("xhrstart").innerHTML += "XHRSTART: da end poll<br>";
284
285         if (xhr_rd_delay > 0) {
286             if (xhr_rd_delayed == null) {
287                 // console.log("XXX DI QUO "+xhr_rd_delay);
288                 xhr_rd_delayed = setTimeout(xhr_rd_start, xhr_rd_delay, sess, stat, subst, gst.st);
289                 // console.log("XXX DI QUO post"+xhr_rd_delayed);
290             }
291         }
292         else {
293             // console.log("yyy DI QUO "+xhr_rd_delay);
294             xhr_rd_start(sess, stat, subst, gst.st);
295         }
296         
297     }
298     
299     if (the_end != true) {
300         setTimeout(xhr_rd_poll, tout, sess);
301     }
302     else {
303         if (xhr_rd_watchdog != null) {
304             clearTimeout(xhr_rd_watchdog);
305             xhr_rd_watchdog = null;
306         }
307     }
308     return;
309 };
310
311 /*
312   window.onload = function () {
313   xhr_rd = createXMLHttpRequest();
314
315   sess = $("user").value;
316   window.setTimeout(xhr_rd_poll, 0, sess);
317   };
318 */