2 #include "osrf_application.h"
4 osrfApplication* __osrfAppList = NULL;
7 int osrfAppRegisterApplication( char* appName, char* soFile ) {
8 if(!appName || ! soFile) return -1;
11 info_handler("Registering application %s with file %s", appName, soFile );
13 osrfApplication* app = safe_malloc(sizeof(osrfApplication));
14 app->handle = dlopen (soFile, RTLD_NOW);
17 warning_handler("Failed to dlopen library file %s: %s", soFile, dlerror() );
18 dlerror(); /* clear the error */
23 app->name = strdup(appName);
25 /* this has to be done before initting the application */
26 app->next = __osrfAppList;
30 /* see if we can run the initialize method */
32 *(void **) (&init) = dlsym(app->handle, "osrfAppInitialize");
34 if( (error = dlerror()) != NULL ) {
35 warning_handler("! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error );
41 if( (ret = (*init)()) ) {
42 warning_handler("Application %s returned non-zero value from "
43 "'osrfAppInitialize', not registering...", appName );
44 //free(app->name); /* need a method to remove an application from the list */
51 info_handler("Application %s registered successfully", appName );
58 int osrfAppRegisterMethod( char* appName,
59 char* methodName, char* symbolName, char* notes, int argc ) {
60 if( !appName || ! methodName || ! symbolName ) return -1;
62 osrfApplication* app = _osrfAppFindApplication(appName);
63 if(!app) return warning_handler("Unable to locate application %s", appName );
65 debug_handler("Registering method %s for app %s", appName, methodName );
67 osrfMethod* method = safe_malloc(sizeof(osrfMethod));
68 method->name = strdup(methodName);
69 method->symbol = strdup(symbolName);
70 if(notes) method->notes = strdup(notes);
73 /* plug the method into the list of methods */
74 method->next = app->methods;
75 app->methods = method;
79 osrfApplication* _osrfAppFindApplication( char* name ) {
80 if(!name) return NULL;
81 osrfApplication* app = __osrfAppList;
83 if(!strcmp(app->name, name))
90 osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ) {
91 if(!app || ! methodName) return NULL;
92 osrfMethod* method = app->methods;
94 if(!strcmp(method->name, methodName))
96 method = method->next;
101 osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ) {
102 if(!appName || ! methodName) return NULL;
103 return __osrfAppFindMethod( _osrfAppFindApplication(appName), methodName );
109 int osrfAppRunMethod( char* appName, char* methodName, osrfAppSession* ses, int reqId, jsonObject* params ) {
110 if( !(appName && methodName && ses) ) return -1;
113 osrfApplication* app;
115 osrfMethodContext context;
117 /* this is the method we're gonna run */
118 int (*meth) (osrfMethodContext*);
120 info_handler("Running method [%s] for app [%s] with request id %d and "
121 "thread trace %s", methodName, appName, reqId, ses->session_id );
123 if( !(app = _osrfAppFindApplication(appName)) )
124 return warning_handler( "Application not found: %s", appName );
126 if( !(method = __osrfAppFindMethod( app, methodName )) )
127 return warning_handler( "NOT FOUND: app %s / method %s", appName, methodName );
129 /* open the method */
130 *(void **) (&meth) = dlsym(app->handle, method->symbol);
132 if( (error = dlerror()) != NULL ) {
133 return warning_handler("Unable to locate method symbol [%s] "
134 "for method %s and app %s", method->symbol, method->name, app->name );
137 context.session = ses;
138 context.method = method;
139 context.params = params;
140 context.request = reqId;
143 int ret = (*meth) (&context);
145 debug_handler("method returned %d", ret );
149 osrfAppSessionStatus( ses, OSRF_STATUS_INTERNALSERVERERROR,
150 reqId, "An unknown server error occurred" );