Advertisement
abagrintsev

Ping.cpp

Sep 3rd, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include "Ping.h"
  2. #include "System.h"
  3.  
  4. Ping *Ping::self = NULL;
  5.  
  6. Ping::Ping()
  7. {
  8.     m_thread = NULL;
  9.  
  10.     self = this;
  11. }
  12.  
  13. Ping::~Ping()
  14. {
  15.     Ping::process::iterator it;
  16.    
  17.     for ( it = m_processes.begin(); it != m_processes.end(); ++it )
  18.     {      
  19.         if ( NULL != it->second )
  20.         {
  21.             delete it->second;
  22.         }
  23.     }
  24.    
  25.     m_processes.clear();
  26.    
  27.     delete m_thread;
  28.    
  29.     KSVC_LOG_INF ( PING_SERVICE ) << "Ping service is gracefully shutdowned";
  30. }
  31.  
  32. /*Ping& Ping::operator= ( const Ping& other )
  33. {
  34.  
  35. }*/
  36.  
  37. /*bool Ping::operator== ( const Ping& other ) const
  38. {
  39.  
  40. }*/
  41.  
  42. bool Ping::Init()
  43. {
  44.     // Register signal handlers so that the daemon may be shut down.
  45.     //m_signals->async_wait ( boost::bind ( &boost::asio::io_service::stop, &m_io_service ) );
  46.    
  47.     KSVC_LOG_INF ( PING_SERVICE ) << "Pinger has started";
  48.    
  49.     return true;
  50. }
  51.  
  52. void Ping::AddPingProcess ( std::string destination, std::string uuid )
  53. {
  54.     try {
  55.         PingProcess *npp = new PingProcess ( this );
  56.         m_processes [ destination ] = npp;
  57.         npp->SetUUID ( uuid );
  58.         npp->SetPingProcessID ( m_processes.size() );
  59.         npp->Init();
  60.         npp->Query ( destination );
  61.        
  62.         KSVC_LOG_INF ( PING_SERVICE ) << "New ping process for host " << destination << " was added";
  63.        
  64.     } catch  ( std::exception &e ) {
  65.        
  66.         KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << "  " << e.what() << std::endl;
  67.     }
  68. }
  69.  
  70. void Ping::Worker()
  71. {
  72.     try {
  73.        
  74.         self->Init();
  75.            
  76.         Configuration& conf = Configuration::Instance();
  77.         boost::property_tree::ptree cameras = conf.GetCameras().get_child ( "Sources" );
  78.        
  79.         BOOST_FOREACH ( boost::property_tree::ptree::value_type &v, cameras )
  80.         {
  81.             std::string uuid = v.first,
  82.             ip = v.second.get < std::string > ( "ip" );
  83.             //std::cout << " here " << ip << std::endl;
  84.             self->AddPingProcess ( ip, uuid );
  85.             //boost::property_tree::write_json ( std::cout, v.second );
  86.         }
  87.        
  88.         // Blocking function. The boost::asio got his job and starts working here
  89.         self->m_io_service.run();
  90.    
  91.     } catch ( std::exception &e ) {
  92.        
  93.         KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << "  " << e.what() << std::endl;
  94.     }
  95. }
  96.  
  97. int Ping::Exec()
  98. {
  99.     try
  100.     {
  101.         m_thread = new boost::thread ( boost::bind ( &Ping::Worker, this ) );
  102.        
  103.         //Worker();
  104.  
  105.     } catch ( std::exception &e ) {
  106.        
  107.         KSVC_LOG_ERR ( PING_SERVICE ) << "EXCEPTION: " << __FILE__ << "::" << __LINE__ << "::" << __FUNCTION__ << "  " << e.what() << std::endl;
  108.         return 1;
  109.     }
  110.    
  111.     return 0;
  112. }
  113.  
  114. void Ping::Stop()
  115. {
  116.     m_io_service.stop();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement