Scheduler/TimerEventProxy: some code clean ups
tho [Tue, 13 Apr 2010 11:57:44 +0000 (11:57 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1592 270642c3-0616-0410-b53a-bc976706d245

senf/Scheduler/TimerEventProxy.ct
senf/Scheduler/TimerEventProxy.hh
senf/Scheduler/TimerEventProxy.test.cc

index cb665b6..edb4b4a 100644 (file)
@@ -4,6 +4,7 @@
 // Fraunhofer Institute for Open Communication Systems (FOKUS)
 // Competence Center NETwork research (NET), St. Augustin, GERMANY
 //     Mathias Kretschmer <mtk@berlios.de>
+//     Jens Moedeker <jens.moedeker@fokus.fraunhofer.de>
 //
 // 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
 #define prefix_ 
 
 template<class T>
-prefix_ senf::scheduler::TimerEventProxy<T>::TimerEventProxy() :
-    timer("timer", senf::membind(&TimerEventProxy<T>::timerEvent, this), 0,
-            false), entrySetById(entrySet.template get<Id> ()),
-            entrySetByTimeout(entrySet.template get<Timeout> ())
-{
-
-}
+prefix_ senf::scheduler::TimerEventProxy<T>::TimerEventProxy() 
+    : timer("TimerEventProxy", senf::membind(&TimerEventProxy<T>::timerEvent, this), 0, false), 
+      entrySetById(entrySet.template get<Id> ()),
+      entrySetByTimeout(entrySet.template get<Timeout> ())
+{ }
 
 template<class T>
 prefix_ senf::scheduler::TimerEventProxy<T>::TimerEventProxy( std::string const & name,
-        senf::console::DirectoryNode & node) :
-    timer("timer", senf::membind(&TimerEventProxy<T>::timerEvent, this), 0,
-            false), entrySetById(entrySet.template get<Id> ()),
-            entrySetByTimeout(entrySet.template get<Timeout> ())
+        senf::console::DirectoryNode & node) 
+    : timer("TimerEventProxy", senf::membind(&TimerEventProxy<T>::timerEvent, this), 0, false),
+      entrySetById(entrySet.template get<Id> ()),
+      entrySetByTimeout(entrySet.template get<Timeout> ())
 {
     node.add(name, senf::console::factory::Command(
             &TimerEventProxy<T>::listTimers, this) .doc("List active Timers"));
@@ -55,11 +54,11 @@ prefix_ void senf::scheduler::TimerEventProxy<T>::timerEvent() {
 
     it = entrySetByTimeout.begin();
     while (it != entrySetByTimeout.end() && it->timeout <= actual) {
-        Entry<T> 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<T>::timerEvent() {
     }
 }
 
-template<class T>
-prefix_ void senf::scheduler::TimerEventProxy<T>::add(
-        senf::ClockService::clock_type timeout, T const & id, Callback fkt)
+template<class IdType>
+prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
+        senf::ClockService::clock_type timeout, IdType const & id, Callback cb)
 {
     // insert new entry
-    entrySetByTimeout.insert(Entry<T> (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<class T>
-prefix_ bool senf::scheduler::TimerEventProxy<T>::del(T const & id)
+template<class IdType>
+prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
 {
-    typename EntrySetById_t::iterator it(entrySetById.find(Entry<T> (0, id, NULL)));
-
-    if (it != entrySetById.end()) {
-        entrySetById.erase(it);
-        return true;
-    }
-    return false;
+    return entrySetById.erase( id) > 0;
 }
 
-template<class T>
-prefix_ std::vector<std::pair<senf::ClockService::clock_type, T> > senf::scheduler::TimerEventProxy<T>::list()
+template<class IdType>
+prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
 {
-    std::vector<std::pair<senf::ClockService::clock_type, T> > tmp;
+    std::vector<std::pair<senf::ClockService::clock_type, IdType> > tmp;
 
     typename EntrySetByTimeout_t::iterator it;
     for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
-        tmp.push_back(std::make_pair<senf::ClockService::clock_type, T>( it->timeout, it->id));
+        tmp.push_back(std::make_pair<senf::ClockService::clock_type, IdType>( it->timeout, it->id));
     }
 
     return tmp;
index ae77e58..9d40e3d 100644 (file)
@@ -4,6 +4,7 @@
 // Fraunhofer Institute for Open Communication Systems (FOKUS)
 // Competence Center NETwork research (NET), St. Augustin, GERMANY
 //     Mathias Kretschmer <mtk@berlios.de>
+//     Jens Moedeker <jens.moedeker@fokus.fraunhofer.de>
 //
 // 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
 #include <boost/range/iterator_range.hpp>
 #include <boost/multi_index_container.hpp>
 #include <boost/multi_index/ordered_index.hpp>
-#include <boost/multi_index/hashed_index.hpp>
-#include <boost/multi_index/sequenced_index.hpp>
-#include <boost/multi_index/key_extractors.hpp>
-#include <boost/multi_index/random_access_index.hpp>
-#include <boost/multi_index/identity.hpp>
 #include <boost/multi_index/member.hpp>
 
 #include <senf/Scheduler/ClockService.hh>
 #include <senf/Scheduler/TimerEvent.hh>
-#include <senf/Utils/Console/Console.hh>
+#include <senf/Utils/Console/Node.hh>
 
 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 T>
-    class TimerEventProxy {
-    private:
+    template<typename IdType>
+    class TimerEventProxy 
+    {
+    public:
+        ///////////////////////////////////////////////////////////////////////////
+        // Types
+        typedef boost::function<void(senf::ClockService::clock_type, IdType const &)> Callback;
 
-        template<class T_>
+        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<std::pair<senf::ClockService::clock_type, IdType> > 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<void(senf::ClockService::clock_type, T_ const &)> fkt;
-
-            bool operator<(const Entry<T_> & e) const {
-                return id < e.id;
-            }
-            //  bool operator==(const Entry<T> &e)const{return id == e.id;}
-            Entry(senf::ClockService::clock_type _timeout, T_ _id, boost::function<void(
-                    senf::ClockService::clock_type, T_)> _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<Entry<T> ,
-                boost::multi_index::indexed_by<
-                        boost::multi_index::ordered_non_unique<
-                                boost::multi_index::tag<Timeout>,
-                                boost::multi_index::member<Entry<T> ,
-                                        senf::ClockService::clock_type,
-                                        &Entry<T>::timeout> >,
-                        boost::multi_index::ordered_unique<boost::multi_index::tag<
-                                Id>, boost::multi_index::identity<Entry<T> > > > >
-                EntrySet;
-
-        typedef typename EntrySet::template index<Timeout>::type
-                EntrySetByTimeout_t;
-        typedef typename EntrySet::template index<Id>::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<Timeout>,
+                    boost::multi_index::member<Entry, senf::ClockService::clock_type, &Entry::timeout> 
+                >,
+                boost::multi_index::ordered_unique<
+                    boost::multi_index::tag<Id>,
+                    boost::multi_index::member<Entry, IdType, &Entry::id> 
+                >
+            >
+        > EntrySet_t;
+
+        typedef typename EntrySet_t::template index<Timeout>::type EntrySetByTimeout_t;
+        typedef typename EntrySet_t::template index<Id>::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<void(senf::ClockService::clock_type, T const &)> 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<std::pair<senf::ClockService::clock_type, T> > list();
-        ///< Returns a vector of all active timers with timeout and id.
     };
 }
 }
index eb07187..e682687 100644 (file)
 // 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 <boost/bind.hpp>
 
 #include <senf/Utils/auto_unit_test.hh>
 #include <boost/test/test_tools.hpp>
 ///////////////////////////////cc.p////////////////////////////////////////
 namespace {
 
-    int count = 0;
+    int mask = 0;
 
     void handler( senf::ClockService::clock_type time, int const &id)
     {
-        std::cerr << "TimerEventProxy handler count="<<count<<" id="<<id<<"\n";
-        ++count;
+        mask = mask + id;
     }
 
-    void myexit(){
-        std::cerr << "TimerEventProxy terminating\n";
-        senf::scheduler::terminate();
+    void run(senf::ClockService::clock_type t) {
+        senf::scheduler::TimerEvent timeout(
+                "test-timeout", &senf::scheduler::terminate, senf::scheduler::now() + t);
+        senf::scheduler::process();
     }
 
 }
 
 SENF_AUTO_UNIT_TEST(timerEventProxy)
 {
-
 //    // abort on watchdog timeout
 //    senf::scheduler::watchdogAbort( true);
 //    senf::scheduler::watchdogTimeout(5000);
@@ -69,18 +63,15 @@ SENF_AUTO_UNIT_TEST(timerEventProxy)
     {
         senf::scheduler::TimerEventProxy<int> 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);
     }
 }