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