Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Trap.Endpoint=function(){Trap.EventObject.constructor.call(this);this.transportsMap=new Trap.Map();this.transports=new Trap.List();this.config=new Trap.Configuration();this.availableTransports=new Trap.List();this._state=Trap.Endpoint.State.CLOSED;this.channels=new Array();this.messageQueue=new Trap.ChannelMessageQueue();this.messageQueueType=Trap.Endpoint.Queue.REGULAR;this._maxActiveTransports=65535;this.sending=false;this.trapID=Trap.Constants.ENDPOINT_ID_UNDEFINED;this.trapFormat=(this.useBinary?Trap.Message.Format.REGULAR:Trap.Message.Format.SEVEN_BIT_SAFE);this._authentication=new Trap.Authentication();this.logger=Trap.Logger.getLogger("TrapEndpoint");this._lastAlive=0;this.canWakeupUntil=0;this.canReconnectUntil=0;this.keepaliveExpiry=5000;this.keepaliveInterval=Trap.Keepalive.Policy.DEFAULT;this.keepaliveTask=null;this.reconnectTimeout=180000;this.async=true;this.compressionEnabled=this.useBinary;Trap._compat.__defineGetter(this,"state",function(){return this._state});Trap._compat.__defineGetter(this,"queueType",function(){return this.messageQueueType});Trap._compat.__defineSetter(this,"queueType",function(b){this.messageQueueType=b});Trap._compat.__defineGetter(this,"maxActiveTransports",function(){return this._maxActiveTransports});Trap._compat.__defineSetter(this,"maxActiveTransports",function(b){this._maxActiveTransports=b});Trap._compat.__defineGetter(this,"authentication",function(){return this._authentication});Trap._compat.__defineSetter(this,"authentication",function(a){this._authentication=a;for(var d=0;d<this.transports.size();d++){this.transports.get(d).setAuthentication(a)}});Trap._compat.__defineGetter(this,"format",function(){return this.trapFormat});Trap._compat.__defineSetter(this,"format",function(c){this.trapFormat=c;var d=this.transports.iterator();while(d.hasNext()){d.next().setFormat(c)}});if(this.useBinary){if(Trap._useGetters){this._dispatchMessageEvent=function(c){var d=new Trap._GetterMessageEvent(c);this._dispatchEvent(d)}}else{this._dispatchMessageEvent=function(c){var d={type:"message",message:c.getData(),dataAsString:c.getString(),buffer:c.getData().buffer.slice(c.data.byteOffset,c.data.byteOffset+c.data.byteLength),compression:c.getCompression(),channel:c.getChannel(),object:JSON.parse(c.getString())};d.data=d.message;d.string=d.dataAsString;this._dispatchEvent(d)}}}else{this._dispatchMessageEvent=function(c){var d={type:"message",message:c.getString(),channel:c.getChannel()};d.data=d.message;d.dataAsString=d.data;d.string=d.data;this._dispatchEvent(d)}}this.channels[0]=new Trap.Channel(this,0);this.channels[0].setPriority(Number.MAX_VALUE);this.messageQueue.rebuild(this.channels)};Trap.Endpoint.prototype=new Trap.EventObject;Trap.Endpoint.prototype.constructor=Trap.Endpoint;Trap.Endpoint.State={CLOSED:"Trap.Endpoint.State.CLOSED",OPENING:"Trap.Endpoint.State.OPENING",OPEN:"Trap.Endpoint.State.OPEN",SLEEPING:"Trap.Endpoint.State.SLEEPING",ERROR:"Trap.Endpoint.State.ERROR",CLOSING:"Trap.Endpoint.State.CLOSING"};Trap.Endpoint.Queue={REGULAR:"Trap.Endpoint.Queue.REGULAR"};Trap.Endpoint.prototype.enableTransport=function(b){if(this.isTransportEnabled(b)){return}this.getTransport(b).enable()};Trap.Endpoint.prototype.disableTransport=function(b){if(!this.isTransportEnabled(b)){return}this.getTransport(b).disable()};Trap.Endpoint.prototype.disableAllTransports=function(){for(var b=0;b<this.transports.size();b++){this.transports.get(b).disable()}};Trap.Endpoint.prototype.isTransportEnabled=function(c){try{return this.getTransport(c).isEnabled()}catch(d){return false}};Trap.Endpoint.prototype.getConfiguration=function(){return this.config.toString()};Trap.Endpoint.prototype.parseConfiguration=function(b){return new Trap.Configuration(b)};Trap.Endpoint.prototype.configure=function(f){this.config=this.parseConfiguration(f);for(var e=0;e<this.transports.size();e++){this.transports.get(e).setConfiguration(this.config)}var d=this.config.getIntOption("trap.keepalive.interval",this.keepaliveInterval);this.setKeepaliveInterval(d);d=this.config.getIntOption("trap.keepalive.expiry",this.keepaliveExpiry);this.setKeepaliveExpiry(d);this.compressionEnabled=this.config.getBooleanOption(Trap.Constants.OPTION_ENABLE_COMPRESSION,this.compressionEnabled)};Trap.Endpoint.prototype.configureTransport=function(d,f,e){this.getTransport(d).configure(f,e)};Trap.Endpoint.prototype.getTransports=function(){return this.transports};Trap.Endpoint.prototype.getTransport=function(c){var d=this.transportsMap.get(c);if(d==null){throw"Unknown Transport"}return d};Trap.Endpoint.prototype.addTransport=function(n,m){if(!n.canConnect()&&!n.canListen()&&n.getState()==Trap.Transport.State.DISCONNECTED){this.logger.debug("Attempting to add transport class [{}] for handler [{}] that can neither connect nor listen. Skipping...",n,n.getTransportName());return false}var p=this.transportsMap.get(n.getTransportName());if(p!=null){var i=p.getTransportPriority();var k=n.getTransportPriority();if(i<k){this.logger.debug("Attempting to add new handler for [{}] when the old handler had a higher priority. New class was [{}]/{}, old class was [{}]{}. Skipping...",n.getTransportName(),n.getClass().getName(),n.getTransportPriority(),p.getClass().getName(),p.getTransportPriority());return false}this.transports.remove(p)}this.transportsMap.put(n.getTransportName(),n);this.transports.add(n);n.setFormat(this.getFormat());var o=this;n.onmessage=function(a){o.ttMessageReceived(a.message,n,null)};n.onmessagesent=function(a){o.ttMessageSent(a.message,n,null)};n.onstatechange=function(a){o.ttStateChanged(a.newState,a.oldState,n,null)};n.onfailedsending=function(a){o.ttMessagesFailedSending(a.messages,n,null)};if(m){n.setConfiguration(this.config);n.setAuthentication(this.authentication);n.setFormat(this.getFormat());var j=new Trap.List(this.availableTransports);var l=function(a){if(a>=j.size()){return}var b=j.get(a);b.isAlive(0,true,true,4000,function(c){if(!c){b.forceError()}else{if(b.getState()==Trap.Transport.State.AVAILABLE){this.addTransportToAvailable(b)}}})};if(n.getState()==Trap.Transport.State.AVAILABLE){this.availableTransports.clear();l(0);this.addTransportToAvailable(n)}else{l(0)}this.ttMessageReceived(m,n,null)}};Trap.Endpoint.prototype.setTrapID=function(b){this.trapID=b};Trap.Endpoint.prototype.getTrapID=function(){return this.trapID};Trap.Endpoint.prototype.removeTransport=function(b){this.transportsMap.remove(b.getTransportName());this.transports.remove(b)};Trap.Endpoint.prototype.close=function(){if(this.getState()!=Trap.Endpoint.State.OPEN){if(this.getState()==Trap.Endpoint.State.SLEEPING){this.setState(Trap.Endpoint.State.CLOSING);this.onEnd(null,null)}else{if(this.getState()==Trap.Endpoint.State.CLOSING||this.getState()==Trap.Endpoint.State.CLOSED){return}if(this.getState()==Trap.Endpoint.State.ERROR){this.logger.debug("Called close() on an endpoint in state ERROR. This might be caused by recovery code shared between regular and normal states");return}if(this.getState()==Trap.Endpoint.State.OPENING){this.logger.debug("Called close() on an endpoint in state OPENING. This message is logged for debug purposes (if we don't fully close).")}}}this.setState(Trap.Endpoint.State.CLOSING);try{this.sendMessage(this.createMessage().setOp(Trap.Message.Operation.END))}catch(b){this.logger.error("Setting Trap.Endpoint.State to ERROR due to an error while disconnecting that may have left the implementation in an inconsistent state");this.setState(Trap.Endpoint.State.ERROR)}};Trap.Endpoint.prototype.send=function(g,e,h){var f=this.createMessage().setOp(Trap.Message.Operation.MESSAGE).setData(g);if(h){f.setCompressed(h&&this.compressionEnabled&&this.useBinary)}else{f.setCompressed(false)}if(typeof(e)=="number"){f.setChannel(e)}this.sendMessage(f)};Trap.Endpoint.prototype.sendMessage=function(c){if(this.getState()!=Trap.Endpoint.State.OPEN&&c.getOp()!=Trap.Message.Operation.END&&this.getState()!=Trap.Endpoint.State.SLEEPING){throw"Tried to send to non-open Trap session"}var d=this.getChannel(c.getChannel());d.send(c);this.kickSendingThread()};Trap.Endpoint.prototype._sendFun=function(){try{for(;;){var g=null;if(this.messageQueue.peek()!=null){try{g=this.availableTransports.get(0)}catch(e){}if(g!=null){while(g.isAvailable()){try{var f=this.messageQueue.peek();if(f==null||typeof(f)=="undefined"){break}this.logger.debug("Attempting to send message with op {}",f.getOp());g.send(f,true);this.messageQueue.pop()}catch(h){this.logger.debug(h);if(g.isAvailable()){this.logger.warn("Forcibly removing transport {} from available due to infinite loop protection. This code should not occur with a well-behaved transport.",g.getTransportName());this.logger.warn("Caused by {}",h);g.forceError()}else{}}}if(g.isAvailable()){g.flushTransport()}}}if(this.messageQueue.peek()==null||g==null){this.sending=false;return}}}catch(e){this.logger.error(e)}finally{this.messageQueue.rewind()}};Trap.Endpoint.prototype.kickSendingThread=function(){if(!this.sending){this.sending=true;var b=this;setTimeout(function(){b._sendFun()},10)}};Trap.Endpoint.prototype.ttStateChanged=function(i,g,h){if(i==Trap.Transport.State.AVAILABLE){this.addTransportToAvailable(h);this.kickSendingThread();return}this.availableTransports.remove(h);if(i==Trap.Transport.State.DISCONNECTED||i==Trap.Transport.State.ERROR){if(this.getState()==Trap.Endpoint.State.CLOSED||this.getState()==Trap.Endpoint.State.CLOSING){if(this.getState()==Trap.Endpoint.State.CLOSING){for(var j=0;j<this.transports.size();j++){var f=this.transports.get(j);if(f.getState()!=Trap.Transport.State.ERROR&&f.getState()!=Trap.Transport.State.DISCONNECTED){return}}this.setState(Trap.Endpoint.State.CLOSED)}}}};Trap.Endpoint.prototype.reconnect=function(d,c){};Trap.Endpoint.prototype.onmessage=function(b){};Trap.Endpoint.prototype.onstatechange=function(b){};Trap.Endpoint.prototype.onfailedsending=function(b){};Trap.Endpoint.prototype.createMessage=function(){return new Trap.Message().setFormat(this.trapFormat)};Trap.Endpoint.prototype.addTransportToAvailable=function(c){var h=false;for(var f=0;f<this.availableTransports.size();f++){var g=this.availableTransports.get(f);if(g.getTransportPriority()>c.getTransportPriority()){this.availableTransports.add(f,c);h=true;break}else{if(g==c){return}}}if(!h){this.availableTransports.addLast(c)}if(this.availableTransports.size()>this.maxActiveTransports){var c=this.availableTransports.getLast();this.logger.debug("Disconnecting transport [{}] as the max active transports were exceeded. ({} active, {} max)",c.getTransportName(),this.availableTransports.size(),this._maxActiveTransports);c.disconnect()}};Trap.Endpoint.prototype.ttMessageReceived=function(d,c){this.logger.debug("Received message with op {}",d.getOp());if(this.async&&(d.getMessageId()!=0)){this.getChannel(d.getChannel()).receiveMessage(d,c)}else{this.executeMessageReceived(d,c)}};Trap.Endpoint.prototype.executeMessageReceived=function(d,c){switch(d.getOp()){case 1:this.onOpen(d,c);break;case 2:this.onOpened(d,c);break;case 3:this.onClose(d,c);break;case 4:this.onEnd(d,c);break;case 5:this.onChallenge(d,c);break;case 6:this.onError(d,c);break;case 8:this.onMessage(d,c);break;case 16:this.onOK(d,c);break;case 17:this.onPing(d,c);break;case 18:this.onPong(d,c);break;case 19:this.onTransport(d,c);break;default:return}};Trap.Endpoint.prototype.onTransport=function(d,c){};Trap.Endpoint.prototype.onPong=function(d,c){};Trap.Endpoint.prototype.onPing=function(d,c){};Trap.Endpoint.prototype.onOK=function(d,c){};Trap.Endpoint.prototype.onMessage=function(d,c){this._dispatchMessageEvent(d)};Trap._GetterMessageEvent=function(b){this._orig=b;this.type="message"};Trap._compat.__defineGetter(Trap._GetterMessageEvent.prototype,"message",function(){return this._orig.data});Trap._compat.__defineGetter(Trap._GetterMessageEvent.prototype,"data",function(){return this._orig.data});Trap._compat.__defineGetter(Trap._GetterMessageEvent.prototype,"string",function(){return this._orig.string});Trap._compat.__defineGetter(Trap._GetterMessageEvent.prototype,"channel",function(){return this._orig.channel});Trap._compat.__defineGetter(Trap._GetterMessageEvent.prototype,"dataAsString",function(){return this._orig.string});Trap._compat.__defineGetter(Trap._GetterMessageEvent.prototype,"object",function(){return JSON.parse(this._orig.string)});Trap._compat.__defineGetter(Trap._GetterMessageEvent.prototype,"buffer",function(){return this._orig.message.data.buffer.slice(message.data.byteOffset,message.data.byteOffset+message.data.byteLength)});Trap.Endpoint.prototype.onError=function(d,c){this.setState(Trap.Endpoint.State.ERROR)};Trap.Endpoint.prototype.onChallenge=function(d,c){};Trap.Endpoint.prototype.onEnd=function(e,g){if(this.getState()==Trap.Endpoint.State.CLOSING){for(var f=0;f<this.transports.size();f++){this.transports.get(f).disconnect()}this.setState(Trap.Endpoint.State.CLOSED)}else{this.setState(Trap.Endpoint.State.CLOSING);try{this.sendMessage(this.createMessage().setOp(Trap.Message.Operation.END))}catch(h){this.logger.warn(h);for(var f=0;f<this.transports.size();f++){this.transports.get(f).disconnect()}}}};Trap.Endpoint.prototype.onClose=function(d,c){};Trap.Endpoint.prototype.onOpened=function(e,g){if(this.getState()==Trap.Endpoint.State.CLOSED){return}if(this.getState()==Trap.Endpoint.State.CLOSING){return}if(this.getState()==Trap.Endpoint.State.ERROR){return}if(this.trapID==Trap.Constants.ENDPOINT_ID_CLIENT){var f=new Trap.Configuration(e.string);var h=f.getOption(Trap.Constants.ENDPOINT_ID);this.setTrapID(h)}this.setState(Trap.Endpoint.State.OPEN)};Trap.Endpoint.prototype.setState=function(f){if(f==this._state){return}var e=this._state;this._state=f;this.logger.debug("TrapEndpoint changing state from {} to {}.",e,f);this._dispatchEvent({type:"statechange",newState:f,oldState:e});if(f==Trap.Endpoint.State.OPEN){this._dispatchEvent({type:"open"})}if(f==Trap.Endpoint.State.CLOSED){this._dispatchEvent({type:"close"})}if(f==Trap.Endpoint.State.SLEEPING){this._dispatchEvent({type:"sleep"})}if(f==Trap.Endpoint.State.SLEEPING){this._dispatchEvent({type:"sleeping"})}if(f==Trap.Endpoint.State.OPENING){this._dispatchEvent({type:"opening"})}if(f==Trap.Endpoint.State.CLOSING){this._dispatchEvent({type:"closing"})}if(f==Trap.Endpoint.State.ERROR){this._dispatchEvent({type:"error"});for(var d=0;d<this.transports.size();d++){this.transports.get(d).disconnect()}}if(f==Trap.Endpoint.State.CLOSED||f==Trap.Endpoint.State.CLOSING||f==Trap.Endpoint.State.ERROR){if(this.keepaliveTask){clearTimeout(this.keepaliveTask)}}};Trap.Endpoint.prototype.onOpen=function(e,g){if(this.getState()==Trap.Endpoint.State.CLOSED||this.getState()==Trap.Endpoint.State.CLOSING||this.getState()==Trap.Endpoint.State.ERROR){this.logger.debug("Connection Error: Received OPEN message on {}. Returning with END",this);g.sendTransportSpecific(this.createMessage().setOp(Trap.Message.Operation.END));var f=this;setTimeout(function(){if(g.getState()!=TrapTransportState.DISCONNECTED&&g.getState()!=TrapTransportState.ERROR){f.logger.debug("Disconnect Error: {} failed to disconnect, despite ending the session on {}",g,Tmt);g.forceError()}},5000);return}try{g.sendTransportSpecific(this.createOnOpenedMessage(e),false);this.setState(Trap.Endpoint.State.OPEN)}catch(h){this.logger.warn(h)}};Trap.Endpoint.prototype.createOnOpenedMessage=function(b){return this.createMessage().setOp(Trap.Message.Operation.OPENED)};Trap.Endpoint.prototype.ttMessagesFailedSending=function(h,g){for(var f=0;f<h.length;f++){var e=h[f];this.getChannel(e.getChannel).addFailedMessage()}for(var f=0;f<this.channels.length;f++){if(this.channels[f]!=null){this.channels[f].rebuildMessageQueue()}}this.kickSendingThread()};Trap.Endpoint.prototype.lastAlive=function(){for(var d=0;d<this.transports.size();d++){var e=this.transports.get(d);var f=e.lastAlive;if(this._lastAlive<f){this._lastAlive=f}}return this._lastAlive};Trap.Endpoint.prototype.isAlive=function(o,q,p,l,k){this.lastAlive();var i=new Date().valueOf()-o;if(this._lastAlive>i){k(true);return}if(!q){k(false);return}var n=0;var m=function(){};var r=this;m=function(a){if(a){k(true);return}if(n<r.availableTransports.size()){r.availableTransports.get(n).isAlive(o,q,l,m);n++}else{if(!p){k(false)}try{r.setState(Trap.Endpoint.State.SLEEPING);r.reconnect(l,function(){k(r.getState()==Trap.Endpoint.State.OPEN)})}catch(b){r.logger.error("Setting TrapEndpoint to state ERROR because reconnect failed. We don't know currently how to recover from this state, so the connection is dropped");r.setState(Trap.Endpoint.State.ERROR)}k(false)}};m(false)};Trap.Endpoint.prototype.getKeepaliveInterval=function(){return this.keepaliveInterval};Trap.Endpoint.prototype.setKeepaliveInterval=function(h){this.keepaliveInterval=h;for(var e=0;e<this.transports.size();e++){this.transports.get(e).setKeepaliveInterval(h)}var g=this.keepaliveInterval;if((g==Trap.Keepalive.Policy.DEFAULT)||(g==Trap.Keepalive.Policy.DISABLED)){return}if(this.keepaliveTask!=null){clearTimeout(this.keepaliveTask)}var f=this;this.keepaliveTask=setTimeout(function(){f._keepaliveFun()},g*1000)};Trap.Endpoint.prototype._keepaliveFun=function(){if((this.getState()==Trap.Endpoint.State.CLOSING)||(this.getState()==Trap.Endpoint.State.CLOSED)||(this.getState()==Trap.Endpoint.State.ERROR)){return}if((this.getKeepaliveInterval()==Trap.Keepalive.Policy.DISABLED)||(this.getKeepaliveInterval()==Trap.Keepalive.Policy.DEFAULT)){return}var k=new Date().valueOf()-(this.keepaliveInterval*1000)-this.keepaliveExpiry;for(var l=0;l<this.transports.size();l++){var e=this.transports.get(l);if(!e.isConnected()){continue}if(e.lastAlive<k){this.logger.debug("Transport {} is not compliant with the keepalive timers. Last alive reported was {}, but expected {}",e.getTransportName(),e.lastAlive,k);try{var h=this;e.isAlive(this.keepaliveExpiry,true,this.keepaliveExpiry,function(a){if(!a){h.logger.info("Disconnecting transport {} because it had timed out while not performing its own checks",e.getTransportName());e.disconnect()}})}catch(i){this.logger.error("Exception while checking non-conforming transport",i)}}}var j=this.keepaliveInterval;if((j==Trap.Keepalive.Policy.DEFAULT)||(j==Trap.Keepalive.Policy.DISABLED)){return}var h=this;this.keepaliveTask=setTimeout(function(){h._keepaliveFun()},j*1000)};Trap.Endpoint.prototype.setKeepaliveExpiry=function(c){this.keepaliveExpiry=c;for(var d=0;d<this.transports.size();d++){this.transports.get(d).setKeepaliveExpiry(c)}};Trap.Endpoint.prototype.getChannel=function(e){var f=this.channels[e];if(f==null){f=new Trap.Channel(this,e);var c=this.getMaxChunkSize();c=Math.min(c,f.getChunkSize());if(c<=0){c=Number.MAX_VALUE}f.setChunkSize(c);this.channels[e]=f;this.messageQueue.rebuild(this.channels)}return f};Trap.Endpoint.prototype.getMaxChunkSize=function(){return this.config.getIntOption("trap."+Trap.Constants.OPTION_MAX_CHUNK_SIZE,64*1024)};Trap.Endpoint.prototype.ttMessageSent=function(d,f,e){this.getChannel(d.getChannel()).messageSent(d)};
New to GrepCode? Check out our FAQ X