home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / mx / messaging / Channel.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  30.1 KB  |  968 lines

  1. package mx.messaging
  2. {
  3.    import flash.errors.IllegalOperationError;
  4.    import flash.events.EventDispatcher;
  5.    import flash.events.TimerEvent;
  6.    import flash.utils.Timer;
  7.    import flash.utils.getDefinitionByName;
  8.    import mx.collections.ArrayCollection;
  9.    import mx.core.IMXMLObject;
  10.    import mx.core.mx_internal;
  11.    import mx.events.PropertyChangeEvent;
  12.    import mx.logging.ILogger;
  13.    import mx.logging.Log;
  14.    import mx.messaging.config.LoaderConfig;
  15.    import mx.messaging.config.ServerConfig;
  16.    import mx.messaging.errors.InvalidChannelError;
  17.    import mx.messaging.errors.InvalidDestinationError;
  18.    import mx.messaging.events.ChannelEvent;
  19.    import mx.messaging.events.ChannelFaultEvent;
  20.    import mx.messaging.messages.AbstractMessage;
  21.    import mx.messaging.messages.CommandMessage;
  22.    import mx.messaging.messages.IMessage;
  23.    import mx.resources.IResourceManager;
  24.    import mx.resources.ResourceManager;
  25.    import mx.rpc.AsyncDispatcher;
  26.    import mx.utils.URLUtil;
  27.    
  28.    use namespace mx_internal;
  29.    
  30.    public class Channel extends EventDispatcher implements IMXMLObject
  31.    {
  32.       private static const CLIENT_LOAD_BALANCING:String = "client-load-balancing";
  33.       
  34.       private static const CONNECT_TIMEOUT_SECONDS:String = "connect-timeout-seconds";
  35.       
  36.       private static const ENABLE_SMALL_MESSAGES:String = "enable-small-messages";
  37.       
  38.       private static const FALSE:String = "false";
  39.       
  40.       private static const RECORD_MESSAGE_TIMES:String = "record-message-times";
  41.       
  42.       private static const RECORD_MESSAGE_SIZES:String = "record-message-sizes";
  43.       
  44.       private static const REQUEST_TIMEOUT_SECONDS:String = "request-timeout-seconds";
  45.       
  46.       private static const SERIALIZATION:String = "serialization";
  47.       
  48.       private static const TRUE:String = "true";
  49.       
  50.       public static const SMALL_MESSAGES_FEATURE:String = "small_messages";
  51.       
  52.       private static const dep:ArrayCollection = null;
  53.       
  54.       private var _failoverIndex:int;
  55.       
  56.       private var _ownsWaitGuard:Boolean;
  57.       
  58.       protected var _recordMessageTimes:Boolean = false;
  59.       
  60.       private var _reconnecting:Boolean = false;
  61.       
  62.       private var _reliableReconnectLastTimestamp:Number;
  63.       
  64.       private var _reliableReconnectAttempts:int;
  65.       
  66.       private var _authenticated:Boolean = false;
  67.       
  68.       protected var messagingVersion:Number = 1;
  69.       
  70.       private var _channelSets:Array = [];
  71.       
  72.       private var _connectTimeout:int = -1;
  73.       
  74.       mx_internal var authenticating:Boolean;
  75.       
  76.       protected var _connecting:Boolean;
  77.       
  78.       private var _connectTimer:Timer;
  79.       
  80.       protected var _recordMessageSizes:Boolean = false;
  81.       
  82.       private var _failoverURIs:Array;
  83.       
  84.       protected var _log:ILogger;
  85.       
  86.       private var _connected:Boolean = false;
  87.       
  88.       private var _smallMessagesSupported:Boolean;
  89.       
  90.       private var _primaryURI:String;
  91.       
  92.       public var enableSmallMessages:Boolean = true;
  93.       
  94.       private var _id:String;
  95.       
  96.       private var _reliableReconnectBeginTimestamp:Number;
  97.       
  98.       private var resourceManager:IResourceManager = ResourceManager.getInstance();
  99.       
  100.       private var _uri:String;
  101.       
  102.       protected var _loginAfterDisconnect:Boolean = false;
  103.       
  104.       private var _isEndpointCalculated:Boolean;
  105.       
  106.       mx_internal var reliableReconnectDuration:int = -1;
  107.       
  108.       private var _shouldBeConnected:Boolean;
  109.       
  110.       private var _previouslyConnected:Boolean;
  111.       
  112.       private var _requestTimeout:int = -1;
  113.       
  114.       private var _endpoint:String;
  115.       
  116.       protected var credentials:String;
  117.       
  118.       public function Channel(param1:String = null, param2:String = null)
  119.       {
  120.          super();
  121.          _log = Log.getLogger("mx.messaging.Channel");
  122.          _failoverIndex = -1;
  123.          this.id = param1;
  124.          _primaryURI = param2;
  125.          this.uri = param2;
  126.       }
  127.       
  128.       private function shuffle(param1:Array) : void
  129.       {
  130.          var _loc4_:int = 0;
  131.          var _loc5_:Object = null;
  132.          var _loc2_:int = int(param1.length);
  133.          var _loc3_:int = 0;
  134.          while(_loc3_ < _loc2_)
  135.          {
  136.             _loc4_ = Math.floor(Math.random() * _loc2_);
  137.             if(_loc4_ != _loc3_)
  138.             {
  139.                _loc5_ = param1[_loc3_];
  140.                param1[_loc3_] = param1[_loc4_];
  141.                param1[_loc4_] = _loc5_;
  142.             }
  143.             _loc3_++;
  144.          }
  145.       }
  146.       
  147.       [Bindable(event="propertyChange")]
  148.       public function get connected() : Boolean
  149.       {
  150.          return _connected;
  151.       }
  152.       
  153.       private function shutdownConnectTimer() : void
  154.       {
  155.          if(_connectTimer != null)
  156.          {
  157.             _connectTimer.stop();
  158.             _connectTimer.removeEventListener(TimerEvent.TIMER,connectTimeoutHandler);
  159.             _connectTimer = null;
  160.          }
  161.       }
  162.       
  163.       public function get connectTimeout() : int
  164.       {
  165.          return _connectTimeout;
  166.       }
  167.       
  168.       public function get id() : String
  169.       {
  170.          return _id;
  171.       }
  172.       
  173.       private function calculateEndpoint() : void
  174.       {
  175.          var _loc3_:String = null;
  176.          if(uri == null)
  177.          {
  178.             _loc3_ = resourceManager.getString("messaging","noURLSpecified");
  179.             throw new InvalidChannelError(_loc3_);
  180.          }
  181.          var _loc1_:String = uri;
  182.          var _loc2_:String = URLUtil.getProtocol(_loc1_);
  183.          if(_loc2_.length == 0)
  184.          {
  185.             _loc1_ = URLUtil.getFullURL(LoaderConfig.url,_loc1_);
  186.          }
  187.          if(!URLUtil.hasUnresolvableTokens())
  188.          {
  189.             _isEndpointCalculated = false;
  190.             return;
  191.          }
  192.          _loc1_ = URLUtil.replaceTokens(_loc1_);
  193.          _loc2_ = URLUtil.getProtocol(_loc1_);
  194.          if(_loc2_.length > 0)
  195.          {
  196.             _endpoint = URLUtil.replaceProtocol(_loc1_,protocol);
  197.          }
  198.          else
  199.          {
  200.             _endpoint = protocol + ":" + _loc1_;
  201.          }
  202.          _isEndpointCalculated = true;
  203.          if(Log.isInfo())
  204.          {
  205.             _log.info("\'{0}\' channel endpoint set to {1}",id,_endpoint);
  206.          }
  207.       }
  208.       
  209.       [Bindable(event="propertyChange")]
  210.       public function get reconnecting() : Boolean
  211.       {
  212.          return _reconnecting;
  213.       }
  214.       
  215.       public function get useSmallMessages() : Boolean
  216.       {
  217.          return _smallMessagesSupported && enableSmallMessages;
  218.       }
  219.       
  220.       public function set connectTimeout(param1:int) : void
  221.       {
  222.          _connectTimeout = param1;
  223.       }
  224.       
  225.       [Bindable(event="propertyChange")]
  226.       public function get authenticated() : Boolean
  227.       {
  228.          return _authenticated;
  229.       }
  230.       
  231.       protected function getMessageResponder(param1:MessageAgent, param2:IMessage) : MessageResponder
  232.       {
  233.          throw new IllegalOperationError("Channel subclasses must override " + " getMessageResponder().");
  234.       }
  235.       
  236.       public function set failoverURIs(param1:Array) : void
  237.       {
  238.          if(param1 != null)
  239.          {
  240.             _failoverURIs = param1;
  241.             _failoverIndex = -1;
  242.          }
  243.       }
  244.       
  245.       protected function internalDisconnect(param1:Boolean = false) : void
  246.       {
  247.       }
  248.       
  249.       public function setCredentials(param1:String, param2:MessageAgent = null, param3:String = null) : void
  250.       {
  251.          var _loc5_:CommandMessage = null;
  252.          var _loc4_:* = this.credentials !== param1;
  253.          if(mx_internal::authenticating && _loc4_)
  254.          {
  255.             throw new IllegalOperationError("Credentials cannot be set while authenticating or logging out.");
  256.          }
  257.          if(authenticated && _loc4_)
  258.          {
  259.             throw new IllegalOperationError("Credentials cannot be set when already authenticated. Logout must be performed before changing credentials.");
  260.          }
  261.          this.credentials = param1;
  262.          if(connected && _loc4_ && param1 != null)
  263.          {
  264.             mx_internal::authenticating = true;
  265.             _loc5_ = new CommandMessage();
  266.             _loc5_.operation = CommandMessage.LOGIN_OPERATION;
  267.             _loc5_.body = param1;
  268.             if(param3 != null)
  269.             {
  270.                _loc5_.headers[CommandMessage.CREDENTIALS_CHARSET_HEADER] = param3;
  271.             }
  272.             internalSend(new AuthenticationMessageResponder(param2,_loc5_,this,_log));
  273.          }
  274.       }
  275.       
  276.       public function set id(param1:String) : void
  277.       {
  278.          if(_id != param1)
  279.          {
  280.             _id = param1;
  281.          }
  282.       }
  283.       
  284.       public function get mpiEnabled() : Boolean
  285.       {
  286.          return _recordMessageSizes || _recordMessageTimes;
  287.       }
  288.       
  289.       protected function applyClientLoadBalancingSettings(param1:XML) : void
  290.       {
  291.          var _loc5_:XML = null;
  292.          var _loc2_:XMLList = param1[CLIENT_LOAD_BALANCING];
  293.          if(_loc2_.length() == 0)
  294.          {
  295.             return;
  296.          }
  297.          var _loc3_:int = int(_loc2_.url.length());
  298.          if(_loc3_ == 0)
  299.          {
  300.             return;
  301.          }
  302.          var _loc4_:Array = new Array();
  303.          for each(_loc5_ in _loc2_.url)
  304.          {
  305.             _loc4_.push(_loc5_.toString());
  306.          }
  307.          shuffle(_loc4_);
  308.          if(Log.isInfo())
  309.          {
  310.             _log.info("\'{0}\' channel picked {1} as its main url.",id,_loc4_[0]);
  311.          }
  312.          this.url = _loc4_[0];
  313.          var _loc6_:Array = _loc4_.slice(1);
  314.          if(_loc6_.length > 0)
  315.          {
  316.             this.failoverURIs = _loc6_;
  317.          }
  318.       }
  319.       
  320.       protected function setFlexClientIdOnMessage(param1:IMessage) : void
  321.       {
  322.          var _loc2_:String = FlexClient.getInstance().id;
  323.          param1.headers[AbstractMessage.FLEX_CLIENT_ID_HEADER] = _loc2_ != null ? _loc2_ : FlexClient.mx_internal::NULL_FLEXCLIENT_ID;
  324.       }
  325.       
  326.       protected function connectTimeoutHandler(param1:TimerEvent) : void
  327.       {
  328.          var _loc2_:String = null;
  329.          var _loc3_:ChannelFaultEvent = null;
  330.          shutdownConnectTimer();
  331.          if(!connected)
  332.          {
  333.             _shouldBeConnected = false;
  334.             _loc2_ = resourceManager.getString("messaging","connectTimedOut");
  335.             _loc3_ = ChannelFaultEvent.createEvent(this,false,"Channel.Connect.Failed","error",_loc2_);
  336.             connectFailed(_loc3_);
  337.          }
  338.       }
  339.       
  340.       protected function flexClientWaitHandler(param1:PropertyChangeEvent) : void
  341.       {
  342.          var _loc2_:FlexClient = null;
  343.          if(param1.property == "waitForFlexClientId")
  344.          {
  345.             _loc2_ = param1.source as FlexClient;
  346.             if(_loc2_.mx_internal::waitForFlexClientId == false)
  347.             {
  348.                _loc2_.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,flexClientWaitHandler);
  349.                _loc2_.mx_internal::waitForFlexClientId = true;
  350.                _ownsWaitGuard = true;
  351.                internalConnect();
  352.             }
  353.          }
  354.       }
  355.       
  356.       protected function get shouldBeConnected() : Boolean
  357.       {
  358.          return _shouldBeConnected;
  359.       }
  360.       
  361.       public function set useSmallMessages(param1:Boolean) : void
  362.       {
  363.          _smallMessagesSupported = param1;
  364.       }
  365.       
  366.       mx_internal function internalSetCredentials(param1:String) : void
  367.       {
  368.          this.credentials = param1;
  369.       }
  370.       
  371.       private function reconnect(param1:TimerEvent = null) : void
  372.       {
  373.          internalConnect();
  374.       }
  375.       
  376.       private function connectCleanup() : void
  377.       {
  378.          if(_ownsWaitGuard)
  379.          {
  380.             _ownsWaitGuard = false;
  381.             FlexClient.getInstance().mx_internal::waitForFlexClientId = false;
  382.          }
  383.          _connecting = false;
  384.          setReconnecting(false);
  385.          reliableReconnectCleanup();
  386.       }
  387.       
  388.       mx_internal function get realtime() : Boolean
  389.       {
  390.          return false;
  391.       }
  392.       
  393.       protected function internalConnect() : void
  394.       {
  395.       }
  396.       
  397.       public function get url() : String
  398.       {
  399.          return uri;
  400.       }
  401.       
  402.       public function get recordMessageTimes() : Boolean
  403.       {
  404.          return _recordMessageTimes;
  405.       }
  406.       
  407.       public function get uri() : String
  408.       {
  409.          return _uri;
  410.       }
  411.       
  412.       private function initializeRequestTimeout(param1:MessageResponder) : void
  413.       {
  414.          var _loc2_:IMessage = param1.message;
  415.          if(_loc2_.headers[AbstractMessage.REQUEST_TIMEOUT_HEADER] != null)
  416.          {
  417.             param1.startRequestTimeout(_loc2_.headers[AbstractMessage.REQUEST_TIMEOUT_HEADER]);
  418.          }
  419.          else if(requestTimeout > 0)
  420.          {
  421.             param1.startRequestTimeout(requestTimeout);
  422.          }
  423.       }
  424.       
  425.       public function send(param1:MessageAgent, param2:IMessage) : void
  426.       {
  427.          var _loc4_:String = null;
  428.          if(param2.destination.length == 0)
  429.          {
  430.             if(param1.destination.length == 0)
  431.             {
  432.                _loc4_ = resourceManager.getString("messaging","noDestinationSpecified");
  433.                throw new InvalidDestinationError(_loc4_);
  434.             }
  435.             param2.destination = param1.destination;
  436.          }
  437.          if(Log.isDebug())
  438.          {
  439.             _log.debug("\'{0}\' channel sending message:\n{1}",id,param2.toString());
  440.          }
  441.          param2.headers[AbstractMessage.ENDPOINT_HEADER] = id;
  442.          var _loc3_:MessageResponder = getMessageResponder(param1,param2);
  443.          initializeRequestTimeout(_loc3_);
  444.          internalSend(_loc3_);
  445.       }
  446.       
  447.       public function logout(param1:MessageAgent) : void
  448.       {
  449.          var _loc2_:CommandMessage = null;
  450.          if(connected && authenticated && credentials || mx_internal::authenticating && credentials)
  451.          {
  452.             _loc2_ = new CommandMessage();
  453.             _loc2_.operation = CommandMessage.LOGOUT_OPERATION;
  454.             internalSend(new AuthenticationMessageResponder(param1,_loc2_,this,_log));
  455.             mx_internal::authenticating = true;
  456.          }
  457.          credentials = null;
  458.       }
  459.       
  460.       public function get endpoint() : String
  461.       {
  462.          if(!_isEndpointCalculated)
  463.          {
  464.             calculateEndpoint();
  465.          }
  466.          return _endpoint;
  467.       }
  468.       
  469.       public function get protocol() : String
  470.       {
  471.          throw new IllegalOperationError("Channel subclasses must override " + "the get function for \'protocol\' to return the proper protocol " + "string.");
  472.       }
  473.       
  474.       public function get failoverURIs() : Array
  475.       {
  476.          return _failoverURIs != null ? _failoverURIs : [];
  477.       }
  478.       
  479.       final public function disconnect(param1:ChannelSet) : void
  480.       {
  481.          if(_ownsWaitGuard)
  482.          {
  483.             _ownsWaitGuard = false;
  484.             FlexClient.getInstance().mx_internal::waitForFlexClientId = false;
  485.          }
  486.          var _loc2_:int = param1 != null ? int(_channelSets.indexOf(param1)) : -1;
  487.          if(_loc2_ != -1)
  488.          {
  489.             _channelSets.splice(_loc2_,1);
  490.             removeEventListener(ChannelEvent.CONNECT,param1.channelConnectHandler,false);
  491.             removeEventListener(ChannelEvent.DISCONNECT,param1.channelDisconnectHandler,false);
  492.             removeEventListener(ChannelFaultEvent.FAULT,param1.channelFaultHandler,false);
  493.             if(connected)
  494.             {
  495.                param1.channelDisconnectHandler(ChannelEvent.createEvent(ChannelEvent.DISCONNECT,this,false));
  496.             }
  497.             if(_channelSets.length == 0)
  498.             {
  499.                _shouldBeConnected = false;
  500.                if(connected)
  501.                {
  502.                   internalDisconnect();
  503.                }
  504.             }
  505.          }
  506.       }
  507.       
  508.       public function set requestTimeout(param1:int) : void
  509.       {
  510.          _requestTimeout = param1;
  511.       }
  512.       
  513.       private function shouldAttemptFailover() : Boolean
  514.       {
  515.          return _shouldBeConnected && (_previouslyConnected || mx_internal::reliableReconnectDuration != -1 || _failoverURIs != null && _failoverURIs.length > 0);
  516.       }
  517.       
  518.       private function setReconnecting(param1:Boolean) : void
  519.       {
  520.          var _loc2_:PropertyChangeEvent = null;
  521.          if(_reconnecting != param1)
  522.          {
  523.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"reconnecting",_reconnecting,param1);
  524.             _reconnecting = param1;
  525.             dispatchEvent(_loc2_);
  526.          }
  527.       }
  528.       
  529.       public function applySettings(param1:XML) : void
  530.       {
  531.          var _loc2_:XML = null;
  532.          var _loc3_:XMLList = null;
  533.          if(Log.isInfo())
  534.          {
  535.             _log.info("\'{0}\' channel settings are:\n{1}",id,param1);
  536.          }
  537.          if(param1.properties.length() != 0)
  538.          {
  539.             _loc2_ = param1.properties[0];
  540.             applyClientLoadBalancingSettings(_loc2_);
  541.             if(_loc2_[CONNECT_TIMEOUT_SECONDS].length() != 0)
  542.             {
  543.                connectTimeout = _loc2_[CONNECT_TIMEOUT_SECONDS].toString();
  544.             }
  545.             if(_loc2_[RECORD_MESSAGE_TIMES].length() != 0)
  546.             {
  547.                _recordMessageTimes = _loc2_[RECORD_MESSAGE_TIMES].toString() == TRUE;
  548.             }
  549.             if(_loc2_[RECORD_MESSAGE_SIZES].length() != 0)
  550.             {
  551.                _recordMessageSizes = _loc2_[RECORD_MESSAGE_SIZES].toString() == TRUE;
  552.             }
  553.             if(_loc2_[REQUEST_TIMEOUT_SECONDS].length() != 0)
  554.             {
  555.                requestTimeout = _loc2_[REQUEST_TIMEOUT_SECONDS].toString();
  556.             }
  557.             _loc3_ = _loc2_[SERIALIZATION];
  558.             if(_loc3_.length() != 0 && _loc3_[ENABLE_SMALL_MESSAGES].toString() == FALSE)
  559.             {
  560.                enableSmallMessages = false;
  561.             }
  562.          }
  563.       }
  564.       
  565.       private function reliableReconnectCleanup() : void
  566.       {
  567.          mx_internal::reliableReconnectDuration = -1;
  568.          _reliableReconnectBeginTimestamp = 0;
  569.          _reliableReconnectLastTimestamp = 0;
  570.          _reliableReconnectAttempts = 0;
  571.       }
  572.       
  573.       protected function connectSuccess() : void
  574.       {
  575.          var _loc1_:int = 0;
  576.          var _loc2_:Array = null;
  577.          var _loc3_:int = 0;
  578.          shutdownConnectTimer();
  579.          if(ServerConfig.mx_internal::fetchedConfig(endpoint))
  580.          {
  581.             _loc1_ = 0;
  582.             while(_loc1_ < channelSets.length)
  583.             {
  584.                _loc2_ = ChannelSet(channelSets[_loc1_]).messageAgents;
  585.                _loc3_ = 0;
  586.                while(_loc3_ < _loc2_.length)
  587.                {
  588.                   _loc2_[_loc3_].needsConfig = false;
  589.                   _loc3_++;
  590.                }
  591.                _loc1_++;
  592.             }
  593.          }
  594.          setConnected(true);
  595.          _failoverIndex = -1;
  596.          if(Log.isInfo())
  597.          {
  598.             _log.info("\'{0}\' channel is connected.",id);
  599.          }
  600.          dispatchEvent(ChannelEvent.createEvent(ChannelEvent.CONNECT,this,reconnecting));
  601.          connectCleanup();
  602.       }
  603.       
  604.       public function get recordMessageSizes() : Boolean
  605.       {
  606.          return _recordMessageSizes;
  607.       }
  608.       
  609.       protected function setConnected(param1:Boolean) : void
  610.       {
  611.          var _loc2_:PropertyChangeEvent = null;
  612.          if(_connected != param1)
  613.          {
  614.             if(_connected)
  615.             {
  616.                _previouslyConnected = true;
  617.             }
  618.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"connected",_connected,param1);
  619.             _connected = param1;
  620.             dispatchEvent(_loc2_);
  621.             if(!param1)
  622.             {
  623.                mx_internal::setAuthenticated(false);
  624.             }
  625.          }
  626.       }
  627.       
  628.       public function get requestTimeout() : int
  629.       {
  630.          return _requestTimeout;
  631.       }
  632.       
  633.       protected function connectFailed(param1:ChannelFaultEvent) : void
  634.       {
  635.          shutdownConnectTimer();
  636.          setConnected(false);
  637.          if(Log.isError())
  638.          {
  639.             _log.error("\'{0}\' channel connect failed.",id);
  640.          }
  641.          if(!param1.rejected && shouldAttemptFailover())
  642.          {
  643.             _connecting = true;
  644.             failover();
  645.          }
  646.          else
  647.          {
  648.             connectCleanup();
  649.          }
  650.          if(reconnecting)
  651.          {
  652.             param1.reconnecting = true;
  653.          }
  654.          dispatchEvent(param1);
  655.       }
  656.       
  657.       public function set uri(param1:String) : void
  658.       {
  659.          if(param1 != null)
  660.          {
  661.             _uri = param1;
  662.             calculateEndpoint();
  663.          }
  664.       }
  665.       
  666.       public function initialized(param1:Object, param2:String) : void
  667.       {
  668.          this.id = param2;
  669.       }
  670.       
  671.       public function set url(param1:String) : void
  672.       {
  673.          uri = param1;
  674.       }
  675.       
  676.       protected function disconnectSuccess(param1:Boolean = false) : void
  677.       {
  678.          setConnected(false);
  679.          if(Log.isInfo())
  680.          {
  681.             _log.info("\'{0}\' channel disconnected.",id);
  682.          }
  683.          if(!param1 && shouldAttemptFailover())
  684.          {
  685.             _connecting = true;
  686.             failover();
  687.          }
  688.          else
  689.          {
  690.             connectCleanup();
  691.          }
  692.          dispatchEvent(ChannelEvent.createEvent(ChannelEvent.DISCONNECT,this,reconnecting,param1));
  693.       }
  694.       
  695.       protected function internalSend(param1:MessageResponder) : void
  696.       {
  697.       }
  698.       
  699.       mx_internal function sendInternalMessage(param1:MessageResponder) : void
  700.       {
  701.          internalSend(param1);
  702.       }
  703.       
  704.       final public function connect(param1:ChannelSet) : void
  705.       {
  706.          var _loc5_:FlexClient = null;
  707.          var _loc2_:Boolean = false;
  708.          var _loc3_:int = int(_channelSets.length);
  709.          var _loc4_:int = 0;
  710.          while(_loc4_ < _channelSets.length)
  711.          {
  712.             if(_channelSets[_loc4_] == param1)
  713.             {
  714.                _loc2_ = true;
  715.                break;
  716.             }
  717.             _loc4_++;
  718.          }
  719.          _shouldBeConnected = true;
  720.          if(!_loc2_)
  721.          {
  722.             _channelSets.push(param1);
  723.             addEventListener(ChannelEvent.CONNECT,param1.channelConnectHandler);
  724.             addEventListener(ChannelEvent.DISCONNECT,param1.channelDisconnectHandler);
  725.             addEventListener(ChannelFaultEvent.FAULT,param1.channelFaultHandler);
  726.          }
  727.          if(connected)
  728.          {
  729.             param1.channelConnectHandler(ChannelEvent.createEvent(ChannelEvent.CONNECT,this,false,false,connected));
  730.          }
  731.          else if(!_connecting)
  732.          {
  733.             _connecting = true;
  734.             if(connectTimeout > 0)
  735.             {
  736.                _connectTimer = new Timer(connectTimeout * 1000,1);
  737.                _connectTimer.addEventListener(TimerEvent.TIMER,connectTimeoutHandler);
  738.                _connectTimer.start();
  739.             }
  740.             if(FlexClient.getInstance().id == null)
  741.             {
  742.                _loc5_ = FlexClient.getInstance();
  743.                if(!_loc5_.mx_internal::waitForFlexClientId)
  744.                {
  745.                   _loc5_.mx_internal::waitForFlexClientId = true;
  746.                   _ownsWaitGuard = true;
  747.                   internalConnect();
  748.                }
  749.                else
  750.                {
  751.                   _loc5_.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,flexClientWaitHandler);
  752.                }
  753.             }
  754.             else
  755.             {
  756.                internalConnect();
  757.             }
  758.          }
  759.       }
  760.       
  761.       private function resetToPrimaryURI() : void
  762.       {
  763.          _connecting = false;
  764.          setReconnecting(false);
  765.          uri = _primaryURI;
  766.          _failoverIndex = -1;
  767.       }
  768.       
  769.       mx_internal function setAuthenticated(param1:Boolean) : void
  770.       {
  771.          var _loc2_:PropertyChangeEvent = null;
  772.          var _loc3_:ChannelSet = null;
  773.          var _loc4_:int = 0;
  774.          if(param1 != _authenticated)
  775.          {
  776.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"authenticated",_authenticated,param1);
  777.             _authenticated = param1;
  778.             _loc4_ = 0;
  779.             while(_loc4_ < _channelSets.length)
  780.             {
  781.                _loc3_ = ChannelSet(_channelSets[_loc4_]);
  782.                _loc3_.mx_internal::setAuthenticated(authenticated,credentials);
  783.                _loc4_++;
  784.             }
  785.             dispatchEvent(_loc2_);
  786.          }
  787.       }
  788.       
  789.       protected function handleServerMessagingVersion(param1:Number) : void
  790.       {
  791.          useSmallMessages = param1 >= messagingVersion;
  792.       }
  793.       
  794.       mx_internal function get loginAfterDisconnect() : Boolean
  795.       {
  796.          return _loginAfterDisconnect;
  797.       }
  798.       
  799.       private function failover() : void
  800.       {
  801.          var acs:Class = null;
  802.          var duration:int = 0;
  803.          var channelSet:ChannelSet = null;
  804.          var d:int = 0;
  805.          var remaining:Number = NaN;
  806.          var delay:int = 0;
  807.          if(_previouslyConnected)
  808.          {
  809.             _previouslyConnected = false;
  810.             acs = null;
  811.             try
  812.             {
  813.                acs = getDefinitionByName("mx.messaging.AdvancedChannelSet") as Class;
  814.             }
  815.             catch(ignore:Error)
  816.             {
  817.             }
  818.             duration = -1;
  819.             if(acs != null)
  820.             {
  821.                for each(channelSet in channelSets)
  822.                {
  823.                   if(channelSet is acs)
  824.                   {
  825.                      d = int((channelSet as acs)["reliableReconnectDuration"]);
  826.                      if(d > duration)
  827.                      {
  828.                         duration = d;
  829.                      }
  830.                   }
  831.                }
  832.             }
  833.             if(duration != -1)
  834.             {
  835.                setReconnecting(true);
  836.                mx_internal::reliableReconnectDuration = duration;
  837.                _reliableReconnectBeginTimestamp = new Date().valueOf();
  838.                new AsyncDispatcher(reconnect,null,1);
  839.                return;
  840.             }
  841.          }
  842.          if(mx_internal::reliableReconnectDuration != -1)
  843.          {
  844.             _reliableReconnectLastTimestamp = new Date().valueOf();
  845.             remaining = mx_internal::reliableReconnectDuration - (_reliableReconnectLastTimestamp - _reliableReconnectBeginTimestamp);
  846.             if(remaining > 0)
  847.             {
  848.                delay = 1000;
  849.                delay << ++_reliableReconnectAttempts;
  850.                if(delay < remaining)
  851.                {
  852.                   new AsyncDispatcher(reconnect,null,delay);
  853.                   return;
  854.                }
  855.             }
  856.             reliableReconnectCleanup();
  857.          }
  858.          ++_failoverIndex;
  859.          if(_failoverIndex + 1 <= failoverURIs.length)
  860.          {
  861.             setReconnecting(true);
  862.             uri = failoverURIs[_failoverIndex];
  863.             if(Log.isInfo())
  864.             {
  865.                _log.info("\'{0}\' channel attempting to connect to {1}.",id,endpoint);
  866.             }
  867.             new AsyncDispatcher(reconnect,null,1);
  868.          }
  869.          else
  870.          {
  871.             if(Log.isInfo())
  872.             {
  873.                _log.info("\'{0}\' channel has exhausted failover options and has reset to its primary endpoint.",id);
  874.             }
  875.             resetToPrimaryURI();
  876.          }
  877.       }
  878.       
  879.       public function get channelSets() : Array
  880.       {
  881.          return _channelSets;
  882.       }
  883.       
  884.       protected function disconnectFailed(param1:ChannelFaultEvent) : void
  885.       {
  886.          _connecting = false;
  887.          setConnected(false);
  888.          if(Log.isError())
  889.          {
  890.             _log.error("\'{0}\' channel disconnect failed.",id);
  891.          }
  892.          if(reconnecting)
  893.          {
  894.             resetToPrimaryURI();
  895.             param1.reconnecting = false;
  896.          }
  897.          dispatchEvent(param1);
  898.       }
  899.    }
  900. }
  901.  
  902. import mx.core.mx_internal;
  903. import mx.logging.ILogger;
  904. import mx.logging.Log;
  905. import mx.messaging.events.ChannelFaultEvent;
  906. import mx.messaging.messages.CommandMessage;
  907. import mx.messaging.messages.ErrorMessage;
  908. import mx.messaging.messages.IMessage;
  909.  
  910. use namespace mx_internal;
  911.  
  912. class AuthenticationMessageResponder extends MessageResponder
  913. {
  914.    private var _log:ILogger;
  915.    
  916.    public function AuthenticationMessageResponder(param1:MessageAgent, param2:IMessage, param3:Channel, param4:ILogger)
  917.    {
  918.       super(param1,param2,param3);
  919.       _log = param4;
  920.    }
  921.    
  922.    override protected function statusHandler(param1:IMessage) : void
  923.    {
  924.       var _loc3_:ErrorMessage = null;
  925.       var _loc4_:ChannelFaultEvent = null;
  926.       var _loc2_:CommandMessage = CommandMessage(message);
  927.       if(Log.isDebug())
  928.       {
  929.          _log.debug("{1} failure: {0}",param1.toString(),_loc2_.operation == CommandMessage.LOGIN_OPERATION ? "Login" : "Logout");
  930.       }
  931.       channel.mx_internal::authenticating = false;
  932.       channel.mx_internal::setAuthenticated(false);
  933.       if(agent != null && Boolean(agent.hasPendingRequestForMessage(message)))
  934.       {
  935.          agent.fault(ErrorMessage(param1),message);
  936.       }
  937.       else
  938.       {
  939.          _loc3_ = ErrorMessage(param1);
  940.          _loc4_ = ChannelFaultEvent.createEvent(channel,false,"Channel.Authentication.Error","warn",_loc3_.faultString);
  941.          _loc4_.rootCause = _loc3_;
  942.          channel.dispatchEvent(_loc4_);
  943.       }
  944.    }
  945.    
  946.    override protected function resultHandler(param1:IMessage) : void
  947.    {
  948.       var _loc2_:CommandMessage = message as CommandMessage;
  949.       channel.mx_internal::authenticating = false;
  950.       if(_loc2_.operation == CommandMessage.LOGIN_OPERATION)
  951.       {
  952.          if(Log.isDebug())
  953.          {
  954.             _log.debug("Login successful");
  955.          }
  956.          channel.mx_internal::setAuthenticated(true);
  957.       }
  958.       else
  959.       {
  960.          if(Log.isDebug())
  961.          {
  962.             _log.debug("Logout successful");
  963.          }
  964.          channel.mx_internal::setAuthenticated(false);
  965.       }
  966.    }
  967. }
  968.