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.dbmath"
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 int osrfMathRun( osrfMethodContext* );
13 int osrfAppInitialize() {
15 osrfAppRegisterMethod(
19 "Addss two numbers", 2, 0 );
21 osrfAppRegisterMethod(
25 "Subtracts two numbers", 2, 0 );
27 osrfAppRegisterMethod(
31 "Multiplies two numbers", 2, 0 );
33 osrfAppRegisterMethod(
37 "Divides two numbers", 2, 0 );
42 int osrfAppChildInit() {
46 int osrfMathRun( osrfMethodContext* ctx ) {
48 OSRF_METHOD_VERIFY_CONTEXT(ctx);
50 const jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
51 const jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
55 char* a = jsonObjectToSimpleString(x);
56 char* b = jsonObjectToSimpleString(y);
60 double i = strtod(a, NULL);
61 double j = strtod(b, NULL);
64 if(!strcmp(ctx->method->name, "add")) r = i + j;
65 if(!strcmp(ctx->method->name, "sub")) r = i - j;
66 if(!strcmp(ctx->method->name, "mult")) r = i * j;
67 if(!strcmp(ctx->method->name, "div")) r = i / j;
69 jsonObject* resp = jsonNewNumberObject(r);
70 osrfAppRespondComplete( ctx, resp );