#include <errno.h>
#include <logfunc.hpp>
#include <boost/bind.hpp>
+#include <boost/foreach.hpp>
#include "boost_assert_handler.h"
// 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,
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)
#include "host/pinger.h"
#include "host/networkinterface.hpp"
+#include "icmp/icmppinger.h"
using boost::asio::ip::icmp;
class IcmpPaketDistributor;
typedef boost::shared_ptr<IcmpPaketDistributor> IcmpPaketDistributorItem;
+typedef std::map<DistributorInstanceIdentifier, IcmpPaketDistributorItem,
+ IcmpPaketDistributorInstanceIdentifierComparator> map_type;
//-----------------------------------------------------------------------------
// IcmpPaketDistributor
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
void register_receive_handler();
void handle_receive( const boost::system::error_code &error,
const size_t &bytes_transferred );
+ void clean_up();
private:
std::set<PingerItem> PingerList;
/// Instances, one for each (protocol, interface) - pair
- static std::map<DistributorInstanceIdentifier, IcmpPaketDistributorItem,
- IcmpPaketDistributorInstanceIdentifierComparator> Instances;
+ static map_type Instances;
};
#include "host/pingerfactory.h"
#include "host/pingprotocol.h"
#include "host/pingscheduler.h"
+#include "icmp/icmppaketdistributor.h"
using namespace std;
{
scheduler->stop_pinging();
}
+
+ IcmpPaketDistributor::clean_up_all();
}