added a clear() method to the TimerEventProxy to allow the removal of all pending...
[senf.git] / senf / Scheduler / TimerEventProxy.ct
index 3bbdcd4..0caaf20 100644 (file)
     \brief TimerEventProxy non-inline template implementation */
 
 // Custom includes
+#include <senf/Utils/membind.hh>
 
 #define prefix_
-///////////////////////////////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 = senf::scheduler::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)
@@ -67,8 +59,15 @@ template<typename IdType>
 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
         ClockService::clock_type timeout, IdType const & id, Callback cb)
 {
-    // insert new entry
-    entrySetByTimeout.insert( Entry(timeout, id, cb));
+    // insert new entry or replace the timeout of an entry already indexed
+    typename EntrySetById_t::iterator i = entrySetById.find(id);
+    if(i == entrySetById.end())
+       entrySetByTimeout.insert( Entry(timeout, id, cb));
+       else{
+               Entry tmp = *i;
+               tmp.timeout = timeout;
+               entrySetById.replace(i,tmp);
+       }
     // the scheduler time to the first earliest timeout (ordered index)
     timer.timeout( entrySetByTimeout.begin()->timeout);
 }
@@ -76,7 +75,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>
@@ -101,7 +105,20 @@ prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::sc
     return tmp;
 }
 
-///////////////////////////////ct.e////////////////////////////////////////
+template<typename IdType>
+prefix_ unsigned senf::scheduler::TimerEventProxy<IdType>::numEvents()
+  const
+{
+    return entrySetByTimeout.size();
+}
+
+template<typename IdType>
+prefix_ void senf::scheduler::TimerEventProxy<IdType>::clear()
+{
+    entrySetByTimeout.clear();
+}
+
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 #undef prefix_
 
 \f