2 * brisk - http_streaming.js
4 * Copyright (C) 2006-2011 Matteo Nastasi
5 * mailto: nastasi@alternativeoutput.it
6 * matteo.nastasi@milug.org
7 * web: http://www.alternativeoutput.it
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.
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.
28 * - myfrom into the constructor
29 * - target page into the constructor
30 * - type of streaming into the constructor
31 * - all iframe related streaming add
32 * - substitute fixed "eval" with a generic command hunks processor
34 * DONE - xhr_rd prefix remove from inner class attrs
35 * DONE - move hbit implementation to external file
36 * DONE - sandbox management
40 function http_streaming(cookiename, sandbox)
42 this.xhr = createXMLHttpRequest();
43 // this.xhr.setRequestHeader("Content-type", "text/html; charset=utf-8");
44 this.cookiename = cookiename;
47 http_streaming.prototype = {
51 cookiepath: "/brisk/",
63 /* watchdog_old: 0, */
69 hbit_set: function (hbit) {
76 if (this.xhr.readyState == 4) {
77 if (this.watchdog != null) {
79 clearTimeout(this.watchdog);
83 // console.log("SS: "+safestatus(xhr));
86 if ((ret = safestatus(this.xhr)) == 200) {
88 // console.log("del a null "+this.delayed);
89 } else if (ret != -1) {
92 // alert('There was a problem with the request.' + ret);
101 xhr_abort: function()
104 if (this.xhr != null)
109 run: function(sess, stat, subst, step)
112 //x alert("the_end1");
113 if (this.watchdog != null) {
115 clearTimeout(this.watchdog);
116 this.watchdog = null;
120 createCookie(this.cookie_name, sess, 24*365, this.cookiepath);
122 // NOTE: *ctx = "" to prevent konqueror stream commands duplication.
126 /* NOTE document.uniqueID exists only under IE */
127 // if (g_is_spawn == 1)
128 // alert("di qui3: "+(g_is_spawn == 1 ? "&table_idx="+g_table_idx : ""));
129 this.xhr.open('GET', 'index_rd.php?sess='+sess+"&stat="+stat+"&subst="+subst+"&step="+step+"&onlyone="+(document.uniqueID ? "TRUE" : "FALSE")+"&myfrom="+myfrom, true);
133 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
136 // TODO: qui avvio del timer per riavviare xhr
138 this.watchdog = setTimeout(function(obj){ obj.xhr_abort(); }, 60000, this);
140 this.stopped = false;
144 /* WORK HERE TO RUN WIN OR LIN STREAM */
146 start: function(sess)
165 if (this.watchdog_old >= 50) {
166 this.watchdog_old = 0;
167 // alert("ABORT XHR");
172 if (this.sandbox != null) {
173 var zug = "POLL sess = "+sess+" stat = "+stat+" subst = "+subst+" step = "+gst.st+" step_loc = "+gst.st_loc+" step_loc_new = "+gst.st_loc_new+" STOP: "+this.stopped;
175 if (zug != this.sandbox.innerHTML)
176 this.sandbox.innerHTML = zug;
183 if (gst.st_loc < gst.st_loc_new) {
184 // there is some slow actions running
187 else if (gst.comms.length > 0) {
190 singlecomm = gst.comms.shift();
191 // alert("EXE"+gugu);
192 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
201 if (this.xhr == null)
203 if (this.xhr.responseText != null)
204 this.newctx = this.xhr.responseText;
207 if (this.stopped == true) {
208 this.stopped = false;
209 // XX $("xhrstart").innerHTML += "XHRSTART: da catch<br>";
210 if (this.delay > 0) {
211 if (this.delayed == null) {
212 // console.log("XXX DI QUI "+this.delay);
214 this.delayed = setTimeout(
215 function(f_obj, f_sess, f_stat, f_subst, f_step){ f_obj.run(f_sess, f_stat, f_subst, f_step); },
216 this.delay, this, sess, stat, subst, gst.st);
217 // console.log("XXX DI QUI post"+this.delayed);
221 // console.log("yyy DI QUI "+this.delay);
222 this.run(sess, stat, subst, gst.st);
227 // $("sandbox").innerHTML += "return 1<br>";
228 if (this.the_end != true) {
229 /* this.watchdog_old = 0; */
230 setTimeout(function(obj, sess){ obj.poll(sess); }, tout, this, sess);
236 //x alert("the_end2");
237 if (this.watchdog != null) {
238 clearTimeout(this.watchdog);
239 this.watchdog = null;
246 // no new char from the last loop, break
247 if (this.old_n == this.cur_n &&
248 this.newctx.length == this.checkedlen) {
249 /* this.watchdog++; */
253 // $("sandbox").innerHTML += "BIG IF<br>";
261 var match_lines = /^_*$/;
263 /* this.watchdog = 0; */
266 // check for the same command group
267 if (this.old_n != this.cur_n) {
268 this.old_n = this.cur_n;
273 delta = this.oldctx.length;
275 // $("xhrlog").innerHTML += "EVERY SEC<br>";
276 for (i = delta ; i < this.newctx.length ; i++) {
277 if (this.newctx[i] != '_')
280 if (i == this.newctx.length) {
285 // $("xhrlog").innerHTML += "CHECK COM<br>";
286 // extracts the new part of the command string
287 comm_newpart = this.newctx.substr(delta);
289 // XX $("xhrlog").innerHTML = newctx.replace("<", "<", "g");
291 // $("response").innerHTML = comm_newpart;
292 comm_match = /_*@BEGIN@(.*?)@END@/g;
293 comm_clean = /_*@BEGIN@(.*?)@END@/;
295 comm_arr = comm_newpart.match(comm_match);
297 // $("sandbox").innerHTML += "PRE COMMARR<br>";
299 // XX $("xhrdeltalog").innerHTML += "DELTA: "+delta +"<br>";
300 // XX alert("newctx: "+this.newctx);
301 // $("sandbox").innerHTML += "POST COMMARR<br>";
302 for (i = 0 ; i < comm_arr.length ; i++) {
303 var temp = comm_arr[i].replace(comm_clean,"$1").split("|");
304 gst.comms = gst.comms.concat(temp);
305 // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+" LEN:"+comm_arr[i].length);
306 comm_len += comm_arr[i].length;
309 this.oldctx += comm_newpart.substr(0,comm_len);
310 // XX alert("OLDCTX: "+this.oldctx);
313 this.checkedlen = this.oldctx.length;
318 if (xhrrestart == 1 && this.stopped == true) {
319 // $("sandbox").innerHTML += "LITTLE IF<br>";
321 // XX $("xhrstart").innerHTML += "XHRSTART: da end poll<br>";
322 if (this.delay > 0) {
323 if (this.delayed == null) {
324 // console.log("XXX DI QUO "+this.delay);
326 this.delayed = setTimeout(
327 function(obj, sess, stat, subst, step){ obj.run(sess, stat, subst, step); },
328 this.delay, this, sess, stat, subst, gst.st);
329 // console.log("XXX DI QUO post"+this.delayed);
333 // console.log("yyy DI QUO "+this.delay);
334 this.run(sess, stat, subst, gst.st);
339 if (this.the_end != true) {
340 setTimeout(function(obj, sess){ obj.poll(sess); }, tout, this, sess);
343 //x alert("the_end3");
344 if (this.watchdog != null) {
345 clearTimeout(this.watchdog);
346 this.watchdog = null;
354 window.onload = function () {
355 xhr = createXMLHttpRequest();
357 sess = $("user").value;
358 window.setTimeout(poll, 0, sess);