<%
/*
 * Copyright:
 *   (C) 2006 by Derrell Lipman
 *       All rights reserved
 *
 * License:
 *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
 */

/*
 * This is the standard qooxdoo test class.  There are tests for each of the
 * primitive types here, along with standard named tests "echo", "sink" and
 * "sleep".
 */

/**
 * Echo the (one and only) parameter.
 *
 * @param params
 *   An array containing the parameters to this method
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: The object containing the result of the method;
 *   Failure: null
 */
function _echo(params, error)
{
    if (params.length != 1)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "Expected 1 parameter; got " + params.length);
        return error;
    }
    return "Client said: [" + params[0] + "]";
}
jsonrpc.method.echo = _echo;

/**
 * Sink all data and never return.
 *
 * @param params
 *   An array containing the parameters to this method (none expected)
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   "Never"
 */
function _sink(params, error)
{
    /* We're never supposed to return.  Just sleep for a very long time. */
    sleep(240);
}
jsonrpc.method.sink = _sink;

/**
 * Sleep for the number of seconds specified by the parameter.
 *
 * @param params
 *   An array containing the parameters to this method (one expected)
 *
 * @param error
 *   An object of class JsonRpcError.
 *
 * @return
 *   Success: The object containing the result of the method;
 *   Failure: null
 */
function _sleep(params, error)
{
    if (params.length != 1)
    {
        error.setError(JsonRpcError_ParameterMismatch,
                       "Expected 1 parameter; got " + params.length);
        return error;
    }
    
    sleep(params[0]);
    return params[0];
}
jsonrpc.method.sleep = _sleep;

/*************************************************************************/

/*
 * The remainder of the functions test each individual primitive type, and
 * test echoing arbitrary types.  Hopefully the name is self-explanatory.
 */

function _getInteger(params, error)
{
    return 1;
}
jsonrpc.method.getInteger = _getInteger;

function _getFloat(params, error)
{
    return 1/3;
}
jsonrpc.method.getFloat = _getFloat;

function _getString(params, error)
{
    return "Hello world";
}
jsonrpc.method.getString = _getString;

function _getBadString(params, error)
{
    return "<!DOCTYPE HTML \"-//IETF//DTD HTML 2.0//EN\">";
}
jsonrpc.method.getBadString = _getBadString;

function _getArrayInteger(params, error)
{
    return new Array(1, 2, 3, 4);
}
jsonrpc.method.getArrayInteger = _getArrayInteger;

function _getArrayString(params, error)
{
    return new Array("one", "two", "three", "four");
}
jsonrpc.method.getArrayString = _getArrayString;

function _getObject(params, error)
{
    o = new Object(); // some arbitrary object
    o.something = 23;
    o.garbage = 'lkasjdff;lajsdfkl;sadf';
    return o;    
}
jsonrpc.method.getObject = _getObject;

function _getTrue(params, error)
{
    return true;
}
jsonrpc.method.getTrue = _getTrue;

function _getFalse(params, error)
{
    return false;
}
jsonrpc.method.getFalse = _getFalse;

function _getNull(params, error)
{
    return null;
}
jsonrpc.method.getNull = _getNull;

function _isInteger(params, error)
{
    var type = nativeTypeOf(params[0]);
    return type == "integer" || type == "integer64";
}
jsonrpc.method.isInteger = _isInteger;

function _isFloat(params, error)
{
    return nativeTypeOf(params[0]) == "float";
}
jsonrpc.method.isFloat = _isFloat;

function _isString(params, error)
{
    return nativeTypeOf(params[0]) == "string";
}
jsonrpc.method.isString = _isString;

function _isBoolean(params, error)
{
    return nativeTypeOf(params[0]) == "boolean";
}
jsonrpc.method.isBoolean = _isBoolean;

function _isArray(params, error)
{
    return nativeTypeOf(params[0]) == "object" && params.length != undefined;
}
jsonrpc.method.isArray = _isArray;

function _isObject(params, error)
{
    return nativeTypeOf(params[0]) == "object";
}
jsonrpc.method.isObject = _isObject;

function _isNull(params, error)
{
    return nativeTypeOf(params[0]) == "null";
}
jsonrpc.method.isNull = _isNull;

function _getParams(params, error)
{
    return params;
}	
jsonrpc.method.getParams = _getParams;

function _getParam(params, error)
{
    return params[0];
}	
jsonrpc.method.getParam = _getParam;

function _getCurrentTimestamp()
{
    now = gettimeofday();
    obj = new Object();
    obj.now = now.sec;
    obj.json = JSON_Date.create(now);
    return obj;
}
jsonrpc.method.getCurrentTimestamp = _getCurrentTimestamp;

function _getError(params, error)
{
    error.setError(23, "This is an application-provided error");
    return error;
}	
jsonrpc.method.getError = _getError;


/*
 * Local Variables:
 * mode: c
 * End:
 */
%>
