1 // old targetpage == page and moved into start method
6 function transport_xhr(doc, xynt_streaming, page)
9 this.xynt_streaming = xynt_streaming;
10 this.xhr = createXMLHttpRequest();
11 this.xhr.open('GET', page);
14 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
20 transport_xhr.prototype = {
22 xynt_streaming: "ready",
32 destroy: function () { /* public */
33 if (this.xhr != null) {
42 if (this.xhr.readyState == 4) {
43 // console.log("SS: "+safestatus(xhr));
45 // NOTE: delay management later
47 // if ((ret = safestatus(this.xhr)) == 200) {
49 // // console.log("del a null "+this.delayed);
50 // } else if (ret != -1) {
53 // // alert('There was a problem with the request.' + ret);
57 // this.delayed = null;
62 xhr_abort: function() {
63 if (this.xhr != null) {
68 xstr_is_init: function () { /* public */
70 if (this.xhr.responseText != null) {
71 this.ctx_new = this.xhr.responseText;
77 return (this.ctx_new != null);
80 /* only after a successfull is_initialized call */
81 xstr_is_ready: function () { /* public */
82 return (this.xynt_streaming == "ready");
85 xstr_set: function () { /* public */
89 ctx_new_is_set: function () { /* public */
90 return (this.ctx_new != null);
93 ctx_new_curlen_get: function () { /* public */
94 return (this.ctx_new.length);
97 ctx_new_getchar: function(idx) { /* public */
98 return (this.ctx_new[idx]);
101 ctx_old_len_is_set: function () { /* public */
105 ctx_old_len_get: function () { /* public */
106 return (this.ctx_old_len);
109 ctx_old_len_set: function (len) { /* public */
110 this.ctx_old_len = len;
113 ctx_old_len_add: function (len) { /* public */
114 this.ctx_old_len += len;
117 new_part: function () { /* public */
118 return (this.ctx_new.substr(this.ctx_old_len));
121 scrcls_set: function (step) { /* public */
122 // this.script_clean = step;
125 postproc: function () {
126 if (this.stopped && !this.xstr_is_ready()) {
127 this.xynt_streaming.reload();
133 // CLASS transport_htmlfile
135 function transport_htmlfile(doc, xynt_streaming, page)
138 this.xynt_streaming = xynt_streaming;
139 this.transfdoc = new ActiveXObject("htmlfile");
140 this.transfdoc.open();
141 this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
142 this.transfdoc.close();
144 this.ifra = this.transfdoc.getElementById("iframe");
145 this.ifra.contentWindow.location.href = page;
146 this.stopped = false;
149 transport_htmlfile.prototype = {
151 xynt_streaming: null,
156 destroy: function () { /* public */
157 if (this.ifra != null) {
158 // this.doc.body.removeChild(this.ifra);
163 if (this.transfdoc) {
164 delete this.transfdoc;
165 this.transfdoc = null;
169 xstr_is_init: function () { /* public */
170 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
173 /* only after a successfull is_initialized call */
174 xstr_is_ready: function () { /* public */
175 return (this.ifra.contentWindow.xynt_streaming == "ready");
178 /* only after a successfull is_initialized call */
179 xstr_set: function () { /* public */
180 if (this.ifra.contentWindow.xynt_streaming == "ready") {
181 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
184 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
192 ctx_new_is_set: function () { /* public */
193 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
196 ctx_new_curlen_get: function () { /* public */
197 return (this.ifra.contentWindow.ctx_new.length);
200 ctx_new_getchar: function(idx) { /* public */
203 ctx_old_len_is_set: function () { /* public */
204 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
207 ctx_old_len_get: function () { /* public */
208 return (this.ifra.contentWindow.ctx_old_len);
211 ctx_old_len_set: function (len) { /* public */
212 this.ifra.contentWindow.ctx_old_len = len;
215 ctx_old_len_add: function (len) { /* public */
216 this.ifra.contentWindow.ctx_old_len += len;
219 new_part: function () { /* public */
220 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
223 scrcls_set: function (step) { /* public */
224 this.ifra.contentWindow.script_clean = step;
227 postproc: function () { /* public */
228 if (this.stopped && !this.xstr_is_ready()) {
229 this.xynt_streaming.reload();
237 // CLASS transport_iframe
239 function transport_iframe(doc, xynt_streaming, page)
242 this.xynt_streaming = xynt_streaming;
243 this.ifra = doc.createElement("iframe");
244 this.ifra.style.visibility = "hidden";
245 doc.body.appendChild(this.ifra);
246 this.ifra.contentWindow.location.href = page;
247 this.stopped = false;
250 transport_iframe.prototype = {
252 xynt_streaming: null,
256 destroy: function () { /* public */
258 if (this.ifra != null) {
259 // NOTE: on Opera this remove child crash js if called from
260 // inside of the iframe, on IE on Windows without
261 // it stream abort fails.
262 // the problem is fixed setting into the iframe's onload
263 // function the stopped attribute to true and delegate
264 // postproc() fired by xynt_streaming watchdog()
265 this.doc.body.removeChild(this.ifra);
270 alert("destroy exception catched");
274 xstr_is_init: function () { /* public */
275 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
278 /* only after a successfull is_initialized call */
279 xstr_is_ready: function () { /* public */
280 return (this.ifra.contentWindow.xynt_streaming == "ready");
283 /* only after a successfull is_initialized call */
284 xstr_set: function () { /* public */
285 if (this.ifra.contentWindow.xynt_streaming == "ready") {
286 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
289 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
298 /* only after a successfull is_ready call to be sure the accessibility of the var */
299 xstr_set_old: function (xynt_streaming) { /* public */
300 this.ifra.contentWindow.xynt_streaming = xynt_streaming;
303 ctx_new_is_set: function () { /* public */
304 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
307 ctx_new_curlen_get: function () { /* public */
308 return (this.ifra.contentWindow.ctx_new.length);
311 ctx_new_getchar: function(idx) { /* public */
314 ctx_old_len_is_set: function () { /* public */
315 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
318 ctx_old_len_get: function () { /* public */
319 return (this.ifra.contentWindow.ctx_old_len);
322 ctx_old_len_set: function (len) { /* public */
323 this.ifra.contentWindow.ctx_old_len = len;
326 ctx_old_len_add: function (len) { /* public */
327 this.ifra.contentWindow.ctx_old_len += len;
330 new_part: function () { /* public */
331 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
334 scrcls_set: function (step) { /* public */
335 this.ifra.contentWindow.script_clean = step;
338 postproc: function () { /* public */
339 if (this.stopped && !this.xstr_is_ready()) {
340 this.xynt_streaming.reload();
345 function xynt_streaming(win, transp_type, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
348 this.transp_type = transp_type;
349 this.console = console;
352 this.cookiename = cookiename;
354 this.sandbox = sandbox;
356 this.cmdproc = cmdproc;
357 // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
358 this.doc = win.document;
359 this.keepalive_old = -1;
360 this.keepalive_new = -1;
363 xynt_streaming.prototype = {
377 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
380 cookiepath: "/brisk/",
386 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
387 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
388 keepalives_eq_max: 4,
389 watchdog_checktm: 40,
390 watchdog_timeout: 100,
394 comm_match: /_*@BEGIN@(.*?)@END@/g,
395 comm_clean: /_*@BEGIN@(.*?)@END@/,
399 start: function() { /* public */
403 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
405 this.log("xynt_streaming:start restart: "+this.restart_n);
406 this.keepalives_equal = 0;
409 this.page = url_complete(this.win.location.href, this.page);
410 // stat, subst, this.gst.st
412 this.page = url_append_args(this.page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
415 // transport instantiation
416 if (this.transp_type == "xhr") {
417 this.page = url_append_args(this.page, "transp", "xhr");
418 this.transp = new transport_xhr(this.doc, this, this.page);
420 else if (this.transp_type == "iframe") {
421 this.page = url_append_args(this.page, "transp", "iframe");
422 this.transp = new transport_iframe(this.doc, this, this.page);
424 else if (this.transp_type == "htmlfile") {
425 this.page = url_append_args(this.page, "transp", "htmlfile");
426 this.transp = new transport_htmlfile(this.doc, this, this.page);
432 this.watchdog_ct = 0;
434 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
437 var date = new Date();
438 this.start_time = date.getTime();
446 hbit_set: function (hbit) {
450 watchdog: function () {
451 // alert("watchdog");
453 var comm_newpart, comm_len, comm_arr;
456 if (this.sandbox != null) {
457 // from old: 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;
458 var zug = "WATCHDOG sess = ["+this.sess+"] step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;
459 if (zug != this.sandbox.innerHTML)
460 this.sandbox.innerHTML = zug;
463 // WATCHDOGING THE CONNECTION
464 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
465 if (!this.watchable) {
468 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
469 if (!this.transp.xstr_is_init()) {
470 this.log("hs::watchdog: xstr_is_init = false");
475 this.log("hs::watchdog: exception");
480 on IE7 the the window frame scope is cleaned after the href is set, so we wait
481 for a well know variable value before assign this object value to it (OO is a passion)
483 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
484 if (this.transp.xstr_set()) {
485 // this.ifra.contentWindow.xynt_streaming = this;
486 this.watchable = true;
487 this.watchdog_ct = 0;
488 this.log("hs::watchdog: watchable = yes");
492 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
493 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
494 if (this.keepalive_old == this.keepalive_new) {
495 this.keepalives_equal++;
498 this.keepalive_old = this.keepalive_new;
499 this.keepalives_equal = 0;
502 if (this.keepalives_equal >= this.keepalives_eq_max) {
503 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
505 // alert("watchdog return reload");
510 // PICK COMMANDS FROM STREAM
512 // alert("do--while begin ["+again+"]");
513 // CHECK: maybe again here isn't needed
516 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
517 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
518 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
525 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
526 ctx_new_len = this.transp.ctx_new_curlen_get();
527 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
528 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
531 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
532 this.keepalive_new++;
533 // alert("pre-loop 1");
534 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
535 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
536 if (this.transp.ctx_new_getchar(i) != '_') {
537 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
541 // this.log("ctx_new.charAt(i) == '_'");
544 // this.ifra.contentWindow.ctx_old_len = i;
545 this.transp.ctx_old_len_set(i);
546 if (i == ctx_new_len) {
547 this.log("old_len == i");
551 this.log("old_len != i: "+i);
553 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
555 comm_newpart = this.transp.new_part();
556 this.log("COM_NEWPART: ["+comm_newpart+"]");
558 comm_arr = comm_newpart.match(this.comm_match);
560 // alert("do--while middle2 ["+again+"]");
562 var comm_arr_len = comm_arr.length;
563 for (i = 0 ; i < comm_arr_len ; i++) {
564 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
565 this.gst.comms = this.gst.comms.concat(temp);
566 comm_len += comm_arr[i].length;
570 // this.ifra.contentWindow.ctx_old_len += comm_len;
571 this.transp.ctx_old_len_add(comm_len);
572 // this.ifra.contentWindow.script_clean = this.gst.st;
573 this.transp.scrcls_set(this.gst.st);
574 // alert("do--while end ["+again+"]");
577 // alert("post while");
578 // EXECUTION OF STREAM COMMANDS
581 //MOP ?? xhrrestart = 0;
582 if (this.gst.st_loc < this.gst.st_loc_new) {
583 // there is some slow actions running
586 else if (this.gst.comms.length > 0) {
589 singlecomm = this.gst.comms.shift();
590 // alert("EXE"+gugu);
591 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
594 // alert("SINGLE: ["+singlecomm+"]");
595 this.cmdproc(singlecomm);
601 var date = new Date();
602 if (date.getTime() > (this.start_time + this.restart_wait)) {
603 this.transp.postproc();
605 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
607 // alert("watchdog return normal");
613 // moved to xynt-streaming-ifra as push()
615 // keepalive: function (s) {
616 // this.log("hs::keepalive");
619 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
622 // this.ifra.contentWindow.ctx_new += "_";
624 // // this.keepalive_new++;
627 abort: function () { /* public */
628 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
631 if (this.watchdog_hdl != null) {
632 clearTimeout(this.watchdog_hdl);
633 this.watchdog_hdl = null;
637 this.log("hs::reload");
638 this.watchable = false;
639 if (this.transp != null) {
640 this.transp.destroy();
646 reload: function () {
652 if (this.console != null) {
653 return (this.console.log(s));