Utils/Logger: Implement TimeSource facility
[senf.git] / Scheduler / Scheduler.hh
index 4914e59..babc199 100644 (file)
@@ -35,6 +35,7 @@
 #include <boost/call_traits.hpp>
 #include <boost/integer.hpp>
 #include "ClockService.hh"
+#include "../Utils/Logger/Target.hh"
 
 //#include "scheduler.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -147,13 +148,13 @@ namespace senf {
                                              \param[in] eventMask arbitrary combination via '|'
                                                  operator of EventId designators. */
 
-        void timeout(ClockService::clock_type timeout, TimerCallback const & cb); 
+        unsigned timeout(ClockService::clock_type timeout, TimerCallback const & cb); 
                                         ///< Add timeout event
                                         /**< \param[in] timeout timeout in nanoseconds
                                              \param[in] cb callback to call after \a timeout
-                                                 milliseconds
-                                             \todo Return some kind of handle/pointer and add
-                                                 support to update or revoke a timeout */
+                                                 milliseconds */
+
+        void cancelTimeout(unsigned id);
 
         void process();                 ///< Event handler main loop
                                         /**< This member must be called at some time to enter the
@@ -173,8 +174,6 @@ namespace senf {
     private:
         typedef boost::function<void (EventId)> SimpleCallback;
 
-        static unsigned const MinTimeout = 1000;
-
         Scheduler();
 
         void do_add(int fd, SimpleCallback const & cb, int eventMask = EV_ALL);
@@ -196,23 +195,40 @@ namespace senf {
         struct TimerSpec
         {
             TimerSpec() : timeout(), cb() {}
-            TimerSpec(ClockService::clock_type timeout_, TimerCallback cb_)
-                : timeout(timeout_), cb(cb_) {}
+            TimerSpec(ClockService::clock_type timeout_, TimerCallback cb_, unsigned id_)
+                : timeout(timeout_), cb(cb_), id(id_), canceled(false) {}
 
             bool operator< (TimerSpec const & other) const
                 { return timeout > other.timeout; }
 
             ClockService::clock_type timeout;
             TimerCallback cb;
+            unsigned id;
+            bool canceled;
         };
 
         typedef std::map<int,EventSpec> FdTable;
-        typedef std::priority_queue<TimerSpec> TimerQueue;
+        typedef std::map<unsigned,TimerSpec> TimerMap; // sorted by id
+
+        struct TimerSpecCompare
+        {
+            typedef TimerMap::iterator first_argument_type;
+            typedef TimerMap::iterator second_argument_type;
+            typedef bool result_type;
+            
+            result_type operator()(first_argument_type a, second_argument_type b);
+        };
+
+        typedef std::priority_queue<TimerMap::iterator, std::vector<TimerMap::iterator>, 
+                                    TimerSpecCompare> TimerQueue; // sorted by time
 
         FdTable fdTable_;
+        unsigned timerIdCounter_;
         TimerQueue timerQueue_;
+        TimerMap timerMap_;
         int epollFd_;
         bool terminate_;
+        ClockService::clock_type eventTime_;
     };
 
     /** \brief Default file descriptor accessor
@@ -224,6 +240,17 @@ namespace senf {
      */
     int retrieve_filehandle(int fd);
 
+    /** \brief Scheduler specific time source for Utils/Logger framework
+
+        This time source may be used to provide timing information for log messages within the
+        Utils/Logger framework. This time source will use Scheduler::eventTime() to provide timing
+        information.
+     */
+    struct SchedulerLogTimeSource : public senf::log::TimeSource
+    {
+        boost::posix_time::ptime operator()() const;
+    };
+
 }
 
 ///////////////////////////////hh.e////////////////////////////////////////