function ClientChannelEventItem(a,b,c){this.eventType=a;this.channel=b;this.callback=c}function ClientChannel(f,g,h,j){this.EVENT_CHANNEL_START=0;this.EVENT_CHANNEL_STOP=1;this.EVENT_SERVER_DISCONNECT=2;this.EVENT_CALLBACK_ADDED=3;this.EVENT_CALLBACK_REMOVED=4;this.callbacks=new Array();this.connectionInfo=h;this.securityToken=(j==null)?(((new Date()).getTime())+""):j;this.admin=new DSAdmin(h);this.onChannelStateChange=null;this.serverChannelName=(g==null)?"":g;this.callbackLoop=null;this.channelId=f!=null?f:"client"+((new Date()).getTime())+Math.round(Math.random()*10000);this.registerCallback=function(a){if(this.callbackLoop!=null&&!this.callbackLoop.stopped){this.callbacks[this.callbacks.length]=a;this.callbackLoop.registerCallback(a)}};this.unregisterCallback=function(a){if(this.callbackLoop!=null){removeItemFromArray(this.callbacks,a);if(!this.callbackLoop.stopped){this.callbackLoop.unregisterCallback(a)}if(this.callbacks.length==0){this.callbackLoop=null}}};this.connect=function(a){if(this.callbackLoop!=null){this.callbackLoop.stop()}this.callbacks.length=0;this.callbacks[this.callbacks.length]=a;this.callbackLoop=new CallbackLoop(this,this.connectionInfo,this.securityToken);this.callbackLoop.start(a)};this.disconnect=function(){this.callbacks.length=0;if(this.callbackLoop!=null){this.callbackLoop.stop();this.callbackLoop=null}};this.closeSession=function(){if(this.admin!=null){this.admin.executor.closeSession()}};this.broadcast=function(a,b){var c=null;if(a!=null){var d=(b==null||b==""||!isString(b))?this.serverChannelName:b;if(d!=""){c=this.admin.BroadcastToChannel(d,a)}}if(c!=null&&c.result==null){return c}return c!=null&&c.result!=null&&c.result};this.notify=function(a,b,c){if(a==null||b==null||c==null){return false}if(a==this.channelId){for(var i=0;i<this.callbacks.length;i++){var d=this.callbacks[i];if(d.callbackId==b){var e=d.notifyCallback(c);return e!=null?e:false}}return false}return this.admin.NotifyCallback(a,b,c)}}function ClientCallback(a,b,c,d){this.clientChannel=a;this.callbackId=b!=null?b:"cb"+((new Date()).getTime())+Math.round(Math.random()*10000);this.notifyCallback=c;this.serverChannelNames=(d==null)?[]:(d=="")?[]:(isString(d))?[d]:(isArray(d))?d:[]}function CallbackLoop(n,o,p){this.executor=new ServerFunctionExecutor("DSAdmin",o,this);this.clientChannel=n;this.stopped=true;this.securityToken=p;this.callback=function(a,b,c){if(c!=null&&!c.stopped&&a!=null){a=(a.result!=null)?a.result:a;a=isArray(a)?a[0]:a;var d=getSessionID();if(d==null){c.stopped=true}if(a.SessionExpired!=null){c.stopped=true;for(var i=0;i<n.callbacks.length;i++){n.callbacks[i].notifyCallback(a)}if(isReferenceAFunction(n.onChannelStateChange)){n.onChannelStateChange(new ClientChannelEventItem(n.EVENT_SERVER_DISCONNECT,n,null))}}else if(a.broadcast!=null){var e=a.broadcast;var f=e[0];var g=e[1];var h=a.channel==null?n.serverChannelName:a.channel;var j=n.serverChannelName==h;for(var i=0;i<n.callbacks.length;i++){var k=n.callbacks[i];if(j||arrayIndexOf(k.serverChannelNames,h)>-1){k.notifyCallback(f,g)}}c.sendResponse(true,c)}else if(a.invoke!=null){var e=a.invoke;var l=e[0];var f=e[1];var g=e[2];var m;for(var i=0;i<n.callbacks.length;i++){m=n.callbacks[i];if(m.callbackId==l){c.sendResponse(m.notifyCallback(f,g),c);break}}}else if(a.error!=null){c.stopped=true;for(var i=0;i<n.callbacks.length;i++){n.callbacks[i].notifyCallback(a,"error")}if(isReferenceAFunction(n.onChannelStateChange)){n.onChannelStateChange(new ClientChannelEventItem(this.clientChannel.EVENT_SERVER_DISCONNECT,this.clientChannel,null))}}else if(a.closeChannel==null&&a.close==null){c.sendResponse(false,c)}else{c.stopped=true;for(var i=0;i<n.callbacks.length;i++){n.callbacks[i].notifyCallback(a,"closed")}
if(isReferenceAFunction(n.onChannelStateChange)){n.onChannelStateChange(new ClientChannelEventItem(n.EVENT_CHANNEL_STOP,n,null))}}}else{if(c!=null){if(!c.stopped&&isReferenceAFunction(n.onChannelStateChange)){n.onChannelStateChange(new ClientChannelEventItem(n.EVENT_SERVER_DISCONNECT,n,null))}c.stopped=true}}};this.start=function(a){if(this.stopped&&(!nullOrEmptyStr(this.clientChannel)||a.serverChannelNames.length>0)){this.stopped=false;this.executor.executeMethod("ConsumeClientChannel","GET",[this.clientChannel.serverChannelName,this.clientChannel.channelId,a.callbackId,arrayToCSV(a.serverChannelNames),this.securityToken,""],this.callback,true);if(isReferenceAFunction(this.clientChannel.onChannelStateChange)){this.clientChannel.onChannelStateChange(new ClientChannelEventItem(this.clientChannel.EVENT_CHANNEL_START,this.clientChannel,a))}}};this.stop=function(){if(!this.stopped&&this.clientChannel!=null){this.stopped=true;this.executor.executeMethod("CloseClientChannel","GET",[this.clientChannel.channelId,this.securityToken],null);if(isReferenceAFunction(this.clientChannel.onChannelStateChange)){this.clientChannel.onChannelStateChange(new ClientChannelEventItem(this.clientChannel.EVENT_CHANNEL_STOP,this.clientChannel,null))}}};this.sendResponse=function(a,b){if(!this.stopped){if(b==null){b=this}this.executor.executeMethod("ConsumeClientChannel","POST",["",b.clientChannel.channelId,"","",this.securityToken,a],b.callback,true)}};this.registerCallback=function(a){if(!this.stopped&&a!=null&&(!nullOrEmptyStr(this.clientChannel)||a.serverChannelNames.length>0)){this.executor.executeMethod("RegisterClientCallbackServer","GET",[this.clientChannel.channelId,a.callbackId,arrayToCSV(a.serverChannelNames),this.securityToken],null);if(isReferenceAFunction(this.clientChannel.onChannelStateChange)){this.clientChannel.onChannelStateChange(new ClientChannelEventItem(this.clientChannel.EVENT_CALLBACK_ADDED,this.clientChannel,a))}}};this.unregisterCallback=function(a){if(!this.stopped&&this.clientChannel!=null&&a!=null){this.executor.executeMethod("UnregisterClientCallback","GET",[this.clientChannel.channelId,a.callbackId,this.securityToken],null);if(isReferenceAFunction(this.clientChannel.onChannelStateChange)){this.clientChannel.onChannelStateChange(new ClientChannelEventItem(this.clientChannel.EVENT_CALLBACK_REMOVED,this.clientChannel,a))}}}}function removeItemFromArray(a,b){if(isArray(a)){for(var i=0;i<a.length;i++){if(a[i]==b){a.splice(i,1)}}}}
