0b57ade6b191636e8b5d3a9d3852afc44a8e5a60
[OpenSRF.git] / src / c-apps / osrf_math.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.math"
7
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 void osrfAppChildExit();
11 int osrfMathRun( osrfMethodContext* );
12
13
14 int osrfAppInitialize() {
15
16         osrfAppRegisterMethod( 
17                         MODULENAME,                             /* which application has this method */
18                         "add",                                  /* the name of the method */
19                         "osrfMathRun",                  /* the symbol that runs the method */
20                         "Adds two numbers",     /* description of the method */
21                         2,                                                      /* the minimum number of params required to run the method */
22                         0 );                                            /* method options, 0 for not special options */
23
24         osrfAppRegisterMethod( 
25                         MODULENAME, 
26                         "sub", 
27                         "osrfMathRun", 
28                         "Subtracts two numbers", 2, 0 );
29
30         osrfAppRegisterMethod( 
31                         MODULENAME, 
32                         "mult", 
33                         "osrfMathRun", 
34                         "Multiplies two numbers", 2, 0 );
35
36         osrfAppRegisterMethod( 
37                         MODULENAME, 
38                         "div", 
39                         "osrfMathRun", 
40                         "Divides two numbers", 2, 0 );
41
42         return 0;
43 }
44
45 /* called when this process is just coming into existence */
46 int osrfAppChildInit() {
47         return 0;
48 }
49
50 /* called when this process is about to exit */
51 void osrfAppChildExit() {
52    osrfLogDebug(OSRF_LOG_MARK, "Child is exiting...");
53 }
54
55
56 int osrfMathRun( osrfMethodContext* ctx ) {
57         if( osrfMethodVerifyContext( ctx ) ) {
58                 osrfLogError( OSRF_LOG_MARK,  "Invalid method context" );
59                 return -1;
60         }
61
62         /* collect the request params */
63         const jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
64         const jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
65
66         if( x && y ) {
67
68                 /* pull out the params as strings since they may be either
69                         strings or numbers depending on the client */
70                 char* a = jsonObjectToSimpleString(x);
71                 char* b = jsonObjectToSimpleString(y);
72
73                 if( a && b ) {
74
75                         osrfLogActivity( OSRF_LOG_MARK, "Running opensrf.math %s [ %s : %s ]", 
76                                         ctx->method->name, a, b );
77
78                         /* construct a new params object to send to dbmath */
79                         jsonObject* newParams = jsonParseFmt( "[ %s, %s ]", a, b );
80                         free(a); free(b);
81
82                         /* connect to db math */
83                         osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
84
85                         /* forcing an explicit connect allows us to talk to one worker backend
86                          * regardless of "stateful" config settings for the server 
87                          * This buys us nothing here since we're only sending one request...
88                          * */
89                         /*osrfAppSessionConnect(ses);*/
90
91                         /* dbmath uses the same method names that math does */
92                         int req_id = osrfAppSessionSendRequest( ses, newParams, ctx->method->name, 1 );
93                         osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
94                         jsonObjectFree(newParams);
95
96                         if(omsg) {
97                                 /* return dbmath's response to the user */
98                                 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) ); 
99                                 osrfMessageFree(omsg);
100                                 osrfAppSessionFree(ses);
101                                 return 0;
102                         }
103
104                         osrfAppSessionFree(ses);
105                 }
106                 else {
107                         if(a) free(a);
108                         if(b) free(b);
109                 }
110         }
111
112         return -1;
113 }
114
115
116