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

  1. package mx.messaging
  2. {
  3.    import flash.events.EventDispatcher;
  4.    import mx.core.IMXMLObject;
  5.    import mx.core.mx_internal;
  6.    import mx.events.PropertyChangeEvent;
  7.    import mx.logging.ILogger;
  8.    import mx.logging.Log;
  9.    import mx.messaging.config.ConfigMap;
  10.    import mx.messaging.config.ServerConfig;
  11.    import mx.messaging.errors.InvalidDestinationError;
  12.    import mx.messaging.events.ChannelEvent;
  13.    import mx.messaging.events.ChannelFaultEvent;
  14.    import mx.messaging.events.MessageAckEvent;
  15.    import mx.messaging.events.MessageEvent;
  16.    import mx.messaging.events.MessageFaultEvent;
  17.    import mx.messaging.messages.AbstractMessage;
  18.    import mx.messaging.messages.AcknowledgeMessage;
  19.    import mx.messaging.messages.CommandMessage;
  20.    import mx.messaging.messages.ErrorMessage;
  21.    import mx.messaging.messages.IMessage;
  22.    import mx.messaging.messages.MessagePerformanceUtils;
  23.    import mx.netmon.NetworkMonitor;
  24.    import mx.resources.IResourceManager;
  25.    import mx.resources.ResourceManager;
  26.    import mx.utils.Base64Encoder;
  27.    import mx.utils.UIDUtil;
  28.    
  29.    use namespace mx_internal;
  30.    
  31.    public class MessageAgent extends EventDispatcher implements IMXMLObject
  32.    {
  33.       mx_internal static const AUTO_CONFIGURED_CHANNELSET:int = 0;
  34.       
  35.       mx_internal static const MANUALLY_ASSIGNED_CHANNELSET:int = 1;
  36.       
  37.       private var _needsConfig:Boolean;
  38.       
  39.       protected var _disconnectBarrier:Boolean;
  40.       
  41.       protected var _log:ILogger;
  42.       
  43.       private var _connected:Boolean = false;
  44.       
  45.       private var _clientId:String;
  46.       
  47.       private var _sendRemoteCredentials:Boolean;
  48.       
  49.       private var _authenticated:Boolean;
  50.       
  51.       protected var _ignoreFault:Boolean = false;
  52.       
  53.       private var _id:String = UIDUtil.createUID();
  54.       
  55.       protected var _credentials:String;
  56.       
  57.       private var resourceManager:IResourceManager = ResourceManager.getInstance();
  58.       
  59.       private var _channelSetMode:int = 0;
  60.       
  61.       mx_internal var configRequested:Boolean = false;
  62.       
  63.       private var _pendingConnectEvent:ChannelEvent;
  64.       
  65.       protected var _credentialsCharset:String;
  66.       
  67.       private var _remoteCredentials:String = "";
  68.       
  69.       private var _destination:String = "";
  70.       
  71.       protected var _agentType:String = "mx.messaging.MessageAgent";
  72.       
  73.       private var _requestTimeout:int = -1;
  74.       
  75.       private var _remoteCredentialsCharset:String;
  76.       
  77.       private var _clientIdWaitQueue:Array;
  78.       
  79.       private var _channelSet:ChannelSet;
  80.       
  81.       public function MessageAgent()
  82.       {
  83.          super();
  84.       }
  85.       
  86.       [Bindable(event="propertyChange")]
  87.       public function get connected() : Boolean
  88.       {
  89.          return _connected;
  90.       }
  91.       
  92.       [Bindable(event="propertyChange")]
  93.       public function get destination() : String
  94.       {
  95.          return _destination;
  96.       }
  97.       
  98.       protected function initChannelSet(param1:IMessage) : void
  99.       {
  100.          if(_channelSet == null)
  101.          {
  102.             _channelSetMode = mx_internal::AUTO_CONFIGURED_CHANNELSET;
  103.             mx_internal::internalSetChannelSet(ServerConfig.getChannelSet(destination));
  104.          }
  105.          if(_channelSet.connected && mx_internal::needsConfig && !mx_internal::configRequested)
  106.          {
  107.             param1.headers[CommandMessage.NEEDS_CONFIG_HEADER] = true;
  108.             mx_internal::configRequested = true;
  109.          }
  110.          _channelSet.connect(this);
  111.          if(_credentials != null)
  112.          {
  113.             channelSet.setCredentials(_credentials,this,_credentialsCharset);
  114.          }
  115.       }
  116.       
  117.       mx_internal function set needsConfig(param1:Boolean) : void
  118.       {
  119.          var cs:ChannelSet = null;
  120.          var value:Boolean = param1;
  121.          if(_needsConfig == value)
  122.          {
  123.             return;
  124.          }
  125.          _needsConfig = value;
  126.          if(_needsConfig)
  127.          {
  128.             cs = channelSet;
  129.             try
  130.             {
  131.                disconnect();
  132.             }
  133.             finally
  134.             {
  135.                mx_internal::internalSetChannelSet(cs);
  136.             }
  137.          }
  138.       }
  139.       
  140.       public function logout() : void
  141.       {
  142.          _credentials = null;
  143.          if(channelSet)
  144.          {
  145.             channelSet.logout(this);
  146.          }
  147.       }
  148.       
  149.       [Bindable(event="propertyChange")]
  150.       public function get id() : String
  151.       {
  152.          return _id;
  153.       }
  154.       
  155.       public function set destination(param1:String) : void
  156.       {
  157.          var _loc2_:PropertyChangeEvent = null;
  158.          if(param1 == null || param1.length == 0)
  159.          {
  160.             return;
  161.          }
  162.          if(_destination != param1)
  163.          {
  164.             if(_channelSetMode == mx_internal::AUTO_CONFIGURED_CHANNELSET && channelSet != null)
  165.             {
  166.                channelSet.disconnect(this);
  167.                channelSet = null;
  168.             }
  169.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"destination",_destination,param1);
  170.             _destination = param1;
  171.             dispatchEvent(_loc2_);
  172.             if(Log.isInfo())
  173.             {
  174.                _log.info("\'{0}\' {2} set destination to \'{1}\'.",id,_destination,_agentType);
  175.             }
  176.          }
  177.       }
  178.       
  179.       mx_internal function get channelSetMode() : int
  180.       {
  181.          return _channelSetMode;
  182.       }
  183.       
  184.       public function acknowledge(param1:AcknowledgeMessage, param2:IMessage) : void
  185.       {
  186.          var mpiutil:MessagePerformanceUtils = null;
  187.          var ackMsg:AcknowledgeMessage = param1;
  188.          var msg:IMessage = param2;
  189.          if(Log.isInfo())
  190.          {
  191.             _log.info("\'{0}\' {2} acknowledge of \'{1}\'.",id,msg.messageId,_agentType);
  192.          }
  193.          if(Log.isDebug() && channelSet != null && channelSet.currentChannel != null && channelSet.currentChannel.mpiEnabled)
  194.          {
  195.             try
  196.             {
  197.                mpiutil = new MessagePerformanceUtils(ackMsg);
  198.                _log.debug(mpiutil.prettyPrint());
  199.             }
  200.             catch(e:Error)
  201.             {
  202.                _log.debug("Could not get message performance information for: " + msg.toString());
  203.             }
  204.          }
  205.          if(mx_internal::configRequested)
  206.          {
  207.             mx_internal::configRequested = false;
  208.             ServerConfig.mx_internal::updateServerConfigData(ackMsg.body as ConfigMap);
  209.             mx_internal::needsConfig = false;
  210.             if(_pendingConnectEvent)
  211.             {
  212.                channelConnectHandler(_pendingConnectEvent);
  213.             }
  214.             _pendingConnectEvent = null;
  215.          }
  216.          if(clientId == null)
  217.          {
  218.             if(ackMsg.clientId != null)
  219.             {
  220.                mx_internal::setClientId(ackMsg.clientId);
  221.             }
  222.             else
  223.             {
  224.                flushClientIdWaitQueue();
  225.             }
  226.          }
  227.          dispatchEvent(MessageAckEvent.createEvent(ackMsg,msg));
  228.          monitorRpcMessage(ackMsg,msg);
  229.       }
  230.       
  231.       mx_internal function internalSetChannelSet(param1:ChannelSet) : void
  232.       {
  233.          var _loc2_:PropertyChangeEvent = null;
  234.          if(_channelSet != param1)
  235.          {
  236.             if(_channelSet != null)
  237.             {
  238.                _channelSet.disconnect(this);
  239.             }
  240.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"channelSet",_channelSet,param1);
  241.             _channelSet = param1;
  242.             if(_channelSet != null)
  243.             {
  244.                if(_credentials)
  245.                {
  246.                   _channelSet.setCredentials(_credentials,this,_credentialsCharset);
  247.                }
  248.                _channelSet.connect(this);
  249.             }
  250.             dispatchEvent(_loc2_);
  251.          }
  252.       }
  253.       
  254.       public function fault(param1:ErrorMessage, param2:IMessage) : void
  255.       {
  256.          if(Log.isError())
  257.          {
  258.             _log.error("\'{0}\' {2} fault for \'{1}\'.",id,param2.messageId,_agentType);
  259.          }
  260.          _ignoreFault = false;
  261.          mx_internal::configRequested = false;
  262.          if(param1.headers[ErrorMessage.RETRYABLE_HINT_HEADER])
  263.          {
  264.             delete param1.headers[ErrorMessage.RETRYABLE_HINT_HEADER];
  265.          }
  266.          if(clientId == null)
  267.          {
  268.             if(param1.clientId != null)
  269.             {
  270.                mx_internal::setClientId(param1.clientId);
  271.             }
  272.             else
  273.             {
  274.                flushClientIdWaitQueue();
  275.             }
  276.          }
  277.          dispatchEvent(MessageFaultEvent.createEvent(param1));
  278.          monitorRpcMessage(param1,param2);
  279.          if(param1.faultCode == "Client.Authentication" && authenticated && channelSet != null && channelSet.currentChannel != null)
  280.          {
  281.             channelSet.currentChannel.mx_internal::setAuthenticated(false);
  282.             if(channelSet.currentChannel.mx_internal::loginAfterDisconnect)
  283.             {
  284.                reAuthorize(param2);
  285.                _ignoreFault = true;
  286.             }
  287.          }
  288.       }
  289.       
  290.       public function set requestTimeout(param1:int) : void
  291.       {
  292.          var _loc2_:PropertyChangeEvent = null;
  293.          if(_requestTimeout != param1)
  294.          {
  295.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"requestTimeout",_requestTimeout,param1);
  296.             _requestTimeout = param1;
  297.             dispatchEvent(_loc2_);
  298.          }
  299.       }
  300.       
  301.       public function disconnect() : void
  302.       {
  303.          if(!_disconnectBarrier)
  304.          {
  305.             _clientIdWaitQueue = null;
  306.             if(connected)
  307.             {
  308.                _disconnectBarrier = true;
  309.             }
  310.             if(_channelSetMode == mx_internal::AUTO_CONFIGURED_CHANNELSET)
  311.             {
  312.                mx_internal::internalSetChannelSet(null);
  313.             }
  314.             else if(_channelSet != null)
  315.             {
  316.                _channelSet.disconnect(this);
  317.             }
  318.          }
  319.       }
  320.       
  321.       public function set id(param1:String) : void
  322.       {
  323.          var _loc2_:PropertyChangeEvent = null;
  324.          if(_id != param1)
  325.          {
  326.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"id",_id,param1);
  327.             _id = param1;
  328.             dispatchEvent(_loc2_);
  329.          }
  330.       }
  331.       
  332.       public function set channelSet(param1:ChannelSet) : void
  333.       {
  334.          mx_internal::internalSetChannelSet(param1);
  335.          _channelSetMode = mx_internal::MANUALLY_ASSIGNED_CHANNELSET;
  336.       }
  337.       
  338.       [Bindable(event="propertyChange")]
  339.       public function get clientId() : String
  340.       {
  341.          return _clientId;
  342.       }
  343.       
  344.       protected function setConnected(param1:Boolean) : void
  345.       {
  346.          var _loc2_:PropertyChangeEvent = null;
  347.          if(_connected != param1)
  348.          {
  349.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"connected",_connected,param1);
  350.             _connected = param1;
  351.             dispatchEvent(_loc2_);
  352.             mx_internal::setAuthenticated(param1 && Boolean(channelSet) && channelSet.authenticated,_credentials);
  353.          }
  354.       }
  355.       
  356.       mx_internal function setClientId(param1:String) : void
  357.       {
  358.          var _loc2_:PropertyChangeEvent = null;
  359.          if(_clientId != param1)
  360.          {
  361.             _loc2_ = PropertyChangeEvent.createUpdateEvent(this,"clientId",_clientId,param1);
  362.             _clientId = param1;
  363.             flushClientIdWaitQueue();
  364.             dispatchEvent(_loc2_);
  365.          }
  366.       }
  367.       
  368.       public function setCredentials(param1:String, param2:String, param3:String = null) : void
  369.       {
  370.          var _loc4_:String = null;
  371.          var _loc5_:Base64Encoder = null;
  372.          if(param1 == null && param2 == null)
  373.          {
  374.             _credentials = null;
  375.             _credentialsCharset = null;
  376.          }
  377.          else
  378.          {
  379.             _loc4_ = param1 + ":" + param2;
  380.             _loc5_ = new Base64Encoder();
  381.             if(param3 == Base64Encoder.CHARSET_UTF_8)
  382.             {
  383.                _loc5_.encodeUTFBytes(_loc4_);
  384.             }
  385.             else
  386.             {
  387.                _loc5_.encode(_loc4_);
  388.             }
  389.             _credentials = _loc5_.drain();
  390.             _credentialsCharset = param3;
  391.          }
  392.          if(channelSet != null)
  393.          {
  394.             channelSet.setCredentials(_credentials,this,_credentialsCharset);
  395.          }
  396.       }
  397.       
  398.       public function channelDisconnectHandler(param1:ChannelEvent) : void
  399.       {
  400.          if(Log.isWarn())
  401.          {
  402.             _log.warn("\'{0}\' {1} channel disconnected.",id,_agentType);
  403.          }
  404.          setConnected(false);
  405.          if(_remoteCredentials != null)
  406.          {
  407.             _sendRemoteCredentials = true;
  408.          }
  409.          dispatchEvent(param1);
  410.       }
  411.       
  412.       public function setRemoteCredentials(param1:String, param2:String, param3:String = null) : void
  413.       {
  414.          var _loc4_:String = null;
  415.          var _loc5_:Base64Encoder = null;
  416.          if(param1 == null && param2 == null)
  417.          {
  418.             _remoteCredentials = "";
  419.             _remoteCredentialsCharset = null;
  420.          }
  421.          else
  422.          {
  423.             _loc4_ = param1 + ":" + param2;
  424.             _loc5_ = new Base64Encoder();
  425.             if(param3 == Base64Encoder.CHARSET_UTF_8)
  426.             {
  427.                _loc5_.encodeUTFBytes(_loc4_);
  428.             }
  429.             else
  430.             {
  431.                _loc5_.encode(_loc4_);
  432.             }
  433.             _remoteCredentials = _loc5_.drain();
  434.             _remoteCredentialsCharset = param3;
  435.          }
  436.          _sendRemoteCredentials = true;
  437.       }
  438.       
  439.       mx_internal function get needsConfig() : Boolean
  440.       {
  441.          return _needsConfig;
  442.       }
  443.       
  444.       public function hasPendingRequestForMessage(param1:IMessage) : Boolean
  445.       {
  446.          return false;
  447.       }
  448.       
  449.       [Bindable(event="propertyChange")]
  450.       public function get authenticated() : Boolean
  451.       {
  452.          return _authenticated;
  453.       }
  454.       
  455.       [Bindable(event="propertyChange")]
  456.       public function get requestTimeout() : int
  457.       {
  458.          return _requestTimeout;
  459.       }
  460.       
  461.       public function initialized(param1:Object, param2:String) : void
  462.       {
  463.          this.id = param2;
  464.       }
  465.       
  466.       mx_internal function getNetmonId() : String
  467.       {
  468.          return null;
  469.       }
  470.       
  471.       final protected function flushClientIdWaitQueue() : void
  472.       {
  473.          var _loc1_:Array = null;
  474.          if(_clientIdWaitQueue != null)
  475.          {
  476.             if(clientId != null)
  477.             {
  478.                while(_clientIdWaitQueue.length > 0)
  479.                {
  480.                   internalSend(_clientIdWaitQueue.shift() as IMessage);
  481.                }
  482.             }
  483.             if(clientId == null)
  484.             {
  485.                if(_clientIdWaitQueue.length > 0)
  486.                {
  487.                   _loc1_ = _clientIdWaitQueue;
  488.                   _clientIdWaitQueue = null;
  489.                   internalSend(_loc1_.shift() as IMessage);
  490.                   _clientIdWaitQueue = _loc1_;
  491.                }
  492.                else
  493.                {
  494.                   _clientIdWaitQueue = null;
  495.                }
  496.             }
  497.          }
  498.       }
  499.       
  500.       private function monitorRpcMessage(param1:IMessage, param2:IMessage) : void
  501.       {
  502.          if(NetworkMonitor.isMonitoring())
  503.          {
  504.             if(param1 is ErrorMessage)
  505.             {
  506.                NetworkMonitor.monitorFault(param2,MessageFaultEvent.createEvent(ErrorMessage(param1)));
  507.             }
  508.             else if(param1 is AcknowledgeMessage)
  509.             {
  510.                NetworkMonitor.monitorResult(param1,MessageEvent.createEvent(MessageEvent.RESULT,param2));
  511.             }
  512.             else
  513.             {
  514.                NetworkMonitor.monitorInvocation(mx_internal::getNetmonId(),param1,this);
  515.             }
  516.          }
  517.       }
  518.       
  519.       final protected function assertCredentials(param1:String) : void
  520.       {
  521.          var _loc2_:ErrorMessage = null;
  522.          if(_credentials != null && _credentials != param1)
  523.          {
  524.             _loc2_ = new ErrorMessage();
  525.             _loc2_.faultCode = "Client.Authentication.Error";
  526.             _loc2_.faultString = "Credentials specified do not match those used on underlying connection.";
  527.             _loc2_.faultDetail = "Channel was authenticated with a different set of credentials than those used for this agent.";
  528.             dispatchEvent(MessageFaultEvent.createEvent(_loc2_));
  529.          }
  530.       }
  531.       
  532.       [Bindable(event="propertyChange")]
  533.       public function get channelSet() : ChannelSet
  534.       {
  535.          return _channelSet;
  536.       }
  537.       
  538.       public function channelConnectHandler(param1:ChannelEvent) : void
  539.       {
  540.          _disconnectBarrier = false;
  541.          if(mx_internal::needsConfig)
  542.          {
  543.             if(Log.isInfo())
  544.             {
  545.                _log.info("\'{0}\' {1} waiting for configuration information.",id,_agentType);
  546.             }
  547.             _pendingConnectEvent = param1;
  548.          }
  549.          else
  550.          {
  551.             if(Log.isInfo())
  552.             {
  553.                _log.info("\'{0}\' {1} connected.",id,_agentType);
  554.             }
  555.             setConnected(true);
  556.             dispatchEvent(param1);
  557.          }
  558.       }
  559.       
  560.       mx_internal function internalSetCredentials(param1:String) : void
  561.       {
  562.          _credentials = param1;
  563.       }
  564.       
  565.       public function channelFaultHandler(param1:ChannelFaultEvent) : void
  566.       {
  567.          if(Log.isWarn())
  568.          {
  569.             _log.warn("\'{0}\' {1} channel faulted with {2} {3}",id,_agentType,param1.faultCode,param1.faultDetail);
  570.          }
  571.          if(!param1.channel.connected)
  572.          {
  573.             setConnected(false);
  574.             if(_remoteCredentials != null)
  575.             {
  576.                _sendRemoteCredentials = true;
  577.             }
  578.          }
  579.          dispatchEvent(param1);
  580.       }
  581.       
  582.       protected function internalSend(param1:IMessage, param2:Boolean = true) : void
  583.       {
  584.          var _loc3_:String = null;
  585.          if(param1.clientId == null && param2 && clientId == null)
  586.          {
  587.             if(_clientIdWaitQueue != null)
  588.             {
  589.                _clientIdWaitQueue.push(param1);
  590.                return;
  591.             }
  592.             _clientIdWaitQueue = [];
  593.          }
  594.          if(param1.clientId == null)
  595.          {
  596.             param1.clientId = clientId;
  597.          }
  598.          if(requestTimeout > 0)
  599.          {
  600.             param1.headers[AbstractMessage.REQUEST_TIMEOUT_HEADER] = requestTimeout;
  601.          }
  602.          if(_sendRemoteCredentials)
  603.          {
  604.             if(!(param1 is CommandMessage && CommandMessage(param1).operation == CommandMessage.TRIGGER_CONNECT_OPERATION))
  605.             {
  606.                param1.headers[AbstractMessage.REMOTE_CREDENTIALS_HEADER] = _remoteCredentials;
  607.                param1.headers[AbstractMessage.REMOTE_CREDENTIALS_CHARSET_HEADER] = _remoteCredentialsCharset;
  608.                _sendRemoteCredentials = false;
  609.             }
  610.          }
  611.          if(channelSet != null)
  612.          {
  613.             if(!connected && _channelSetMode == mx_internal::MANUALLY_ASSIGNED_CHANNELSET)
  614.             {
  615.                _channelSet.connect(this);
  616.             }
  617.             if(channelSet.connected && mx_internal::needsConfig && !mx_internal::configRequested)
  618.             {
  619.                param1.headers[CommandMessage.NEEDS_CONFIG_HEADER] = true;
  620.                mx_internal::configRequested = true;
  621.             }
  622.             channelSet.send(this,param1);
  623.             monitorRpcMessage(param1,param1);
  624.          }
  625.          else
  626.          {
  627.             if(!(destination != null && destination.length > 0))
  628.             {
  629.                _loc3_ = resourceManager.getString("messaging","destinationNotSet");
  630.                throw new InvalidDestinationError(_loc3_);
  631.             }
  632.             initChannelSet(param1);
  633.             if(channelSet != null)
  634.             {
  635.                channelSet.send(this,param1);
  636.                monitorRpcMessage(param1,param1);
  637.             }
  638.          }
  639.       }
  640.       
  641.       mx_internal function setAuthenticated(param1:Boolean, param2:String) : void
  642.       {
  643.          var _loc3_:PropertyChangeEvent = null;
  644.          if(_authenticated != param1)
  645.          {
  646.             _loc3_ = PropertyChangeEvent.createUpdateEvent(this,"authenticated",_authenticated,param1);
  647.             _authenticated = param1;
  648.             dispatchEvent(_loc3_);
  649.             if(param1)
  650.             {
  651.                assertCredentials(param2);
  652.             }
  653.          }
  654.       }
  655.       
  656.       protected function reAuthorize(param1:IMessage) : void
  657.       {
  658.          disconnect();
  659.          internalSend(param1);
  660.       }
  661.    }
  662. }
  663.  
  664.