1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4 #include "opensrf/log.h"
6 #define MODULENAME "opensrf.math"
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 int osrfMathRun( osrfMethodContext* );
13 int osrfAppInitialize() {
15 osrfAppRegisterMethod(
16 MODULENAME, /* which application has this method */
17 "add", /* the name of the method */
18 "osrfMathRun", /* the symbol that runs the method */
19 "Adds two numbers", /* description of the method */
20 2, /* the minimum number of params required to run the method */
21 0 ); /* method options, 0 for not special options */
23 osrfAppRegisterMethod(
27 "Subtracts two numbers", 2, 0 );
29 osrfAppRegisterMethod(
33 "Multiplies two numbers", 2, 0 );
35 osrfAppRegisterMethod(
39 "Divides two numbers", 2, 0 );
44 int osrfAppChildInit() {
48 int osrfMathRun( osrfMethodContext* ctx ) {
50 OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
52 /* collect the request params */
53 jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
54 jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
58 /* pull out the params as strings since they may be either
59 strings or numbers depending on the client */
60 char* a = jsonObjectToSimpleString(x);
61 char* b = jsonObjectToSimpleString(y);
65 osrfLogActivity( OSRF_LOG_MARK, "Running opensrf.math %s [ %s : %s ]",
66 ctx->method->name, a, b );
68 /* construct a new params object to send to dbmath */
69 jsonObject* newParams = jsonParseStringFmt( "[ %s, %s ]", a, b );
72 /* connect to db math */
73 osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
75 /* forcing an explicit connect allows us to talk to one worker backend
76 * regardless of "stateful" config settings for the server
77 * This buys us nothing here since we're only sending one request...
79 /*osrfAppSessionConnect(ses);*/
81 /* dbmath uses the same method names that math does */
82 int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
83 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
86 /* return dbmath's response to the user */
87 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) );
88 osrfMessageFree(omsg);
89 osrfAppSessionFree(ses);
93 osrfAppSessionFree(ses);