1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4 #include "opensrf/osrf_log.h"
6 int osrfAppInitialize();
7 int osrfAppChildInit();
8 int osrfMathRun( osrfMethodContext* );
11 int osrfAppInitialize() {
12 osrfLogInit("opensrf.math");
13 /* tell the server about the methods we handle */
14 osrfAppRegisterMethod( "opensrf.math", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
15 osrfAppRegisterMethod( "opensrf.math", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
16 osrfAppRegisterMethod( "opensrf.math", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
17 osrfAppRegisterMethod( "opensrf.math", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 );
21 int osrfAppChildInit() {
25 int osrfMathRun( osrfMethodContext* ctx ) {
27 OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
29 osrfLog( OSRF_DEBUG, "Running opensrf.math %s", ctx->method->name );
31 /* collect the request params */
32 jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
33 jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
37 /* pull out the params as strings since they may be either
38 strings or numbers depending on the client */
39 char* a = jsonObjectToSimpleString(x);
40 char* b = jsonObjectToSimpleString(y);
44 /* construct a new params object to send to dbmath */
45 jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
48 /* connect to db math */
49 osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
51 /* dbmath uses the same method names that math does */
52 int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
53 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
56 /* return dbmath's response to the user */
57 osrfAppRequestRespondComplete( ctx->session, ctx->request, osrfMessageGetResult(omsg) );
58 osrfMessageFree(omsg);
59 osrfAppSessionFree(ses);
63 osrfAppSessionFree(ses);