2005-08-05 23:02:01 +04:00
/ *
server side js functions for handling async calls from js clients
Copyright Andrew Tridgell 2005
released under the GNU GPL Version 2 or later
* /
libinclude ( "encoder.js" ) ;
/ *
register a new call
* /
function _ _register _call ( name , func )
{
var c = this ;
c . calls [ name ] = func ;
}
/ *
run a call sent from the client , and output the returned object ( if any )
* /
function _ _run _call ( ) {
var c = this ;
2005-08-10 10:58:05 +04:00
var name = form [ 'ajaj_func' ] ;
2005-08-05 23:02:01 +04:00
if ( name == undefined ) {
2005-08-10 10:58:05 +04:00
/* no function to run */
2005-08-05 23:02:01 +04:00
return ;
}
2005-08-10 10:58:05 +04:00
var args = form [ 'ajaj_args' ] ;
2005-08-05 23:02:01 +04:00
if ( args == undefined ) {
println ( "no function arguments given in run_call" ) ;
2005-08-10 10:58:05 +04:00
exit ( 0 ) ;
2005-08-05 23:02:01 +04:00
}
args = decodeObject ( args ) ;
if ( c . calls [ name ] == undefined ) {
println ( "undefined remote call " + name ) ;
2005-08-10 10:58:05 +04:00
exit ( 0 ) ;
2005-08-05 23:02:01 +04:00
}
var f = c . calls [ name ] ;
var res ;
/* oh what a hack - should write a varargs ejs helper */
if ( args . length == 0 ) {
res = f ( ) ;
} else if ( args . length == 1 ) {
res = f ( args [ 0 ] ) ;
} else if ( args . length == 2 ) {
res = f ( args [ 0 ] , args [ 1 ] ) ;
} else if ( args . length == 3 ) {
res = f ( args [ 0 ] , args [ 1 ] , args [ 2 ] ) ;
} else if ( args . length == 4 ) {
res = f ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] ) ;
} else if ( args . length == 5 ) {
res = f ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] , args [ 4 ] ) ;
} else if ( args . length == 6 ) {
res = f ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] , args [ 4 ] , args [ 5 ] ) ;
} else if ( args . length == 7 ) {
res = f ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] , args [ 4 ] , args [ 5 ] , args [ 6 ] ) ;
} else if ( args . length == 8 ) {
res = f ( args [ 0 ] , args [ 1 ] , args [ 2 ] , args [ 3 ] , args [ 4 ] , args [ 5 ] , args [ 6 ] , args [ 7 ] ) ;
} else {
println ( "too many arguments for remote call: " + name ) ;
2005-08-10 10:58:05 +04:00
exit ( 0 ) ;
2005-08-05 23:02:01 +04:00
}
var repobj = new Object ( ) ;
repobj . res = res ;
write ( encodeObject ( repobj ) ) ;
2005-08-10 10:58:05 +04:00
exit ( 0 ) ;
2005-08-05 23:02:01 +04:00
}
/ *
initialise a server call object
* /
function servCallObj ( )
{
var c = new Object ( ) ;
c . add = _ _register _call ;
c . run = _ _run _call ;
c . calls = new Object ( ) ;
return c ;
}