patching up some more logging stuff
[OpenSRF.git] / src / router / osrf_router_main.c
1 #include "osrf_router.h"
2 #include "opensrf/osrfConfig.h"
3 #include "opensrf/utils.h"
4 #include "opensrf/log.h"
5 #include <signal.h>
6
7 osrfRouter* __osrfRouter = NULL;
8
9 void routerSignalHandler( int signal ) {
10         osrfLogWarning("Received signal [%d], cleaning up...", signal );
11         osrfConfigCleanup();
12         osrfRouterFree(__osrfRouter);
13 }
14
15 static int __setupRouter( char* config, char* context );
16
17
18 int main( int argc, char* argv[] ) {
19
20         if( argc < 3 ) {
21                 osrfLogError( "Usage: %s <path_to_config_file> <config_context>", argv[0] );
22                 exit(0);
23         }
24
25         char* config = strdup( argv[1] );
26         char* context = strdup( argv[2] );
27         init_proc_title( argc, argv );
28         set_proc_title( "OpenSRF Router" );
29
30         return __setupRouter( config, context );
31         free(config);
32         free(context);
33
34 }
35
36 int __setupRouter( char* config, char* context ) {
37
38         fprintf(stderr, "Launching router with config %s and config context %s", config, context );
39         osrfConfig* cfg = osrfConfigInit( config, context );
40         osrfConfigSetDefaultConfig(cfg);
41
42
43         char* server                    = osrfConfigGetValue(NULL, "/transport/server");
44         char* port                              = osrfConfigGetValue(NULL, "/transport/port");
45         char* username                  = osrfConfigGetValue(NULL, "/transport/username");
46         char* password                  = osrfConfigGetValue(NULL, "/transport/password");
47         char* resource                  = osrfConfigGetValue(NULL, "/transport/resource");
48
49         /* set up the logger */
50         char* level = osrfConfigGetValue(NULL, "/loglevel");
51         char* log_file = osrfConfigGetValue(NULL, "/logfile");
52         char* facility = osrfConfigGetValue(NULL, "/syslog");
53
54         int llevel = 1;
55         if(level) llevel = atoi(level);
56
57         /*
58         if(!log_init( llevel, log_file )) 
59                 fprintf(stderr, "Unable to init logging, going to stderr...\n" );
60                 */
61
62         if(!log_file) { fprintf(stderr, "Log file needed\n"); return -1; }
63
64         if(!strcmp(log_file, "syslog")) {
65                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, "router", llevel );
66                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
67
68         } else {
69                 osrfLogInit( OSRF_LOG_TYPE_FILE, "router", llevel );
70                 osrfLogSetFile( log_file );
71         }
72
73         free(facility);
74         free(level);
75         free(log_file);
76
77         osrfLogInfo( "Router connecting as: server: %s port: %s "
78                         "user: %s resource: %s", server, port, username, resource );
79
80         int iport = 0;
81         if(port)        iport = atoi( port );
82
83         osrfStringArray* tclients = osrfNewStringArray(4);
84         osrfStringArray* tservers = osrfNewStringArray(4);
85         osrfConfigGetValueList(NULL, tservers, "/trusted_domains/server" );
86         osrfConfigGetValueList(NULL, tclients, "/trusted_domains/client" );
87
88         int i;
89         for( i = 0; i != tservers->size; i++ ) 
90                 osrfLogInfo( "Router adding trusted server: %s", osrfStringArrayGetString( tservers, i ) );
91
92         for( i = 0; i != tclients->size; i++ ) 
93                 osrfLogInfo( "Router adding trusted client: %s", osrfStringArrayGetString( tclients, i ) );
94
95         if( tclients->size == 0 || tservers->size == 0 ) {
96                 osrfLogError("We need trusted servers and trusted client to run the router...");
97                 return -1;
98         }
99
100         osrfRouter* router = osrfNewRouter( server, 
101                         username, resource, password, iport, tclients, tservers );
102         
103         signal(SIGHUP,routerSignalHandler);
104         signal(SIGINT,routerSignalHandler);
105         signal(SIGTERM,routerSignalHandler);
106
107         if( (osrfRouterConnect(router)) != 0 ) {
108                 fprintf(stderr, "!!!! Unable to connect router to jabber server %s... exiting", server );
109                 return -1;
110         }
111
112         free(server); free(port); 
113         free(username); free(password); 
114
115         __osrfRouter = router;
116         daemonize();
117         osrfRouterRun( router );
118
119         return -1;
120 }
121
122