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() {
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 "( num1, num2 )", /* description of the method params */
21 2, /* the minimum number of params required to run the method */
22 0 ); /* streaming method? yes / no */
24 osrfAppRegisterMethod(
28 "Subtracts two numbers",
29 "( num1, num2 )", 2, 0 );
31 osrfAppRegisterMethod(
35 "Multiplies two numbers",
36 "( num1, num2 )", 2, 0 );
38 osrfAppRegisterMethod(
42 "Divides two numbers",
43 "( num1, num2 )", 2, 0 );
48 int osrfAppChildInit() {
52 int osrfMathRun( osrfMethodContext* ctx ) {
54 OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
56 osrfLog( OSRF_DEBUG, "Running opensrf.math %s", ctx->method->name );
58 /* collect the request params */
59 jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
60 jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
64 /* pull out the params as strings since they may be either
65 strings or numbers depending on the client */
66 char* a = jsonObjectToSimpleString(x);
67 char* b = jsonObjectToSimpleString(y);
71 /* construct a new params object to send to dbmath */
72 jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
75 /* connect to db math */
76 osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
78 /* dbmath uses the same method names that math does */
79 int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
80 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
83 /* return dbmath's response to the user */
84 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) );
85 osrfMessageFree(omsg);
86 osrfAppSessionFree(ses);
90 osrfAppSessionFree(ses);