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 */
201 return (this.ifra.contentWindow.ctx_new.charAt(idx));
204 ctx_old_len_is_set: function () { /* public */
205 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
208 ctx_old_len_get: function () { /* public */
209 return (this.ifra.contentWindow.ctx_old_len);
212 ctx_old_len_set: function (len) { /* public */
213 this.ifra.contentWindow.ctx_old_len = len;
216 ctx_old_len_add: function (len) { /* public */
217 this.ifra.contentWindow.ctx_old_len += len;
220 new_part: function () { /* public */
221 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
224 scrcls_set: function (step) { /* public */
225 this.ifra.contentWindow.script_clean = step;
228 postproc: function () { /* public */
229 if (this.stopped && !this.xstr_is_ready()) {
230 this.xynt_streaming.reload();
238 // CLASS transport_iframe
240 function transport_iframe(doc, xynt_streaming, page)
243 this.xynt_streaming = xynt_streaming;
244 this.ifra = doc.createElement("iframe");
245 this.ifra.style.visibility = "hidden";
246 doc.body.appendChild(this.ifra);
247 this.ifra.contentWindow.location.href = page;
248 this.stopped = false;
251 transport_iframe.prototype = {
253 xynt_streaming: null,
257 destroy: function () { /* public */
259 if (this.ifra != null) {
260 // NOTE: on Opera this remove child crash js if called from
261 // inside of the iframe, on IE on Windows without
262 // it stream abort fails.
263 // the problem is fixed setting into the iframe's onload
264 // function the stopped attribute to true and delegate
265 // postproc() fired by xynt_streaming watchdog()
266 this.doc.body.removeChild(this.ifra);
271 alert("destroy exception catched");
275 xstr_is_init: function () { /* public */
276 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
279 /* only after a successfull is_initialized call */
280 xstr_is_ready: function () { /* public */
281 return (this.ifra.contentWindow.xynt_streaming == "ready");
284 /* only after a successfull is_initialized call */
285 xstr_set: function () { /* public */
286 if (this.ifra.contentWindow.xynt_streaming == "ready") {
287 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
290 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
299 /* only after a successfull is_ready call to be sure the accessibility of the var */
300 xstr_set_old: function (xynt_streaming) { /* public */
301 this.ifra.contentWindow.xynt_streaming = xynt_streaming;
304 ctx_new_is_set: function () { /* public */
305 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
308 ctx_new_curlen_get: function () { /* public */
309 return (this.ifra.contentWindow.ctx_new.length);
312 ctx_new_getchar: function(idx) { /* public */
313 return (this.ifra.contentWindow.ctx_new.charAt(idx));
316 ctx_old_len_is_set: function () { /* public */
317 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
320 ctx_old_len_get: function () { /* public */
321 return (this.ifra.contentWindow.ctx_old_len);
324 ctx_old_len_set: function (len) { /* public */
325 this.ifra.contentWindow.ctx_old_len = len;
328 ctx_old_len_add: function (len) { /* public */
329 this.ifra.contentWindow.ctx_old_len += len;
332 new_part: function () { /* public */
333 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
336 scrcls_set: function (step) { /* public */
337 this.ifra.contentWindow.script_clean = step;
340 postproc: function () { /* public */
341 if (this.stopped && !this.xstr_is_ready()) {
342 this.xynt_streaming.reload();
347 function xynt_streaming(win, transp_type, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
350 this.transp_type = transp_type;
351 this.console = console;
354 this.cookiename = cookiename;
356 this.sandbox = sandbox;
358 this.cmdproc = cmdproc;
359 // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
360 this.doc = win.document;
361 this.keepalive_old = -1;
362 this.keepalive_new = -1;
364 this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
365 this.mon_wrntime = this.mon_errtime / 2;
371 xynt_streaming.prototype = {
385 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
388 cookiepath: "/brisk/",
390 hbit: function () {},
394 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
395 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
396 keepalives_eq_max: 4,
397 watchdog_checktm: 40,
398 watchdog_timeout: 100,
402 comm_match: /_*@BEGIN@(.*?)@END@/g,
403 comm_clean: /_*@BEGIN@(.*?)@END@/,
413 mon_update: function()
415 var date = new Date();
417 this.mon_time = date.getTime();
421 ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
423 return values: 0 unknown
428 mon_status: function()
432 var date = new Date();
434 delta = date.getTime() - this.mon_time;
436 if (delta < this.mon_wrntime)
438 else if (delta < this.mon_errtime)
444 start: function() { /* public */
448 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
450 this.log("xynt_streaming:start restart: "+this.restart_n);
451 this.keepalives_equal = 0;
454 this.page = url_complete(this.win.location.href, this.page);
455 // stat, subst, this.gst.st
457 this.page = url_append_args(this.page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
460 // transport instantiation
461 if (this.transp_type == "xhr") {
462 this.page = url_append_args(this.page, "transp", "xhr");
463 this.transp = new transport_xhr(this.doc, this, this.page);
465 else if (this.transp_type == "iframe") {
466 this.page = url_append_args(this.page, "transp", "iframe");
467 this.transp = new transport_iframe(this.doc, this, this.page);
469 else if (this.transp_type == "htmlfile") {
470 this.page = url_append_args(this.page, "transp", "htmlfile");
471 this.transp = new transport_htmlfile(this.doc, this, this.page);
477 this.watchdog_ct = 0;
479 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
482 var date = new Date();
483 this.start_time = date.getTime();
491 hbit_set: function (hbit) {
496 hbit_status: function () {
499 ret = this.mon_status();
500 // console.log("mon_status: "+ret+" 0: "+this.mon_time);
516 if (this.mon_stat_old != mon_stat) {
518 this.mon_stat_old = mon_stat;
523 watchdog: function () {
524 // alert("watchdog");
526 var comm_newpart, comm_len, comm_arr;
529 if (this.sandbox != null) {
530 // 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;
531 var zug = "WATCHDOG sess = ["+this.sess+"] step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;
532 if (zug != this.sandbox.innerHTML)
533 this.sandbox.innerHTML = zug;
536 // WATCHDOGING THE CONNECTION
537 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
538 if (!this.watchable) {
541 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
542 if (!this.transp.xstr_is_init()) {
543 this.log("hs::watchdog: xstr_is_init = false");
548 this.log("hs::watchdog: exception");
553 on IE7 the the window frame scope is cleaned after the href is set, so we wait
554 for a well know variable value before assign this object value to it (OO is a passion)
556 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
557 if (this.transp.xstr_set()) {
558 // this.ifra.contentWindow.xynt_streaming = this;
559 this.watchable = true;
560 this.watchdog_ct = 0;
561 this.log("hs::watchdog: watchable = yes");
565 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
566 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
567 if (this.keepalive_old == this.keepalive_new) {
568 this.keepalives_equal++;
571 this.keepalive_old = this.keepalive_new;
572 this.keepalives_equal = 0;
575 if (this.keepalives_equal >= this.keepalives_eq_max) {
576 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
578 // alert("watchdog return reload");
584 // PICK COMMANDS FROM STREAM
586 // alert("do--while begin ["+again+"]");
587 // CHECK: maybe again here isn't needed
590 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
591 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
592 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
599 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
600 ctx_new_len = this.transp.ctx_new_curlen_get();
601 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
602 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
605 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
606 this.keepalive_new++;
607 // alert("pre-loop 1");
608 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
609 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
610 if (this.transp.ctx_new_getchar(i) != '_') {
611 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
618 // this.log("ctx_new.charAt(i) == '_'");
621 // this.ifra.contentWindow.ctx_old_len = i;
622 this.transp.ctx_old_len_set(i);
623 if (i == ctx_new_len) {
624 this.log("old_len == i");
628 this.log("old_len != i: "+i);
630 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
632 comm_newpart = this.transp.new_part();
633 this.log("COM_NEWPART: ["+comm_newpart+"]");
635 comm_arr = comm_newpart.match(this.comm_match);
637 // alert("do--while middle2 ["+again+"]");
639 var comm_arr_len = comm_arr.length;
640 for (i = 0 ; i < comm_arr_len ; i++) {
641 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
642 this.gst.comms = this.gst.comms.concat(temp);
643 comm_len += comm_arr[i].length;
649 // this.ifra.contentWindow.ctx_old_len += comm_len;
650 this.transp.ctx_old_len_add(comm_len);
651 // this.ifra.contentWindow.script_clean = this.gst.st;
652 this.transp.scrcls_set(this.gst.st);
653 // alert("do--while end ["+again+"]");
656 // alert("post while");
657 // EXECUTION OF STREAM COMMANDS
660 //MOP ?? xhrrestart = 0;
661 if (this.gst.st_loc < this.gst.st_loc_new) {
662 // there is some slow actions running
665 else if (this.gst.comms.length > 0) {
668 singlecomm = this.gst.comms.shift();
669 // alert("EXE"+gugu);
670 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
673 // alert("SINGLE: ["+singlecomm+"]");
674 this.cmdproc(singlecomm);
680 var date = new Date();
681 if (date.getTime() > (this.start_time + this.restart_wait)) {
682 this.transp.postproc();
684 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
687 // alert("watchdog return normal");
693 // moved to xynt-streaming-ifra as push()
695 // keepalive: function (s) {
696 // this.log("hs::keepalive");
699 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
702 // this.ifra.contentWindow.ctx_new += "_";
704 // // this.keepalive_new++;
707 abort: function () { /* public */
708 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
711 if (this.watchdog_hdl != null) {
712 clearTimeout(this.watchdog_hdl);
713 this.watchdog_hdl = null;
717 this.log("hs::reload");
718 this.watchable = false;
719 if (this.transp != null) {
720 this.transp.destroy();
726 reload: function () {
732 if (this.console != null) {
733 return (this.console.log(s));