some more sanity checks
[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/logging.h"
5 #include <signal.h>
6
7 osrfRouter* __osrfRouter = NULL;
8
9 void routerSignalHandler( int signal ) {
10         warning_handler("Received signal [%d], cleaning up...", signal );
11         osrfConfigCleanup();
12         osrfRouterFree(__osrfRouter);
13         log_free();
14 }
15
16 static int __setupRouter( char* config, char* context );
17
18
19 int main( int argc, char* argv[] ) {
20
21         if( argc < 3 ) {
22                 fatal_handler( "Usage: %s <path_to_config_file> <config_context>", argv[0] );
23                 exit(0);
24         }
25
26         char* config = strdup( argv[1] );
27         char* context = strdup( argv[2] );
28         init_proc_title( argc, argv );
29         set_proc_title( "OpenSRF Router" );
30
31         return __setupRouter( config, context );
32         free(config);
33         free(context);
34
35 }
36
37 int __setupRouter( char* config, char* context ) {
38
39         fprintf(stderr, "Launching router with config %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
54         int llevel = 1;
55         if(level) llevel = atoi(level);
56
57         if(!log_init( llevel, log_file )) 
58                 fprintf(stderr, "Unable to init logging, going to stderr...\n" );
59
60         free(level);
61         free(log_file);
62
63         info_handler( "Router connecting as: server: %s port: %s "
64                         "user: %s resource: %s", server, port, username, resource );
65
66         int iport = 0;
67         if(port)        iport = atoi( port );
68
69         osrfStringArray* tclients = osrfNewStringArray(4);
70         osrfStringArray* tservers = osrfNewStringArray(4);
71         osrfConfigGetValueList(NULL, tservers, "/trusted_domains/server" );
72         osrfConfigGetValueList(NULL, tclients, "/trusted_domains/client" );
73
74         int i;
75         for( i = 0; i != tservers->size; i++ ) 
76                 info_handler( "Router adding trusted server: %s", osrfStringArrayGetString( tservers, i ) );
77
78         for( i = 0; i != tclients->size; i++ ) 
79                 info_handler( "Router adding trusted client: %s", osrfStringArrayGetString( tclients, i ) );
80
81         if( tclients->size == 0 || tservers->size == 0 )
82                 fatal_handler("We need trusted servers and trusted client to run the router...");
83
84         osrfRouter* router = osrfNewRouter( server, 
85                         username, resource, password, iport, tclients, tservers );
86         
87         signal(SIGHUP,routerSignalHandler);
88         signal(SIGINT,routerSignalHandler);
89         signal(SIGTERM,routerSignalHandler);
90
91         if( (osrfRouterConnect(router)) != 0 ) {
92                 fprintf(stderr, "!!!! Unable to connect router to jabber server %s... exiting", server );
93                 return -1;
94         }
95
96         free(server); free(port); 
97         free(username); free(password); 
98
99         __osrfRouter = router;
100         daemonize();
101         osrfRouterRun( router );
102
103         return -1;
104
105 }
106
107