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