added dbmath
[working/Evergreen.git] / OpenSRF / 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         OSRF_METHOD_VERIFY_DISPATCHER(d);       
25
26         jsonObject* x = jsonObjectGetIndex(params, 0);
27         jsonObject* y = jsonObjectGetIndex(params, 1);
28
29         if( x && y ) {
30
31                 char* a = jsonObjectToSimpleString(x);
32                 char* b = jsonObjectToSimpleString(y);
33
34                 if( a && b ) {
35
36                         jsonObject* new_params = jsonParseString("[]");
37                         jsonObjectPush(new_params, jsonNewObject(a));
38                         jsonObjectPush(new_params, jsonNewObject(b));
39
40                         free(a); free(b);
41
42                         osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
43                         int req_id = osrfAppSessionMakeRequest( ses, new_params, method->name, 1, NULL );
44                         osrf_message* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
45
46                         if(omsg) {
47                                 osrfAppRequestRespond( session, request, omsg->_result_content ); 
48                                 osrf_message_free(omsg);
49                                 return 0;
50                         }
51                 }
52         }
53
54         return -1;
55 }
56
57
58