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 ) {
58 OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
60 /* collect the request params */
61 const jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
62 const jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
66 /* pull out the params as strings since they may be either
67 strings or numbers depending on the client */
68 char* a = jsonObjectToSimpleString(x);
69 char* b = jsonObjectToSimpleString(y);
73 osrfLogActivity( OSRF_LOG_MARK, "Running opensrf.math %s [ %s : %s ]",
74 ctx->method->name, a, b );
76 /* construct a new params object to send to dbmath */
77 jsonObject* newParams = jsonParseStringFmt( "[ %s, %s ]", a, b );
80 /* connect to db math */
81 osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
83 /* forcing an explicit connect allows us to talk to one worker backend
84 * regardless of "stateful" config settings for the server
85 * This buys us nothing here since we're only sending one request...
87 /*osrfAppSessionConnect(ses);*/
89 /* dbmath uses the same method names that math does */
90 int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
91 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
94 /* return dbmath's response to the user */
95 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) );
96 osrfMessageFree(omsg);
97 osrfAppSessionFree(ses);
101 osrfAppSessionFree(ses);