var cdform={debug_:!1,cdworkers_:"182.254.152.123,182.254.169.96,115.159.87.31,115.159.39.166,115.159.46.67,115.159.91.80:80,115.159.24.72".split(","),cdworkertimestamp_:0,cdrid_:"0000000000000000",version_:4,inited_:!1,opened_:!1,callback_:null,clientid_:"",callbacks_:[],parallelcallback_:null,parallelmsg_:null,parallelProcess_:null,cid2cdr_:[],log:function(a){this.debug_&&(typeof kernel!="undefined"&&kernel.debug_?kernel.log(a):console.log(a))},loadcdworker:function(a){var b=this;b.log((new Date).toString()+
" loadcdworker"+b.version_);var d=document.getElementById("cdworker"+b.version_);null!=d&&document.body.removeChild(d);d=document.createElement("iframe");d.id="cdworker"+b.version_;d.style.display="none";document.body.appendChild(d);b.form_=d;d.src=this.selectcdworker(a);this.cdworkertimestamp_=0;setTimeout(function(){0==b.cdworkertimestamp_&&b.loadcdworker(a)},3E4)},selectcdworker:function(a){var b;if(a)for(c in b=[],this.cdworkers_)this.cdworkers_[c].indexOf(":"+a)>0&&b.push(this.cdworkers_[c]);
else b=this.cdworkers_;a=Math.floor(Math.random()*b.length);b=b[a];b.indexOf(":")<0&&(b+=":12851");this.log("loadcdworker:"+b);return"http://"+b+"/www.dianshangji.com/dianshangji/cdworker"+this.version_+"?_="+(new Date).getTime()},postmaster:function(a){this.form_.contentWindow.postMessage("00000000000000000000000000000000"+a,"*")},postpeer:function(a,b,d){a="0000000000000000"+a+b;typeof d!="undefined"&&(a=a+"\n"+d);this.log((new Date).toString()+" postpeer: "+a);this.form_.contentWindow.postMessage(a,
"*")},resolvePeer:function(a,b){var d=this;a in d.cid2cdr_?b(d.cid2cdr_[a]):a.length==16?b(a):d.invokeMaster("cid2cdr "+a,function(f){null!=f&&f.length>0&&(d.cid2cdr_[a]=f);b(f)})},unresolvePeer:function(a){a in this.cid2cdr_&&delete this.cid2cdr_[a]},invokeParallel:function(a,b,d,f,e){var g=this;if(a>250)return!1;if(g.parallelcallback_)return!1;g.parallelcallback_=e;g.parallelmsg_=d;d="00000000000000000000000000000000par "+(a+65536).toString(16).substr(3,2)+"000000"+d;typeof f!="undefined"&&(d=d+
"\n"+f);g.form_.contentWindow.postMessage(d,"*");setTimeout(function(){if(g.parallelcallback_)g.parallelcallback_=null,g.parallelmsg_=null,e(a)},b*1E3);return!0},endParallel:function(a){if(this.parallelmsg_==a)this.parallelcallback_=this.parallelmsg_=null},enableParallel:function(a){this.parallelProcess_=a;this.form_.contentWindow.postMessage("ffffffffffffffffffffffffffffffffpar enableParallel","*")},invokePeer:function(a,b,d,f,e){var g=this,b="msg "+b,j;f&&(j=e?a+"msg "+e:a+b,this.callbacks_[j]=
f);this.postpeer(a,b,d);f&&setTimeout(function(){j in g.callbacks_&&(g.log("invokePeer timeout: "+j),delete g.callbacks_[j],f(null))},1E4)},invokeServer:function(a,b,d){this.invokePeer("FF00000000000000",a,b,d)},invokeRtformdata:function(a,b,d){this.invokePeer("FE00000000000000","rtformdata "+a,b,d)},invokeServerWrite:function(a,b,d){this.invokeServer("godbwrite "+a,b,d)},invokeServerRead:function(a,b){this.invokeServer("godbread "+a,"",b)},invokeNode:function(a,b,d,f){var e=this;a in e.cid2cdr_?
(peer=e.cid2cdr_[a],e.invokePeer(peer,b,d,f)):e.resolvePeer(a,function(a){null==a||a.length==0?f(null):e.invokePeer(a,b,d,f)})},invokeMaster:function(a,b){var d=this,f="cmd "+d.clientid_+" "+a;b&&(d.callbacks_[f]=b);d.postmaster(f);b&&setTimeout(function(){f in d.callbacks_&&(d.log("invokeMaster timeout: "+f),delete d.callbacks_[f],b(null))},1E4)},processCommand:function(a,b){this.log("processCommand: master msg = "+a+", data = "+b);var d=a.split(" "),f=d[1];if(f!=this.clientid_)this.log("Invalid cid = "+
f+", while clientid_ = "+this.clientid_);else switch(d[2]){case "uid":d=arg.split("\t");if(this.uid_!=d[3]){this.log("corrupted uid: "+request);break}this.log("data of uid "+d[0]+": "+d[1]);break;default:a in this.callbacks_?(d=this.callbacks_[a],delete this.callbacks_[a],d(b)):this.log("no callback for "+a)}},processMessage:function(a,b,d,f){this.log((new Date).toString()+" processMessage: peer = "+a+", msg = "+b+", data = "+d);var e=a+b;e in this.callbacks_?(a=this.callbacks_[e],delete this.callbacks_[e],
a(d)):f(a,b,d)},processParallelMessage:function(a,b,d){var f=parseInt(b.substr(0,2),16)+parseInt(b.substr(2,2),16)*256,e=parseInt(b.substr(4,2),16)+parseInt(b.substr(6,2),16)*256;!(e>=f)&&b.substr(8)==this.parallelmsg_&&(this.log("processParallelMessage: peer = "+a+", parallel number = "+f+", parallel index = "+e+", data = "+d),this.parallelcallback_(e,a,d))},isinited:function(){return this.inited_},isopened:function(){return this.opened_},init:function(a,b,d,f){var e=this;this.clientid_=a;this.inited_||
(e.loadcdworker(f),window.addEventListener("message",function(a){if(e.form_!=null&&a.source==e.form_.contentWindow)if(a=a.data,a.match("^open"))e.log("send to cdworker: cid "+e.clientid_),e.postmaster("cid "+e.clientid_),typeof e.autoSignin!="undefined"&&e.autoSignin();else if(a.match("^close"))e.opened_=!1,e.log("cdworker close: cid "+e.clientid_),setTimeout(function(){e.loadcdworker()},1E4),e.closeCallback&&e.closeCallback();else if(!(a.length<=32)){var f=a.substr(0,16),l=a.substr(16,16),i=a.indexOf("\n"),
h,k=null;i>0?(h=a.substr(32,i-32),k=a.substr(i+1)):h=a.substr(32);i=h.indexOf(" ");i>0?(func=h.substr(0,i),arg=h.substr(i+1)):(func=h,arg="");switch(func){case "cid":if(arg!=e.clientid_){e.log("ERROR: cid("+arg+") from master is different from bg.clientid_("+e.clientid_+")");break}e.cdworkertimestamp_=(new Date).getTime();e.cdrid_=l;e.inited_=!0;e.opened_=!0;e.log("my cdrid = "+e.cdrid_);b(e.cdrid_);break;case "cmd":e.processCommand(h,k);break;case "msg":arg.match(/^par /)?e.processParallelMessage(f,
h.substr(8),k):e.processMessage(f,h,k,d);break;case "par":e.parallelProcess_(f,h,k);break;default:e.log("cdform unknown request: "+a)}}}))}};
