]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/c-apps/osrf_math.c
slight C api change, method options are now passed as a single OR'ed group of option...
[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/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
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         osrfLog( OSRF_DEBUG, "Running opensrf.math %s", ctx->method->name );
53
54         /* collect the request params */
55         jsonObject* x = jsonObjectGetIndex(ctx->params, 0);
56         jsonObject* y = jsonObjectGetIndex(ctx->params, 1);
57
58         if( x && y ) {
59
60                 /* pull out the params as strings since they may be either
61                         strings or numbers depending on the client */
62                 char* a = jsonObjectToSimpleString(x);
63                 char* b = jsonObjectToSimpleString(y);
64
65                 if( a && b ) {
66
67                         /* construct a new params object to send to dbmath */
68                         jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b );
69                         free(a); free(b);
70
71                         /* connect to db math */
72                         osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath");
73
74                         /* dbmath uses the same method names that math does */
75                         int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL );
76                         osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 );
77
78                         if(omsg) {
79                                 /* return dbmath's response to the user */
80                                 osrfAppRespondComplete( ctx, osrfMessageGetResult(omsg) ); 
81                                 osrfMessageFree(omsg);
82                                 osrfAppSessionFree(ses);
83                                 return 0;
84                         }
85
86                         osrfAppSessionFree(ses);
87                 }
88         }
89
90         return -1;
91 }
92
93
94