nicolas
|
 |
« on: April 23, 2011, 08:41:45 » |
|
I am building a PureMVC application that makes use of AMFPHP to retrieve (a lot of) data. There are now 40+ different services (AMFPHP functions) that may be called during the application runtime. Some of them simultaneously.
First I started implementing ONE proxy to manage all the calls to the services, sharing only one NetConnection. I soon realized that making a second call when the result of the previous one was not arrived would likely stop the first call. (For example, upon login I simultaneously call a service "checkMails" and a service "getNews"). So, I added an Array to this proxy which contained all opened NetConnections to AMFPHP. (And removing from array when the result arrives).
I am now rewriting this part, I wrote a class "AMF_Proxy" that extends "Proxy", and several classes extending "AMF_Proxy" (one for each AMFPHP service). So that I now have multiple proxies created dynamically, one for each call to an AMFPHP service. When the service returns a result or fails, the proxy is supposed to remove itself.
LoginCommand.as
/* Simple Command - PureMVC */ package com.project.controller { import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.command.SimpleCommand; import org.puremvc.as3.patterns.observer.Notification; import com.project.ApplicationNotifications; import com.project.ApplicationParameters; import com.project.model.*; import com.project.model.amf.*; /** * Login */ public class LoginCommand extends SimpleCommand { override public function execute(note:INotification):void { var notificationData:Object = note.getBody() as Object; facade.registerProxy( new AMF_LoginProxy(notificationData) ); } } } AMF_Proxy.as
package com.project.model.amf { import flash.events.*; import flash.net.*; import flash.system.Security; import org.puremvc.as3.interfaces.IProxy; import org.puremvc.as3.patterns.proxy.Proxy; import org.puremvc.as3.interfaces.IMediator; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.mediator.Mediator; import com.project.utils.MyUtils; import com.project.model.*; import com.project.model.vo.*; import com.project.ApplicationNotifications; import com.project.ApplicationParameters; public class AMF_Proxy extends Proxy implements IProxy { // Cannonical name of the Proxy public static const NAME:String = "AMF_Proxy"; public var connection:NetConnection; private var responder:Responder; public function AMF_Proxy(calldata:Object = null) { // To register multiple times super(MyUtils.getUniqueProxyName() + NAME); // Initializing connection = new NetConnection(); connection.addEventListener(IOErrorEvent.IO_ERROR, IOerrorConnection); connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorConnection); connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorConnection); //connection.addEventListener(NetStatusEvent.NET_STATUS, status ); // Initiate connection on creation this.connect(); this.serviceCall(calldata); } private function connect():Boolean { try { connection.connect((facade.retrieveProxy (DataProxy.NAME) as DataProxy).gatewayURL); } catch (error:Error) { trace("Error accessing Gateway"); } return true; } public function serviceCall(calldata:Object = null):void { } public function onServiceResult(result:Object):void { } private function disconnect():void { connection.close(); } //PHP Fault public function onFault(fault:Object):void { for (var i in fault) { trace("onFault: " + fault[i]); } this.destroy(); } private function IOerrorConnection(e:IOErrorEvent):void { trace("IO_ERROR: " + e); this.destroy(); } private function errorConnection(pEvt:Event):void { trace("errorConnection: " + pEvt); this.destroy(); } public function destroy():void { this.disconnect(); facade.removeProxy(this.getProxyName()); } // Method to remove everything override public function onRemove():void { // Remove All Listeners connection.removeEventListener(IOErrorEvent.IO_ERROR, IOerrorConnection); connection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorConnection); connection.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, errorConnection); try { connection.close(); connection = null; }catch (e:Error) { trace("** Error"); } } } } AMF_LoginProxy.as
package com.project.model.amf { import flash.events.*; import flash.net.*; import flash.system.Security; import org.puremvc.as3.interfaces.IProxy; import org.puremvc.as3.patterns.proxy.Proxy; import org.puremvc.as3.interfaces.IMediator; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.mediator.Mediator; import com.project.utils.MyUtils; import com.project.model.*; import com.project.model.vo.*; import com.project.ApplicationNotifications; /** * Login */ public class AMF_LoginProxy extends AMF_Proxy { public function AMF_LoginProxy(calldata:Object) { super(calldata); } override public function serviceCall(calldata:Object = null):void { var serviceResponder:Responder = new Responder(onServiceResult, onFault); try { connection.call("User.logIn", serviceResponder, String(calldata.nickname), String(calldata.password)); } catch (error:Error) { trace("Error calling serviceCall. " + error.getStackTrace()); } } override public function onServiceResult(result:Object):void { if (result.res == "OK") { sendNotification(ApplicationNotifications.LOGIN_OK, result); }else { sendNotification(ApplicationNotifications.LOGIN_KO, result); } this.destroy(); } } } My main concerns are: * I would like to make sure that every AMFPHP call is handled properly (ie. always gives some result) * I worry that it won't create too many simultaneous connections that would overload the server
Am I doing wrong?
Thank you.
|