home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Darbas / kidoz_v1.air / kidoz.swf / scripts / air / net / ServiceMonitor.as next >
Encoding:
Text File  |  2009-05-06  |  5.5 KB  |  202 lines

  1. package air.net
  2. {
  3.    import flash.desktop.NativeApplication;
  4.    import flash.events.Event;
  5.    import flash.events.EventDispatcher;
  6.    import flash.events.StatusEvent;
  7.    import flash.events.TimerEvent;
  8.    import flash.utils.Timer;
  9.    
  10.    public dynamic class ServiceMonitor extends EventDispatcher
  11.    {
  12.       private static const kInvalidParamError:uint = 2004;
  13.       
  14.       private static const kDelayRangeError:uint = 2066;
  15.       
  16.       private var _running:Boolean = false;
  17.       
  18.       private var _delay:Number = 0;
  19.       
  20.       private var _timer:Timer = new Timer(_delay);
  21.       
  22.       private var _available:Boolean = false;
  23.       
  24.       private var _specializer:Object = null;
  25.       
  26.       private var _notifyRegardless:Boolean = false;
  27.       
  28.       private var _statusTime:Date = new Date();
  29.       
  30.       public function ServiceMonitor()
  31.       {
  32.          super();
  33.          _timer.addEventListener(TimerEvent.TIMER,onTimer);
  34.       }
  35.       
  36.       private static function makeForwarder(param1:String) : Function
  37.       {
  38.          var name:String = param1;
  39.          return function forwarder(... rest):*
  40.          {
  41.             return this.__monitor__[name].apply(this.__monitor__,rest);
  42.          };
  43.       }
  44.       
  45.       public static function makeJavascriptSubclass(param1:Object) : void
  46.       {
  47.          var name:String = null;
  48.          var constructorFunction:Object = param1;
  49.          var proto:Object = constructorFunction.prototype;
  50.          var names:Array = ["start","stop","willTrigger","removeEventListener","addEventListener","dispatchEvent","hasEventListener"];
  51.          for each(name in names)
  52.          {
  53.             proto[name] = makeForwarder(name);
  54.          }
  55.          proto.setAvailable = function(param1:Boolean):void
  56.          {
  57.             this.__monitor__.available = param1;
  58.          };
  59.          proto.getAvailable = function():Boolean
  60.          {
  61.             return this.__monitor__.available;
  62.          };
  63.          proto.toString = makeForwarder("_toString");
  64.          proto.initServiceMonitor = function():*
  65.          {
  66.             return _initServiceMonitor(this);
  67.          };
  68.       }
  69.       
  70.       private static function _initServiceMonitor(param1:Object) : ServiceMonitor
  71.       {
  72.          var _loc2_:ServiceMonitor = new ServiceMonitor();
  73.          param1.__monitor__ = _loc2_;
  74.          _loc2_._specializer = param1;
  75.          return _loc2_;
  76.       }
  77.       
  78.       public function stop() : void
  79.       {
  80.          if(!_running)
  81.          {
  82.             return;
  83.          }
  84.          _running = false;
  85.          _timer.stop();
  86.          NativeApplication.nativeApplication.removeEventListener(Event.NETWORK_CHANGE,onNetworkChange);
  87.       }
  88.       
  89.       public function set pollInterval(param1:Number) : void
  90.       {
  91.          if(param1 < 0 || !isFinite(param1))
  92.          {
  93.             Error.throwError(RangeError,kDelayRangeError);
  94.          }
  95.          _delay = param1;
  96.          _timer.stop();
  97.          if(_delay > 0)
  98.          {
  99.             _timer.delay = _delay;
  100.             if(_running)
  101.             {
  102.                _timer.start();
  103.             }
  104.          }
  105.       }
  106.       
  107.       public function get available() : Boolean
  108.       {
  109.          return _available;
  110.       }
  111.       
  112.       private function _toString() : String
  113.       {
  114.          return "[ServiceMonitor available=\"" + available + "\"]";
  115.       }
  116.       
  117.       public function get lastStatusUpdate() : Date
  118.       {
  119.          return new Date(_statusTime.time);
  120.       }
  121.       
  122.       public function set available(param1:Boolean) : void
  123.       {
  124.          var _loc3_:String = null;
  125.          var _loc4_:String = null;
  126.          var _loc2_:Boolean = _available;
  127.          _available = param1;
  128.          _statusTime = new Date();
  129.          if(_loc2_ != _available || _notifyRegardless)
  130.          {
  131.             _loc3_ = _available ? "Service.available" : "Service.unavailable";
  132.             _loc4_ = "status";
  133.             dispatchEvent(new StatusEvent(StatusEvent.STATUS,false,false,_loc3_,_loc4_));
  134.          }
  135.          _notifyRegardless = false;
  136.       }
  137.       
  138.       protected function checkStatus() : void
  139.       {
  140.          if(Boolean(_specializer) && Boolean(_specializer.checkStatus))
  141.          {
  142.             _specializer.checkStatus();
  143.          }
  144.       }
  145.       
  146.       private function onNetworkChange(param1:Event) : void
  147.       {
  148.          if(!_running)
  149.          {
  150.             return;
  151.          }
  152.          if(_delay > 0)
  153.          {
  154.             _timer.stop();
  155.             _timer.start();
  156.          }
  157.          checkStatus();
  158.       }
  159.       
  160.       public function start() : void
  161.       {
  162.          if(_running)
  163.          {
  164.             return;
  165.          }
  166.          _running = true;
  167.          _notifyRegardless = true;
  168.          if(_delay > 0)
  169.          {
  170.             _timer.start();
  171.          }
  172.          NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE,onNetworkChange,false,0,true);
  173.          checkStatus();
  174.       }
  175.       
  176.       public function get pollInterval() : Number
  177.       {
  178.          return _delay;
  179.       }
  180.       
  181.       private function onTimer(param1:TimerEvent) : void
  182.       {
  183.          checkStatus();
  184.       }
  185.       
  186.       override public function toString() : String
  187.       {
  188.          if(Boolean(_specializer) && Boolean(_specializer.toString))
  189.          {
  190.             return _specializer.toString();
  191.          }
  192.          return _toString();
  193.       }
  194.       
  195.       public function get running() : Boolean
  196.       {
  197.          return _running;
  198.       }
  199.    }
  200. }
  201.  
  202.