This function is called by a server drone shortly after it is spawned by the listener.
*/
-int osrfAppChildInit() {
+int osrfAppChildInit( void ) {
- osrfLogDebug( OSRF_LOG_MARK, "Attempting to initialize libdbi..." );
- dbi_initialize( NULL );
- osrfLogDebug( OSRF_LOG_MARK, "... libdbi initialized." );
-
- char* driver = osrf_settings_host_value( "/apps/%s/app_settings/driver", modulename );
- char* user = osrf_settings_host_value( "/apps/%s/app_settings/database/user", modulename );
- char* host = osrf_settings_host_value( "/apps/%s/app_settings/database/host", modulename );
- char* port = osrf_settings_host_value( "/apps/%s/app_settings/database/port", modulename );
- char* db = osrf_settings_host_value( "/apps/%s/app_settings/database/db", modulename );
- char* pw = osrf_settings_host_value( "/apps/%s/app_settings/database/pw", modulename );
-
- osrfLogDebug( OSRF_LOG_MARK, "Attempting to load the database driver [%s]...", driver );
- dbhandle = dbi_conn_new( driver );
-
- if( !dbhandle ) {
- osrfLogError( OSRF_LOG_MARK, "Error loading database driver [%s]", driver );
+ dbhandle = oilsConnectDB( modulename );
+ if( !dbhandle )
return -1;
- }
- osrfLogDebug( OSRF_LOG_MARK, "Database driver [%s] seems OK", driver );
-
- osrfLogInfo(OSRF_LOG_MARK, "%s connecting to database. host=%s, "
- "port=%s, user=%s, db=%s", modulename, host, port, user, db );
-
- if( host ) dbi_conn_set_option( dbhandle, "host", host );
- if( port ) dbi_conn_set_option_numeric( dbhandle, "port", atoi( port ));
- if( user ) dbi_conn_set_option( dbhandle, "username", user );
- if( pw ) dbi_conn_set_option( dbhandle, "password", pw );
- if( db ) dbi_conn_set_option( dbhandle, "dbname", db );
-
- free( user );
- free( host );
- free( port );
- free( db );
- free( pw );
-
- const char* err;
- if( dbi_conn_connect( dbhandle ) < 0 ) {
- sleep( 1 );
- if( dbi_conn_connect( dbhandle ) < 0 ) {
- dbi_conn_error( dbhandle, &err );
- osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s", err );
- return -1;
- }
- }
-
- oilsSetDBConnection( dbhandle );
- osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", modulename );
-
- // Add datatypes from database to the fields in the IDL
- //if( oilsExtendIDL() ) {
- // osrfLogError( OSRF_LOG_MARK, "Error extending the IDL" );
- // return -1;
- //}
- //else
+ else {
+ oilsSetDBConnection( dbhandle );
+ osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", modulename );
+
+ // Apply datatypes from database to the fields in the IDL
+ //if( oilsExtendIDL() ) {
+ // osrfLogError( OSRF_LOG_MARK, "Error extending the IDL" );
+ // return -1;
+ //}
+ //else
return 0;
+ }
}
/**
static char* modulename = NULL;
/**
+ @brief Connect to the database.
+ @return A database connection if successful, or NULL if not.
+ */
+dbi_conn oilsConnectDB( const char* mod_name ) {
+
+ osrfLogDebug( OSRF_LOG_MARK, "Attempting to initialize libdbi..." );
+ if( dbi_initialize( NULL ) == -1 ) {
+ osrfLogError( OSRF_LOG_MARK, "Unable to initialize libdbi" );
+ return NULL;
+ } else
+ osrfLogDebug( OSRF_LOG_MARK, "... libdbi initialized." );
+
+ char* driver = osrf_settings_host_value( "/apps/%s/app_settings/driver", mod_name );
+ char* user = osrf_settings_host_value( "/apps/%s/app_settings/database/user", mod_name );
+ char* host = osrf_settings_host_value( "/apps/%s/app_settings/database/host", mod_name );
+ char* port = osrf_settings_host_value( "/apps/%s/app_settings/database/port", mod_name );
+ char* db = osrf_settings_host_value( "/apps/%s/app_settings/database/db", mod_name );
+ char* pw = osrf_settings_host_value( "/apps/%s/app_settings/database/pw", mod_name );
+
+ osrfLogDebug( OSRF_LOG_MARK, "Attempting to load the database driver [%s]...", driver );
+ dbi_conn handle = dbi_conn_new( driver );
+
+ if( !handle ) {
+ osrfLogError( OSRF_LOG_MARK, "Error loading database driver [%s]", driver );
+ return NULL;
+ }
+ osrfLogDebug( OSRF_LOG_MARK, "Database driver [%s] seems OK", driver );
+
+ osrfLogInfo(OSRF_LOG_MARK, "%s connecting to database. host=%s, "
+ "port=%s, user=%s, db=%s", mod_name, host, port, user, db );
+
+ if( host ) dbi_conn_set_option( handle, "host", host );
+ if( port ) dbi_conn_set_option_numeric( handle, "port", atoi( port ));
+ if( user ) dbi_conn_set_option( handle, "username", user );
+ if( pw ) dbi_conn_set_option( handle, "password", pw );
+ if( db ) dbi_conn_set_option( handle, "dbname", db );
+
+ free( user );
+ free( host );
+ free( port );
+ free( db );
+ free( pw );
+
+ if( dbi_conn_connect( handle ) < 0 ) {
+ sleep( 1 );
+ if( dbi_conn_connect( handle ) < 0 ) {
+ const char* errmsg;
+ dbi_conn_error( handle, &errmsg );
+ osrfLogError( OSRF_LOG_MARK, "Error connecting to database: %s", errmsg );
+ return NULL;
+ }
+ }
+
+ osrfLogInfo( OSRF_LOG_MARK, "%s successfully connected to the database", mod_name );
+
+ return handle;
+}
+
+/**
@brief Select some options.
@param module_name: Name of the server.
@param do_pcrud: Boolean. True if we are to enforce PCRUD permissions.