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