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( "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 = jsonParseString( "[ %s, %s ]", a, b );
72 /* connect to db math */
73 osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
75 /* dbmath uses the same method names that math does */
76 int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
77 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
80 /* return dbmath's response to the user */
81 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) );
82 osrfMessageFree(omsg);
83 osrfAppSessionFree(ses);
87 osrfAppSessionFree(ses);