Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Ping.h"
- #include "System.h"
- Ping::Ping()
- {
- m_thread = NULL;
- }
- Ping::~Ping()
- {
- Ping::process::iterator it;
- for ( it = m_processes.begin(); it != m_processes.end(); ++it )
- {
- //std::cout << it->first << " = " << it->second << std::endl;
- if ( NULL != it->second )
- {
- delete it->second;
- }
- }
- m_processes.clear();
- delete m_thread;
- try
- {
- KSVC_LOG_INF ( PING_SERVICE ) << "Ping service is gracefully shutdowned";
- } catch (...) {}
- }
- bool Ping::Init()
- {
- // Register signal handlers so that the daemon may be shut down.
- //m_signals->async_wait ( boost::bind ( &boost::asio::io_service::stop, &m_io_service ) );
- //AddPingProcess("127.0.0.1", "localhost");
- try
- {
- KSVC_LOG_INF ( PING_SERVICE ) << "Pinger has started";
- } catch (...) {}
- return true;
- }
- void Ping::AddPingProcess ( std::string destination, std::string uuid )
- {
- PingProcess *npp = 0;
- try {
- npp = new PingProcess ( this );
- Configuration::Instance().SetCameraAvailability ( uuid, -1 );
- m_processes [ uuid ] = npp;
- npp->SetUUID ( uuid );
- npp->SetPingProcessID ( m_processes.size() );
- npp->Init();
- npp->Query ( destination );
- Reload();
- KSVC_LOG_INF ( PING_SERVICE ) << "New ping process for host " << destination << " was added";
- }
- catch ( std::exception &e )
- {
- KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << " Host '" << destination << "': " << e.what() << std::endl;
- }
- }
- bool Ping::DeletePingProcess ( std::string uuid )
- {
- process::iterator it;
- if ( ( it = m_processes.find ( uuid ) ) == m_processes.end() )
- {
- return false;
- }
- //m_io_service.stop();
- delete it->second;
- it->second = NULL;
- m_processes.erase ( it );
- Reload();
- //m_io_service.run();
- try
- {
- KSVC_LOG_INF ( PING_SERVICE ) << "Ping process for UUID " << uuid << " was deleted.";
- } catch (...) {}
- return true;
- }
- void Ping::Worker()
- {
- try {
- Init();
- try {
- Configuration& conf = Configuration::Instance();
- boost::property_tree::ptree cameras = conf.GetCameras().get_child ( "Sources" );
- BOOST_FOREACH ( boost::property_tree::ptree::value_type &v, cameras )
- {
- std::string uuid = v.first,
- ip = v.second.get < std::string > ( "ip" );
- AddPingProcess ( ip, uuid );
- }
- } catch ( std::exception &e ) {
- KSVC_LOG_TRC ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << " " << e.what() << std::endl;
- }
- // Blocking function. The boost::asio got his job and starts working here
- m_work.reset(new boost::asio::io_service::work(m_io_service));
- m_io_service.run();
- KSVC_LOG_TRC ( PING_SERVICE ) << "Ping worker has exited";
- } catch ( std::exception &e ) {
- KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << " " << e.what() << std::endl;
- }
- }
- int Ping::Exec()
- {
- try
- {
- m_thread = new boost::thread ( boost::bind ( &Ping::Worker, this ) );
- //Worker();
- } catch ( std::exception &e ) {
- KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << " " << e.what() << std::endl;
- return 1;
- }
- return 0;
- }
- void Ping::Reload()
- {
- //m_io_service.stop();
- m_io_service.reset();
- }
- void Ping::Stop()
- {
- m_work.reset(); // Останавливает цикл обработки после окончания текущих работ
- m_io_service.stop(); // Принудительно останавливает все текущие работы
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement