set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
-find_package(Boost COMPONENTS filesystem program_options system REQUIRED)
+find_package(Boost COMPONENTS filesystem program_options system thread REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
using boost::posix_time::ptime;
using boost::posix_time::seconds;
using boost::shared_ptr;
+using boost::thread;
using I2n::Logger::GlobalLogger;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
PingScheduler::PingScheduler(
- io_service &io_serv,
const string &network_interface,
const string &destination_address,
const long ping_interval_in_sec,
shared_ptr<LinkStatusAnalyzer> link_analyzer
) :
- IoService( io_serv ),
+ IoService(),
LocalNetworkInterfaceName( network_interface ),
- NextPingTimer( io_serv ),
+ NextPingTimer( IoService ),
TimeSentLastPing( microsec_clock::universal_time() ),
PingIntervalInSec( ping_interval_in_sec ),
IpList( destination_address, nameserver ),
- HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer )
+ HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer ),
+ Thread()
{
}
{
}
+bool PingScheduler::start_pinging_thread()
+{
+ Thread = thread( &PingScheduler::start_pinging, this );
+
+ return true;
+}
+
+void PingScheduler::wait_pinging_thread()
+{
+ BOOST_ASSERT( Thread.joinable() );
+
+ Thread.join();
+}
+
bool PingScheduler::start_pinging()
{
bool address_resolved = resolve_ping_address();
schedule_next_ping();
+ IoService.run();
+
return true;
}
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
+#include <boost/thread.hpp>
#include "dns/dnsresolver.h"
#include "link/linkstatusanalyzer.h"
{
public:
PingScheduler(
- boost::asio::io_service &io_serv,
const std::string &network_interface,
const std::string &destination_address,
const long ping_interval_in_sec,
);
~PingScheduler();
+ bool start_pinging_thread();
+ void wait_pinging_thread();
bool start_pinging();
private:
private:
/// service object, which has the event loop
- boost::asio::io_service &IoService;
+ boost::asio::io_service IoService;
/// name of the network device used to send the packets
std::string LocalNetworkInterfaceName;
/// timer to trigger the next ping
DnsResolver IpList;
/// object responsible to evaluate the status of the host
HostStatusAnalyzer HostAnalyzer;
+ /// thread object
+ boost::thread Thread;
};
bool read_success = config_reader.parse( argc, argv );
if ( read_success )
{
- io_service io_serv;
-
Configuration config = config_reader.get_configuration();
bool daemon_mode = config.get_daemon();
int ping_interval_in_sec = (*host).get_interval_in_sec();
PingSchedulerItem scheduler(
new PingScheduler(
- io_serv,
local_interface,
destination_address,
ping_interval_in_sec,
// TODO ping_loop()
BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list )
{
- bool started = scheduler->start_pinging();
+ bool started = scheduler->start_pinging_thread();
if ( !started )
{
GlobalLogger.error() << "Error: could not start pinger."
}
// Main loop to handle scheduled ping requests
- (void) io_serv.run();
+ BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list )
+ {
+ scheduler->wait_pinging_thread();
+ }
}
return 0;