2 * brisk - http_streaming.js
4 * Copyright (C) 2006-2012 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
30 * DONE - 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, cmdproc)
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;
50 this.cmdproc = cmdproc;
53 http_streaming.prototype = {
61 /* cookiepath is automatically customized in installation phase */
62 cookiepath: "/brisk/",
74 /* watchdog_old: 0, */
80 hbit_set: function (hbit) {
87 if (this.xhr.readyState == 4) {
88 if (this.watchdog != null) {
90 clearTimeout(this.watchdog);
94 // console.log("SS: "+safestatus(xhr));
97 if ((ret = safestatus(this.xhr)) == 200) {
99 // console.log("del a null "+this.delayed);
100 } else if (ret != -1) {
103 // alert('There was a problem with the request.' + ret);
112 xhr_abort: function()
115 if (this.xhr != null)
120 run: function(sess, stat, subst, step)
123 //x alert("the_end1");
124 if (this.watchdog != null) {
126 clearTimeout(this.watchdog);
127 this.watchdog = null;
131 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
133 // NOTE: *ctx = "" to prevent konqueror stream commands duplication.
137 /* NOTE document.uniqueID exists only under IE */
138 // if (g_is_spawn == 1)
139 // alert("di qui3: "+(g_is_spawn == 1 ? "&table_idx="+g_table_idx : ""));
140 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);
144 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
147 // TODO: qui avvio del timer per riavviare xhr
149 this.watchdog = setTimeout(function(obj){ obj.xhr_abort(); }, 60000, this);
151 this.stopped = false;
155 /* WORK HERE TO RUN WIN OR LIN STREAM */
157 start: function(sess)
176 if (this.watchdog_old >= 50) {
177 this.watchdog_old = 0;
178 // alert("ABORT XHR");
183 if (this.sandbox != null) {
184 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;
186 if (zug != this.sandbox.innerHTML)
187 this.sandbox.innerHTML = zug;
194 if (this.gst.st_loc < this.gst.st_loc_new) {
195 // there is some slow actions running
198 else if (this.gst.comms.length > 0) {
201 singlecomm = this.gst.comms.shift();
202 // alert("EXE"+gugu);
203 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
206 this.cmdproc(singlecomm);
212 if (this.xhr == null)
214 if (this.xhr.responseText != null)
215 this.newctx = this.xhr.responseText;
218 if (this.stopped == true) {
219 this.stopped = false;
220 // XX $("xhrstart").innerHTML += "XHRSTART: da catch<br>";
221 if (this.delay > 0) {
222 if (this.delayed == null) {
223 // console.log("XXX DI QUI "+this.delay);
225 this.delayed = setTimeout(
226 function(f_obj, f_sess, f_stat, f_subst, f_step){ f_obj.run(f_sess, f_stat, f_subst, f_step); },
227 this.delay, this, sess, stat, subst, this.gst.st);
228 // console.log("XXX DI QUI post"+this.delayed);
232 // console.log("yyy DI QUI "+this.delay);
233 this.run(sess, stat, subst, this.gst.st);
238 // $("sandbox").innerHTML += "return 1<br>";
239 if (this.the_end != true) {
240 /* this.watchdog_old = 0; */
241 setTimeout(function(obj, sess){ obj.poll(sess); }, tout, this, sess);
247 //x alert("the_end2");
248 if (this.watchdog != null) {
249 clearTimeout(this.watchdog);
250 this.watchdog = null;
257 // no new char from the last loop, break
258 if (this.old_n == this.cur_n &&
259 this.newctx.length == this.checkedlen) {
260 /* this.watchdog++; */
264 // $("sandbox").innerHTML += "BIG IF<br>";
272 var match_lines = /^_*$/;
274 /* this.watchdog = 0; */
277 // check for the same command group
278 if (this.old_n != this.cur_n) {
279 this.old_n = this.cur_n;
284 delta = this.oldctx.length;
286 // $("xhrlog").innerHTML += "EVERY SEC<br>";
287 for (i = delta ; i < this.newctx.length ; i++) {
288 if (this.newctx[i] != '_')
291 if (i == this.newctx.length) {
296 // $("xhrlog").innerHTML += "CHECK COM<br>";
297 // extracts the new part of the command string
298 comm_newpart = this.newctx.substr(delta);
300 // XX $("xhrlog").innerHTML = newctx.replace("<", "<", "g");
302 // $("response").innerHTML = comm_newpart;
303 comm_match = /_*@BEGIN@(.*?)@END@/g;
304 comm_clean = /_*@BEGIN@(.*?)@END@/;
306 comm_arr = comm_newpart.match(comm_match);
308 // $("sandbox").innerHTML += "PRE COMMARR<br>";
310 // XX $("xhrdeltalog").innerHTML += "DELTA: "+delta +"<br>";
311 // XX alert("newctx: "+this.newctx);
312 // $("sandbox").innerHTML += "POST COMMARR<br>";
313 for (i = 0 ; i < comm_arr.length ; i++) {
314 var temp = comm_arr[i].replace(comm_clean,"$1").split("|");
315 this.gst.comms = this.gst.comms.concat(temp);
316 // XX alert("COMM_ARR["+i+"]: "+comm_arr[i]+" LEN:"+comm_arr[i].length);
317 comm_len += comm_arr[i].length;
320 this.oldctx += comm_newpart.substr(0,comm_len);
321 // XX alert("OLDCTX: "+this.oldctx);
324 this.checkedlen = this.oldctx.length;
329 if (xhrrestart == 1 && this.stopped == true) {
330 // $("sandbox").innerHTML += "LITTLE IF<br>";
332 // XX $("xhrstart").innerHTML += "XHRSTART: da end poll<br>";
333 if (this.delay > 0) {
334 if (this.delayed == null) {
335 // console.log("XXX DI QUO "+this.delay);
337 this.delayed = setTimeout(
338 function(obj, sess, stat, subst, step){ obj.run(sess, stat, subst, step); },
339 this.delay, this, sess, stat, subst, this.gst.st);
340 // console.log("XXX DI QUO post"+this.delayed);
344 // console.log("yyy DI QUO "+this.delay);
345 this.run(sess, stat, subst, this.gst.st);
350 if (this.the_end != true) {
351 setTimeout(function(obj, sess){ obj.poll(sess); }, tout, this, sess);
354 //x alert("the_end3");
355 if (this.watchdog != null) {
356 clearTimeout(this.watchdog);
357 this.watchdog = null;
365 window.onload = function () {
366 xhr = createXMLHttpRequest();
368 sess = $("user").value;
369 window.setTimeout(poll, 0, sess);