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