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

  1. package mx.messaging
  2. {
  3.    import flash.utils.Dictionary;
  4.    import mx.core.mx_internal;
  5.    import mx.logging.Log;
  6.    import mx.messaging.events.MessageEvent;
  7.    
  8.    use namespace mx_internal;
  9.    
  10.    public class ConsumerMessageDispatcher
  11.    {
  12.       private static var _instance:ConsumerMessageDispatcher;
  13.       
  14.       private const _consumerDuplicateMessageBarrier:Object = {};
  15.       
  16.       private const _channelSetRefCounts:Dictionary = new Dictionary();
  17.       
  18.       private const _consumers:Object = {};
  19.       
  20.       public function ConsumerMessageDispatcher()
  21.       {
  22.          super();
  23.       }
  24.       
  25.       public static function getInstance() : ConsumerMessageDispatcher
  26.       {
  27.          if(!_instance)
  28.          {
  29.             _instance = new ConsumerMessageDispatcher();
  30.          }
  31.          return _instance;
  32.       }
  33.       
  34.       public function registerSubscription(param1:AbstractConsumer) : void
  35.       {
  36.          _consumers[param1.clientId] = param1;
  37.          if(_channelSetRefCounts[param1.channelSet] == null)
  38.          {
  39.             param1.channelSet.addEventListener(MessageEvent.MESSAGE,messageHandler);
  40.             _channelSetRefCounts[param1.channelSet] = 1;
  41.          }
  42.          else
  43.          {
  44.             ++_channelSetRefCounts[param1.channelSet];
  45.          }
  46.       }
  47.       
  48.       private function messageHandler(param1:MessageEvent) : void
  49.       {
  50.          var _loc3_:int = 0;
  51.          var _loc4_:ChannelSet = null;
  52.          var _loc5_:Array = null;
  53.          var _loc2_:AbstractConsumer = _consumers[param1.message.clientId];
  54.          if(_loc2_ == null)
  55.          {
  56.             if(Log.isDebug())
  57.             {
  58.                Log.getLogger("mx.messaging.Consumer").debug("\'{0}\' received pushed message for consumer but no longer subscribed: {1}",param1.message.clientId,param1.message);
  59.             }
  60.             return;
  61.          }
  62.          if(param1.target.currentChannel.channelSets.length > 1)
  63.          {
  64.             _loc3_ = 0;
  65.             for each(_loc4_ in param1.target.currentChannel.channelSets)
  66.             {
  67.                if(_channelSetRefCounts[_loc4_] != null)
  68.                {
  69.                   _loc3_++;
  70.                }
  71.             }
  72.             if(_loc3_ > 1)
  73.             {
  74.                if(_consumerDuplicateMessageBarrier[_loc2_.id] == null)
  75.                {
  76.                   _consumerDuplicateMessageBarrier[_loc2_.id] = [param1.messageId,_loc3_];
  77.                   _loc2_.mx_internal::messageHandler(param1);
  78.                }
  79.                _loc5_ = _consumerDuplicateMessageBarrier[_loc2_.id];
  80.                if(_loc5_[0] == param1.messageId)
  81.                {
  82.                   if(--_loc5_[1] == 0)
  83.                   {
  84.                      delete _consumerDuplicateMessageBarrier[_loc2_.id];
  85.                   }
  86.                }
  87.                return;
  88.             }
  89.          }
  90.          _loc2_.mx_internal::messageHandler(param1);
  91.       }
  92.       
  93.       public function unregisterSubscription(param1:AbstractConsumer) : void
  94.       {
  95.          delete _consumers[param1.clientId];
  96.          var _loc2_:int = int(_channelSetRefCounts[param1.channelSet]);
  97.          if(--_loc2_ == 0)
  98.          {
  99.             param1.channelSet.removeEventListener(MessageEvent.MESSAGE,messageHandler);
  100.             delete _channelSetRefCounts[param1.channelSet];
  101.             if(_consumerDuplicateMessageBarrier[param1.id] != null)
  102.             {
  103.                delete _consumerDuplicateMessageBarrier[param1.id];
  104.             }
  105.          }
  106.          else
  107.          {
  108.             _channelSetRefCounts[param1.channelSet] = _loc2_;
  109.          }
  110.       }
  111.       
  112.       public function isChannelUsedForSubscriptions(param1:Channel) : Boolean
  113.       {
  114.          var _loc2_:Array = param1.channelSets;
  115.          var _loc3_:ChannelSet = null;
  116.          var _loc4_:int = int(_loc2_.length);
  117.          var _loc5_:int = 0;
  118.          while(_loc5_ < _loc4_)
  119.          {
  120.             _loc3_ = _loc2_[_loc5_];
  121.             if(_channelSetRefCounts[_loc3_] != null && _loc3_.currentChannel == param1)
  122.             {
  123.                return true;
  124.             }
  125.             _loc5_++;
  126.          }
  127.          return false;
  128.       }
  129.    }
  130. }
  131.  
  132.