some api changes
[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 #include "opensrf/osrf_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         osrfLogInit(MODULENAME);
15
16         osrfAppRegisterMethod( 
17                         MODULENAME, 
18                         "add", 
19                         "osrfMathRun", 
20                         "Addss two numbers",
21                         "( num1, num2 )", 2 );
22
23         osrfAppRegisterMethod( 
24                         MODULENAME, 
25                         "sub", 
26                         "osrfMathRun", 
27                         "Subtracts two numbers",
28                         "( num1, num2 )", 2 );
29
30         osrfAppRegisterMethod( 
31                         MODULENAME, 
32                         "mult", 
33                         "osrfMathRun", 
34                         "Multiplies two numbers",
35                         "( num1, num2 )", 2 );
36
37         osrfAppRegisterMethod( 
38                         MODULENAME, 
39                         "div", 
40                         "osrfMathRun", 
41                         "Divides two numbers",
42                         "( num1, num2 )", 2 );
43
44         return 0;
45 }
46
47 int osrfAppChildInit() {
48         return 0;
49 }
50
51 int osrfMathRun( osrfMethodContext* ctx ) {
52
53         OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */
54
55         osrfLog( OSRF_DEBUG, "Running opensrf.math %s", ctx->method->name );
56
57         /* collect the request params */
58         jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
59         jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
60
61         if( x && y ) {
62
63                 /* pull out the params as strings since they may be either
64                         strings or numbers depending on the client */
65                 char* a = jsonObjectToSimpleString(x);
66                 char* b = jsonObjectToSimpleString(y);
67
68                 if( a && b ) {
69
70                         /* construct a new params object to send to dbmath */
71                         jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
72                         free(a); free(b);
73
74                         /* connect to db math */
75                         osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
76
77                         /* dbmath uses the same method names that math does */
78                         int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
79                         osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
80
81                         if(omsg) {
82                                 /* return dbmath's response to the user */
83                                 osrfAppRequestRespondComplete( ctx->session, ctx->request, osrfMessageGetResult(omsg) ); 
84                                 osrfMessageFree(omsg);
85                                 osrfAppSessionFree(ses);
86                                 return 0;
87                         }
88
89                         osrfAppSessionFree(ses);
90                 }
91         }
92
93         return -1;
94 }
95
96
97