]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/c-apps/osrf_math.c
7c3cf29460b09095de6cd1bc7c3e0836c1c2a8d3
[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
58         OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
59
60         /* collect the request params */
61         jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
62         jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
63
64         if( x && y ) {
65
66                 /* pull out the params as strings since they may be either
67                         strings or numbers depending on the client */
68                 char* a = jsonObjectToSimpleString(x);
69                 char* b = jsonObjectToSimpleString(y);
70
71                 if( a && b ) {
72
73                         osrfLogActivity( OSRF_LOG_MARK, "Running opensrf.math %s [ %s : %s ]", 
74                                         ctx->method->name, a, b );
75
76                         /* construct a new params object to send to dbmath */
77                         jsonObject* newParams = jsonParseStringFmt( "[ %s, %s ]", a, b );
78                         free(a); free(b);
79
80                         /* connect to db math */
81                         osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
82
83                         /* forcing an explicit connect allows us to talk to one worker backend
84                          * regardless of "stateful" config settings for the server 
85                          * This buys us nothing here since we're only sending one request...
86                          * */
87                         /*osrfAppSessionConnect(ses);*/
88
89                         /* dbmath uses the same method names that math does */
90                         int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
91                         osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
92
93                         if(omsg) {
94                                 /* return dbmath's response to the user */
95                                 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) ); 
96                                 osrfMessageFree(omsg);
97                                 osrfAppSessionFree(ses);
98                                 return 0;
99                         }
100
101                         osrfAppSessionFree(ses);
102                 }
103         }
104
105         return -1;
106 }
107
108
109