merge with spawn branch
[brisk.git] / web / xhr.js
1 /*
2  *  brisk - xhr.js
3  *
4  *  Copyright (C) 2006-2008 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  * $Id$
23  *
24  */
25
26 var xhr_rd_cookiepath = "/brisk/";
27 var xhr_rd = false;
28 var xhr_rd_stopped = true;
29 var xhr_rd_oldctx = "";
30 var xhr_rd_newctx = "";
31
32 var xhr_rd_cur_n = -1;
33 var xhr_rd_old_n = -1;
34 var xhr_rd_checkedlen = 0;
35 var the_end = false;
36 var ct = 0;
37 var watchdog = 0;
38
39 function hbit(symb)
40 {
41     if ($("heartbit").innerHTML.length >= 40)
42         $("heartbit").innerHTML = symb;
43     else
44         $("heartbit").innerHTML += symb;
45 }
46
47 function xhr_rd_cb(xhr_rd) 
48 {
49     var ret;
50
51     if (xhr_rd.readyState == 4) {
52         try {
53             if ((ret = safestatus(xhr_rd)) == 200) {
54             } else if (ret != -1) {
55                 alert('There was a problem with the request.' + ret);
56             }
57         } catch(b) {};
58
59         xhr_rd_stopped = true;
60     }
61 };
62
63 function xhr_rd_start(sess,stat,subst,step) 
64 {
65     if (the_end)
66         return;
67     createCookie("sess", sess, 24*365, xhr_rd_cookiepath);
68
69     // NOTE: *ctx = "" to prevent konqueror stream commands duplication.
70     xhr_rd_oldctx = "";
71     xhr_rd_newctx = "";
72
73     /* NOTE document.uniqueID exists only under IE  */
74     // if (g_is_spawn == 1)
75     // alert("di qui3: "+(g_is_spawn == 1 ? "&table_idx="+g_table_idx : ""));
76     xhr_rd.open('GET', 'index_rd.php?sess='+sess+"&stat="+stat+"&subst="+subst+"&step="+step+"&onlyone="+(document.uniqueID ? "TRUE" : "FALSE")+"&myfrom="+myfrom, true);
77     //    try { 
78     xhr_rd.onreadystatechange = function() { xhr_rd_cb(xhr_rd); }
79     xhr_rd.send(null);
80     xhr_rd_cur_n++;
81     xhr_rd_stopped = false;
82     // } catch (e) {}
83 };
84
85 function xhr_rd_poll(sess)
86 {
87     var tout = 100;
88     var again;
89     var xhrrestart;
90     ct++;
91
92     /*
93     if (watchdog >= 50) {
94         watchdog = 0;
95         // alert("ABORT XHR_RD");
96         xhr_rd_stopped = true;
97         xhr_rd.abort(); 
98     }
99     */
100     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;
101
102     if (zug != $("sandbox").innerHTML)
103         $("sandbox").innerHTML = zug;
104
105     /* heartbit log */
106     hbit("_");
107     
108     do {
109         again = 0;
110         xhrrestart = 0;
111         if (gst.st_loc < gst.st_loc_new) {
112             // there is some slow actions running
113             break;
114         }
115         else if (gst.comms.length > 0) {
116             var singlecomm;
117
118             singlecomm = gst.comms.shift();
119             // alert("EXE"+gugu);
120             // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "&lt;", "g"); +"<br>";
121             hbit("+");
122
123             eval(singlecomm);
124             again = 1;
125         }
126         else {
127             xhrrestart = 1;
128             try { 
129                 if (xhr_rd == null)
130                         throw "restart";
131                 if (xhr_rd.responseText != null)
132                         xhr_rd_newctx = xhr_rd.responseText;
133             }
134             catch (e) {
135                 if (xhr_rd_stopped == true) {
136                     xhr_rd_stopped = false;
137                     // XX $("xhrstart").innerHTML += "XHRSTART: da catch<br>";
138                     xhr_rd_start(sess, stat, subst, gst.st);
139                 }
140                 
141                 
142                 // $("sandbox").innerHTML += "return 1<br>";
143                 if (the_end != true) {
144                     watchdog = 0;
145                     setTimeout(xhr_rd_poll, tout, sess);
146                     
147                     // hbit(".");
148                     
149                 }
150                 return;
151             }
152             
153
154             // no new char from the last loop, break
155             if (xhr_rd_old_n == xhr_rd_cur_n && 
156                 xhr_rd_newctx.length == xhr_rd_checkedlen) {
157                 watchdog++;
158                 break;
159             }
160             else {
161                 watchdog = 0;
162                 // $("sandbox").innerHTML += "BIG IF<br>";
163                 var comm_match;
164                 var comm_clean;
165                 var comm_len;
166                 var comm_newpart;
167                 var comm_arr;
168                 var i;
169                 var delta = 0;
170                 var match_lines = /^_*$/;
171
172                 hbit("/\\");
173
174                 // check for the same command group
175                 if (xhr_rd_old_n != xhr_rd_cur_n) {
176                     xhr_rd_old_n = xhr_rd_cur_n;
177                     xhr_rd_checkedlen = 0;
178                     xhr_rd_oldctx = "";
179                 }
180                 else
181                     delta = xhr_rd_oldctx.length;
182
183                 // $("xhrlog").innerHTML += "EVERY SEC<br>";            
184                 for (i = delta ; i < xhr_rd_newctx.length ; i++) {
185                     if (xhr_rd_newctx[i] != '_') 
186                         break;
187                 }
188                 if (i == xhr_rd_newctx.length) {
189                     xhr_rd_checkedlen = i;
190                     break;
191                 }
192
193                 // $("xhrlog").innerHTML += "CHECK COM<br>";            
194                 // extracts the new part of the command string
195                 comm_newpart = xhr_rd_newctx.substr(delta);
196                 
197                 // XX $("xhrlog").innerHTML = xhr_rd_newctx.replace("<", "&lt;", "g");
198
199                 // $("response").innerHTML = comm_newpart;
200                 comm_match = /_*@BEGIN@(.*?)@END@/g;
201                 comm_clean = /_*@BEGIN@(.*?)@END@/;
202                 comm_len = 0;
203                 comm_arr = comm_newpart.match(comm_match);
204                 
205                 // $("sandbox").innerHTML += "PRE COMMARR<br>";
206                 if (comm_arr) {
207                     // XX $("xhrdeltalog").innerHTML += "DELTA: "+delta +"<br>";
208                     // XX alert("xhr_rd_newctx: "+xhr_rd_newctx);
209                     // $("sandbox").innerHTML += "POST COMMARR<br>";
210                     for (i = 0 ; i < comm_arr.length ; i++) {
211                         var temp = comm_arr[i].replace(comm_clean,"$1").split("|");
212                         gst.comms = gst.comms.concat(temp);
213                         // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+"  LEN:"+comm_arr[i].length);
214                         comm_len += comm_arr[i].length;
215                     }
216                     tout = 0;
217                     xhr_rd_oldctx += comm_newpart.substr(0,comm_len);
218                     // XX alert("XHR_RD_OLDCTX: "+xhr_rd_oldctx);
219                     again = 1;
220                 }
221                 xhr_rd_checkedlen = xhr_rd_oldctx.length;
222             }
223         }
224     } while (again);
225
226     if (xhrrestart == 1 && xhr_rd_stopped == true) {
227         // $("sandbox").innerHTML += "LITTLE IF<br>";
228         // alert("di qui");
229         // XX $("xhrstart").innerHTML += "XHRSTART: da end poll<br>";
230         xhr_rd_start(sess, stat, subst, gst.st);
231         // $("sandbox").innerHTML += "return 2<br>";
232     }
233         
234     // $("sandbox").innerHTML += "return 3<br>";
235     if (the_end != true) {
236         setTimeout(xhr_rd_poll, tout, sess);
237         
238         // hbit(".");
239         
240     }
241     return;
242 };
243
244 /*
245   window.onload = function () {
246   xhr_rd = createXMLHttpRequest();
247
248   sess = $("user").value;
249   window.setTimeout(xhr_rd_poll, 0, sess);
250   };
251 */