1 #include "osrf_system.h"
3 #include "osrf_application.h"
4 #include "osrf_prefork.h"
6 void __osrfSystemSignalHandler( int sig );
8 transport_client* __osrfGlobalTransportClient;
10 transport_client* osrfSystemGetTransportClient() {
11 return __osrfGlobalTransportClient;
14 transport_client* osrf_system_get_transport_client() {
15 return __osrfGlobalTransportClient;
18 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
19 return osrf_system_bootstrap_client_resc(config_file, contextnode, NULL);
22 int osrfSystemBootstrapClientResc( char* config_file, char* contextnode, char* resource ) {
23 return osrf_system_bootstrap_client_resc( config_file, contextnode, resource );
27 int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) {
28 if( !(configfile && contextNode) ) return -1;
30 /* first we grab the settings */
31 if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) {
32 return fatal_handler("Unable to bootstrap");
35 osrf_settings_retrieve(hostname);
36 osrf_system_disconnect_client();
38 jsonObject* apps = osrf_settings_host_value_object("/activeapps/appname");
39 osrfStringArray* arr = osrfNewStringArray(8);
44 if(apps->type == JSON_STRING) {
45 osrfStringArrayAdd(arr, jsonObjectGetString(apps));
49 while( (app = jsonObjectGetIndex(apps, i++)) )
50 osrfStringArrayAdd(arr, jsonObjectGetString(app));
55 while( (appname = osrfStringArrayGetString(arr, i++)) ) {
57 char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname);
58 info_handler("Launching application %s with implementation %s", appname, libfile);
60 if(! (appname && libfile) ) {
61 warning_handler("Missing appname / libfile in settings config");
67 if( (pid = fork()) ) {
68 // storage pid in local table for re-launching dead children...
69 info_handler("Launched application child %d", pid);
73 osrfAppRegisterApplication( appname, libfile );
74 osrf_prefork_run(appname);
82 /* let our children do their thing */
84 signal(SIGCHLD, __osrfSystemSignalHandler);
91 int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) {
93 if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() )
94 return fatal_handler("No Config File Specified\n" );
98 osrfConfig* cfg = osrfConfigInit( config_file, contextnode );
99 osrfConfigSetDefaultConfig(cfg);
103 char* log_file = osrfConfigGetValue( NULL, "/logfile");
104 char* log_level = osrfConfigGetValue( NULL, "/loglevel" );
105 osrfStringArray* arr = osrfNewStringArray(8);
106 osrfConfigGetValueList(NULL, arr, "/domains/domain");
107 char* username = osrfConfigGetValue( NULL, "/username" );
108 char* password = osrfConfigGetValue( NULL, "/passwd" );
109 char* port = osrfConfigGetValue( NULL, "/port" );
110 char* unixpath = osrfConfigGetValue( NULL, "/unixpath" );
112 char* domain = osrfStringArrayGetString( arr, 0 ); /* just the first for now */
113 osrfStringArrayFree(arr);
118 if(port) iport = atoi(port);
119 if(log_level) llevel = atoi(log_level);
121 log_init( llevel, log_file );
123 info_handler("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath );
125 transport_client* client = client_init( domain, iport, unixpath, 0 );
127 char* host = getenv("HOSTNAME");
129 if(!resource) resource = "";
130 int len = strlen(resource) + 256;
133 snprintf(buf, len - 1, "opensrf_%s_%s_%d", resource, host, getpid() );
135 if(client_connect( client, username, password, buf, 10, AUTH_DIGEST )) {
136 /* child nodes will leak the parents client... but we can't free
137 it without disconnecting the parents client :( */
138 __osrfGlobalTransportClient = client;
148 if(__osrfGlobalTransportClient)
154 int osrf_system_disconnect_client() {
155 client_disconnect( __osrfGlobalTransportClient );
156 client_free( __osrfGlobalTransportClient );
157 __osrfGlobalTransportClient = NULL;
161 int osrf_system_shutdown() {
163 osrf_system_disconnect_client();
164 osrf_settings_free_host_config(NULL);
172 void __osrfSystemSignalHandler( int sig ) {
177 while( (pid = waitpid(-1, &status, WNOHANG)) > 0) {
178 warning_handler("We lost child %d", pid);
181 /** relaunch the server **/