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