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, "initialize");
34 if( (error = dlerror()) != NULL ) {
35 warning_handler("! Unable to locate method symbol [initialize] for app %s: %s", appName, error );
41 if( (ret = (*init)()) ) {
42 warning_handler("Application %s returned non-zero value from "
43 "'initialize', 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 info_handler("Running method [%s] for app [%s] with request id %d and "
114 "thread trace %s", methodName, appName, reqId, ses->session_id );
116 osrfApplication* app = _osrfAppFindApplication(appName);
117 if(!app) return warning_handler( "Application not found: %s", appName );
119 osrfMethod* method = __osrfAppFindMethod( app, methodName );
120 if(!method) return warning_handler( "NOT FOUND: app %s / method %s", appName, methodName );
122 /* this is the method we're gonna run */
123 int (*meth) (osrfMethodDispatcher*);
125 /* open the method */
126 *(void **) (&meth) = dlsym(app->handle, method->symbol);
128 if( (error = dlerror()) != NULL ) {
129 return warning_handler("Unable to locate method symbol [%s] "
130 "for method %s and app %s", method->symbol, method->name, app->name );
133 osrfMethodDispatcher d;
140 int ret = (*meth) (&d);
142 debug_handler("method returned %d", ret );
146 osrfAppSessionStatus( ses, OSRF_STATUS_INTERNALSERVERERROR,
147 reqId, "An unknown server error occurred" );