]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/c-apps/osrf_version.c
2936607bee775b8ba919536a98b3d92e8b83e87d
[working/Evergreen.git] / OpenSRF / src / c-apps / osrf_version.c
1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4 #include "opensrf/utils.h"
5 #include "opensrf/osrf_log.h"
6
7 #define OSRF_VERSION_CACHE_TIME 300
8
9 int osrfAppInitialize();
10 int osrfAppChildInit();
11 int osrfVersion( osrfMethodContext* );
12
13
14 int osrfAppInitialize() {
15         osrfLogInit("opensrf.version");
16
17         osrfAppRegisterMethod( 
18                         "opensrf.version", 
19                         "opensrf.version.verify", 
20                         "osrfVersion", 
21                         "The data for a service/method/params combination will be retrieved "
22                         "from the necessary server and the MD5 sum of the total values received "
23                         "will be returned", 
24                         "( serviceName, methodName, [param1, ...] )", 2 );
25         
26         return 0;
27 }
28
29 int osrfAppChildInit() {
30         return 0;
31 }
32
33 int osrfVersion( osrfMethodContext* ctx ) {
34
35         OSRF_METHOD_VERIFY_CONTEXT(ctx); 
36
37         /* First, see if the data is in the cache */
38         char* json = jsonObjectToJSON(ctx->params);
39         char* paramsmd5 = md5sum(json);
40         char* cachedmd5 = osrfCacheGetString(paramsmd5);
41         free(json); 
42
43         if( cachedmd5 ) {
44                 osrfLog( OSRF_DEBUG, "Found %s object in cache, returning....", cachedmd5 );
45                 jsonObject* resp = jsonNewObject(cachedmd5);
46                 osrfAppRequestRespondComplete( ctx->session, ctx->request, resp  );
47                 jsonObjectFree(resp);
48                 free(paramsmd5);
49                 free(cachedmd5);
50                 return 0;
51         }
52
53         jsonObject* serv = jsonObjectGetIndex(ctx->params, 0);
54         jsonObject* meth = jsonObjectGetIndex(ctx->params, 1);
55         char* service = jsonObjectGetString(serv);
56         char* methd = jsonObjectGetString(meth);
57
58         if( service && methd ) {
59                 /* shove the additional params into an array */
60                 jsonObject* tmpArray = jsonNewObject(NULL);
61                 int i;
62                 for( i = 2; i != ctx->params->size; i++ ) 
63                         jsonObjectPush( tmpArray, jsonObjectClone(jsonObjectGetIndex(ctx->params, i)));
64
65                 osrfAppSession* ses = osrfAppSessionClientInit(service);
66                 int reqid = osrfAppSessionMakeRequest( ses, tmpArray, methd, 1, NULL );
67                 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, reqid, 60 );
68                 jsonObjectFree(tmpArray);
69
70                 if( omsg ) {
71
72                         jsonObject* result = osrfMessageGetResult( omsg );
73                         char* resultjson = jsonObjectToJSON(result);
74                         char* resultmd5 = md5sum(resultjson);
75                         free(resultjson);
76                         osrfMessageFree(omsg);
77
78                         if( resultmd5 ) {
79                                 jsonObject* resp = jsonNewObject(resultmd5);
80                                 osrfAppRequestRespondComplete( ctx->session, ctx->request, resp );
81                                 jsonObjectFree(resp);
82                                 osrfAppSessionFree(ses);
83                                 osrfLog(OSRF_DEBUG, "Found version string %s, caching and returning...", resultmd5 );
84                                 osrfCachePutString( paramsmd5, resultmd5, OSRF_VERSION_CACHE_TIME );
85                                 free(resultmd5);
86                                 free(paramsmd5);
87                                 return 0;
88                         } 
89                 }
90                 osrfAppSessionFree(ses);
91         }
92
93         free(paramsmd5);
94
95         return -1;
96 }
97
98
99