Scheduler: Fix for TimerEventProxy
tho [Wed, 28 Jul 2010 11:20:04 +0000 (11:20 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1651 270642c3-0616-0410-b53a-bc976706d245

senf/PPI/SocketSource.ct
senf/PPI/SocketSource.cti
senf/Scheduler/TimerEventProxy.ct
senf/Scheduler/TimerEventProxy.hh
senf/Scheduler/TimerEventProxy.test.cc

index bc60d29..58131fa 100644 (file)
@@ -76,15 +76,6 @@ prefix_ senf::ppi::module::ActiveSocketSource<Reader>::ActiveSocketSource(Handle
     route(event_, output);
 }
 
-////////////////////////////////////////
-// private members
-
-template <class Reader>
-prefix_ void senf::ppi::module::ActiveSocketSource<Reader>::read()
-{
-    output(reader_(handle_));
-}
-
 ///////////////////////////////ct.e////////////////////////////////////////
 #undef prefix_
 
index 8bb2e7c..ffffdd5 100644 (file)
@@ -53,6 +53,15 @@ prefix_ void senf::ppi::module::ActiveSocketSource<Reader>::handle(Handle handle
     event_.set(handle_, IOEvent::Read);
 }
 
+////////////////////////////////////////
+// private members
+
+template <class Reader>
+prefix_ void senf::ppi::module::ActiveSocketSource<Reader>::read()
+{
+    output(reader_(handle_));
+}
+
 ///////////////////////////////cti.e///////////////////////////////////////
 #undef prefix_
 
index 3bbdcd4..a5548d9 100644 (file)
 ///////////////////////////////ct.p////////////////////////////////////////
 
 template<typename IdType>
-prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy()
+prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & description)
     : entrySetById( entrySet.template get<Id>()),
       entrySetByTimeout( entrySet.template get<Timeout> ()),
-      timer( "TimerEventProxy", membind(&TimerEventProxy<IdType>::timerEvent, this), 0, false)
+      timer( "TimerEventProxy " + description,
+              membind(&TimerEventProxy<IdType>::timerEvent, this), 0, false)
 { }
 
 template<typename IdType>
-prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & name, console::DirectoryNode & node)
-    : entrySetById( entrySet.template get<Id>()),
-      entrySetByTimeout( entrySet.template get<Timeout> ()),
-      timer( "TimerEventProxy", membind(&TimerEventProxy<IdType>::timerEvent, this), 0, false)
-{
-    node.add(name, console::factory::Command(
-            &TimerEventProxy<IdType>::list, this) .doc("List active Timers"));
-}
-
-template<typename IdType>
 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
 {
-    ClockService::clock_type actual = ClockService::now();
+    ClockService::clock_type now = ClockService::now();
     typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
-    while (it != entrySetByTimeout.end() && it->timeout <= actual) {
+    while (it != entrySetByTimeout.end() && it->timeout <= now) {
         Entry item (*it);
         // remove due entry from set
         entrySetByTimeout.erase(it);
         // call callback
-        item.cb(actual, item.id);
+        item.cb(now, item.id);
         it = entrySetByTimeout.begin();
     }
     if (entrySet.size() > 0)
@@ -76,7 +67,12 @@ prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
 template<typename IdType>
 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
 {
-    return entrySetById.erase( id) > 0;
+    bool removed (entrySetById.erase( id) > 0);
+    if (entrySet.size() > 0)
+        timer.timeout(entrySetByTimeout.begin()->timeout);
+    else
+        timer.disable();
+    return removed;
 }
 
 template<typename IdType>
index 3517db6..75c4f8c 100644 (file)
@@ -55,10 +55,9 @@ namespace scheduler {
     public:
         typedef boost::function<void(ClockService::clock_type, IdType const &)> Callback;
 
-        TimerEventProxy();              ///< Instantiate a TimerEventProxy
-        TimerEventProxy(std::string const & name, console::DirectoryNode & node);
-                                        /**< \brief Instantiate a TimerEventProxy and add the list
-                                                    command to the give DirectoryNode */
+        TimerEventProxy(std::string const & description = "");
+                                        ///< Instantiate a TimerEventProxy
+                                        /**< \param[in] description Descriptive name (purely informational) */
 
         void add(ClockService::clock_type timeout, IdType const & id, Callback cb);
                                         ///< Add new deadline timer
index 992af10..dfab9cc 100644 (file)
@@ -61,7 +61,7 @@ SENF_AUTO_UNIT_TEST(timerEventProxy)
 
     senf::ClockService::clock_type t (senf::ClockService::now());
     {
-        senf::scheduler::TimerEventProxy<int> timers;
+        senf::scheduler::TimerEventProxy<int> timers ("unit-test");
 
 //        timers.add( t + senf::ClockService::milliseconds(10000), 0 , &handler);
         timers.add( t + senf::ClockService::milliseconds(800), 4, &handler);
@@ -78,8 +78,6 @@ SENF_AUTO_UNIT_TEST(timerEventProxy)
 
         BOOST_CHECK( mask == 3);
     }
-
-    senf::scheduler::TimerEventProxy<int> timers ("test", senf::console::ScopedDirectory<>());
 }