home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Darbas / kidoz_v1.air / kidoz.swf / scripts / mx / messaging / ConsumerMessageDispatcher.as < prev    next >
Encoding:
Text File  |  2009-05-06  |  3.8 KB  |  120 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_:String = null;
  51.          var _loc4_:String = null;
  52.          var _loc2_:AbstractConsumer = _consumers[param1.message.clientId];
  53.          if(_loc2_ == null)
  54.          {
  55.             if(Log.isDebug())
  56.             {
  57.                Log.getLogger("mx.messaging.Consumer").debug("\'{0}\' received pushed message for consumer but no longer subscribed: {1}",param1.message.clientId,param1.message);
  58.             }
  59.             return;
  60.          }
  61.          if(param1.target.currentChannel.channelSets.length > 1)
  62.          {
  63.             _loc3_ = _loc2_.id;
  64.             if(_consumerDuplicateMessageBarrier[_loc3_] == null)
  65.             {
  66.                _consumerDuplicateMessageBarrier[_loc3_] = {};
  67.             }
  68.             _loc4_ = param1.target.currentChannel.id;
  69.             if(_consumerDuplicateMessageBarrier[_loc3_][_loc4_] != param1.messageId)
  70.             {
  71.                _consumerDuplicateMessageBarrier[_loc3_][_loc4_] = param1.messageId;
  72.                _loc2_.mx_internal::messageHandler(param1);
  73.             }
  74.          }
  75.          else
  76.          {
  77.             _loc2_.mx_internal::messageHandler(param1);
  78.          }
  79.       }
  80.       
  81.       public function unregisterSubscription(param1:AbstractConsumer) : void
  82.       {
  83.          delete _consumers[param1.clientId];
  84.          var _loc2_:int = int(_channelSetRefCounts[param1.channelSet]);
  85.          if(--_loc2_ == 0)
  86.          {
  87.             param1.channelSet.removeEventListener(MessageEvent.MESSAGE,messageHandler);
  88.             _channelSetRefCounts[param1.channelSet] = null;
  89.             if(_consumerDuplicateMessageBarrier[param1.id] != null)
  90.             {
  91.                delete _consumerDuplicateMessageBarrier[param1.id];
  92.             }
  93.          }
  94.          else
  95.          {
  96.             _channelSetRefCounts[param1.channelSet] = _loc2_;
  97.          }
  98.       }
  99.       
  100.       public function isChannelUsedForSubscriptions(param1:Channel) : Boolean
  101.       {
  102.          var _loc2_:Array = param1.channelSets;
  103.          var _loc3_:ChannelSet = null;
  104.          var _loc4_:int = int(_loc2_.length);
  105.          var _loc5_:int = 0;
  106.          while(_loc5_ < _loc4_)
  107.          {
  108.             _loc3_ = _loc2_[_loc5_];
  109.             if(_channelSetRefCounts[_loc3_] != null && _loc3_.currentChannel == param1)
  110.             {
  111.                return true;
  112.             }
  113.             _loc5_++;
  114.          }
  115.          return false;
  116.       }
  117.    }
  118. }
  119.  
  120.