// Copyright (c) 2007-2009 The PyAMF Project.
// See LICENSE for details.

// Load the Remoting classes
load("netservices.asc");

application.onAppStart = function() {
    // Setup connection
    NetServices.setDefaultGatewayUrl("http://localhost:8000");
    // Gateway connection object
    this.gateway = NetServices.createGatewayConnection();
}

application.onConnect = function( clientObj, username, password ) {
	
	// Authentication
	this.gateway.setCredentials(username, password);
	
    // Create service object to access the remote service called 'calc',
    // and set responder property to the object that will receive the 
    // result or fault condition that the service returns.
    clientObj.service = this.gateway.getService("calc", new PyAMFResult( clientObj ) );

    // These vars hold the data we want to pass to the remote service.
    var a = 1;
    var b = 2;
   
    // Call remote method 'sum' to send data and do authentication.
    this.service.sum( a, b );
}

/**
 * Remoting Object.
 * Default responder object for Remoting calls.
 */
function PyAMFResult( client ) {
    // Set the client object
    this.client = client;
}

// Result handler method 
PyAMFResult.prototype.calc_Result = function( result ) {
    trace( result ); // prints "3"
	
	// Accept the client connection
    this.acceptConnection( clientObj );
}

// Fault handler method displays error message 
PyAMFResult.prototype.calc_Status = function( error ) {    
   // Notify the user of the problem
   trace( "Remoting error: " );
   for ( var d in error ) {
       trace( error[d] + "\n" );
   }
   // Reject the client connection
    this.rejectConnection( clientObj );
}
