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.
23 // TODO: Fix EventId parameter (probably to int) to allow |-ing without casting ...
26 #define HH_Scheduler_ 1
31 #include <boost/function.hpp>
32 #include <boost/utility.hpp>
33 #include <boost/call_traits.hpp>
35 #include "Utils/MicroTime.hh"
37 //#include "scheduler.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
43 /** \brief Singleton class to manage the event loop
45 This class manages a single select() type event loop. A
46 customer of this class may register any number of file
47 descriptiors with this class and pass callback functions to be
48 called on input, output or error. This functions are specified
49 using boost::function objects
55 ///////////////////////////////////////////////////////////////////////////
58 enum EventId { EV_NONE=0,
59 EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16,
62 template <class Handle>
63 struct GenericCallback {
64 typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
67 typedef boost::function<void (EventId)> SimpleCallback;
68 typedef boost::function<void ()> TimerCallback;
70 ///////////////////////////////////////////////////////////////////////////
71 ///\name Structors and default members
74 // private default constructor
75 // no copy constructor
78 // no conversion constructors
80 static Scheduler & instance();
83 ///////////////////////////////////////////////////////////////////////////
85 template <class Handle>
86 void add(Handle const & handle,
87 typename GenericCallback<Handle>::Callback const & cb,
88 int eventMask = EV_ALL);
89 template <class Handle>
90 void remove(Handle const & handle, int eventMask = EV_ALL);
92 void timeout(unsigned long timeout, TimerCallback const & cb);
102 void do_add(int fd, SimpleCallback const & cb, int eventMask = EV_ALL);
103 void do_remove(int fd, int eventMask = EV_ALL);
107 SimpleCallback cb_read;
108 SimpleCallback cb_prio;
109 SimpleCallback cb_write;
110 SimpleCallback cb_hup;
111 SimpleCallback cb_err;
113 int epollMask() const;
118 TimerSpec() : timeout(), cb() {}
119 TimerSpec(unsigned long long timeout_, TimerCallback cb_)
120 : timeout(timeout_), cb(cb_) {}
122 bool operator< (TimerSpec const & other) const
123 { return timeout > other.timeout; }
125 unsigned long long timeout;
129 typedef std::map<int,EventSpec> FdTable;
130 typedef std::priority_queue<TimerSpec> TimerQueue;
133 TimerQueue timerQueue_;
138 int retrieve_filehandle(int fd);
142 ///////////////////////////////hh.e////////////////////////////////////////
143 #include "Scheduler.cci"
144 #include "Scheduler.ct"
145 #include "Scheduler.cti"
151 // c-file-style: "satcom"