From: g0dil Date: Fri, 17 Aug 2007 08:08:43 +0000 (+0000) Subject: PPI: Fix resource management on module destruction X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=122719831e4d0ce7c335a66ed7c80a7ffc2e00e8;p=senf.git PPI: Fix resource management on module destruction git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@394 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/PPI/Connectors.cci b/PPI/Connectors.cci index a046feb..1f3f192 100644 --- a/PPI/Connectors.cci +++ b/PPI/Connectors.cci @@ -53,7 +53,10 @@ prefix_ senf::ppi::connector::Connector::Connector() {} prefix_ senf::ppi::connector::Connector::~Connector() -{} +{ + if (peer_) + peer_->peer_ = 0; +} prefix_ void senf::ppi::connector::Connector::connect(Connector & target) { diff --git a/PPI/Connectors.test.cc b/PPI/Connectors.test.cc index 80548f2..feb7266 100644 --- a/PPI/Connectors.test.cc +++ b/PPI/Connectors.test.cc @@ -161,6 +161,8 @@ namespace { class PassiveInputTest : public ppi::module::Module { + SENF_PPI_MODULE(PassiveInputTest); + public: ppi::connector::PassiveInput input; diff --git a/PPI/DebugModules.hh b/PPI/DebugModules.hh index 1a38d4b..14fbb02 100644 --- a/PPI/DebugModules.hh +++ b/PPI/DebugModules.hh @@ -44,6 +44,8 @@ namespace debug { : public Module, public SafeBool { + SENF_PPI_MODULE(ActivePacketSource); + public: connector::ActiveOutput output; @@ -57,6 +59,8 @@ namespace debug { class PassivePacketSource : public Module { + SENF_PPI_MODULE(PassivePacketSource); + typedef std::deque Queue; public: @@ -82,6 +86,8 @@ namespace debug { : public Module, public SafeBool { + SENF_PPI_MODULE(ActivePacketSink); + public: connector::ActiveInput input; @@ -95,6 +101,8 @@ namespace debug { class PassivePacketSink : public Module { + SENF_PPI_MODULE(PassivePacketSink); + typedef std::deque Queue; public: diff --git a/PPI/EventManager.cc b/PPI/EventManager.cc new file mode 100644 index 0000000..1b3e16b --- /dev/null +++ b/PPI/EventManager.cc @@ -0,0 +1,67 @@ +// $Id$ +// +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief EventManager non-inline non-template implementation */ + +#include "EventManager.hh" +//#include "EventManager.ih" + +// Custom includes +#include + +//#include "EventManager.mpp" +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// senf::ppi::EventManager + +//////////////////////////////////////// +// private members + +prefix_ void senf::ppi::EventManager::destroyModule(module::Module & module) +{ + using boost::lambda::_1; + + // boost::ptr_vector::erase(f,l) asserts !empty() .. why ?? + if (!registrations_.empty()) + registrations_.erase( + std::remove_if(registrations_.begin(), registrations_.end(), + ((&_1) ->* & detail::EventBindingBase::module_) == & module), + registrations_.end()); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ +//#include "EventManager.mpp" + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/PPI/EventManager.hh b/PPI/EventManager.hh index 0c428ca..18f41d1 100644 --- a/PPI/EventManager.hh +++ b/PPI/EventManager.hh @@ -84,6 +84,8 @@ namespace ppi { protected: private: + void destroyModule(module::Module & module); + typedef boost::ptr_vector EventRegistrations; EventRegistrations registrations_; @@ -92,6 +94,7 @@ namespace ppi { boost::posix_time::ptime eventTime_; friend class detail::EventBindingBase; + friend class module::Module; }; }} diff --git a/PPI/Module.cci b/PPI/Module.cci index 382ac57..533fbd3 100644 --- a/PPI/Module.cci +++ b/PPI/Module.cci @@ -59,6 +59,11 @@ prefix_ void senf::ppi::module::Module::noroute(connector::Connector & connector connector.setModule(*this); } +prefix_ void senf::ppi::module::Module::destroy() +{ + eventManager().destroyModule(*this); +} + //////////////////////////////////////// // private members diff --git a/PPI/Module.hh b/PPI/Module.hh index 0002b05..c1b0a98 100644 --- a/PPI/Module.hh +++ b/PPI/Module.hh @@ -124,6 +124,10 @@ namespace module { boost::posix_time::ptime eventTime(); ///< Return timestamp of the currently processing ///< event + void destroy(); + + virtual void macro_SENF_PPI_MODULE_missing() = 0; + private: virtual void init(); @@ -144,13 +148,11 @@ namespace module { friend class senf::ppi::ModuleManager; }; - /** \brief Connect compatible connectors - - connect() will connect two compatible connectors: One connector must be active, the other - passive. - */ - template - void connect(Source const & source, Target const & target); +# define SENF_PPI_MODULE(name) \ + public: \ + ~ name() { destroy(); } \ + void macro_SENF_PPI_MODULE_missing() {} \ + private: }}} diff --git a/PPI/Module.test.cc b/PPI/Module.test.cc index 1fe9b7b..c54ef51 100644 --- a/PPI/Module.test.cc +++ b/PPI/Module.test.cc @@ -44,6 +44,8 @@ namespace debug = ppi::module::debug; namespace { class TestModule : public ppi::module::Module { + SENF_PPI_MODULE(TestModule); + public: connector::ActiveOutput output; diff --git a/PPI/Route.test.cc b/PPI/Route.test.cc index a70d869..f31b8a9 100644 --- a/PPI/Route.test.cc +++ b/PPI/Route.test.cc @@ -47,6 +47,8 @@ namespace debug = module::debug; namespace { class RouteTester : public module::Module { + SENF_PPI_MODULE(RouteTester); + public: connector::ActiveInput activeIn; connector::PassiveInput passiveIn; diff --git a/PPI/SocketReader.hh b/PPI/SocketReader.hh index 7c348f2..5c0ca9e 100644 --- a/PPI/SocketReader.hh +++ b/PPI/SocketReader.hh @@ -91,6 +91,8 @@ namespace module { class ActiveSocketReader : public Module { + SENF_PPI_MODULE(ActiveSocketReader); + public: typedef typename Reader::Handle Handle; ///< Handle type requested by the reader diff --git a/PPI/SocketWriter.hh b/PPI/SocketWriter.hh index 233322c..263f349 100644 --- a/PPI/SocketWriter.hh +++ b/PPI/SocketWriter.hh @@ -87,6 +87,8 @@ namespace module { template class ActiveSocketWriter : public Module { + SENF_PPI_MODULE(ActiveSocketWriter); + public: typedef typename Writer::Handle Handle; ///< Handle type requested by writer @@ -125,6 +127,8 @@ namespace module { template class PassiveSocketWriter : public Module { + SENF_PPI_MODULE(PassiveSocketWriter); + public: typedef typename Writer::Handle Handle; ///< Handle type requested by writer diff --git a/PPI/SocketWriter.test.cc b/PPI/SocketWriter.test.cc index 5e40772..78814ca 100644 --- a/PPI/SocketWriter.test.cc +++ b/PPI/SocketWriter.test.cc @@ -64,7 +64,6 @@ BOOST_AUTO_UNIT_TEST(passiveSocketWriter) senf::UDPv4ClientSocketHandle inputSocket; inputSocket.bind(senf::INet4SocketAddress("localhost:44344")); - inputSocket.blocking(false); senf::ppi::init(); source.submit(p); @@ -85,8 +84,7 @@ BOOST_AUTO_UNIT_TEST(activeSocketWriter) senf::UDPv4ClientSocketHandle inputSocket; inputSocket.bind(senf::INet4SocketAddress("localhost:44344")); - inputSocket.blocking(false); - senf::Scheduler::instance().timeout(1000, &timeout); + senf::Scheduler::instance().timeout(100, &timeout); source.submit(p); senf::ppi::run(); diff --git a/PPI/detail/EventBinding.cci b/PPI/detail/EventBinding.cci index ad4e89e..7e27b3a 100644 --- a/PPI/detail/EventBinding.cci +++ b/PPI/detail/EventBinding.cci @@ -24,6 +24,7 @@ \brief EventBinding inline non-template implementation */ // Custom includes +#include "../Events.hh" #define prefix_ inline ///////////////////////////////cci.p/////////////////////////////////////// @@ -31,6 +32,11 @@ /////////////////////////////////////////////////////////////////////////// // senf::ppi::detail::EventBindingBase +prefix_ senf::ppi::detail::EventBindingBase::~EventBindingBase() +{ + descriptor_->enabled(false); +} + //////////////////////////////////////// // protected members @@ -51,5 +57,5 @@ prefix_ senf::ppi::detail::EventBindingBase::EventBindingBase(EventManager & man // c-file-style: "senf" // indent-tabs-mode: nil // ispell-local-dictionary: "american" -// compile-command: "scons -u test" +// compile-command: "scons -u ../test" // End: diff --git a/PPI/detail/EventBinding.hh b/PPI/detail/EventBinding.hh index addda33..468e713 100644 --- a/PPI/detail/EventBinding.hh +++ b/PPI/detail/EventBinding.hh @@ -40,6 +40,9 @@ namespace detail { class EventBindingBase { + public: + ~EventBindingBase(); + protected: EventBindingBase(EventManager & manager, module::Module & module, EventDescriptor & descriptor); @@ -50,6 +53,8 @@ namespace detail { EventManager * manager_; module::Module * module_; EventDescriptor * descriptor_; + + friend class senf::ppi::EventManager; }; template