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 #define MODULENAME "opensrf.math"
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 int osrfMathRun( osrfMethodContext* );
13 int osrfAppInitialize() {
14 osrfLogInit(MODULENAME);
16 osrfAppRegisterMethod(
21 "( num1, num2 )", 2 );
23 osrfAppRegisterMethod(
27 "Subtracts two numbers",
28 "( num1, num2 )", 2 );
30 osrfAppRegisterMethod(
34 "Multiplies two numbers",
35 "( num1, num2 )", 2 );
37 osrfAppRegisterMethod(
41 "Divides two numbers",
42 "( num1, num2 )", 2 );
47 int osrfAppChildInit() {
51 int osrfMathRun( osrfMethodContext* ctx ) {
53 OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
55 osrfLog( OSRF_DEBUG, "Running opensrf.math %s", ctx->method->name );
57 /* collect the request params */
58 jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
59 jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
63 /* pull out the params as strings since they may be either
64 strings or numbers depending on the client */
65 char* a = jsonObjectToSimpleString(x);
66 char* b = jsonObjectToSimpleString(y);
70 /* construct a new params object to send to dbmath */
71 jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
74 /* connect to db math */
75 osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
77 /* dbmath uses the same method names that math does */
78 int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
79 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
82 /* return dbmath's response to the user */
83 osrfAppRequestRespondComplete( ctx->session, ctx->request, osrfMessageGetResult(omsg) );
84 osrfMessageFree(omsg);
85 osrfAppSessionFree(ses);
89 osrfAppSessionFree(ses);