Advertisement
abagrintsev

PingProcess.cpp

Sep 3rd, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.44 KB | None | 0 0
  1. #include "PingProcess.h"
  2. #include "Ping.h"
  3. #include "System.h"
  4. #include <Logging_Controller.h>
  5.  
  6. PingProcess::PingProcess ( Ping *parent )
  7. {
  8.     m_timer = NULL;
  9.     m_resolver = NULL;
  10.     m_reply_buffer = NULL;
  11.     m_num_replies = 0;
  12.     m_num_timeout = 0;
  13.     m_socket = NULL;
  14.     m_camera_availability = true;
  15.  
  16.     this->parent = parent;
  17.    
  18.     KSVC_TRACE ( PING_SERVICE ) << "Created new ping process";
  19. }
  20.  
  21. PingProcess::~PingProcess()
  22. {
  23.     delete m_timer;
  24.     m_timer = NULL;
  25.     delete m_reply_buffer;
  26.     m_reply_buffer = NULL;
  27.     delete m_resolver;
  28.     m_resolver = NULL;
  29.    
  30.     m_socket->close();
  31.     delete m_socket;
  32.     m_socket = NULL;
  33.    
  34.     KSVC_LOG_INF ( PING_SERVICE ) << "Ping process for host " << m_destination.address() << " is destoyed";
  35. }
  36.  
  37. /*
  38.  
  39. PingProcess::PingProcess ( const PingProcess& other )
  40. {
  41.  
  42. }
  43.  
  44. PingProcess& PingProcess::operator= ( const PingProcess& other )
  45. {
  46.  
  47. }
  48.  
  49. bool PingProcess::operator== ( const PingProcess& other ) const
  50. {
  51.  
  52. }*/
  53.  
  54. bool PingProcess::Init()
  55. {
  56.     m_reply_buffer = new boost::asio::streambuf;
  57.     m_timer = new boost::asio::deadline_timer ( parent->m_io_service );
  58.     m_socket = new boost::asio::ip::icmp::socket ( parent->m_io_service, boost::asio::ip::icmp::v4() );
  59.    
  60.     return true;
  61. }
  62.  
  63.  
  64. void PingProcess::StartSend()
  65. {
  66.     if ( NULL == m_socket )
  67.         return;
  68.    
  69.     std::string body ( "KSVD testing ICMP request" );
  70.    
  71.     // Create an ICMP header for an echo request.
  72.     icmp_header echo_request;
  73.     echo_request.type ( icmp_header::echo_request );
  74.     echo_request.code ( 0 );
  75.     echo_request.identifier ( m_ping_process_id );
  76.    
  77.     echo_request.sequence_number ( ++m_sequence_number );
  78.     compute_checksum ( echo_request, body.begin(), body.end() );
  79.    
  80.     // Encode the request packet.
  81.     boost::asio::streambuf request_buffer;
  82.     std::ostream os ( &request_buffer );
  83.     os << echo_request << body;
  84.    
  85.     // Send the request.
  86.     m_time_sent = posix_time::microsec_clock::universal_time();
  87.     m_socket->send_to ( request_buffer.data(), m_destination );
  88.    
  89.     // Wait up to five seconds for a reply.
  90.     m_num_replies = 0;
  91.     m_timer->expires_at ( m_time_sent + boost::posix_time::seconds ( timeout_to_read_reply ) );
  92.     m_timer->async_wait ( boost::bind ( &PingProcess::HandleTimeout, this ) );
  93.    
  94.     //KSVC_TRACE ( PING_SERVICE ) << "In func: " << __FUNCTION__;
  95. }
  96.  
  97. void PingProcess::StartReceive()
  98. {
  99.     if ( NULL == m_socket )
  100.         return;
  101.    
  102.     // Discard any data already in the buffer.
  103.     m_reply_buffer->consume ( m_reply_buffer->size() );
  104.    
  105.     // Wait for a reply. We prepare the buffer to receive up to 64KB.
  106.     m_socket->async_receive ( m_reply_buffer->prepare ( 65536 ), boost::bind ( &PingProcess::HandleReceive, this, _2 ) );
  107.    
  108.     //KSVC_TRACE ( PING_SERVICE ) << "In func: " << __FUNCTION__;
  109. }
  110.  
  111. void PingProcess::HandleTimeout()
  112. {
  113.     if ( m_num_replies == 0 )
  114.     {
  115.         // just warn if pong does't returned
  116.         KSVC_LOG_WAR ( PING_SERVICE ) << "Request timed out for host " << m_destination.address();
  117.        
  118.         // the host is considered unreachable here
  119.         if ( ( m_num_timeout++ ) >= timeout_to_fail )
  120.         {
  121.             SetCameraUnavailable ( UUID );
  122.         }
  123.     } else {
  124.    
  125.         // we are changing the status only if there is no answer N times in a row
  126.         m_num_timeout = 0;
  127.        
  128.         // Pong has arrived so update status
  129.         if ( m_camera_availability == false )
  130.         {
  131.             SetCameraAvailable ( UUID );
  132.         }
  133.     }  
  134.  
  135.     // Requests must be sent no less than one second apart.
  136.     m_timer->expires_at ( m_time_sent + boost::posix_time::seconds ( timeout_to_send_request ) );
  137.     m_timer->async_wait ( boost::bind ( &PingProcess::StartSend, this ) );
  138.    
  139.     //KSVC_TRACE ( PING_SERVICE ) << "In func: " << __FUNCTION__;
  140. }
  141.  
  142. void PingProcess::HandleReceive ( std::size_t length )
  143. {
  144.     if ( NULL == m_reply_buffer )
  145.         return;
  146.    
  147.     // The actual number of bytes received is committed to the buffer so that we
  148.     // can extract it using a std::istream object.
  149.     m_reply_buffer->commit ( length );
  150.    
  151.     // Decode the reply packet.
  152.     std::istream is ( m_reply_buffer );
  153.     ipv4_header ipv4_hdr;
  154.     icmp_header icmp_hdr;
  155.     is >> ipv4_hdr >> icmp_hdr;
  156.    
  157.     // We can receive all ICMP packets received by the host, so we need to
  158.     // filter out only the echo replies that match the our identifier and
  159.     // expected sequence number.
  160.     if ( is && icmp_hdr.type() == icmp_header::echo_reply
  161.         && icmp_hdr.identifier() == m_ping_process_id
  162.         && icmp_hdr.sequence_number() == m_sequence_number )
  163.     {
  164.         // If this is the first reply, interrupt the five second timeout.
  165.         if ( m_num_replies++ == 0 )
  166.             m_timer->cancel();
  167.        
  168.         // Print out some information about the reply packet.
  169.         boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
  170.        
  171.         KSVC_LOG_INF ( PING_SERVICE ) << length - ipv4_hdr.header_length()
  172.             << " bytes from " << ipv4_hdr.source_address()
  173.             << ": icmp_seq=" << icmp_hdr.sequence_number()
  174.             << ", ttl=" << ipv4_hdr.time_to_live()
  175.             << ", time=" << ( now - m_time_sent ).total_milliseconds() << " ms";
  176.     }
  177.    
  178.     StartReceive();
  179.     //KSVC_TRACE ( PING_SERVICE ) << "In func: " << __FUNCTION__;
  180. }
  181.  
  182. bool PingProcess::Query ( std::string destination )
  183. {
  184.     boost::asio::ip::icmp::resolver::query query ( boost::asio::ip::icmp::v4(), destination.c_str(), "" );
  185.     m_resolver = new boost::asio::ip::icmp::resolver ( parent->m_io_service );
  186.     m_destination = *( m_resolver->resolve ( query ) );
  187.    
  188.     StartSend();
  189.     StartReceive();
  190. }
  191.  
  192. /*short unsigned int PingProcess::GetIdentidier()
  193. {
  194.     #if defined ( BOOST_WINDOWS )
  195.     return static_cast < unsigned short > ( ::GetCurrentProcessId() );
  196.     #else
  197.     return static_cast < unsigned short > ( ::getpid() );
  198.     #endif
  199. }*/
  200.  
  201. void PingProcess::SetUUID ( std::string uuid )
  202. {
  203.     UUID = uuid;
  204. }
  205.  
  206. void PingProcess::SetCameraUnavailable ( std::string uuid )
  207. {
  208.     m_num_timeout = 0;
  209.    
  210.     //boost::property_tree::ptree tree;
  211.     Configuration& conf = Configuration::Instance();
  212.     conf.SetCameraAvailability ( uuid, false );
  213.     m_camera_availability = false;
  214.     //tree.put ( ".is_available", "false" );
  215.     //conf.SetCameraSettings ( G_SYSTEM_USER, "system", uuid.c_str(), tree, true );
  216.    
  217.     KSVC_LOG_INF ( PING_SERVICE ) << "Host " << m_destination.address() << " has changed status to not available";
  218. }
  219.  
  220. void PingProcess::SetCameraAvailable ( std::string uuid )
  221. {
  222.     m_num_timeout = 0;
  223.     Configuration& conf = Configuration::Instance();
  224.     conf.SetCameraAvailability ( uuid, true );
  225.     m_camera_availability = true;
  226.    
  227.     KSVC_LOG_INF ( PING_SERVICE ) << "Host " << m_destination.address() << " has changed status to available";
  228. }
  229.  
  230. void PingProcess::SetPingProcessID ( int ID )
  231. {
  232.     m_ping_process_id = ID;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement