#include <boost/function.hpp>
#include <boost/utility.hpp>
#include <boost/call_traits.hpp>
-
-#include "Utils/MicroTime.hh"
+#include <boost/integer.hpp>
+#include "ClockService.hh"
//#include "scheduler.mpp"
///////////////////////////////hh.p////////////////////////////////////////
/** \brief Singleton class to manage the event loop
This class manages a single select() type event loop. A customer of this class may register
- any number of file descriptiors with this class and pass callback functions to be called on
+ any number of file descriptors with this class and pass callback functions to be called on
input, output or error. This functions are specified using boost::function objects (See <a
href="http://www.boost.org/doc/html/function.html">Boost.Function</a>)
\code
int fd = retrieve_filehandle(object);
\endcode
- is valid and places the relevent file descriptor into fd can be used as a Handle type. There
+ is valid and places the relevant file descriptor into fd can be used as a Handle type. There
is an implementation of retrieve_filehandle(int) within the library to handle explicit file
- descrptors. The <a href="../../../Socket/doc/html/index.html">Socket library</a> provides an
- implementation of <tt>retrive_filehandle(FileHandle handle)</tt>. If you want to support
- some other handle type, just define an apropriate \c retrieve_filehandle function <em>in
+ descriptors. The <a href="../../../Socket/doc/html/index.html">Socket library</a> provides an
+ implementation of <tt>retrieve_filehandle(FileHandle handle)</tt>. If you want to support
+ some other handle type, just define an appropriate \c retrieve_filehandle function <em>in
that types namespace</em>.
It is important to note, that for every combination of file descriptor and event, only a \e
single handler may be installed. Installing more handlers does not make sense. If you need
- to distribute data to serveral interested parties, you must take care of this yourself.
+ to distribute data to several interested parties, you must take care of this yourself.
\todo Fix EventId parameter (probably to int) to allow |-ing without casting ...
*/
///////////////////////////////////////////////////////////////////////////
// Types
- /// \brief Types of file descriptor events */
+ /** \brief Types of file descriptor events */
enum EventId { EV_NONE=0,
- EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16,
- EV_ALL=31 };
+ EV_READ=1, EV_PRIO=2, EV_WRITE=4,
+ EV_ALL=7,
+ EV_HUP=8, EV_ERR=16 };
/** \brief Template typedef for Callback type
typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
EventId) > Callback;
};
+
/** \brief Callback type for timer events */
typedef boost::function<void ()> TimerCallback;
time
\implementation This static member just defines the Scheduler as a static method
- variable. The C++ standard then provides above guaratee. The instance will be
+ variable. The C++ standard then provides above guarantee. The instance will be
initialized the first time, the code flow passes the variable declaration found in
the instance() body.
*/
typename GenericCallback<Handle>::Callback const & cb,
int eventMask = EV_ALL); ///< Add file handle event callback
/**< add() will add a callback to the Scheduler. The
- callbeck will be called for the given type of event on
+ callback will be called for the given type of event on
the given arbitrary file-descriptor or
handle-like object. If there already is a Callback
- register ed for one of the events requested, the new
+ registered for one of the events requested, the new
handler will replace the old one.
\param[in] handle file descriptor or handle providing
the Handle interface defined above.
\param[in] eventMask arbitrary combination via '|'
operator of EventId designators. */
- void timeout(unsigned long timeout, TimerCallback const & cb); ///< Add timeout event
- /**< \param[in] timeout timeout in milliseconds
+ 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 */
+ void cancelTimeout(unsigned id);
+
void process(); ///< Event handler main loop
/**< This member must be called at some time to enter the
event handler main loop. Only while this function is
main loop to terminate. The main loop will return to
it's caller after the currently running callback
returns. */
+
+ ClockService::clock_type eventTime() const; ///< Return date/time of last event
protected:
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);
SimpleCallback cb_read;
SimpleCallback cb_prio;
SimpleCallback cb_write;
- SimpleCallback cb_hup;
- SimpleCallback cb_err;
int epollMask() const;
};
struct TimerSpec
{
TimerSpec() : timeout(), cb() {}
- TimerSpec(unsigned long long 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; }
- unsigned long long 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;
+
+ 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;
FdTable fdTable_;
+ unsigned timerIdCounter_;
TimerQueue timerQueue_;
+ TimerMap timerMap_;
int epollFd_;
bool terminate_;
+ ClockService::clock_type eventTime_;
};
/** \brief Default file descriptor accessor
// c-file-style: "senf"
// indent-tabs-mode: nil
// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
// End: