]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_application.c
ad20ef6ee00c9c9c97d80cdd5a20c45b0872ac51
[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                 atomicMethod->userData = method->userData;
126         }
127
128         return 0;
129 }
130
131
132
133 osrfMethod* _osrfAppBuildMethod( char* methodName, 
134         char* symbolName, char* notes, int argc, int options, void* user_data ) {
135
136         osrfMethod* method                                      = safe_malloc(sizeof(osrfMethod));
137
138         if(methodName) method->name             = strdup(methodName);
139         if(symbolName) method->symbol           = strdup(symbolName);
140         if(notes) method->notes                         = strdup(notes);
141         if(user_data) method->userData  = user_data;
142
143         method->argc                                                    = argc;
144         method->options                                         = options;
145
146         if(options & OSRF_METHOD_ATOMIC) { /* add ".atomic" to the end of the name */
147                 char mb[strlen(method->name) + 8];
148                 sprintf(mb, "%s.atomic", method->name);
149                 free(method->name);
150                 method->name = strdup(mb);
151                 method->options |= OSRF_METHOD_STREAMING;
152         }
153
154         return method;
155 }
156
157
158 int __osrfAppRegisterSysMethods( char* app ) {
159
160         osrfAppRegisterMethod( 
161                         app, OSRF_SYSMETHOD_INTROSPECT, NULL, 
162                         "Return a list of methods whose names have the same initial "
163                         "substring as that of the provided method name PARAMS( methodNameSubstring )", 
164                         1, OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
165
166         osrfAppRegisterMethod( 
167                         app, OSRF_SYSMETHOD_INTROSPECT_ALL, NULL, 
168                         "Returns a complete list of methods. PARAMS()", 0, 
169                         OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
170
171         osrfAppRegisterMethod( 
172                         app, OSRF_SYSMETHOD_ECHO, NULL, 
173                         "Echos all data sent to the server back to the client. PARAMS([a, b, ...])", 0, 
174                         OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
175
176         return 0;
177 }
178
179 osrfApplication* _osrfAppFindApplication( char* name ) {
180         if(!name) return NULL;
181         return (osrfApplication*) osrfHashGet(__osrfAppHash, name);
182 }
183
184 osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ) {
185         if(!app || ! methodName) return NULL;
186         return (osrfMethod*) osrfHashGet( app->methods, methodName );
187 }
188
189 osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ) {
190         if(!appName || ! methodName) return NULL;
191         return __osrfAppFindMethod( _osrfAppFindApplication(appName), methodName );
192 }
193
194
195 int osrfAppRunMethod( char* appName, char* methodName, 
196                 osrfAppSession* ses, int reqId, jsonObject* params ) {
197
198         if( !(appName && methodName && ses) ) return -1;
199
200         char* error;
201         osrfApplication* app;
202         osrfMethod* method;
203         osrfMethodContext context;
204
205         context.session = ses;
206         context.params = params;
207         context.request = reqId;
208         context.responses = NULL;
209
210         /* this is the method we're gonna run */
211         int (*meth) (osrfMethodContext*);       
212
213         osrfLogInfo( OSRF_LOG_MARK, "Running method [%s] for app [%s] with request id %d and "
214                         "thread trace %s", methodName, appName, reqId, ses->session_id );
215
216         if( !(app = _osrfAppFindApplication(appName)) )
217                 return osrfAppRequestRespondException( ses, 
218                                 reqId, "Application not found: %s", appName );
219         
220         if( !(method = __osrfAppFindMethod( app, methodName )) ) 
221                 return osrfAppRequestRespondException( ses, reqId, 
222                                 "Method [%s] not found for service %s", methodName, appName );
223
224         context.method = method;
225
226         #ifdef OSRF_STRICT_PARAMS
227         if( method->argc > 0 ) {
228                 if(!params || params->type != JSON_ARRAY || params->size < method->argc )
229                         return osrfAppRequestRespondException( ses, reqId, 
230                                 "Not enough params for method %s / service %s", methodName, appName );
231         }
232         #endif
233
234         int retcode = 0;
235
236         if( method->options & OSRF_METHOD_SYSTEM ) {
237                 retcode = __osrfAppRunSystemMethod(&context);
238
239         } else {
240
241                 /* open and now run the method */
242                 *(void **) (&meth) = dlsym(app->handle, method->symbol);
243
244                 if( (error = dlerror()) != NULL ) {
245                         return osrfAppRequestRespondException( ses, reqId, 
246                                 "Unable to execute method [%s]  for service %s", methodName, appName );
247                 }
248
249                 retcode = (*meth) (&context);
250         }
251
252         if(retcode < 0) 
253                 return osrfAppRequestRespondException( 
254                                 ses, reqId, "An unknown server error occurred" );
255
256         return __osrfAppPostProcess( &context, retcode );
257
258 }
259
260
261 int osrfAppRespond( osrfMethodContext* ctx, jsonObject* data ) {
262         return _osrfAppRespond( ctx, data, 0 );
263 }
264
265 int osrfAppRespondComplete( osrfMethodContext* context, jsonObject* data ) {
266         return _osrfAppRespond( context, data, 1 );
267 }
268
269 int _osrfAppRespond( osrfMethodContext* ctx, jsonObject* data, int complete ) {
270         if(!(ctx && ctx->method)) return -1;
271
272         if( ctx->method->options & OSRF_METHOD_ATOMIC ) {
273                 osrfLogDebug( OSRF_LOG_MARK,   
274                         "Adding responses to stash for atomic method %s", ctx->method->name );
275
276                 if( ctx->responses == NULL )                                                                                            
277                         ctx->responses = jsonParseString("[]");                                                 
278
279                 if ( data != NULL )
280                         jsonObjectPush( ctx->responses, jsonObjectClone(data) );        
281         }
282
283
284         if(     !(ctx->method->options & OSRF_METHOD_ATOMIC) && 
285                         !(ctx->method->options & OSRF_METHOD_CACHABLE) ) {
286
287                 if(complete) 
288                         osrfAppRequestRespondComplete( ctx->session, ctx->request, data );
289                 else
290                         osrfAppRequestRespond( ctx->session, ctx->request, data );
291                 return 0;
292         }
293
294         return 0;
295 }
296
297
298
299
300 int __osrfAppPostProcess( osrfMethodContext* ctx, int retcode ) {
301         if(!(ctx && ctx->method)) return -1;
302
303         osrfLogDebug( OSRF_LOG_MARK,  "Postprocessing method %s with retcode %d",
304                         ctx->method->name, retcode );
305
306         if(ctx->responses) { /* we have cached responses to return (no responses have been sent) */
307
308                 osrfAppRequestRespondComplete( ctx->session, ctx->request, ctx->responses );
309                 jsonObjectFree(ctx->responses);
310                 ctx->responses = NULL;
311
312         } else {
313
314                 if( retcode > 0 ) 
315                         osrfAppSessionStatus( ctx->session, OSRF_STATUS_COMPLETE,  
316                                         "osrfConnectStatus", ctx->request, "Request Complete" );
317         }
318
319         return 0;
320
321 }
322
323 int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, ... ) {
324         if(!ses) return -1;
325         if(!msg) msg = "";
326         VA_LIST_TO_STRING(msg);
327         osrfLogWarning( OSRF_LOG_MARK,  "Returning method exception with message: %s", VA_BUF );
328         osrfAppSessionStatus( ses, OSRF_STATUS_NOTFOUND, "osrfMethodException", request,  VA_BUF );
329         return 0;
330 }
331
332
333 static void __osrfAppSetIntrospectMethod( osrfMethodContext* ctx, osrfMethod* method, jsonObject* resp ) {
334         if(!(ctx && resp)) return;
335
336         jsonObjectSetKey(resp, "api_name",      jsonNewObject(method->name));
337         jsonObjectSetKey(resp, "method",                jsonNewObject(method->symbol));
338         jsonObjectSetKey(resp, "service",       jsonNewObject(ctx->session->remote_service));
339         jsonObjectSetKey(resp, "notes",         jsonNewObject(method->notes));
340         jsonObjectSetKey(resp, "argc",          jsonNewNumberObject(method->argc));
341
342         jsonObjectSetKey(resp, "sysmethod", 
343                         jsonNewNumberObject( (method->options & OSRF_METHOD_SYSTEM) ? 1 : 0 ));
344         jsonObjectSetKey(resp, "atomic",                
345                         jsonNewNumberObject( (method->options & OSRF_METHOD_ATOMIC) ? 1 : 0 ));
346         jsonObjectSetKey(resp, "cachable",      
347                         jsonNewNumberObject( (method->options & OSRF_METHOD_CACHABLE) ? 1 : 0 ));
348
349         jsonObjectSetClass(resp, "method");
350 }
351
352
353
354 int __osrfAppRunSystemMethod(osrfMethodContext* ctx) {
355         OSRF_METHOD_VERIFY_CONTEXT(ctx);
356
357         if(     !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL ) || 
358                         !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL_ATOMIC )) {
359
360                 return osrfAppIntrospectAll(ctx);
361         }
362
363
364         if(     !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT ) ||
365                         !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ATOMIC )) {
366
367                 return osrfAppIntrospect(ctx);
368         }
369
370         osrfAppRequestRespondException( ctx->session, 
371                         ctx->request, "System method implementation not found");
372
373         return 0;
374 }
375
376
377 int osrfAppIntrospect( osrfMethodContext* ctx ) {
378
379         jsonObject* resp = NULL;
380         char* methodSubstring = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
381         osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
382         int len = 0;
383
384         if(!methodSubstring) return 1; /* respond with no methods */
385
386         if(app) {
387
388                 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
389                 osrfMethod* method;
390
391                 while( (method = osrfHashIteratorNext(itr)) ) {
392                         if( (len = strlen(methodSubstring)) <= strlen(method->name) ) {
393                                 if( !strncmp( method->name, methodSubstring, len) ) {
394                                         resp = jsonNewObject(NULL);
395                                         __osrfAppSetIntrospectMethod( ctx, method, resp );
396                                         osrfAppRespond(ctx, resp);
397                                         jsonObjectFree(resp);
398                                 }
399                         }
400                 }
401                 osrfHashIteratorFree(itr);
402                 return 1;
403         }
404
405         return -1;
406
407 }
408
409
410 int osrfAppIntrospectAll( osrfMethodContext* ctx ) {
411         jsonObject* resp = NULL;
412         osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
413
414         if(app) {
415                 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
416                 osrfMethod* method;
417                 while( (method = osrfHashIteratorNext(itr)) ) {
418                         resp = jsonNewObject(NULL);
419                         __osrfAppSetIntrospectMethod( ctx, method, resp );
420                         osrfAppRespond(ctx, resp);
421                         jsonObjectFree(resp);
422                 }
423                 osrfHashIteratorFree(itr);
424                 return 1;
425         }
426
427         return -1;
428 }
429
430