moved C code to a unified logging framework which currently supports syslogging and...
[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
53         int llevel = 1;
54         if(level) llevel = atoi(level);
55
56         /*
57         if(!log_init( llevel, log_file )) 
58                 fprintf(stderr, "Unable to init logging, going to stderr...\n" );
59                 */
60
61         osrfLogInit( OSRF_LOG_TYPE_SYSLOG, "router", llevel ); /* XXX config option */
62         osrfLogSetSyslogFacility( LOG_LOCAL3 ); /* XXX config option */
63
64         free(level);
65         free(log_file);
66
67         osrfLogInfo( "Router connecting as: server: %s port: %s "
68                         "user: %s resource: %s", server, port, username, resource );
69
70         int iport = 0;
71         if(port)        iport = atoi( port );
72
73         osrfStringArray* tclients = osrfNewStringArray(4);
74         osrfStringArray* tservers = osrfNewStringArray(4);
75         osrfConfigGetValueList(NULL, tservers, "/trusted_domains/server" );
76         osrfConfigGetValueList(NULL, tclients, "/trusted_domains/client" );
77
78         int i;
79         for( i = 0; i != tservers->size; i++ ) 
80                 osrfLogInfo( "Router adding trusted server: %s", osrfStringArrayGetString( tservers, i ) );
81
82         for( i = 0; i != tclients->size; i++ ) 
83                 osrfLogInfo( "Router adding trusted client: %s", osrfStringArrayGetString( tclients, i ) );
84
85         if( tclients->size == 0 || tservers->size == 0 ) {
86                 osrfLogError("We need trusted servers and trusted client to run the router...");
87                 return -1;
88         }
89
90         osrfRouter* router = osrfNewRouter( server, 
91                         username, resource, password, iport, tclients, tservers );
92         
93         signal(SIGHUP,routerSignalHandler);
94         signal(SIGINT,routerSignalHandler);
95         signal(SIGTERM,routerSignalHandler);
96
97         if( (osrfRouterConnect(router)) != 0 ) {
98                 fprintf(stderr, "!!!! Unable to connect router to jabber server %s... exiting", server );
99                 return -1;
100         }
101
102         free(server); free(port); 
103         free(username); free(password); 
104
105         __osrfRouter = router;
106         daemonize();
107         osrfRouterRun( router );
108
109         return -1;
110 }
111
112