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 int osrfAppInitialize();
7 int osrfAppChildInit();
8 int osrfMathRun( osrfMethodContext* );
11 int osrfAppInitialize() {
12 osrfLogInit("opensrf.dbmath");
13 osrfAppRegisterMethod( "opensrf.dbmath", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
14 osrfAppRegisterMethod( "opensrf.dbmath", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
15 osrfAppRegisterMethod( "opensrf.dbmath", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
16 osrfAppRegisterMethod( "opensrf.dbmath", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 );
20 int osrfAppChildInit() {
24 int osrfMathRun( osrfMethodContext* ctx ) {
26 OSRF_METHOD_VERIFY_CONTEXT(ctx);
28 jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
29 jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
33 char* a = jsonObjectToSimpleString(x);
34 char* b = jsonObjectToSimpleString(y);
38 double i = strtod(a, NULL);
39 double j = strtod(b, NULL);
42 if(!strcmp(ctx->method->name, "add")) r = i + j;
43 if(!strcmp(ctx->method->name, "sub")) r = i - j;
44 if(!strcmp(ctx->method->name, "mult")) r = i * j;
45 if(!strcmp(ctx->method->name, "div")) r = i / j;
47 jsonObject* resp = jsonNewNumberObject(r);
48 osrfAppRequestRespondComplete( ctx->session, ctx->request, resp );