/*
 Copyright (c) 2006 - 2007 Eric J. Feminella  <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.rpc
{
    import mx.rpc.remoting.RemoteObject;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.IResponder;

    /**
     * 
     * Provides a generic base class API for creating Weborb PHP 
     * gateway connections and performing RPC based invocations 
     * on remote PHP objects.
     * 
     */
    public class WebORBPHPRemoteGateway extends RemoteObject implements IResponder
    {
        /**
         *
         * Instantiates a new instance of WeborbRemoteGateway 
         * and Iniatializes the destination for the remote PHP 
         * object as specified via remoting-config.xml
         *
         * @param destination as defined in remoting-config.xml
         * 
         */
        public function WebORBPHPRemoteGateway(destination:String)
        {
            this.destination = destination;
        }

        /**
         * 
         * Performs RPC method invocation on a remote PHP object 
         * and adds result and fault listeners to the call
         *
         * @param the method to invoke on the remote object
         * @param a parameter of arbitrary length
         * 
         */
        public function invoke(method:String, ...args):void
        {
            this[method].addEventListener( ResultEvent.RESULT, result );
            this[method].addEventListener( FaultEvent.FAULT,   fault );
            this[method].send.apply( null, [].concat(args) );
        }

        /**
         * 
         * Handles an asynchronous result response
         *
         * @param the data object associated with the result
         * 
         */
        public function result(data:Object) : void
        {
            var result:ResultEvent = data as ResultEvent;
        }

        /**
         * 
         * Handles an asynchronous fault response
         *
         * @param the info object associated with the fault
         * 
         */
        public function fault(info:Object) : void
        {
            var fault:FaultEvent = info as FaultEvent;
        }
    }
}