1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
7 int osrfMathRun( osrfMethodDispatcher* );
11 osrfAppRegisterMethod( "opensrf.dbmath", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
12 osrfAppRegisterMethod( "opensrf.dbmath", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
13 osrfAppRegisterMethod( "opensrf.dbmath", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
14 osrfAppRegisterMethod( "opensrf.dbmath", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 );
22 int osrfMathRun( osrfMethodDispatcher* d ) {
25 OSRF_METHOD_VERIFY_DISPATCHER(d)
26 Verifies viability of the dispatcher components.
27 Checks for NULLness of key components.
28 Creates local variables :
29 session - the app session ( osrfAppSession* )
30 method - the method ( osrfMethod* )
31 params - the methd parameters ( jsonObject* )
32 request - the request id ( int ) */
34 OSRF_METHOD_VERIFY_DISPATCHER(d);
36 jsonObject* x = jsonObjectGetIndex(params, 0);
37 jsonObject* y = jsonObjectGetIndex(params, 1);
41 char* a = jsonObjectToSimpleString(x);
42 char* b = jsonObjectToSimpleString(y);
46 double i = strtod(a, NULL);
47 double j = strtod(b, NULL);
50 if(!strcmp(method->name, "add")) r = i + j;
51 if(!strcmp(method->name, "sub")) r = i - j;
52 if(!strcmp(method->name, "mult")) r = i * j;
53 if(!strcmp(method->name, "div")) r = i / j;
55 jsonObject* resp = jsonNewNumberObject(r);
56 osrfAppRequestRespond( session, request, resp );