4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Scheduler public header
28 #define HH_Scheduler_ 1
33 #include <boost/function.hpp>
34 #include <boost/utility.hpp>
35 #include <boost/call_traits.hpp>
36 #include <boost/integer.hpp>
37 #include "ClockService.hh"
39 //#include "scheduler.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
42 /** \brief SENF Project namespace */
45 /** \brief Singleton class to manage the event loop
47 This class manages a single select() type event loop. A customer of this class may register
48 any number of file descriptors with this class and pass callback functions to be called on
49 input, output or error. This functions are specified using boost::function objects (See <a
50 href="http://www.boost.org/doc/html/function.html">Boost.Function</a>)
52 The Scheduler is based on a generic handle representation. The only information needed from
53 a handle, is the intrinsic file descriptor. Any object for which the statement
55 int fd = retrieve_filehandle(object);
57 is valid and places the relevant file descriptor into fd can be used as a Handle type. There
58 is an implementation of retrieve_filehandle(int) within the library to handle explicit file
59 descriptors. The <a href="../../../Socket/doc/html/index.html">Socket library</a> provides an
60 implementation of <tt>retrieve_filehandle(FileHandle handle)</tt>. If you want to support
61 some other handle type, just define an appropriate \c retrieve_filehandle function <em>in
62 that types namespace</em>.
64 It is important to note, that for every combination of file descriptor and event, only a \e
65 single handler may be installed. Installing more handlers does not make sense. If you need
66 to distribute data to several interested parties, you must take care of this yourself.
68 \todo Fix EventId parameter (probably to int) to allow |-ing without casting ...
74 ///////////////////////////////////////////////////////////////////////////
77 /** \brief Types of file descriptor events */
78 enum EventId { EV_NONE=0,
79 EV_READ=1, EV_PRIO=2, EV_WRITE=4,
81 EV_HUP=8, EV_ERR=16 };
83 /** \brief Template typedef for Callback type
85 This is a template typedef (which does not exist in C++) that is, a template class whose
86 sole member is a typedef symbol defining the callback type given the handle type.
88 The Callback is any callable object taking a \c Handle and an \c EventId as argument.
90 template <class Handle>
91 struct GenericCallback {
92 typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
96 /** \brief Callback type for timer events */
97 typedef boost::function<void ()> TimerCallback;
99 ///////////////////////////////////////////////////////////////////////////
100 ///\name Structors and default members
103 // private default constructor
104 // no copy constructor
105 // no copy assignment
106 // default destructor
107 // no conversion constructors
109 /** \brief Return Scheduler instance
111 This static member is used to access the singleton instance. This member is save to
112 return a correctly initialized Scheduler instance even if called at global construction
115 \implementation This static member just defines the Scheduler as a static method
116 variable. The C++ standard then provides above guarantee. The instance will be
117 initialized the first time, the code flow passes the variable declaration found in
120 static Scheduler & instance();
123 ///////////////////////////////////////////////////////////////////////////
125 template <class Handle>
126 void add(Handle const & handle,
127 typename GenericCallback<Handle>::Callback const & cb,
128 int eventMask = EV_ALL); ///< Add file handle event callback
129 /**< add() will add a callback to the Scheduler. The
130 callback will be called for the given type of event on
131 the given arbitrary file-descriptor or
132 handle-like object. If there already is a Callback
133 registered for one of the events requested, the new
134 handler will replace the old one.
135 \param[in] handle file descriptor or handle providing
136 the Handle interface defined above.
137 \param[in] cb callback
138 \param[in] eventMask arbitrary combination via '|'
139 operator of EventId designators. */
140 template <class Handle>
141 void remove(Handle const & handle, int eventMask = EV_ALL); ///< Remove event callback
142 /**< remove() will remove any callback registered for any of
143 the given events on the given file descriptor or handle
145 \param[in] handle file descriptor or handle providing
146 the Handle interface defined above.
147 \param[in] eventMask arbitrary combination via '|'
148 operator of EventId designators. */
150 unsigned timeout(ClockService::clock_type timeout, TimerCallback const & cb);
151 ///< Add timeout event
152 /**< \param[in] timeout timeout in nanoseconds
153 \param[in] cb callback to call after \a timeout
156 void cancelTimeout(unsigned id);
158 void process(); ///< Event handler main loop
159 /**< This member must be called at some time to enter the
160 event handler main loop. Only while this function is
161 running any events are handled. The call will return
162 only, if any callback calls terminate(). */
163 void terminate(); ///< Called by callbacks to terminate the main loop
164 /**< This member may be called by any callback to tell the
165 main loop to terminate. The main loop will return to
166 it's caller after the currently running callback
169 ClockService::clock_type eventTime() const; ///< Return date/time of last event
174 typedef boost::function<void (EventId)> SimpleCallback;
178 void do_add(int fd, SimpleCallback const & cb, int eventMask = EV_ALL);
179 void do_remove(int fd, int eventMask = EV_ALL);
181 /** \brief Descriptor event specification
185 SimpleCallback cb_read;
186 SimpleCallback cb_prio;
187 SimpleCallback cb_write;
189 int epollMask() const;
192 /** \brief Timer event specification
196 TimerSpec() : timeout(), cb() {}
197 TimerSpec(ClockService::clock_type timeout_, TimerCallback cb_, unsigned id_)
198 : timeout(timeout_), cb(cb_), id(id_), canceled(false) {}
200 bool operator< (TimerSpec const & other) const
201 { return timeout > other.timeout; }
203 ClockService::clock_type timeout;
209 typedef std::map<int,EventSpec> FdTable;
210 typedef std::map<unsigned,TimerSpec> TimerMap; // sorted by id
212 struct TimerSpecCompare
214 typedef TimerMap::iterator first_argument_type;
215 typedef TimerMap::iterator second_argument_type;
216 typedef bool result_type;
218 result_type operator()(first_argument_type a, second_argument_type b);
221 typedef std::priority_queue<TimerMap::iterator, std::vector<TimerMap::iterator>,
222 TimerSpecCompare> TimerQueue; // sorted by time
225 unsigned timerIdCounter_;
226 TimerQueue timerQueue_;
230 ClockService::clock_type eventTime_;
233 /** \brief Default file descriptor accessor
235 retrieve_filehandle() provides the Scheduler with support for explicit file descriptors as
236 file handle argument.
240 int retrieve_filehandle(int fd);
244 ///////////////////////////////hh.e////////////////////////////////////////
245 #include "Scheduler.cci"
246 //#include "Scheduler.ct"
247 #include "Scheduler.cti"
254 // c-file-style: "senf"
255 // indent-tabs-mode: nil
256 // ispell-local-dictionary: "american"
257 // compile-command: "scons -u test"
258 // comment-column: 40