]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_application.c
572221469ace8b70ce0cc44e5dc9276ce2a10f5f
[OpenSRF.git] / src / libstack / osrf_application.c
1 #include "osrf_application.h"
2 #include "objson/object.h"
3
4 //osrfApplication* __osrfAppList = NULL; 
5
6 osrfHash* __osrfAppHash = NULL;
7
8
9 int osrfAppRegisterApplication( char* appName, char* soFile ) {
10         if(!appName || ! soFile) return -1;
11         char* error;
12
13         if(!__osrfAppHash) __osrfAppHash = osrfNewHash();
14
15         osrfLogInfo( OSRF_LOG_MARK, "Registering application %s with file %s", appName, soFile );
16
17         osrfApplication* app = safe_malloc(sizeof(osrfApplication));
18         app->handle = dlopen (soFile, RTLD_NOW);
19
20         if(!app->handle) {
21                 osrfLogWarning( OSRF_LOG_MARK, "Failed to dlopen library file %s: %s", soFile, dlerror() );
22                 dlerror(); /* clear the error */
23                 free(app);
24                 return -1;
25         }
26
27         app->methods = osrfNewHash();
28         osrfHashSet( __osrfAppHash, app, appName );
29
30         /* see if we can run the initialize method */
31         int (*init) (void);
32         *(void **) (&init) = dlsym(app->handle, "osrfAppInitialize");
33
34         if( (error = dlerror()) != NULL ) {
35                 osrfLogWarning( OSRF_LOG_MARK, 
36                         "! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error );
37
38         } else {
39
40                 /* run the method */
41                 int ret;
42                 if( (ret = (*init)()) ) {
43                         osrfLogWarning( OSRF_LOG_MARK, "Application %s returned non-zero value from "
44                                         "'osrfAppInitialize', not registering...", appName );
45                         //free(app->name); /* need a method to remove an application from the list */
46                         //free(app);
47                         return ret;
48                 }
49         }
50
51         __osrfAppRegisterSysMethods(appName);
52
53         osrfLogInfo( OSRF_LOG_MARK, "Application %s registered successfully", appName );
54
55         osrfLogSetAppname(appName);
56
57         return 0;
58 }
59
60 int osrfAppRunChildInit(char* appname) {
61         osrfApplication* app = _osrfAppFindApplication(appname);
62         if(!app) return -1;
63
64         char* error;
65         int ret;
66         int (*childInit) (void);
67
68         *(void**) (&childInit) = dlsym(app->handle, "osrfAppChildInit");
69
70         if( (error = dlerror()) != NULL ) {
71                 osrfLogInfo( OSRF_LOG_MARK, "No child init defined for app %s : %s", appname, error);
72                 return 0;
73         }
74
75         if( (ret = (*childInit)()) ) {
76                 osrfLogError(OSRF_LOG_MARK, "App %s child init failed", appname);
77                 return -1;
78         }
79
80         osrfLogInfo(OSRF_LOG_MARK, "%s child init succeeded", appname);
81         return 0;
82 }
83
84
85 int osrfAppRegisterMethod( char* appName, char* methodName, 
86                 char* symbolName, char* notes, int argc, int options ) {
87
88         return osrfAppRegisterExtendedMethod(
89                         appName,
90                         methodName,
91                         symbolName,
92                         notes,
93                         argc,
94                         options,
95                         NULL
96         );
97
98 }
99
100 int osrfAppRegisterExtendedMethod( char* appName, char* methodName, 
101                 char* symbolName, char* notes, int argc, int options, void * user_data ) {
102
103         if( !appName || ! methodName  ) return -1;
104
105         osrfApplication* app = _osrfAppFindApplication(appName);
106         if(!app) {
107                 osrfLogWarning( OSRF_LOG_MARK, "Unable to locate application %s", appName );
108                 return -1;
109         }
110
111         osrfLogDebug( OSRF_LOG_MARK, "Registering method %s for app %s", methodName, appName );
112
113         osrfMethod* method = _osrfAppBuildMethod(
114                 methodName, symbolName, notes, argc, options, user_data );              
115         method->options = options;
116
117         /* plug the method into the list of methods */
118         osrfHashSet( app->methods, method, method->name );
119
120         if( options & OSRF_METHOD_STREAMING ) { /* build the atomic counterpart */
121                 int newops = options | OSRF_METHOD_ATOMIC;
122                 osrfMethod* atomicMethod = _osrfAppBuildMethod(
123                         methodName, symbolName, notes, argc, newops, NULL );            
124                 osrfHashSet( app->methods, atomicMethod, atomicMethod->name );
125         }
126
127         return 0;
128 }
129
130
131
132 osrfMethod* _osrfAppBuildMethod( char* methodName, 
133         char* symbolName, char* notes, int argc, int options, void* user_data ) {
134
135         osrfMethod* method                                      = safe_malloc(sizeof(osrfMethod));
136
137         if(methodName) method->name             = strdup(methodName);
138         if(symbolName) method->symbol           = strdup(symbolName);
139         if(notes) method->notes                         = strdup(notes);
140         if(user_data) method->userData                  = user_data;
141
142         method->argc                                                    = argc;
143         method->options                                         = options;
144
145         if(options & OSRF_METHOD_ATOMIC) { /* add ".atomic" to the end of the name */
146                 char mb[strlen(method->name) + 8];
147                 sprintf(mb, "%s.atomic", method->name);
148                 free(method->name);
149                 method->name = strdup(mb);
150                 method->options |= OSRF_METHOD_STREAMING;
151         }
152
153         return method;
154 }
155
156
157 int __osrfAppRegisterSysMethods( char* app ) {
158
159         osrfAppRegisterMethod( 
160                         app, OSRF_SYSMETHOD_INTROSPECT, NULL, 
161                         "Return a list of methods whose names have the same initial "
162                         "substring as that of the provided method name PARAMS( methodNameSubstring )", 
163                         1, OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
164
165         osrfAppRegisterMethod( 
166                         app, OSRF_SYSMETHOD_INTROSPECT_ALL, NULL, 
167                         "Returns a complete list of methods. PARAMS()", 0, 
168                         OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
169
170         osrfAppRegisterMethod( 
171                         app, OSRF_SYSMETHOD_ECHO, NULL, 
172                         "Echos all data sent to the server back to the client. PARAMS([a, b, ...])", 0, 
173                         OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
174
175         return 0;
176 }
177
178 osrfApplication* _osrfAppFindApplication( char* name ) {
179         if(!name) return NULL;
180         return (osrfApplication*) osrfHashGet(__osrfAppHash, name);
181 }
182
183 osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ) {
184         if(!app || ! methodName) return NULL;
185         return (osrfMethod*) osrfHashGet( app->methods, methodName );
186 }
187
188 osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ) {
189         if(!appName || ! methodName) return NULL;
190         return __osrfAppFindMethod( _osrfAppFindApplication(appName), methodName );
191 }
192
193
194 int osrfAppRunMethod( char* appName, char* methodName, 
195                 osrfAppSession* ses, int reqId, jsonObject* params ) {
196
197         if( !(appName && methodName && ses) ) return -1;
198
199         char* error;
200         osrfApplication* app;
201         osrfMethod* method;
202         osrfMethodContext context;
203
204         context.session = ses;
205         context.params = params;
206         context.request = reqId;
207         context.responses = NULL;
208
209         /* this is the method we're gonna run */
210         int (*meth) (osrfMethodContext*);       
211
212         osrfLogInfo( OSRF_LOG_MARK, "Running method [%s] for app [%s] with request id %d and "
213                         "thread trace %s", methodName, appName, reqId, ses->session_id );
214
215         if( !(app = _osrfAppFindApplication(appName)) )
216                 return osrfAppRequestRespondException( ses, 
217                                 reqId, "Application not found: %s", appName );
218         
219         if( !(method = __osrfAppFindMethod( app, methodName )) ) 
220                 return osrfAppRequestRespondException( ses, reqId, 
221                                 "Method [%s] not found for service %s", methodName, appName );
222
223         context.method = method;
224
225         #ifdef OSRF_STRICT_PARAMS
226         if( method->argc > 0 ) {
227                 if(!params || params->type != JSON_ARRAY || params->size < method->argc )
228                         return osrfAppRequestRespondException( ses, reqId, 
229                                 "Not enough params for method %s / service %s", methodName, appName );
230         }
231         #endif
232
233         int retcode = 0;
234
235         if( method->options & OSRF_METHOD_SYSTEM ) {
236                 retcode = __osrfAppRunSystemMethod(&context);
237
238         } else {
239
240                 /* open and now run the method */
241                 *(void **) (&meth) = dlsym(app->handle, method->symbol);
242
243                 if( (error = dlerror()) != NULL ) {
244                         return osrfAppRequestRespondException( ses, reqId, 
245                                 "Unable to execute method [%s]  for service %s", methodName, appName );
246                 }
247
248                 retcode = (*meth) (&context);
249         }
250
251         if(retcode < 0) 
252                 return osrfAppRequestRespondException( 
253                                 ses, reqId, "An unknown server error occurred" );
254
255         return __osrfAppPostProcess( &context, retcode );
256
257 }
258
259
260 int osrfAppRespond( osrfMethodContext* ctx, jsonObject* data ) {
261         return _osrfAppRespond( ctx, data, 0 );
262 }
263
264 int osrfAppRespondComplete( osrfMethodContext* context, jsonObject* data ) {
265         return _osrfAppRespond( context, data, 1 );
266 }
267
268 int _osrfAppRespond( osrfMethodContext* ctx, jsonObject* data, int complete ) {
269         if(!(ctx && ctx->method)) return -1;
270
271         if( ctx->method->options & OSRF_METHOD_ATOMIC ) {
272                 osrfLogDebug( OSRF_LOG_MARK,   
273                         "Adding responses to stash for atomic method %s", ctx->method );
274
275                 if( ctx->responses == NULL )                                                                                            
276                         ctx->responses = jsonParseString("[]");                                                 
277                 jsonObjectPush( ctx->responses, jsonObjectClone(data) );        
278         }
279
280
281         if(     !(ctx->method->options & OSRF_METHOD_ATOMIC) && 
282                         !(ctx->method->options & OSRF_METHOD_CACHABLE) ) {
283
284                 if(complete) 
285                         osrfAppRequestRespondComplete( ctx->session, ctx->request, data );
286                 else
287                         osrfAppRequestRespond( ctx->session, ctx->request, data );
288                 return 0;
289         }
290
291         return 0;
292 }
293
294
295
296
297 int __osrfAppPostProcess( osrfMethodContext* ctx, int retcode ) {
298         if(!(ctx && ctx->method)) return -1;
299
300         osrfLogDebug( OSRF_LOG_MARK,  "Postprocessing method %s with retcode %d",
301                         ctx->method->name, retcode );
302
303         if(ctx->responses) { /* we have cached responses to return (no responses have been sent) */
304
305                 osrfAppRequestRespondComplete( ctx->session, ctx->request, ctx->responses );
306                 jsonObjectFree(ctx->responses);
307                 ctx->responses = NULL;
308
309         } else {
310
311                 if( retcode > 0 ) 
312                         osrfAppSessionStatus( ctx->session, OSRF_STATUS_COMPLETE,  
313                                         "osrfConnectStatus", ctx->request, "Request Complete" );
314         }
315
316         return 0;
317
318 }
319
320 int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, ... ) {
321         if(!ses) return -1;
322         if(!msg) msg = "";
323         VA_LIST_TO_STRING(msg);
324         osrfLogWarning( OSRF_LOG_MARK,  "Returning method exception with message: %s", VA_BUF );
325         osrfAppSessionStatus( ses, OSRF_STATUS_NOTFOUND, "osrfMethodException", request,  VA_BUF );
326         return 0;
327 }
328
329
330 static void __osrfAppSetIntrospectMethod( osrfMethodContext* ctx, osrfMethod* method, jsonObject* resp ) {
331         if(!(ctx && resp)) return;
332
333         jsonObjectSetKey(resp, "api_name",      jsonNewObject(method->name));
334         jsonObjectSetKey(resp, "method",                jsonNewObject(method->symbol));
335         jsonObjectSetKey(resp, "service",       jsonNewObject(ctx->session->remote_service));
336         jsonObjectSetKey(resp, "notes",         jsonNewObject(method->notes));
337         jsonObjectSetKey(resp, "argc",          jsonNewNumberObject(method->argc));
338
339         jsonObjectSetKey(resp, "sysmethod", 
340                         jsonNewNumberObject( (method->options & OSRF_METHOD_SYSTEM) ? 1 : 0 ));
341         jsonObjectSetKey(resp, "atomic",                
342                         jsonNewNumberObject( (method->options & OSRF_METHOD_ATOMIC) ? 1 : 0 ));
343         jsonObjectSetKey(resp, "cachable",      
344                         jsonNewNumberObject( (method->options & OSRF_METHOD_CACHABLE) ? 1 : 0 ));
345
346         jsonObjectSetClass(resp, "method");
347 }
348
349
350
351 int __osrfAppRunSystemMethod(osrfMethodContext* ctx) {
352         OSRF_METHOD_VERIFY_CONTEXT(ctx);
353
354         if(     !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL ) || 
355                         !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL_ATOMIC )) {
356
357                 return osrfAppIntrospectAll(ctx);
358         }
359
360
361         if(     !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT ) ||
362                         !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ATOMIC )) {
363
364                 return osrfAppIntrospect(ctx);
365         }
366
367         osrfAppRequestRespondException( ctx->session, 
368                         ctx->request, "System method implementation not found");
369
370         return 0;
371 }
372
373
374 int osrfAppIntrospect( osrfMethodContext* ctx ) {
375
376         jsonObject* resp = NULL;
377         char* methodSubstring = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
378         osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
379         int len = 0;
380
381         if(!methodSubstring) return 1; /* respond with no methods */
382
383         if(app) {
384
385                 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
386                 osrfMethod* method;
387
388                 while( (method = osrfHashIteratorNext(itr)) ) {
389                         if( (len = strlen(methodSubstring)) <= strlen(method->name) ) {
390                                 if( !strncmp( method->name, methodSubstring, len) ) {
391                                         resp = jsonNewObject(NULL);
392                                         __osrfAppSetIntrospectMethod( ctx, method, resp );
393                                         osrfAppRespond(ctx, resp);
394                                         jsonObjectFree(resp);
395                                 }
396                         }
397                 }
398                 osrfHashIteratorFree(itr);
399                 return 1;
400         }
401
402         return -1;
403
404 }
405
406
407 int osrfAppIntrospectAll( osrfMethodContext* ctx ) {
408         jsonObject* resp = NULL;
409         osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
410
411         if(app) {
412                 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
413                 osrfMethod* method;
414                 while( (method = osrfHashIteratorNext(itr)) ) {
415                         resp = jsonNewObject(NULL);
416                         __osrfAppSetIntrospectMethod( ctx, method, resp );
417                         osrfAppRespond(ctx, resp);
418                         jsonObjectFree(resp);
419                 }
420                 osrfHashIteratorFree(itr);
421                 return 1;
422         }
423
424         return -1;
425 }
426
427