]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/c-apps/osrf_math.c
math server (really dbmath code)
[OpenSRF.git] / src / c-apps / osrf_math.c
1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4
5 int initialize();
6 int childInit();
7 int osrfMathRun( osrfMethodDispatcher* );
8
9
10 int initialize() {
11         osrfAppRegisterMethod( "opensrf.math", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 );
12         osrfAppRegisterMethod( "opensrf.math", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 );
13         osrfAppRegisterMethod( "opensrf.math", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 );
14         osrfAppRegisterMethod( "opensrf.math", "div", "osrfMathRun", "send 2 numbers and I'll subtract them", 2 );
15         return 0;
16 }
17
18 int childInit() {
19         return 0;
20 }
21
22 int osrfMathRun( osrfMethodDispatcher* d ) {
23
24         /*
25                 OSRF_METHOD_VERIFY_DISPATCHER(d)        
26                 Verifies viability of the dispatcher components.
27                 Checks for NULLness of key components.
28                 Creates local variables :
29                 session - the app session ( osrfAppSession* )
30                 method - the method ( osrfMethod* )
31                 params - the methd parameters ( jsonObject* )
32                 request - the request id ( int ) */
33
34         OSRF_METHOD_VERIFY_DISPATCHER(d);       
35
36         jsonObject* x = jsonObjectGetIndex(params, 0);
37         jsonObject* y = jsonObjectGetIndex(params, 1);
38
39         if( x && y ) {
40
41                 char* a = jsonObjectToSimpleString(x);
42                 char* b = jsonObjectToSimpleString(y);
43
44                 if( a && b ) {
45
46                         double i = strtod(a, NULL);
47                         double j = strtod(b, NULL);
48                         double r = 0;
49
50                         if(!strcmp(method->name, "add"))                r = i + j;
51                         if(!strcmp(method->name, "sub"))                r = i - j;
52                         if(!strcmp(method->name, "mult"))       r = i * j;
53                         if(!strcmp(method->name, "div"))                r = i / j;
54
55                         jsonObject* resp = jsonNewNumberObject(r);
56                         osrfAppRequestRespond( session, request, resp );
57                         jsonObjectFree(resp);
58
59                         free(a); free(b);
60                         return 0;
61                 }
62         }
63
64         return -1;
65 }
66
67
68