clean up IcmpPaketDistributors after stopping pingers
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 26 Jan 2015 14:20:26 +0000 (15:20 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 26 Jan 2015 14:20:26 +0000 (15:20 +0100)
src/icmp/icmppaketdistributor.cpp
src/icmp/icmppaketdistributor.h
src/main.cpp

index 9910b5c..1772b32 100644 (file)
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <logfunc.hpp>
 #include <boost/bind.hpp>
+#include <boost/foreach.hpp>
 
 #include "boost_assert_handler.h"
 
@@ -65,8 +66,8 @@ bool IcmpPaketDistributorInstanceIdentifierComparator::operator() (
 // Definition of IcmpPaketDistributor
 //-----------------------------------------------------------------------------
 
-std::map<DistributorInstanceIdentifier, IcmpPaketDistributorItem,
-         IcmpPaketDistributorInstanceIdentifierComparator> IcmpPaketDistributor::Instances;
+map_type IcmpPaketDistributor::Instances;   // initialize
+
 
 IcmpPaketDistributorItem IcmpPaketDistributor::get_distributor(
         const icmp::socket::protocol_type &protocol,
@@ -187,5 +188,39 @@ bool IcmpPaketDistributor::unregister_pinger( const PingerItem old_pinger )
     return was_erased;
 }
 
+/**
+ * @brief for all instances: close sockets, unregister all pingers
+ */
+void IcmpPaketDistributor::clean_up_all()
+{
+    BOOST_FOREACH( map_type::value_type &instance, Instances )
+        instance.second->clean_up();
+
+    Instances.clear();
+}
+
+void IcmpPaketDistributor::clean_up()
+{
+    if (PingerList.size() > 0)
+        GlobalLogger.warning() << "There were still " << PingerList.size()
+            << " pingers registered in IcmpPaketDistributor!" << std::endl;
+    PingerList.clear();
+
+    boost::system::error_code error;
+    //Socket.shutdown(icmp::socket::shutdown_both, error);    // both=send and receive
+    //if ( error )
+    //    GlobalLogger.warning() << "Received error " << error << " when shutting down ICMP socket";
+    // always gave an error system:9 (probably EBADF: Bad file descriptor)
+
+    Socket.close(error);
+    if ( error )
+        GlobalLogger.warning() << "Received error " << error << " when closing ICMP socket";
+}
+
+IcmpPaketDistributor::~IcmpPaketDistributor()
+{
+    GlobalLogger.info() << "Destroying IcmpPaketDistributor" << std::endl;
+}
+
 // (created using vim -- the world's best text editor)
 
index 29310ba..fbcf711 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "host/pinger.h"
 #include "host/networkinterface.hpp"
+#include "icmp/icmppinger.h"
 
 using boost::asio::ip::icmp;
 
@@ -46,6 +47,8 @@ struct IcmpPaketDistributorInstanceIdentifierComparator
 class IcmpPaketDistributor;
 
 typedef boost::shared_ptr<IcmpPaketDistributor> IcmpPaketDistributorItem;
+typedef std::map<DistributorInstanceIdentifier, IcmpPaketDistributorItem,
+                 IcmpPaketDistributorInstanceIdentifierComparator> map_type;
 
 //-----------------------------------------------------------------------------
 // IcmpPaketDistributor
@@ -66,7 +69,9 @@ public:
             const icmp::socket::protocol_type &protocol,
             const std::string &network_interface );
 
-    ~IcmpPaketDistributor() {};
+    static void clean_up_all();
+
+    ~IcmpPaketDistributor();
 
 private:
     // hide away constructor, copy constructor and copy operator
@@ -80,6 +85,7 @@ private:
     void register_receive_handler();
     void handle_receive( const boost::system::error_code &error,
                          const size_t &bytes_transferred );
+    void clean_up();
 
 
 private:
@@ -95,8 +101,7 @@ private:
     std::set<PingerItem> PingerList;
 
     /// Instances, one for each (protocol, interface) - pair
-    static std::map<DistributorInstanceIdentifier, IcmpPaketDistributorItem,
-                    IcmpPaketDistributorInstanceIdentifierComparator> Instances;
+    static map_type Instances;
 
 };
 
index dfbc078..3b69d10 100644 (file)
@@ -41,6 +41,7 @@ on this file might be covered by the GNU General Public License.
 #include "host/pingerfactory.h"
 #include "host/pingprotocol.h"
 #include "host/pingscheduler.h"
+#include "icmp/icmppaketdistributor.h"
 
 
 using namespace std;
@@ -306,6 +307,8 @@ void stop_pingers(
     {
         scheduler->stop_pinging();
     }
+
+    IcmpPaketDistributor::clean_up_all();
 }