From: tho Date: Tue, 13 Apr 2010 11:57:44 +0000 (+0000) Subject: Scheduler/TimerEventProxy: some code clean ups X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=2b2af7d62142f61f5804df2184b58e5f7ce3bd06;p=senf.git Scheduler/TimerEventProxy: some code clean ups git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1592 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Scheduler/TimerEventProxy.ct b/senf/Scheduler/TimerEventProxy.ct index cb665b6..edb4b4a 100644 --- a/senf/Scheduler/TimerEventProxy.ct +++ b/senf/Scheduler/TimerEventProxy.ct @@ -4,6 +4,7 @@ // Fraunhofer Institute for Open Communication Systems (FOKUS) // Competence Center NETwork research (NET), St. Augustin, GERMANY // Mathias Kretschmer +// Jens Moedeker // // 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 @@ -26,20 +27,18 @@ #define prefix_ template -prefix_ senf::scheduler::TimerEventProxy::TimerEventProxy() : - timer("timer", senf::membind(&TimerEventProxy::timerEvent, this), 0, - false), entrySetById(entrySet.template get ()), - entrySetByTimeout(entrySet.template get ()) -{ - -} +prefix_ senf::scheduler::TimerEventProxy::TimerEventProxy() + : timer("TimerEventProxy", senf::membind(&TimerEventProxy::timerEvent, this), 0, false), + entrySetById(entrySet.template get ()), + entrySetByTimeout(entrySet.template get ()) +{ } template prefix_ senf::scheduler::TimerEventProxy::TimerEventProxy( std::string const & name, - senf::console::DirectoryNode & node) : - timer("timer", senf::membind(&TimerEventProxy::timerEvent, this), 0, - false), entrySetById(entrySet.template get ()), - entrySetByTimeout(entrySet.template get ()) + senf::console::DirectoryNode & node) + : timer("TimerEventProxy", senf::membind(&TimerEventProxy::timerEvent, this), 0, false), + entrySetById(entrySet.template get ()), + entrySetByTimeout(entrySet.template get ()) { node.add(name, senf::console::factory::Command( &TimerEventProxy::listTimers, this) .doc("List active Timers")); @@ -55,11 +54,11 @@ prefix_ void senf::scheduler::TimerEventProxy::timerEvent() { it = entrySetByTimeout.begin(); while (it != entrySetByTimeout.end() && it->timeout <= actual) { - Entry item(*it); + Entry item (*it); // remove due entry from set entrySetByTimeout.erase(it); // call callback - item.fkt(actual, item.id); + item.cb(actual, item.id); it = entrySetByTimeout.begin(); } @@ -69,42 +68,31 @@ prefix_ void senf::scheduler::TimerEventProxy::timerEvent() { } } -template -prefix_ void senf::scheduler::TimerEventProxy::add( - senf::ClockService::clock_type timeout, T const & id, Callback fkt) +template +prefix_ void senf::scheduler::TimerEventProxy::add( + senf::ClockService::clock_type timeout, IdType const & id, Callback cb) { // insert new entry - entrySetByTimeout.insert(Entry (timeout, id, fkt)); + entrySetByTimeout.insert( Entry(timeout, id, cb)); // the scheduler time to the first earliest timeout (ordered index) - timer.timeout(entrySetByTimeout.begin()->timeout); - - // // if map was empty before, hence we need to activate the time event object - // if( entrySetByTimeout.size() >= 1){ - // timer.enable(); - // } + timer.timeout( entrySetByTimeout.begin()->timeout); } -template -prefix_ bool senf::scheduler::TimerEventProxy::del(T const & id) +template +prefix_ bool senf::scheduler::TimerEventProxy::remove(IdType const & id) { - typename EntrySetById_t::iterator it(entrySetById.find(Entry (0, id, NULL))); - - if (it != entrySetById.end()) { - entrySetById.erase(it); - return true; - } - return false; + return entrySetById.erase( id) > 0; } -template -prefix_ std::vector > senf::scheduler::TimerEventProxy::list() +template +prefix_ std::vector > senf::scheduler::TimerEventProxy::list() { - std::vector > tmp; + std::vector > tmp; typename EntrySetByTimeout_t::iterator it; for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) { - tmp.push_back(std::make_pair( it->timeout, it->id)); + tmp.push_back(std::make_pair( it->timeout, it->id)); } return tmp; diff --git a/senf/Scheduler/TimerEventProxy.hh b/senf/Scheduler/TimerEventProxy.hh index ae77e58..9d40e3d 100644 --- a/senf/Scheduler/TimerEventProxy.hh +++ b/senf/Scheduler/TimerEventProxy.hh @@ -4,6 +4,7 @@ // Fraunhofer Institute for Open Communication Systems (FOKUS) // Competence Center NETwork research (NET), St. Augustin, GERMANY // Mathias Kretschmer +// Jens Moedeker // // 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 @@ -21,7 +22,7 @@ // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /** \file - \brief TimerEventProxy public header */ + \brief TimerEventProxy public header */ #ifndef HH_SENF_Scheduler_TimerEventProxy_ #define HH_SENF_Scheduler_TimerEventProxy_ 1 @@ -34,51 +35,55 @@ #include #include #include -#include -#include -#include -#include -#include #include #include #include -#include +#include namespace senf { namespace scheduler { - /** \brief Deadline timer proxy The TimerEventProxy is meant to host long term deadline timers to reduce the load of the - Scheduler with a hugh count of TimerEvent items. It registers deadline timer callbacks which + Scheduler with a huge count of TimerEvent items. It registers deadline timer callbacks which will be called when the timer expires. The functionality is based on one TimerEvent instance per TimerEventProxy instance and could host a big count of timers. */ - template - class TimerEventProxy { - private: + template + class TimerEventProxy + { + public: + /////////////////////////////////////////////////////////////////////////// + // Types + typedef boost::function Callback; - template + TimerEventProxy(); + ///< Instantiate a TimerEventProxy + + TimerEventProxy(std::string const & name, senf::console::DirectoryNode & node); + ///< Instantiate a TimerEventProxy and add the list command to the give DirectoryNode + + void add(senf::ClockService::clock_type timeout, IdType const &id, Callback cb); + ///< Add new deadline timer + bool remove(IdType const & id); + ///< Remove timer by given \a id. + std::vector > list(); + ///< Returns a vector of all active timers with timeout and id. + + private: +#ifndef DOXYGEN struct Entry { - public: senf::ClockService::clock_type timeout; - T id; - boost::function fkt; - - bool operator<(const Entry & e) const { - return id < e.id; - } - // bool operator==(const Entry &e)const{return id == e.id;} - Entry(senf::ClockService::clock_type _timeout, T_ _id, boost::function _fkt) : - timeout(_timeout), id(_id), fkt(_fkt) { - } - }; + IdType id; + Callback cb; + Entry(senf::ClockService::clock_type _timeout, IdType _id, Callback _cb) + : timeout(_timeout), id(_id), cb(_cb) { } + }; senf::scheduler::TimerEvent timer; @@ -87,46 +92,30 @@ namespace scheduler { // struct Timeout {}; struct Id {}; - typedef boost::multi_index_container , - boost::multi_index::indexed_by< - boost::multi_index::ordered_non_unique< - boost::multi_index::tag, - boost::multi_index::member , - senf::ClockService::clock_type, - &Entry::timeout> >, - boost::multi_index::ordered_unique, boost::multi_index::identity > > > > - EntrySet; - - typedef typename EntrySet::template index::type - EntrySetByTimeout_t; - typedef typename EntrySet::template index::type EntrySetById_t; - - EntrySet entrySet; +#endif + typedef boost::multi_index_container< + Entry, + boost::multi_index::indexed_by< + boost::multi_index::ordered_non_unique< + boost::multi_index::tag, + boost::multi_index::member + >, + boost::multi_index::ordered_unique< + boost::multi_index::tag, + boost::multi_index::member + > + > + > EntrySet_t; + + typedef typename EntrySet_t::template index::type EntrySetByTimeout_t; + typedef typename EntrySet_t::template index::type EntrySetById_t; + + EntrySet_t entrySet; EntrySetById_t & entrySetById; EntrySetByTimeout_t & entrySetByTimeout; - private: // callback for the Scheduler timer event void timerEvent(); - - public: - /////////////////////////////////////////////////////////////////////////// - // Types - typedef boost::function Callback; - - TimerEventProxy(); - ///< Instantiate a TimerEventProxy - - TimerEventProxy(std::string const & name, senf::console::DirectoryNode & node); - ///< Instantiate a TimerEventProxy and add the list command to the give DirectoryNode - - void add(senf::ClockService::clock_type timeout, T const &id, Callback cb); - ///< Add new deadline timer - bool del(T const & id); - ///< Remove timer by given \a id. - std::vector > list(); - ///< Returns a vector of all active timers with timeout and id. }; } } diff --git a/senf/Scheduler/TimerEventProxy.test.cc b/senf/Scheduler/TimerEventProxy.test.cc index eb07187..e682687 100644 --- a/senf/Scheduler/TimerEventProxy.test.cc +++ b/senf/Scheduler/TimerEventProxy.test.cc @@ -21,10 +21,6 @@ // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /** \file - \brief TimerEventProxy public header */ -// - -/** \file \brief TimerEventProxy.test non-inline non-template implementation */ //#include "TimerEventProxy.test.hh" @@ -33,7 +29,6 @@ // Custom includes #include "TimerEventProxy.hh" #include "Scheduler.hh" -#include #include #include @@ -43,24 +38,23 @@ ///////////////////////////////cc.p//////////////////////////////////////// namespace { - int count = 0; + int mask = 0; void handler( senf::ClockService::clock_type time, int const &id) { - std::cerr << "TimerEventProxy handler count="< timers; - SENF_CHECK_NO_THROW( timers.add( t + senf::ClockService::milliseconds(10000), 0 , &handler)); - SENF_CHECK_NO_THROW( timers.add( t + senf::ClockService::milliseconds(800), 3, &handler)); - SENF_CHECK_NO_THROW( timers.add( t + senf::ClockService::milliseconds(200), 1, &handler)); - SENF_CHECK_NO_THROW( timers.del( 3)); - SENF_CHECK_NO_THROW( timers.add( t + senf::ClockService::milliseconds(700), 2, &handler)); - - // set timeout for termination - senf::scheduler::TimerEvent te_exit( "myexit", &myexit, t + senf::ClockService::milliseconds( 1000)); + timers.add( t + senf::ClockService::milliseconds(10000), 0 , &handler); + timers.add( t + senf::ClockService::milliseconds(800), 4, &handler); + timers.add( t + senf::ClockService::milliseconds(200), 1, &handler); + BOOST_CHECK( timers.remove( 4)); + timers.add( t + senf::ClockService::milliseconds(700), 2, &handler); - SENF_CHECK_NO_THROW( senf::scheduler::process() ); + run( senf::ClockService::milliseconds( 1000)); - BOOST_CHECK( count == 2); + BOOST_CHECK( mask == 3); } }