From 2d59123544d90f444ce8904da797a0d4d1906ada Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Mon, 21 Feb 2011 10:43:54 +0100 Subject: [PATCH] Added Effective C++ warnings level (-Weffc++) - Detects possible misuses of C++ features --- CMakeLists.txt | 4 +++- src/ping/boostpinger.cpp | 29 +++++++++++++++++++---------- src/ping/boostpinger.h | 5 ++--- src/ping/host.cpp | 27 ++++++++++++++++----------- src/ping/host.h | 20 ++++++++++---------- src/ping/pingcheck.cpp | 29 ++++++++++------------------- src/ping/pingcheck.h | 6 ++++-- src/ping/pingmanager.cpp | 18 ++++++++++++++++-- src/ping/pingmanager.h | 5 ++++- 9 files changed, 84 insertions(+), 59 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea9aa73..2ddab70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,9 @@ set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} ) set( CMAKE_VERBOSE_MAKEFILE OFF ) # add definitions, compiler switches, etc. -add_definitions( -Werror -Wall -Wextra -O2 ) +add_definitions( + -Werror -Wall -Wextra -Weffc++ -O2 -DVERSION_STRING=${VERSION_STRING} +) # more build in the source folder add_subdirectory( src ) diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index 9119a89..e4b42cc 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -11,6 +11,8 @@ #include "ipv4_header.h" #include "boostpinger.h" +namespace posix_time = boost::posix_time; + //----------------------------------------------------------------------------- // BoostPinger //----------------------------------------------------------------------------- @@ -18,8 +20,14 @@ BoostPinger::BoostPinger( boost::asio::io_service& io_service ) : - resolver( io_service ), timer( io_service ), sequence_number( 0 ), - num_replies( 0 ), socket( io_service, icmp::v4() ) + resolver( io_service ), + destination_endpoint(), + socket( io_service, icmp::v4() ), + timer( io_service ), + sequence_number( 0 ), + time_sent( posix_time::microsec_clock::universal_time() ), + reply_buffer( 0 ), + num_replies( 0 ) { #if 0 try @@ -42,8 +50,9 @@ BoostPinger::~BoostPinger() void BoostPinger::ping( const Host &host ) { - std::string destination = host.getIp(); + std::string destination = host.get_ip(); // TODO what if it is an DNS? + // TODO what about ping multiple hosts? icmp::resolver::query query( icmp::v4(), destination, "" ); destination_endpoint = *resolver.resolve( query ); @@ -82,7 +91,7 @@ void BoostPinger::start_send() void BoostPinger::handle_timeout() { - if (num_replies == 0) + if ( num_replies == 0 ) std::cout << "Request timed out" << std::endl; // Requests must be sent no less than one second apart. @@ -98,7 +107,8 @@ void BoostPinger::start_receive() // Wait for a reply. We prepare the buffer to receive up to 64KB. socket.async_receive( reply_buffer.prepare( 65536 ), - boost::bind( &BoostPinger::handle_receive, this, _2 ) ); + boost::bind( &BoostPinger::handle_receive, this, _2 ) + ); } void BoostPinger::handle_receive( std::size_t length ) @@ -116,12 +126,12 @@ void BoostPinger::handle_receive( std::size_t length ) // We can receive all ICMP packets received by the host, so we need to // filter out only the echo replies that match the our identifier and // expected sequence number. - if (is && icmp_hdr.type() == IcmpHeader::EchoReply + if ( is && icmp_hdr.type() == IcmpHeader::EchoReply && icmp_hdr.identifier() == get_identifier() - && icmp_hdr.sequence_number() == sequence_number) + && icmp_hdr.sequence_number() == sequence_number ) { // If this is the first reply, interrupt the five second timeout. - if (num_replies++ == 0) + if ( num_replies++ == 0 ) timer.cancel(); // Print out some information about the reply packet. @@ -130,8 +140,7 @@ void BoostPinger::handle_receive( std::size_t length ) << ipv4_hdr.source_address() << ": icmp_seq=" << icmp_hdr.sequence_number() << ", ttl=" << ipv4_hdr.time_to_live() << ", time=" - << (now - time_sent).total_milliseconds() << " ms" - << std::endl; + << (now - time_sent).total_milliseconds() << " ms" << std::endl; } start_receive(); diff --git a/src/ping/boostpinger.h b/src/ping/boostpinger.h index a7c9f3f..734ca24 100644 --- a/src/ping/boostpinger.h +++ b/src/ping/boostpinger.h @@ -9,8 +9,6 @@ using boost::asio::ip::address; using boost::asio::ip::icmp; using boost::asio::deadline_timer; -namespace posix_time = boost::posix_time; - //----------------------------------------------------------------------------- // BoostPinger //----------------------------------------------------------------------------- @@ -32,12 +30,13 @@ private: static uint16_t get_identifier(); +private: icmp::resolver resolver; icmp::endpoint destination_endpoint; icmp::socket socket; deadline_timer timer; uint16_t sequence_number; - posix_time::ptime time_sent; + boost::posix_time::ptime time_sent; boost::asio::streambuf reply_buffer; std::size_t num_replies; diff --git a/src/ping/host.cpp b/src/ping/host.cpp index 4ee1c19..1476a9c 100644 --- a/src/ping/host.cpp +++ b/src/ping/host.cpp @@ -1,6 +1,11 @@ #include "host.h" -Host::Host( std::string address ) +Host::Host( std::string address ) : + ip( "" ), + dns( address ), + port( 0 ), + interval( 0 ), + options() { } @@ -8,51 +13,51 @@ Host::~Host() { } -std::string Host::getDns() const +std::string Host::get_dns() const { return dns; } -void Host::setDns( std::string dns ) +void Host::set_dns( std::string dns ) { this->dns = dns; } -uint32_t Host::getInterval() const +uint32_t Host::get_interval() const { return interval; } -void Host::setInterval( uint32_t interval ) +void Host::set_interval( uint32_t interval ) { this->interval = interval; } -std::string Host::getIp() const +std::string Host::get_ip() const { return ip; } -void Host::setIp( std::string ip ) +void Host::set_ip( std::string ip ) { this->ip = ip; } -std::vector Host::getOptions() const +std::vector Host::get_options() const { return options; } -void Host::setOptions( std::vector options ) +void Host::set_options( std::vector options ) { this->options = options; } -uint16_t Host::getPort() const +uint16_t Host::get_port() const { return port; } -void Host::setPort( uint16_t port ) +void Host::set_port( uint16_t port ) { this->port = port; } diff --git a/src/ping/host.h b/src/ping/host.h index 36b8772..f07aafb 100644 --- a/src/ping/host.h +++ b/src/ping/host.h @@ -12,20 +12,20 @@ public: Host( std::string address ); virtual ~Host(); - std::string getDns() const; - void setDns( std::string dns ); + std::string get_dns() const; + void set_dns( std::string dns ); - uint32_t getInterval() const; - void setInterval( uint32_t interval ); + uint32_t get_interval() const; + void set_interval( uint32_t interval ); - std::string getIp() const; - void setIp( std::string ip ); + std::string get_ip() const; + void set_ip( std::string ip ); - std::vector getOptions() const; - void setOptions( std::vector options ); + std::vector get_options() const; + void set_options( std::vector options ); - uint16_t getPort() const; - void setPort( uint16_t port ); + uint16_t get_port() const; + void set_port( uint16_t port ); private: std::string ip; diff --git a/src/ping/pingcheck.cpp b/src/ping/pingcheck.cpp index 262b3f8..af8288a 100644 --- a/src/ping/pingcheck.cpp +++ b/src/ping/pingcheck.cpp @@ -1,36 +1,27 @@ -#include "configurationreader.h" - #include "pingcheck.h" PingCheck::PingCheck() : - configuration( NULL ) + configuration( NULL ), + ping_managers() { +} +PingCheck::PingCheck( const PingCheck& ) : + configuration( NULL ), + ping_managers() +{ } PingCheck::~PingCheck() { } -void PingCheck::read_configuration() +PingCheck& PingCheck::operator=( const PingCheck& ) { - // sends the program command line to be parsed by the configuration reader - // check if it reads correctly - - // - // - // - // - // - // - // - // - // - // - // + return *this; } -void PingCheck::init_ping_managers() +void PingCheck::init_ping_managers() const { // starts N ping managers with a host and an interval, this was read in configuration // for each host in configuration diff --git a/src/ping/pingcheck.h b/src/ping/pingcheck.h index fafb59f..965cfa9 100644 --- a/src/ping/pingcheck.h +++ b/src/ping/pingcheck.h @@ -10,10 +10,12 @@ class PingCheck { public: PingCheck(); + PingCheck( const PingCheck& other ); virtual ~PingCheck(); - void read_configuration(); - void init_ping_managers(); + PingCheck& operator=( const PingCheck& other ); + + void init_ping_managers() const; private: Configuration *configuration; diff --git a/src/ping/pingmanager.cpp b/src/ping/pingmanager.cpp index 9ceee4e..680cf06 100644 --- a/src/ping/pingmanager.cpp +++ b/src/ping/pingmanager.cpp @@ -1,14 +1,28 @@ #include "pingmanager.h" -PingManager::PingManager() +PingManager::PingManager() : + pinger( NULL ), + interval( 0 ), + host( NULL ) { +} +PingManager::PingManager( const PingManager& ) : + pinger( NULL ), + interval( 0 ), + host( NULL ) +{ } PingManager::~PingManager() { } -void PingManager::ping() +PingManager& PingManager::operator=( const PingManager& ) +{ + return *this; +} + +void PingManager::ping() const { } diff --git a/src/ping/pingmanager.h b/src/ping/pingmanager.h index de79ce2..8619a41 100644 --- a/src/ping/pingmanager.h +++ b/src/ping/pingmanager.h @@ -8,9 +8,12 @@ class PingManager { public: PingManager(); + PingManager( const PingManager& ); virtual ~PingManager(); - void ping(); + PingManager& operator=( const PingManager& ); + + void ping() const; private: Pinger *pinger; -- 1.7.1