Advertisement
abagrintsev

Ping.cpp (new)

Sep 3rd, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include "Ping.h"
  2. #include "System.h"
  3.  
  4.  
  5.  
  6. Ping::Ping()
  7. {
  8.     m_thread = NULL;
  9. }
  10.  
  11.  
  12. Ping::~Ping()
  13. {
  14.     Ping::process::iterator it;
  15.    
  16.     for ( it = m_processes.begin(); it != m_processes.end(); ++it )
  17.     {
  18.         //std::cout << it->first << " = " << it->second << std::endl;
  19.        
  20.         if ( NULL != it->second )
  21.         {
  22.             delete it->second;
  23.         }
  24.     }
  25.    
  26.     m_processes.clear();
  27.    
  28.     delete m_thread;
  29.    
  30.     try
  31.     {
  32.         KSVC_LOG_INF ( PING_SERVICE ) << "Ping service is gracefully shutdowned";
  33.     } catch (...) {}
  34.  
  35. }
  36.  
  37.  
  38. bool Ping::Init()
  39. {
  40.     // Register signal handlers so that the daemon may be shut down.
  41.     //m_signals->async_wait ( boost::bind ( &boost::asio::io_service::stop, &m_io_service ) );
  42.  
  43.   //AddPingProcess("127.0.0.1", "localhost");
  44.  
  45.     try
  46.     {
  47.         KSVC_LOG_INF ( PING_SERVICE ) << "Pinger has started";
  48.     } catch (...) {}
  49.    
  50.     return true;
  51. }
  52.  
  53.  
  54. void Ping::AddPingProcess ( std::string destination, std::string uuid )
  55. {
  56.   PingProcess *npp = 0;
  57.     try {
  58.         npp = new PingProcess ( this );
  59.         Configuration::Instance().SetCameraAvailability ( uuid, -1 );
  60.         m_processes [ uuid ] = npp;
  61.         npp->SetUUID ( uuid );
  62.         npp->SetPingProcessID ( m_processes.size() );
  63.         npp->Init();
  64.         npp->Query ( destination );
  65.         Reload();
  66.         KSVC_LOG_INF ( PING_SERVICE ) << "New ping process for host " << destination << " was added";
  67.     }
  68.     catch  ( std::exception &e )
  69.     {
  70.         KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << "  Host '" << destination << "': " << e.what() << std::endl;
  71.     }
  72. }
  73.  
  74.  
  75. bool Ping::DeletePingProcess ( std::string uuid )
  76. {
  77.  
  78.     process::iterator it;
  79.     if ( ( it = m_processes.find ( uuid ) ) == m_processes.end() )
  80.     {
  81.         return false;
  82.     }
  83.    
  84.     //m_io_service.stop();
  85.  
  86.     delete it->second;
  87.     it->second = NULL;
  88.    
  89.     m_processes.erase ( it );
  90.    
  91.     Reload();
  92.  
  93.     //m_io_service.run();
  94.    
  95.     try
  96.     {
  97.         KSVC_LOG_INF ( PING_SERVICE ) << "Ping process for UUID " << uuid << " was deleted.";
  98.     } catch (...) {}
  99.        
  100.     return true;
  101. }
  102.  
  103. void Ping::Worker()
  104. {
  105.   try {
  106.  
  107.     Init();
  108.  
  109.     try {
  110.  
  111.       Configuration& conf = Configuration::Instance();
  112.       boost::property_tree::ptree cameras = conf.GetCameras().get_child ( "Sources" );
  113.  
  114.       BOOST_FOREACH ( boost::property_tree::ptree::value_type &v, cameras )
  115.       {
  116.         std::string uuid = v.first,
  117.             ip = v.second.get < std::string > ( "ip" );
  118.         AddPingProcess ( ip, uuid );
  119.       }
  120.     } catch ( std::exception &e ) {
  121.       KSVC_LOG_TRC ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << "  " << e.what() << std::endl;
  122.     }
  123.  
  124.     // Blocking function. The boost::asio got his job and starts working here
  125.     m_work.reset(new boost::asio::io_service::work(m_io_service));
  126.     m_io_service.run();
  127.     KSVC_LOG_TRC ( PING_SERVICE ) << "Ping worker has exited";
  128.  
  129.   } catch ( std::exception &e ) {
  130.     KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << "  " << e.what() << std::endl;
  131.   }
  132. }
  133.  
  134. int Ping::Exec()
  135. {
  136.     try
  137.     {
  138.         m_thread = new boost::thread ( boost::bind ( &Ping::Worker, this ) );
  139.        
  140.         //Worker();
  141.  
  142.     } catch ( std::exception &e ) {
  143.        
  144.         KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << "  " << e.what() << std::endl;
  145.         return 1;
  146.     }
  147.    
  148.     return 0;
  149. }
  150.  
  151. void Ping::Reload()
  152. {
  153.     //m_io_service.stop();
  154.     m_io_service.reset();
  155. }
  156.  
  157. void Ping::Stop()
  158. {
  159.   m_work.reset();       // Останавливает цикл обработки после окончания текущих работ
  160.   m_io_service.stop();  // Принудительно останавливает все текущие работы
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement