home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Dual.swf / scripts / __Packages / mochi / MochiServices.as < prev   
Encoding:
Text File  |  2008-09-03  |  11.3 KB  |  330 lines

  1. class mochi.MochiServices
  2. {
  3.    var isLoading;
  4.    var startTime;
  5.    var waitInterval;
  6.    var _callbacks;
  7.    static var _id;
  8.    static var _container;
  9.    static var _clip;
  10.    static var _sendChannelName;
  11.    static var _rcvChannelName;
  12.    static var onError;
  13.    static var _listenChannel;
  14.    static var _rcvChannel;
  15.    static var _loader;
  16.    static var _loaderListener;
  17.    static var _sendChannel;
  18.    static var _gatewayURL = "http://www.mochiads.com/static/lib/services/services.swf";
  19.    static var _listenChannelName = "__mochiservices";
  20.    static var _connecting = false;
  21.    static var _connected = false;
  22.    function MochiServices()
  23.    {
  24.    }
  25.    static function get id()
  26.    {
  27.       return mochi.MochiServices._id;
  28.    }
  29.    static function get clip()
  30.    {
  31.       return mochi.MochiServices._container;
  32.    }
  33.    static function get childClip()
  34.    {
  35.       return mochi.MochiServices._clip;
  36.    }
  37.    static function getVersion()
  38.    {
  39.       return "1.31";
  40.    }
  41.    static function allowDomains(server)
  42.    {
  43.       var _loc1_ = server.split("/")[2].split(":")[0];
  44.       if(System.security)
  45.       {
  46.          if(System.security.allowDomain)
  47.          {
  48.             System.security.allowDomain("*");
  49.             System.security.allowDomain(_loc1_);
  50.          }
  51.          if(System.security.allowInsecureDomain)
  52.          {
  53.             System.security.allowInsecureDomain("*");
  54.             System.security.allowInsecureDomain(_loc1_);
  55.          }
  56.       }
  57.       return _loc1_;
  58.    }
  59.    static function get isNetworkAvailable()
  60.    {
  61.       if(System.security)
  62.       {
  63.          var _loc1_ = System.security;
  64.          if(_loc1_.sandboxType == "localWithFile")
  65.          {
  66.             return false;
  67.          }
  68.       }
  69.       return true;
  70.    }
  71.    static function set comChannelName(val)
  72.    {
  73.       if(val != undefined)
  74.       {
  75.          if(val.length > 3)
  76.          {
  77.             mochi.MochiServices._sendChannelName = val + "_fromgame";
  78.             mochi.MochiServices._rcvChannelName = val;
  79.             mochi.MochiServices.initComChannels();
  80.          }
  81.       }
  82.    }
  83.    static function get connected()
  84.    {
  85.       return mochi.MochiServices._connected;
  86.    }
  87.    static function connect(id, clip, onError)
  88.    {
  89.       if(!mochi.MochiServices._connected && mochi.MochiServices._clip == undefined)
  90.       {
  91.          trace("MochiServices Connecting...");
  92.          mochi.MochiServices._connecting = true;
  93.          mochi.MochiServices.init(id,clip);
  94.       }
  95.       if(onError != undefined)
  96.       {
  97.          mochi.MochiServices.onError = onError;
  98.       }
  99.       else if(mochi.MochiServices.onError == undefined)
  100.       {
  101.          mochi.MochiServices.onError = function(errorCode)
  102.          {
  103.             trace(errorCode);
  104.          };
  105.       }
  106.    }
  107.    static function disconnect()
  108.    {
  109.       if(mochi.MochiServices._connected || mochi.MochiServices._connecting)
  110.       {
  111.          mochi.MochiServices._connecting = mochi.MochiServices._connected = false;
  112.          mochi.MochiServices.flush(true);
  113.          if(mochi.MochiServices._clip != undefined)
  114.          {
  115.             mochi.MochiServices._clip.removeMovieClip();
  116.             delete mochi.MochiServices._clip;
  117.          }
  118.          mochi.MochiServices._listenChannel.close();
  119.          mochi.MochiServices._rcvChannel.close();
  120.       }
  121.    }
  122.    static function init(id, clip)
  123.    {
  124.       mochi.MochiServices._id = id;
  125.       if(clip != undefined)
  126.       {
  127.          mochi.MochiServices._container = clip;
  128.       }
  129.       else
  130.       {
  131.          mochi.MochiServices._container = _root;
  132.       }
  133.       mochi.MochiServices.loadCommunicator(id,mochi.MochiServices._container);
  134.    }
  135.    static function loadCommunicator(id, clip)
  136.    {
  137.       var _loc2_ = "_mochiservices_com_" + id;
  138.       if(mochi.MochiServices._clip != null)
  139.       {
  140.          return mochi.MochiServices._clip;
  141.       }
  142.       if(!mochi.MochiServices.isNetworkAvailable)
  143.       {
  144.          return null;
  145.       }
  146.       mochi.MochiServices.allowDomains(mochi.MochiServices._gatewayURL);
  147.       mochi.MochiServices._clip = clip.createEmptyMovieClip(_loc2_,10336,false);
  148.       mochi.MochiServices._loader = new MovieClipLoader();
  149.       if(mochi.MochiServices._loaderListener.waitInterval != null)
  150.       {
  151.          clearInterval(mochi.MochiServices._loaderListener.waitInterval);
  152.       }
  153.       mochi.MochiServices._loaderListener = {};
  154.       mochi.MochiServices._loaderListener.onLoadError = function(target_mc, errorCode, httpStatus)
  155.       {
  156.          trace("MochiServices could not load.");
  157.          mochi.MochiServices.disconnect();
  158.          mochi.MochiServices.onError.apply(null,[errorCode]);
  159.       };
  160.       mochi.MochiServices._loaderListener.onLoadStart = function(target_mc)
  161.       {
  162.          this.isLoading = true;
  163.       };
  164.       mochi.MochiServices._loaderListener.startTime = getTimer();
  165.       mochi.MochiServices._loaderListener.wait = function()
  166.       {
  167.          if(getTimer() - this.startTime > 10000)
  168.          {
  169.             if(!this.isLoading)
  170.             {
  171.                mochi.MochiServices.disconnect();
  172.                mochi.MochiServices.onError.apply(null,["IOError"]);
  173.             }
  174.             clearInterval(this.waitInterval);
  175.          }
  176.       };
  177.       mochi.MochiServices._loaderListener.waitInterval = setInterval(mochi.MochiServices._loaderListener,"wait",1000);
  178.       mochi.MochiServices._loader.addListener(mochi.MochiServices._loaderListener);
  179.       mochi.MochiServices._loader.loadClip(mochi.MochiServices._gatewayURL,mochi.MochiServices._clip);
  180.       mochi.MochiServices._sendChannel = new LocalConnection();
  181.       mochi.MochiServices._sendChannel._queue = [];
  182.       mochi.MochiServices._rcvChannel = new LocalConnection();
  183.       mochi.MochiServices._rcvChannel.allowDomain = function(d)
  184.       {
  185.          return true;
  186.       };
  187.       mochi.MochiServices._rcvChannel.allowInsecureDomain = mochi.MochiServices._rcvChannel.allowDomain;
  188.       mochi.MochiServices._rcvChannel._nextcallbackID = 0;
  189.       mochi.MochiServices._rcvChannel._callbacks = {};
  190.       mochi.MochiServices.listen();
  191.       return mochi.MochiServices._clip;
  192.    }
  193.    static function onStatus(infoObject)
  194.    {
  195.       var _loc0_ = null;
  196.       if((_loc0_ = infoObject.level) === "error")
  197.       {
  198.          mochi.MochiServices._connected = false;
  199.          mochi.MochiServices._listenChannel.connect(mochi.MochiServices._listenChannelName);
  200.       }
  201.    }
  202.    static function listen()
  203.    {
  204.       mochi.MochiServices._listenChannel = new LocalConnection();
  205.       mochi.MochiServices._listenChannel.handshake = function(args)
  206.       {
  207.          mochi.MochiServices.comChannelName = args.newChannel;
  208.       };
  209.       mochi.MochiServices._listenChannel.allowDomain = function(d)
  210.       {
  211.          return true;
  212.       };
  213.       mochi.MochiServices._listenChannel.allowInsecureDomain = mochi.MochiServices._listenChannel.allowDomain;
  214.       mochi.MochiServices._listenChannel.connect(mochi.MochiServices._listenChannelName);
  215.       trace("Waiting for MochiAds services to connect...");
  216.    }
  217.    static function initComChannels()
  218.    {
  219.       if(!mochi.MochiServices._connected)
  220.       {
  221.          mochi.MochiServices._sendChannel.onStatus = function(infoObject)
  222.          {
  223.             mochi.MochiServices.onStatus(infoObject);
  224.          };
  225.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:"handshakeDone"});
  226.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:"registerGame",id:mochi.MochiServices._id,clip:mochi.MochiServices._clip,version:getVersion()});
  227.          mochi.MochiServices._rcvChannel.onStatus = function(infoObject)
  228.          {
  229.             mochi.MochiServices.onStatus(infoObject);
  230.          };
  231.          mochi.MochiServices._rcvChannel.onReceive = function(pkg)
  232.          {
  233.             var _loc5_ = pkg.callbackID;
  234.             var _loc4_ = this._callbacks[_loc5_];
  235.             if(!_loc4_)
  236.             {
  237.                return undefined;
  238.             }
  239.             var _loc2_ = _loc4_.callbackMethod;
  240.             var _loc3_ = _loc4_.callbackObject;
  241.             if(_loc3_ && typeof _loc2_ == "string")
  242.             {
  243.                _loc2_ = _loc3_[_loc2_];
  244.             }
  245.             if(_loc2_ != undefined)
  246.             {
  247.                _loc2_.apply(_loc3_,pkg.args);
  248.             }
  249.             delete this._callbacks[_loc5_];
  250.          };
  251.          mochi.MochiServices._rcvChannel.onError = function()
  252.          {
  253.             mochi.MochiServices.onError.apply(null,["IOError"]);
  254.          };
  255.          mochi.MochiServices._rcvChannel.connect(mochi.MochiServices._rcvChannelName);
  256.          trace("connected!");
  257.          mochi.MochiServices._connecting = false;
  258.          mochi.MochiServices._connected = true;
  259.          mochi.MochiServices._listenChannel.close();
  260.          while(mochi.MochiServices._sendChannel._queue.length > 0)
  261.          {
  262.             mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",mochi.MochiServices._sendChannel._queue.shift());
  263.          }
  264.       }
  265.    }
  266.    static function flush(error)
  267.    {
  268.       var _loc1_ = undefined;
  269.       var _loc2_ = undefined;
  270.       while(mochi.MochiServices._sendChannel._queue.length > 0)
  271.       {
  272.          _loc1_ = mochi.MochiServices._sendChannel._queue.shift();
  273.          false;
  274.          if(_loc1_.callbackID != null)
  275.          {
  276.             _loc2_ = mochi.MochiServices._rcvChannel._callbacks[_loc1_.callbackID];
  277.          }
  278.          delete mochi.MochiServices._rcvChannel._callbacks[_loc1_.callbackID];
  279.          if(error)
  280.          {
  281.             mochi.MochiServices.handleError(_loc1_.args,_loc2_.callbackObject,_loc2_.callbackMethod);
  282.          }
  283.       }
  284.    }
  285.    static function handleError(args, callbackObject, callbackMethod)
  286.    {
  287.       if(args != null)
  288.       {
  289.          if(args.onError != null)
  290.          {
  291.             args.onError.apply(null,["NotConnected"]);
  292.          }
  293.       }
  294.       if(callbackMethod != null)
  295.       {
  296.          args = {};
  297.          args.error = true;
  298.          args.errorCode = "NotConnected";
  299.          if(callbackObject != null && typeof callbackMethod == "string")
  300.          {
  301.             callbackObject[callbackMethod](args);
  302.          }
  303.          else if(callbackMethod != null)
  304.          {
  305.             callbackMethod.apply(args);
  306.          }
  307.       }
  308.    }
  309.    static function send(methodName, args, callbackObject, callbackMethod)
  310.    {
  311.       if(mochi.MochiServices._connected)
  312.       {
  313.          mochi.MochiServices._sendChannel.send(mochi.MochiServices._sendChannelName,"onReceive",{methodName:methodName,args:args,callbackID:mochi.MochiServices._rcvChannel._nextcallbackID});
  314.       }
  315.       else
  316.       {
  317.          if(mochi.MochiServices._clip == undefined || !mochi.MochiServices._connecting)
  318.          {
  319.             mochi.MochiServices.onError.apply(null,["NotConnected"]);
  320.             mochi.MochiServices.handleError(args,callbackObject,callbackMethod);
  321.             mochi.MochiServices.flush(true);
  322.             return undefined;
  323.          }
  324.          mochi.MochiServices._sendChannel._queue.push({methodName:methodName,args:args,callbackID:mochi.MochiServices._rcvChannel._nextcallbackID});
  325.       }
  326.       mochi.MochiServices._rcvChannel._callbacks[mochi.MochiServices._rcvChannel._nextcallbackID] = {callbackObject:callbackObject,callbackMethod:callbackMethod};
  327.       mochi.MochiServices._rcvChannel._nextcallbackID = mochi.MochiServices._rcvChannel._nextcallbackID + 1;
  328.    }
  329. }
  330.