added connection oriented statefull session handling to the server stack
[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 #include "opensrf/log.h"
5
6 #define MODULENAME "opensrf.math"
7
8 int osrfAppInitialize();
9 int osrfAppChildInit();
10 int osrfMathRun( osrfMethodContext* );
11
12
13 int osrfAppInitialize() {
14
15         osrfAppRegisterMethod( 
16                         MODULENAME,                             /* which application has this method */
17                         "add",                                  /* the name of the method */
18                         "osrfMathRun",                  /* the symbol that runs the method */
19                         "Adds two numbers",     /* description of the method */
20                         2,                                                      /* the minimum number of params required to run the method */
21                         0 );                                            /* method options, 0 for not special options */
22
23         osrfAppRegisterMethod( 
24                         MODULENAME, 
25                         "sub", 
26                         "osrfMathRun", 
27                         "Subtracts two numbers", 2, 0 );
28
29         osrfAppRegisterMethod( 
30                         MODULENAME, 
31                         "mult", 
32                         "osrfMathRun", 
33                         "Multiplies two numbers", 2, 0 );
34
35         osrfAppRegisterMethod( 
36                         MODULENAME, 
37                         "div", 
38                         "osrfMathRun", 
39                         "Divides two numbers", 2, 0 );
40
41         return 0;
42 }
43
44 int osrfAppChildInit() {
45         return 0;
46 }
47
48 int osrfMathRun( osrfMethodContext* ctx ) {
49
50         OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
51
52         /* collect the request params */
53         jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
54         jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
55
56         if( x && y ) {
57
58                 /* pull out the params as strings since they may be either
59                         strings or numbers depending on the client */
60                 char* a = jsonObjectToSimpleString(x);
61                 char* b = jsonObjectToSimpleString(y);
62
63                 if( a && b ) {
64
65                         osrfLogActivity( OSRF_LOG_MARK, "Running opensrf.math %s [ %s : %s ]", 
66                                         ctx->method->name, a, b );
67
68                         /* construct a new params object to send to dbmath */
69                         jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
70                         free(a); free(b);
71
72                         /* connect to db math */
73                         osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
74
75                         /* forcing an explicit connect allows us to talk to one worker backend
76                          * regardless of "stateful" config settings for the server 
77                          * This buys us nothing here since we're only sending one request...
78                          * */
79                         /*osrfAppSessionConnect(ses);*/
80
81                         /* dbmath uses the same method names that math does */
82                         int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
83                         osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
84
85                         if(omsg) {
86                                 /* return dbmath's response to the user */
87                                 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) ); 
88                                 osrfMessageFree(omsg);
89                                 osrfAppSessionFree(ses);
90                                 return 0;
91                         }
92
93                         osrfAppSessionFree(ses);
94                 }
95         }
96
97         return -1;
98 }
99
100
101