From f2c0a5db51ac10a9dd35a3ab8913906fd45b6c71 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Mon, 2 May 2011 18:16:43 +0200 Subject: [PATCH] Modularization of the main.cpp - semantic order enforced by parameters - less spaghetti code - code more readable - can be moved to a class more easily --- src/main.cpp | 151 ++++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 94 insertions(+), 57 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d52e2fe..46f7c09 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,42 @@ using boost::asio::io_service; using boost::shared_ptr; using I2n::Logger::GlobalLogger; +ConfigurationItem get_configuration( + int argc, + char *argv[] +) +{ + ConfigurationItem configuration; + + ConfigurationReader config_reader; + bool parsed_success = config_reader.parse( argc, argv ); + if ( parsed_success ) + { + Configuration config_obj = config_reader.get_configuration(); + configuration.reset( new Configuration( config_obj ) ); + } + + return configuration; +} + +LinkStatusAnalyzerItem get_status_notifier( + const ConfigurationItem &configuration +) +{ + int hosts_down_limit = configuration->get_hosts_down_limit(); + int link_up_interval_in_min = configuration->get_link_up_interval_in_min(); + string status_notifier_cmd = configuration->get_status_notifier_cmd(); + LinkStatusAnalyzerItem link_analyzer( + new LinkStatusAnalyzer( + hosts_down_limit, + link_up_interval_in_min, + status_notifier_cmd + ) + ); + + return link_analyzer; +} + void init_log() { I2n::Logger::enable_syslog( I2n::Logger::Facility::User ); @@ -25,75 +61,76 @@ void init_log() I2n::Logger::set_log_level( I2n::Logger::LogLevel::Info ); } -int main( int argc, char* argv[] ) +void init_pingers( + const ConfigurationItem &configuration, + const LinkStatusAnalyzerItem &status_notifier, + PingSchedulerList *scheduler_list +) { - init_log(); + string local_interface = configuration->get_source_network_interface(); + string nameserver = configuration->get_nameserver(); + int ping_fail_limit = configuration->get_ping_fail_limit(); - // TODO load_configuration() or read_configuration() - ConfigurationReader config_reader; - bool read_success = config_reader.parse( argc, argv ); - if ( read_success ) + HostList hosts = configuration->get_hosts(); + BOOST_FOREACH( HostItem host, hosts ) { - Configuration config = config_reader.get_configuration(); - - bool daemon_mode = config.get_daemon(); - if ( daemon_mode ) - { - I2n::Daemon::daemonize(); - } - int ping_fail_limit = config.get_ping_fail_limit(); - int link_up_interval_in_min = config.get_link_up_interval_in_min(); - - // TODO init_notifier and get_notifier - int hosts_down_limit = config.get_hosts_down_limit(); - string status_notifier_cmd = config.get_status_notifier_cmd(); - shared_ptr link_analyzer( - new LinkStatusAnalyzer( - hosts_down_limit, - link_up_interval_in_min, - status_notifier_cmd + string destination_address = host->get_address(); + int ping_interval_in_sec = host->get_interval_in_sec(); + PingSchedulerItem scheduler( + new PingScheduler( + local_interface, + destination_address, + ping_interval_in_sec, + ping_fail_limit, + nameserver, + status_notifier ) ); + scheduler_list->push_back( scheduler ); + } +} - string local_interface = config.get_source_network_interface(); - string nameserver = config.get_nameserver(); - - // TODO init_pingers() - vector< HostItem > hosts = config.get_hosts(); - vector< PingSchedulerItem > scheduler_list; - BOOST_FOREACH( HostItem host, hosts ) +void start_pingers( + const PingSchedulerList &scheduler_list +) +{ + // start each scheduler + BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) + { + bool started = scheduler->start_pinging_thread(); + if ( !started ) { - string destination_address = (*host).get_address(); - int ping_interval_in_sec = (*host).get_interval_in_sec(); - PingSchedulerItem scheduler( - new PingScheduler( - local_interface, - destination_address, - ping_interval_in_sec, - ping_fail_limit, - nameserver, - link_analyzer - ) - ); - scheduler_list.push_back( scheduler ); + GlobalLogger.error() << "Error: could not start pinger." + << endl; } + } - // TODO ping_loop() - BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) - { - bool started = scheduler->start_pinging_thread(); - if ( !started ) - { - GlobalLogger.error() << "Error: could not start pinger." - << endl; - } - } + // Main loop to handle scheduled ping requests + BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) + { + scheduler->wait_pinging_thread(); + } +} - // Main loop to handle scheduled ping requests - BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) +int main( int argc, char *argv[] ) +{ + init_log(); + + ConfigurationItem configuration = get_configuration( argc, argv ); + if ( configuration.get() != NULL ) + { + bool daemon_mode = configuration->get_daemon(); + if ( daemon_mode ) { - scheduler->wait_pinging_thread(); + I2n::Daemon::daemonize(); } + + LinkStatusAnalyzerItem status_notifier = get_status_notifier( configuration ); + + PingSchedulerList scheduler_list; + init_pingers( configuration, status_notifier, &scheduler_list ); + + start_pingers( scheduler_list ); } return 0; -- 1.7.1