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

  1. package mx.messaging
  2. {
  3.    import flash.events.TimerEvent;
  4.    import flash.utils.Timer;
  5.    import mx.core.mx_internal;
  6.    import mx.events.PropertyChangeEvent;
  7.    import mx.logging.Log;
  8.    import mx.messaging.events.ChannelEvent;
  9.    import mx.messaging.events.ChannelFaultEvent;
  10.    import mx.messaging.messages.AcknowledgeMessage;
  11.    import mx.messaging.messages.CommandMessage;
  12.    import mx.messaging.messages.ErrorMessage;
  13.    import mx.messaging.messages.IMessage;
  14.    import mx.resources.IResourceManager;
  15.    import mx.resources.ResourceManager;
  16.    
  17.    use namespace mx_internal;
  18.    
  19.    public class AbstractProducer extends MessageAgent
  20.    {
  21.       private var _priority:int = -1;
  22.       
  23.       private var _reconnectInterval:int;
  24.       
  25.       private var _autoConnect:Boolean = true;
  26.       
  27.       private var _reconnectTimer:Timer;
  28.       
  29.       protected var _shouldBeConnected:Boolean;
  30.       
  31.       private var _connectMsg:CommandMessage;
  32.       
  33.       private var _defaultHeaders:Object;
  34.       
  35.       private var _currentAttempt:int;
  36.       
  37.       private var _reconnectAttempts:int;
  38.       
  39.       private var resourceManager:IResourceManager = ResourceManager.getInstance();
  40.       
  41.       public function AbstractProducer()
  42.       {
  43.          super();
  44.       }
  45.       
  46.       public function set reconnectAttempts(param1:int) : void
  47.       {
  48.          var _loc2_:PropertyChangeEvent = null;
  49.          if(_reconnectAttempts != param1)
  50.          {
  51.             if(param1 == 0)
  52.             {
  53.                stopReconnectTimer();
  54.             }
  55.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"reconnectAttempts",_reconnectAttempts,param1);
  56.             _reconnectAttempts = param1;
  57.             dispatchEvent(_loc2_);
  58.          }
  59.       }
  60.       
  61.       [Bindable(event="propertyChange")]
  62.       public function get defaultHeaders() : Object
  63.       {
  64.          return _defaultHeaders;
  65.       }
  66.       
  67.       public function set reconnectInterval(param1:int) : void
  68.       {
  69.          var _loc2_:PropertyChangeEvent = null;
  70.          var _loc3_:String = null;
  71.          if(_reconnectInterval != param1)
  72.          {
  73.             if(param1 < 0)
  74.             {
  75.                _loc3_ = resourceManager.getString("messaging","reconnectIntervalNegative");
  76.                throw new ArgumentError(_loc3_);
  77.             }
  78.             if(param1 == 0)
  79.             {
  80.                stopReconnectTimer();
  81.             }
  82.             else if(_reconnectTimer != null)
  83.             {
  84.                _reconnectTimer.delay = param1;
  85.             }
  86.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"reconnectInterval",_reconnectInterval,param1);
  87.             _reconnectInterval = param1;
  88.             dispatchEvent(_loc2_);
  89.          }
  90.       }
  91.       
  92.       public function set defaultHeaders(param1:Object) : void
  93.       {
  94.          var _loc2_:PropertyChangeEvent = null;
  95.          if(_defaultHeaders != param1)
  96.          {
  97.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"defaultHeaders",_defaultHeaders,param1);
  98.             _defaultHeaders = param1;
  99.             dispatchEvent(_loc2_);
  100.          }
  101.       }
  102.       
  103.       public function set priority(param1:int) : void
  104.       {
  105.          var _loc2_:PropertyChangeEvent = null;
  106.          if(_priority != param1)
  107.          {
  108.             param1 = param1 < 0 ? 0 : (param1 > 9 ? 9 : param1);
  109.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"priority",_priority,param1);
  110.             _priority = param1;
  111.             dispatchEvent(_loc2_);
  112.          }
  113.       }
  114.       
  115.       protected function stopReconnectTimer() : void
  116.       {
  117.          if(_reconnectTimer != null)
  118.          {
  119.             if(Log.isDebug())
  120.             {
  121.                _log.debug("\'{0}\' {1} stopping reconnect timer.",id,_agentType);
  122.             }
  123.             _reconnectTimer.removeEventListener(TimerEvent.TIMER,reconnect);
  124.             _reconnectTimer.reset();
  125.             _reconnectTimer = null;
  126.          }
  127.       }
  128.       
  129.       override public function channelDisconnectHandler(param1:ChannelEvent) : void
  130.       {
  131.          super.channelDisconnectHandler(param1);
  132.          if(_shouldBeConnected && !param1.rejected)
  133.          {
  134.             startReconnectTimer();
  135.          }
  136.       }
  137.       
  138.       public function send(param1:IMessage) : void
  139.       {
  140.          var _loc2_:String = null;
  141.          var _loc3_:ErrorMessage = null;
  142.          if(!connected && autoConnect)
  143.          {
  144.             _shouldBeConnected = true;
  145.          }
  146.          if(defaultHeaders != null)
  147.          {
  148.             for(_loc2_ in defaultHeaders)
  149.             {
  150.                if(!param1.headers.hasOwnProperty(_loc2_))
  151.                {
  152.                   param1.headers[_loc2_] = defaultHeaders[_loc2_];
  153.                }
  154.             }
  155.          }
  156.          if(!connected && !autoConnect)
  157.          {
  158.             _shouldBeConnected = false;
  159.             _loc3_ = new ErrorMessage();
  160.             _loc3_.faultCode = "Client.Error.MessageSend";
  161.             _loc3_.faultString = resourceManager.getString("messaging","producerSendError");
  162.             _loc3_.faultDetail = resourceManager.getString("messaging","producerSendErrorDetails");
  163.             _loc3_.correlationId = param1.messageId;
  164.             mx_internal::internalFault(_loc3_,param1,false,true);
  165.          }
  166.          else
  167.          {
  168.             if(Log.isInfo())
  169.             {
  170.                _log.info("\'{0}\' {1} sending message \'{2}\'",id,_agentType,param1.messageId);
  171.             }
  172.             internalSend(param1);
  173.          }
  174.       }
  175.       
  176.       private function buildConnectErrorMessage() : ErrorMessage
  177.       {
  178.          var _loc1_:ErrorMessage = new ErrorMessage();
  179.          _loc1_.faultCode = "Client.Error.Connect";
  180.          _loc1_.faultString = resourceManager.getString("messaging","producerConnectError");
  181.          _loc1_.faultDetail = resourceManager.getString("messaging","failedToConnect");
  182.          _loc1_.correlationId = _connectMsg.messageId;
  183.          return _loc1_;
  184.       }
  185.       
  186.       override public function acknowledge(param1:AcknowledgeMessage, param2:IMessage) : void
  187.       {
  188.          if(_disconnectBarrier)
  189.          {
  190.             return;
  191.          }
  192.          super.acknowledge(param1,param2);
  193.          if(param2 is CommandMessage && CommandMessage(param2).operation == CommandMessage.TRIGGER_CONNECT_OPERATION)
  194.          {
  195.             stopReconnectTimer();
  196.          }
  197.       }
  198.       
  199.       [Bindable(event="propertyChange")]
  200.       public function get reconnectInterval() : int
  201.       {
  202.          return _reconnectInterval;
  203.       }
  204.       
  205.       override public function fault(param1:ErrorMessage, param2:IMessage) : void
  206.       {
  207.          mx_internal::internalFault(param1,param2);
  208.       }
  209.       
  210.       override public function disconnect() : void
  211.       {
  212.          _shouldBeConnected = false;
  213.          stopReconnectTimer();
  214.          super.disconnect();
  215.       }
  216.       
  217.       mx_internal function internalFault(param1:ErrorMessage, param2:IMessage, param3:Boolean = true, param4:Boolean = false) : void
  218.       {
  219.          var _loc5_:ErrorMessage = null;
  220.          if(_disconnectBarrier && !param4)
  221.          {
  222.             return;
  223.          }
  224.          if(param2 is CommandMessage && CommandMessage(param2).operation == CommandMessage.TRIGGER_CONNECT_OPERATION)
  225.          {
  226.             if(_reconnectTimer == null)
  227.             {
  228.                if(_connectMsg != null && param1.correlationId == _connectMsg.messageId)
  229.                {
  230.                   _shouldBeConnected = false;
  231.                   _loc5_ = buildConnectErrorMessage();
  232.                   _loc5_.rootCause = param1.rootCause;
  233.                   super.fault(_loc5_,param2);
  234.                }
  235.                else
  236.                {
  237.                   super.fault(param1,param2);
  238.                }
  239.             }
  240.          }
  241.          else
  242.          {
  243.             super.fault(param1,param2);
  244.          }
  245.       }
  246.       
  247.       public function connect() : void
  248.       {
  249.          if(!connected)
  250.          {
  251.             _shouldBeConnected = true;
  252.             if(_connectMsg == null)
  253.             {
  254.                _connectMsg = buildConnectMessage();
  255.             }
  256.             internalSend(_connectMsg,false);
  257.          }
  258.       }
  259.       
  260.       [Bindable(event="propertyChange")]
  261.       public function get priority() : int
  262.       {
  263.          return _priority;
  264.       }
  265.       
  266.       private function buildConnectMessage() : CommandMessage
  267.       {
  268.          var _loc1_:CommandMessage = new CommandMessage();
  269.          _loc1_.operation = CommandMessage.TRIGGER_CONNECT_OPERATION;
  270.          _loc1_.clientId = clientId;
  271.          _loc1_.destination = destination;
  272.          return _loc1_;
  273.       }
  274.       
  275.       protected function reconnect(param1:TimerEvent) : void
  276.       {
  277.          if(_reconnectAttempts != -1 && _currentAttempt >= _reconnectAttempts)
  278.          {
  279.             stopReconnectTimer();
  280.             _shouldBeConnected = false;
  281.             fault(buildConnectErrorMessage(),_connectMsg);
  282.             return;
  283.          }
  284.          if(Log.isDebug())
  285.          {
  286.             _log.debug("\'{0}\' {1} trying to reconnect.",id,_agentType);
  287.          }
  288.          _reconnectTimer.delay = _reconnectInterval;
  289.          ++_currentAttempt;
  290.          if(_connectMsg == null)
  291.          {
  292.             _connectMsg = buildConnectMessage();
  293.          }
  294.          internalSend(_connectMsg,false);
  295.       }
  296.       
  297.       protected function startReconnectTimer() : void
  298.       {
  299.          if(_shouldBeConnected && _reconnectTimer == null)
  300.          {
  301.             if(_reconnectAttempts != 0 && _reconnectInterval > 0)
  302.             {
  303.                if(Log.isDebug())
  304.                {
  305.                   _log.debug("\'{0}\' {1} starting reconnect timer.",id,_agentType);
  306.                }
  307.                _reconnectTimer = new Timer(1);
  308.                _reconnectTimer.addEventListener(TimerEvent.TIMER,reconnect);
  309.                _reconnectTimer.start();
  310.                _currentAttempt = 0;
  311.             }
  312.          }
  313.       }
  314.       
  315.       override public function channelFaultHandler(param1:ChannelFaultEvent) : void
  316.       {
  317.          super.channelFaultHandler(param1);
  318.          if(_shouldBeConnected && !param1.rejected && !param1.channel.connected)
  319.          {
  320.             startReconnectTimer();
  321.          }
  322.       }
  323.       
  324.       public function set autoConnect(param1:Boolean) : void
  325.       {
  326.          var _loc2_:PropertyChangeEvent = null;
  327.          if(_autoConnect != param1)
  328.          {
  329.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"autoConnect",_autoConnect,param1);
  330.             _autoConnect = param1;
  331.             dispatchEvent(_loc2_);
  332.          }
  333.       }
  334.       
  335.       [Bindable(event="propertyChange")]
  336.       public function get autoConnect() : Boolean
  337.       {
  338.          return _autoConnect;
  339.       }
  340.       
  341.       [Bindable(event="propertyChange")]
  342.       public function get reconnectAttempts() : int
  343.       {
  344.          return _reconnectAttempts;
  345.       }
  346.    }
  347. }
  348.  
  349.