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.
27 * - type of streaming into the constructor
28 * - all iframe related streaming add
29 * - substitute fixed "eval" with a generic command hunks processor
31 * DONE - myfrom (now from) into the constructor
32 * DONE - target page into the constructor
33 * DONE - gst management
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(gst, from, cookiename, sandbox, targetpage)
42 this.xhr = createXMLHttpRequest();
43 // this.xhr.setRequestHeader("Content-type", "text/html; charset=utf-8");
47 this.cookiename = cookiename;
48 this.sandbox = sandbox;
49 this.targetpage = targetpage;
52 http_streaming.prototype = {
59 /* cookiepath is automatically customized in installation phase */
60 cookiepath: "/brisk/",
72 /* watchdog_old: 0, */
78 hbit_set: function (hbit) {
85 if (this.xhr.readyState == 4) {
86 if (this.watchdog != null) {
88 clearTimeout(this.watchdog);
92 // console.log("SS: "+safestatus(xhr));
95 if ((ret = safestatus(this.xhr)) == 200) {
97 // console.log("del a null "+this.delayed);
98 } else if (ret != -1) {
101 // alert('There was a problem with the request.' + ret);
110 xhr_abort: function()
113 if (this.xhr != null)
118 run: function(sess, stat, subst, step)
121 //x alert("the_end1");
122 if (this.watchdog != null) {
124 clearTimeout(this.watchdog);
125 this.watchdog = null;
129 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
131 // NOTE: *ctx = "" to prevent konqueror stream commands duplication.
135 /* NOTE document.uniqueID exists only under IE */
136 // if (g_is_spawn == 1)
137 // alert("di qui3: "+(g_is_spawn == 1 ? "&table_idx="+g_table_idx : ""));
138 this.xhr.open('GET', this.targetpage+'?'+this.cookie_name+'='+sess+"&stat="+stat+"&subst="+subst+"&step="+step+"&onlyone="+(document.uniqueID ? "TRUE" : "FALSE")+"&from="+this.from, true);
142 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
145 // TODO: qui avvio del timer per riavviare xhr
147 this.watchdog = setTimeout(function(obj){ obj.xhr_abort(); }, 60000, this);
149 this.stopped = false;
153 /* WORK HERE TO RUN WIN OR LIN STREAM */
155 start: function(sess)
174 if (this.watchdog_old >= 50) {
175 this.watchdog_old = 0;
176 // alert("ABORT XHR");
181 if (this.sandbox != null) {
182 var zug = "POLL sess = "+sess+" stat = "+stat+" subst = "+subst+" step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new+" STOP: "+this.stopped;
184 if (zug != this.sandbox.innerHTML)
185 this.sandbox.innerHTML = zug;
192 if (this.gst.st_loc < this.gst.st_loc_new) {
193 // there is some slow actions running
196 else if (this.gst.comms.length > 0) {
199 singlecomm = this.gst.comms.shift();
200 // alert("EXE"+gugu);
201 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
210 if (this.xhr == null)
212 if (this.xhr.responseText != null)
213 this.newctx = this.xhr.responseText;
216 if (this.stopped == true) {
217 this.stopped = false;
218 // XX $("xhrstart").innerHTML += "XHRSTART: da catch<br>";
219 if (this.delay > 0) {
220 if (this.delayed == null) {
221 // console.log("XXX DI QUI "+this.delay);
223 this.delayed = setTimeout(
224 function(f_obj, f_sess, f_stat, f_subst, f_step){ f_obj.run(f_sess, f_stat, f_subst, f_step); },
225 this.delay, this, sess, stat, subst, this.gst.st);
226 // console.log("XXX DI QUI post"+this.delayed);
230 // console.log("yyy DI QUI "+this.delay);
231 this.run(sess, stat, subst, this.gst.st);
236 // $("sandbox").innerHTML += "return 1<br>";
237 if (this.the_end != true) {
238 /* this.watchdog_old = 0; */
239 setTimeout(function(obj, sess){ obj.poll(sess); }, tout, this, sess);
245 //x alert("the_end2");
246 if (this.watchdog != null) {
247 clearTimeout(this.watchdog);
248 this.watchdog = null;
255 // no new char from the last loop, break
256 if (this.old_n == this.cur_n &&
257 this.newctx.length == this.checkedlen) {
258 /* this.watchdog++; */
262 // $("sandbox").innerHTML += "BIG IF<br>";
270 var match_lines = /^_*$/;
272 /* this.watchdog = 0; */
275 // check for the same command group
276 if (this.old_n != this.cur_n) {
277 this.old_n = this.cur_n;
282 delta = this.oldctx.length;
284 // $("xhrlog").innerHTML += "EVERY SEC<br>";
285 for (i = delta ; i < this.newctx.length ; i++) {
286 if (this.newctx[i] != '_')
289 if (i == this.newctx.length) {
294 // $("xhrlog").innerHTML += "CHECK COM<br>";
295 // extracts the new part of the command string
296 comm_newpart = this.newctx.substr(delta);
298 // XX $("xhrlog").innerHTML = newctx.replace("<", "<", "g");
300 // $("response").innerHTML = comm_newpart;
301 comm_match = /_*@BEGIN@(.*?)@END@/g;
302 comm_clean = /_*@BEGIN@(.*?)@END@/;
304 comm_arr = comm_newpart.match(comm_match);
306 // $("sandbox").innerHTML += "PRE COMMARR<br>";
308 // XX $("xhrdeltalog").innerHTML += "DELTA: "+delta +"<br>";
309 // XX alert("newctx: "+this.newctx);
310 // $("sandbox").innerHTML += "POST COMMARR<br>";
311 for (i = 0 ; i < comm_arr.length ; i++) {
312 var temp = comm_arr[i].replace(comm_clean,"$1").split("|");
313 this.gst.comms = this.gst.comms.concat(temp);
314 // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+" LEN:"+comm_arr[i].length);
315 comm_len += comm_arr[i].length;
318 this.oldctx += comm_newpart.substr(0,comm_len);
319 // XX alert("OLDCTX: "+this.oldctx);
322 this.checkedlen = this.oldctx.length;
327 if (xhrrestart == 1 && this.stopped == true) {
328 // $("sandbox").innerHTML += "LITTLE IF<br>";
330 // XX $("xhrstart").innerHTML += "XHRSTART: da end poll<br>";
331 if (this.delay > 0) {
332 if (this.delayed == null) {
333 // console.log("XXX DI QUO "+this.delay);
335 this.delayed = setTimeout(
336 function(obj, sess, stat, subst, step){ obj.run(sess, stat, subst, step); },
337 this.delay, this, sess, stat, subst, this.gst.st);
338 // console.log("XXX DI QUO post"+this.delayed);
342 // console.log("yyy DI QUO "+this.delay);
343 this.run(sess, stat, subst, this.gst.st);
348 if (this.the_end != true) {
349 setTimeout(function(obj, sess){ obj.poll(sess); }, tout, this, sess);
352 //x alert("the_end3");
353 if (this.watchdog != null) {
354 clearTimeout(this.watchdog);
355 this.watchdog = null;
363 window.onload = function () {
364 xhr = createXMLHttpRequest();
366 sess = $("user").value;
367 window.setTimeout(poll, 0, sess);