6788f61aa283127a1d638c69bd5fdb91e6f4661d
[senf.git] / Scheduler / Scheduler.hh
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
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.
12 //
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.
17 //
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.
22
23 // TODO: Fix EventId parameter (probably to int) to allow |-ing without casting ...
24
25 #ifndef HH_Scheduler_
26 #define HH_Scheduler_ 1
27
28 // Custom includes
29 #include <map>
30 #include <queue>
31 #include <boost/function.hpp>
32 #include <boost/utility.hpp>
33 #include <boost/call_traits.hpp>
34
35 #include "Utils/MicroTime.hh"
36
37 //#include "scheduler.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace satcom {
41 namespace lib {
42
43     /** \brief Singleton class to manage the event loop
44
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
50       */
51     class Scheduler
52         : boost::noncopyable
53     {
54     public:
55         ///////////////////////////////////////////////////////////////////////////
56         // Types
57
58         enum EventId { EV_NONE=0, 
59                        EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16, 
60                        EV_ALL=31 };
61
62         template <class Handle>
63         struct GenericCallback {
64             typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
65                                           EventId) > Callback;
66         };
67         typedef boost::function<void (EventId)> SimpleCallback;
68         typedef boost::function<void ()> TimerCallback;
69
70         ///////////////////////////////////////////////////////////////////////////
71         ///\name Structors and default members
72         ///@{
73
74         // private default constructor
75         // no copy constructor
76         // no copy assignment
77         // default destructor
78         // no conversion constructors
79
80         static Scheduler & instance();
81
82         ///@}
83         ///////////////////////////////////////////////////////////////////////////
84
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);
91
92         void timeout(unsigned long timeout, TimerCallback const & cb);
93
94         void process();
95         void terminate();
96
97     protected:
98
99     private:
100         Scheduler();
101         
102         void do_add(int fd, SimpleCallback const & cb, int eventMask = EV_ALL);
103         void do_remove(int fd, int eventMask = EV_ALL);
104
105         struct EventSpec 
106         {
107             SimpleCallback cb_read;
108             SimpleCallback cb_prio;
109             SimpleCallback cb_write;
110             SimpleCallback cb_hup;
111             SimpleCallback cb_err;
112
113             int epollMask() const;
114         };
115         
116         struct TimerSpec
117         {
118             TimerSpec() : timeout(), cb() {}
119             TimerSpec(unsigned long long timeout_, TimerCallback cb_)
120                 : timeout(timeout_), cb(cb_) {}
121
122             bool operator< (TimerSpec const & other) const
123                 { return timeout > other.timeout; }
124             
125             unsigned long long timeout;
126             TimerCallback cb;
127         };
128         
129         typedef std::map<int,EventSpec> FdTable;
130         typedef std::priority_queue<TimerSpec> TimerQueue;
131
132         FdTable fdTable_;
133         TimerQueue timerQueue_;
134         int epollFd_;
135         bool terminate_;
136     };
137
138     int retrieve_filehandle(int fd);
139
140 }}
141
142 ///////////////////////////////hh.e////////////////////////////////////////
143 #include "Scheduler.cci"
144 #include "Scheduler.ct"
145 #include "Scheduler.cti"
146 #endif
147
148 \f
149 // Local Variables:
150 // mode: c++
151 // c-file-style: "satcom"
152 // End: