package substance.amfphp { /** * Service * * @author shaun.tinney@findsubstance.com; * @since 16.04.2009; * @desc dynamic amfphp web service; */ import flash.events.*; import flash.external.*; import flash.net.*; import flash.utils.*; dynamic public class AMFService extends Proxy { //-------------------------------------- // CLASS CONSTANTS //-------------------------------------- public static const NAME : String = 'substance.amfphp.AMFService'; //-------------------------------------- // CONSTRUCTOR //-------------------------------------- public function AMFService ( service : String, gateway : String ) { super(); // look up service; _service = service; // establish gateway connection; _gateway = new NetConnection(); _gateway.addEventListener( AsyncErrorEvent.ASYNC_ERROR, onAsyncError ); _gateway.addEventListener( IOErrorEvent.IO_ERROR, onIOError ); _gateway.addEventListener( NetStatusEvent.NET_STATUS, onNetStatus ); _gateway.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError ); _gateway.connect( gateway ); // set up response handler; _returnResponder = new Responder( onReturn, onFault ); } //-------------------------------------- // INSTANCE VARIABLES //-------------------------------------- // public properties; public var data : *; public var debug : Boolean = false; // private properties; private var _service : String; // helpers; protected var _gateway : NetConnection; protected var _dispatcher : EventDispatcher = new EventDispatcher(); // responders; protected var _returnResponder : Responder; //-------------------------------------- // GETTER/SETTERS //-------------------------------------- public function get service () : String { return _service; } public function set service ( value : String ) : void { _service = value; } //-------------------------------------- // PUBLIC METHODS //-------------------------------------- public function addEventListener ( type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReferences : Boolean = false ) : void { _dispatcher.addEventListener( type, listener, useCapture, priority, useWeakReferences ); } public function removeEventListener ( type : String, listener : Function, useCapture : Boolean = false ) : void { _dispatcher.removeEventListener( type, listener, useCapture ); } public function dispatchEvent ( event : Event ) : void { _dispatcher.dispatchEvent( event ); } //-------------------------------------- // EVENT HANDLERS //-------------------------------------- protected function onReturn ( returnData : * ) : void { data = returnData; dispatchEvent( new AMFServiceEvent( AMFServiceEvent.RETURN, data ) ); } protected function onFault ( response : Object ) : void { var message : String = _service + ' - fault : ' + objectToString( response ); ( ExternalInterface.available ) ? ExternalInterface.call( 'console.log', message ) : trace( message ); } private function onAsyncError ( e : AsyncErrorEvent ) : void { if ( debug ) trace( _service + ' - ' + e ); } private function onIOError ( e : IOErrorEvent ) : void { if ( debug ) trace( _service + ' - ' + e ); } private function onNetStatus ( e : NetStatusEvent ) : void { if ( debug ) trace( _service + ' - ' + e ); } private function onSecurityError ( e : SecurityErrorEvent ) : void { if ( debug ) trace( _service + ' - ' + e ); } //-------------------------------------- // PRIVATE & PROTECTED INSTANCE METHODS //-------------------------------------- private function objectToString ( object : Object ) : String { if ( !object ) return ''; var s : String = object.toString() + '\n'; for ( var n : * in object ) s += '--> ' + n + ' : ' + object[ n ] + '\n'; return s; } // catch errors; override flash_proxy function setProperty ( name : *, value : * ) : void { trace( _service + ' - attempted to set unused property ' + name ); } override flash_proxy function getProperty ( name : * ) : * { trace( _service + ' - attempted to get unused property ' + name ); } // attempt to call generic service function; override flash_proxy function callProperty ( method : *, ...args ) : * { try { // output function call; trace( String( _service + '.' + method ), args ); // call service method with up to 8 arguments; _gateway.call( String( _service + '.' + method ), _returnResponder, args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], args[ 4 ], args[ 5 ], args[ 6 ], args[ 7 ] ); } catch ( e : * ) { trace( _service + ' - ' + e ); } } } }