1 #include <opensrf/osrf_app_session.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_json.h>
4 #include <opensrf/log.h>
6 #define MODULENAME "opensrf.math"
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 void osrfAppChildExit();
11 int osrfMathRun( osrfMethodContext* );
14 int osrfAppInitialize() {
16 osrfAppRegisterMethod(
17 MODULENAME, /* which application has this method */
18 "add", /* the name of the method */
19 "osrfMathRun", /* the symbol that runs the method */
20 "Adds two numbers", /* description of the method */
21 2, /* the minimum number of params required to run the method */
22 0 ); /* method options, 0 for not special options */
24 osrfAppRegisterMethod(
28 "Subtracts two numbers", 2, 0 );
30 osrfAppRegisterMethod(
34 "Multiplies two numbers", 2, 0 );
36 osrfAppRegisterMethod(
40 "Divides two numbers", 2, 0 );
45 /* called when this process is just coming into existence */
46 int osrfAppChildInit() {
50 /* called when this process is about to exit */
51 void osrfAppChildExit() {
52 osrfLogDebug(OSRF_LOG_MARK, "Child is exiting...");
56 int osrfMathRun( osrfMethodContext* ctx ) {
57 if( osrfMethodVerifyContext( ctx ) ) {
58 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
62 /* collect the request params */
63 const jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
64 const jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
68 /* pull out the params as strings since they may be either
69 strings or numbers depending on the client */
70 char* a = jsonObjectToSimpleString(x);
71 char* b = jsonObjectToSimpleString(y);
75 osrfLogActivity( OSRF_LOG_MARK, "Running opensrf.math %s [ %s : %s ]",
76 ctx->method->name, a, b );
78 /* construct a new params object to send to dbmath */
79 jsonObject* newParams = jsonParseFmt( "[ %s, %s ]", a, b );
82 /* connect to db math */
83 osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
85 /* forcing an explicit connect allows us to talk to one worker backend
86 * regardless of "stateful" config settings for the server
87 * This buys us nothing here since we're only sending one request...
89 /*osrfAppSessionConnect(ses);*/
91 /* dbmath uses the same method names that math does */
92 int req_id = osrfAppSessionSendRequest( ses, newParams, ctx->method->name, 1 );
93 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
94 jsonObjectFree(newParams);
97 /* return dbmath's response to the user */
98 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) );
99 osrfMessageFree(omsg);
100 osrfAppSessionFree(ses);
104 osrfAppSessionFree(ses);