providing option to connect to memcache at general connect time
[OpenSRF.git] / src / c-apps / osrf_dbmath.c
1 #include <opensrf/osrf_app_session.h>
2 #include <opensrf/osrf_application.h>
3 #include <opensrf/osrf_json.h>
4 #include <opensrf/log.h>
5
6 #define MODULENAME "opensrf.dbmath"
7
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 int osrfMathRun( osrfMethodContext* );
11
12
13 int osrfAppInitialize() {
14
15         osrfAppRegisterMethod( 
16                         MODULENAME, 
17                         "add", 
18                         "osrfMathRun", 
19                         "Addss two numbers", 2, 0 );
20
21         osrfAppRegisterMethod( 
22                         MODULENAME, 
23                         "sub", 
24                         "osrfMathRun", 
25                         "Subtracts two numbers", 2, 0 );
26
27         osrfAppRegisterMethod( 
28                         MODULENAME, 
29                         "mult", 
30                         "osrfMathRun", 
31                         "Multiplies two numbers", 2, 0 );
32
33         osrfAppRegisterMethod( 
34                         MODULENAME, 
35                         "div", 
36                         "osrfMathRun", 
37                         "Divides two numbers", 2, 0 );
38
39         return 0;
40 }
41
42 int osrfAppChildInit() {
43         return 0;
44 }
45
46 int osrfMathRun( osrfMethodContext* ctx ) {
47
48         OSRF_METHOD_VERIFY_CONTEXT(ctx);        
49
50         const jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
51         const jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
52
53         if( x && y ) {
54
55                 char* a = jsonObjectToSimpleString(x);
56                 char* b = jsonObjectToSimpleString(y);
57
58                 if( a && b ) {
59
60                         double i = strtod(a, NULL);
61                         double j = strtod(b, NULL);
62                         double r = 0;
63
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;
68
69                         jsonObject* resp = jsonNewNumberObject(r);
70                         osrfAppRespondComplete( ctx, resp );
71                         jsonObjectFree(resp);
72
73                         free(a); free(b);
74                         return 0;
75                 }
76         }
77
78         return -1;
79 }
80
81
82