moved stderr line to a log line
[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( OSRF_LOG_MARK, "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( OSRF_LOG_MARK,  "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         osrfLogInfo(OSRF_LOG_MARK, "Launching router with config "
39                         "%s and config context %s", config, context );
40         osrfConfig* cfg = osrfConfigInit( config, context );
41         osrfConfigSetDefaultConfig(cfg);
42
43
44         char* server                    = osrfConfigGetValue(NULL, "/transport/server");
45         char* port                              = osrfConfigGetValue(NULL, "/transport/port");
46         char* username                  = osrfConfigGetValue(NULL, "/transport/username");
47         char* password                  = osrfConfigGetValue(NULL, "/transport/password");
48         char* resource                  = osrfConfigGetValue(NULL, "/transport/resource");
49
50         /* set up the logger */
51         char* level = osrfConfigGetValue(NULL, "/loglevel");
52         char* log_file = osrfConfigGetValue(NULL, "/logfile");
53         char* facility = osrfConfigGetValue(NULL, "/syslog");
54
55         int llevel = 1;
56         if(level) llevel = atoi(level);
57
58         /*
59         if(!log_init( llevel, log_file )) 
60                 fprintf(stderr, "Unable to init logging, going to stderr...\n" );
61                 */
62
63         if(!log_file) { fprintf(stderr, "Log file needed\n"); return -1; }
64
65         if(!strcmp(log_file, "syslog")) {
66                 osrfLogInit( OSRF_LOG_TYPE_SYSLOG, "router", llevel );
67                 osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility));
68
69         } else {
70                 osrfLogInit( OSRF_LOG_TYPE_FILE, "router", llevel );
71                 osrfLogSetFile( log_file );
72         }
73
74         free(facility);
75         free(level);
76         free(log_file);
77
78         osrfLogInfo(  OSRF_LOG_MARK, "Router connecting as: server: %s port: %s "
79                         "user: %s resource: %s", server, port, username, resource );
80
81         int iport = 0;
82         if(port)        iport = atoi( port );
83
84         osrfStringArray* tclients = osrfNewStringArray(4);
85         osrfStringArray* tservers = osrfNewStringArray(4);
86         osrfConfigGetValueList(NULL, tservers, "/trusted_domains/server" );
87         osrfConfigGetValueList(NULL, tclients, "/trusted_domains/client" );
88
89         int i;
90         for( i = 0; i != tservers->size; i++ ) 
91                 osrfLogInfo( OSRF_LOG_MARK,  "Router adding trusted server: %s", osrfStringArrayGetString( tservers, i ) );
92
93         for( i = 0; i != tclients->size; i++ ) 
94                 osrfLogInfo( OSRF_LOG_MARK,  "Router adding trusted client: %s", osrfStringArrayGetString( tclients, i ) );
95
96         if( tclients->size == 0 || tservers->size == 0 ) {
97                 osrfLogError( OSRF_LOG_MARK, "We need trusted servers and trusted client to run the router...");
98                 return -1;
99         }
100
101         osrfRouter* router = osrfNewRouter( server, 
102                         username, resource, password, iport, tclients, tservers );
103         
104         signal(SIGHUP,routerSignalHandler);
105         signal(SIGINT,routerSignalHandler);
106         signal(SIGTERM,routerSignalHandler);
107
108         if( (osrfRouterConnect(router)) != 0 ) {
109                 fprintf(stderr, "!!!! Unable to connect router to jabber server %s... exiting", server );
110                 return -1;
111         }
112
113         free(server); free(port); 
114         free(username); free(password); 
115
116         __osrfRouter = router;
117         daemonize();
118         osrfRouterRun( router );
119
120         return -1;
121 }
122
123