1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
5 int osrfAppInitialize();
6 int osrfAppChildInit();
7 int osrfMathRun( osrfMethodContext* );
10 int osrfAppInitialize() {
12 /* tell the server about the methods we handle */
13 osrfAppRegisterMethod( "opensrf.math", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
14 osrfAppRegisterMethod( "opensrf.math", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
15 osrfAppRegisterMethod( "opensrf.math", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
16 osrfAppRegisterMethod( "opensrf.math", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 );
20 int osrfAppChildInit() {
24 int osrfMathRun( osrfMethodContext* c ) {
26 OSRF_METHOD_VERIFY_CONTEXT(c); /* see osrf_application.h */
28 /* collect the request params */
29 jsonObject* x = jsonObjectGetIndex(params, 0);
30 jsonObject* y = jsonObjectGetIndex(params, 1);
34 /* pull out the params as strings since they may be either
35 strings or numbers depending on the client */
36 char* a = jsonObjectToSimpleString(x);
37 char* b = jsonObjectToSimpleString(y);
41 /* construct a new params object to send to dbmath */
42 jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
45 /* connect to db math */
46 osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
48 /* dbmath uses the same method names that math does */
49 int req_id = osrfAppSessionMakeRequest( ses, newParams, method->name, 1, NULL );
50 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
54 /* return dbmath's response to the user */
55 osrfAppRequestRespondComplete( session, request, osrfMessageGetResult(omsg) );
56 osrfMessageFree(omsg);