1 #include <opensrf/osrf_application.h>
3 osrfHash* __osrfAppHash = NULL;
5 int osrfAppRegisterApplication( char* appName, char* soFile ) {
6 if(!appName || ! soFile) return -1;
9 if(!__osrfAppHash) __osrfAppHash = osrfNewHash();
11 osrfLogInfo( OSRF_LOG_MARK, "Registering application %s with file %s", appName, soFile );
13 osrfApplication* app = safe_malloc(sizeof(osrfApplication));
14 app->handle = dlopen (soFile, RTLD_NOW);
18 osrfLogWarning( OSRF_LOG_MARK, "Failed to dlopen library file %s: %s", soFile, dlerror() );
19 dlerror(); /* clear the error */
24 app->methods = osrfNewHash();
25 osrfHashSet( __osrfAppHash, app, appName );
27 /* see if we can run the initialize method */
29 *(void **) (&init) = dlsym(app->handle, "osrfAppInitialize");
31 if( (error = dlerror()) != NULL ) {
32 osrfLogWarning( OSRF_LOG_MARK,
33 "! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error );
39 if( (ret = (*init)()) ) {
40 osrfLogWarning( OSRF_LOG_MARK, "Application %s returned non-zero value from "
41 "'osrfAppInitialize', not registering...", appName );
42 //free(app->name); /* need a method to remove an application from the list */
48 __osrfAppRegisterSysMethods(appName);
50 osrfLogInfo( OSRF_LOG_MARK, "Application %s registered successfully", appName );
52 osrfLogSetAppname(appName);
54 osrfAppSetOnExit(app, appName);
60 void osrfAppSetOnExit(osrfApplication* app, char* appName) {
61 if(!(app && appName)) return;
63 /* see if we can run the initialize method */
65 void (*onExit) (void);
66 *(void **) (&onExit) = dlsym(app->handle, "osrfAppChildExit");
68 if( (error = dlerror()) != NULL ) {
69 osrfLogDebug(OSRF_LOG_MARK, "No exit handler defined for %s", appName);
73 osrfLogInfo(OSRF_LOG_MARK, "registering exit handler for %s", appName);
74 app->onExit = (*onExit);
78 int osrfAppRunChildInit(char* appname) {
79 osrfApplication* app = _osrfAppFindApplication(appname);
84 int (*childInit) (void);
86 *(void**) (&childInit) = dlsym(app->handle, "osrfAppChildInit");
88 if( (error = dlerror()) != NULL ) {
89 osrfLogInfo( OSRF_LOG_MARK, "No child init defined for app %s : %s", appname, error);
93 if( (ret = (*childInit)()) ) {
94 osrfLogError(OSRF_LOG_MARK, "App %s child init failed", appname);
98 osrfLogInfo(OSRF_LOG_MARK, "%s child init succeeded", appname);
103 void osrfAppRunExitCode() {
104 osrfHashIterator* itr = osrfNewHashIterator(__osrfAppHash);
105 osrfApplication* app;
106 while( (app = osrfHashIteratorNext(itr)) ) {
108 osrfLogInfo(OSRF_LOG_MARK, "Running onExit handler for app %s", itr->current);
115 int osrfAppRegisterMethod( char* appName, char* methodName,
116 char* symbolName, char* notes, int argc, int options ) {
118 return osrfAppRegisterExtendedMethod(
130 int osrfAppRegisterExtendedMethod( char* appName, char* methodName,
131 char* symbolName, char* notes, int argc, int options, void * user_data ) {
133 if( !appName || ! methodName ) return -1;
135 osrfApplication* app = _osrfAppFindApplication(appName);
137 osrfLogWarning( OSRF_LOG_MARK, "Unable to locate application %s", appName );
141 osrfLogDebug( OSRF_LOG_MARK, "Registering method %s for app %s", methodName, appName );
143 osrfMethod* method = _osrfAppBuildMethod(
144 methodName, symbolName, notes, argc, options, user_data );
145 method->options = options;
147 /* plug the method into the list of methods */
148 osrfHashSet( app->methods, method, method->name );
150 if( options & OSRF_METHOD_STREAMING ) { /* build the atomic counterpart */
151 int newops = options | OSRF_METHOD_ATOMIC;
152 osrfMethod* atomicMethod = _osrfAppBuildMethod(
153 methodName, symbolName, notes, argc, newops, NULL );
154 osrfHashSet( app->methods, atomicMethod, atomicMethod->name );
155 atomicMethod->userData = method->userData;
163 osrfMethod* _osrfAppBuildMethod( char* methodName,
164 char* symbolName, char* notes, int argc, int options, void* user_data ) {
166 osrfMethod* method = safe_malloc(sizeof(osrfMethod));
168 if(methodName) method->name = strdup(methodName);
169 if(symbolName) method->symbol = strdup(symbolName);
170 if(notes) method->notes = strdup(notes);
171 if(user_data) method->userData = user_data;
174 method->options = options;
176 if(options & OSRF_METHOD_ATOMIC) { /* add ".atomic" to the end of the name */
177 char mb[strlen(method->name) + 8];
178 sprintf(mb, "%s.atomic", method->name);
180 method->name = strdup(mb);
181 method->options |= OSRF_METHOD_STREAMING;
188 int __osrfAppRegisterSysMethods( char* app ) {
190 osrfAppRegisterMethod(
191 app, OSRF_SYSMETHOD_INTROSPECT, NULL,
192 "Return a list of methods whose names have the same initial "
193 "substring as that of the provided method name PARAMS( methodNameSubstring )",
194 1, OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
196 osrfAppRegisterMethod(
197 app, OSRF_SYSMETHOD_INTROSPECT_ALL, NULL,
198 "Returns a complete list of methods. PARAMS()", 0,
199 OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
201 osrfAppRegisterMethod(
202 app, OSRF_SYSMETHOD_ECHO, NULL,
203 "Echos all data sent to the server back to the client. PARAMS([a, b, ...])", 0,
204 OSRF_METHOD_SYSTEM | OSRF_METHOD_STREAMING );
209 osrfApplication* _osrfAppFindApplication( char* name ) {
210 if(!name) return NULL;
211 return (osrfApplication*) osrfHashGet(__osrfAppHash, name);
214 osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ) {
215 if(!app || ! methodName) return NULL;
216 return (osrfMethod*) osrfHashGet( app->methods, methodName );
219 osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ) {
220 if(!appName || ! methodName) return NULL;
221 return __osrfAppFindMethod( _osrfAppFindApplication(appName), methodName );
225 int osrfAppRunMethod( char* appName, char* methodName,
226 osrfAppSession* ses, int reqId, jsonObject* params ) {
228 if( !(appName && methodName && ses) ) return -1;
231 osrfApplication* app;
233 osrfMethodContext context;
235 context.session = ses;
236 context.params = params;
237 context.request = reqId;
238 context.responses = NULL;
240 /* this is the method we're gonna run */
241 int (*meth) (osrfMethodContext*);
243 if( !(app = _osrfAppFindApplication(appName)) )
244 return osrfAppRequestRespondException( ses,
245 reqId, "Application not found: %s", appName );
247 if( !(method = __osrfAppFindMethod( app, methodName )) )
248 return osrfAppRequestRespondException( ses, reqId,
249 "Method [%s] not found for service %s", methodName, appName );
251 context.method = method;
253 #ifdef OSRF_STRICT_PARAMS
254 if( method->argc > 0 ) {
255 if(!params || params->type != JSON_ARRAY || params->size < method->argc )
256 return osrfAppRequestRespondException( ses, reqId,
257 "Not enough params for method %s / service %s", methodName, appName );
263 if( method->options & OSRF_METHOD_SYSTEM ) {
264 retcode = __osrfAppRunSystemMethod(&context);
268 /* open and now run the method */
269 *(void **) (&meth) = dlsym(app->handle, method->symbol);
271 if( (error = dlerror()) != NULL ) {
272 return osrfAppRequestRespondException( ses, reqId,
273 "Unable to execute method [%s] for service %s", methodName, appName );
276 retcode = (*meth) (&context);
280 return osrfAppRequestRespondException(
281 ses, reqId, "An unknown server error occurred" );
283 return __osrfAppPostProcess( &context, retcode );
288 int osrfAppRespond( osrfMethodContext* ctx, jsonObject* data ) {
289 return _osrfAppRespond( ctx, data, 0 );
292 int osrfAppRespondComplete( osrfMethodContext* context, jsonObject* data ) {
293 return _osrfAppRespond( context, data, 1 );
296 int _osrfAppRespond( osrfMethodContext* ctx, jsonObject* data, int complete ) {
297 if(!(ctx && ctx->method)) return -1;
299 if( ctx->method->options & OSRF_METHOD_ATOMIC ) {
300 osrfLogDebug( OSRF_LOG_MARK,
301 "Adding responses to stash for atomic method %s", ctx->method->name );
303 if( ctx->responses == NULL )
304 ctx->responses = jsonParseString("[]");
307 jsonObjectPush( ctx->responses, jsonObjectClone(data) );
311 if( !(ctx->method->options & OSRF_METHOD_ATOMIC) &&
312 !(ctx->method->options & OSRF_METHOD_CACHABLE) ) {
315 osrfAppRequestRespondComplete( ctx->session, ctx->request, data );
317 osrfAppRequestRespond( ctx->session, ctx->request, data );
327 int __osrfAppPostProcess( osrfMethodContext* ctx, int retcode ) {
328 if(!(ctx && ctx->method)) return -1;
330 osrfLogDebug( OSRF_LOG_MARK, "Postprocessing method %s with retcode %d",
331 ctx->method->name, retcode );
333 if(ctx->responses) { /* we have cached responses to return (no responses have been sent) */
335 osrfAppRequestRespondComplete( ctx->session, ctx->request, ctx->responses );
336 jsonObjectFree(ctx->responses);
337 ctx->responses = NULL;
342 osrfAppSessionStatus( ctx->session, OSRF_STATUS_COMPLETE,
343 "osrfConnectStatus", ctx->request, "Request Complete" );
349 int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, ... ) {
352 VA_LIST_TO_STRING(msg);
353 osrfLogWarning( OSRF_LOG_MARK, "Returning method exception with message: %s", VA_BUF );
354 osrfAppSessionStatus( ses, OSRF_STATUS_NOTFOUND, "osrfMethodException", request, VA_BUF );
359 static void __osrfAppSetIntrospectMethod( osrfMethodContext* ctx, osrfMethod* method, jsonObject* resp ) {
360 if(!(ctx && resp)) return;
362 jsonObjectSetKey(resp, "api_name", jsonNewObject(method->name));
363 jsonObjectSetKey(resp, "method", jsonNewObject(method->symbol));
364 jsonObjectSetKey(resp, "service", jsonNewObject(ctx->session->remote_service));
365 jsonObjectSetKey(resp, "notes", jsonNewObject(method->notes));
366 jsonObjectSetKey(resp, "argc", jsonNewNumberObject(method->argc));
368 jsonObjectSetKey(resp, "sysmethod",
369 jsonNewNumberObject( (method->options & OSRF_METHOD_SYSTEM) ? 1 : 0 ));
370 jsonObjectSetKey(resp, "atomic",
371 jsonNewNumberObject( (method->options & OSRF_METHOD_ATOMIC) ? 1 : 0 ));
372 jsonObjectSetKey(resp, "cachable",
373 jsonNewNumberObject( (method->options & OSRF_METHOD_CACHABLE) ? 1 : 0 ));
375 jsonObjectSetClass(resp, "method");
380 int __osrfAppRunSystemMethod(osrfMethodContext* ctx) {
381 OSRF_METHOD_VERIFY_CONTEXT(ctx);
383 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL ) ||
384 !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL_ATOMIC )) {
386 return osrfAppIntrospectAll(ctx);
390 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT ) ||
391 !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ATOMIC )) {
393 return osrfAppIntrospect(ctx);
396 if( !strcmp(ctx->method->name, OSRF_SYSMETHOD_ECHO ) ||
397 !strcmp(ctx->method->name, OSRF_SYSMETHOD_ECHO_ATOMIC )) {
399 return osrfAppEcho(ctx);
403 osrfAppRequestRespondException( ctx->session,
404 ctx->request, "System method implementation not found");
410 int osrfAppIntrospect( osrfMethodContext* ctx ) {
412 jsonObject* resp = NULL;
413 char* methodSubstring = jsonObjectGetString( jsonObjectGetIndex(ctx->params, 0) );
414 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
417 if(!methodSubstring) return 1; /* respond with no methods */
421 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
424 while( (method = osrfHashIteratorNext(itr)) ) {
425 if( (len = strlen(methodSubstring)) <= strlen(method->name) ) {
426 if( !strncmp( method->name, methodSubstring, len) ) {
427 resp = jsonNewObject(NULL);
428 __osrfAppSetIntrospectMethod( ctx, method, resp );
429 osrfAppRespond(ctx, resp);
430 jsonObjectFree(resp);
434 osrfHashIteratorFree(itr);
443 int osrfAppIntrospectAll( osrfMethodContext* ctx ) {
444 jsonObject* resp = NULL;
445 osrfApplication* app = _osrfAppFindApplication( ctx->session->remote_service );
448 osrfHashIterator* itr = osrfNewHashIterator(app->methods);
450 while( (method = osrfHashIteratorNext(itr)) ) {
451 resp = jsonNewObject(NULL);
452 __osrfAppSetIntrospectMethod( ctx, method, resp );
453 osrfAppRespond(ctx, resp);
454 jsonObjectFree(resp);
456 osrfHashIteratorFree(itr);
463 int osrfAppEcho( osrfMethodContext* ctx ) {
464 OSRF_METHOD_VERIFY_CONTEXT(ctx);
466 for( i = 0; i < ctx->params->size; i++ ) {
467 jsonObject* str = jsonObjectGetIndex(ctx->params,i);
468 osrfAppRespond(ctx, str);