Simple timeout implementation
[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 #ifndef HH_Scheduler_
24 #define HH_Scheduler_ 1
25
26 // Custom includes
27 #include <map>
28 #include <queue>
29 #include <boost/function.hpp>
30 #include <boost/utility.hpp>
31 #include <boost/call_traits.hpp>
32
33 #include "Utils/MicroTime.hh"
34
35 //#include "scheduler.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace satcom {
39 namespace lib {
40
41     /** \brief Singleton class to manage the event loop
42
43         This class manages a single select() type event loop. A
44         customer of this class may register any number of file
45         descriptiors with this class and pass callback functions to be
46         called on input, output or error. This functions are specified
47         using boost::function objects
48       */
49     class Scheduler
50         : boost::noncopyable
51     {
52     public:
53         ///////////////////////////////////////////////////////////////////////////
54         // Types
55
56         enum EventId { EV_NONE=0, 
57                        EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16, 
58                        EV_ALL=31 };
59
60         template <class Handle>
61         struct GenericCallback {
62             typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
63                                           EventId) > Callback;
64         };
65         typedef boost::function<void (EventId)> SimpleCallback;
66         typedef boost::function<void ()> TimerCallback;
67
68         ///////////////////////////////////////////////////////////////////////////
69         ///\name Structors and default members
70         ///@{
71
72         // private default constructor
73         // no copy constructor
74         // no copy assignment
75         // default destructor
76         // no conversion constructors
77
78         static Scheduler & instance();
79
80         ///@}
81         ///////////////////////////////////////////////////////////////////////////
82
83         template <class Handle>
84         void add(Handle const & handle, 
85                  typename GenericCallback<Handle>::Callback const & cb,
86                  EventId eventMask = EV_ALL); 
87         template <class Handle>
88         void remove(Handle const & handle, EventId eventMask = EV_ALL);
89
90         void timeout(unsigned long timeout, TimerCallback const & cb);
91
92         void process();
93         void terminate();
94
95     protected:
96
97     private:
98         Scheduler();
99         
100         void do_add(int fd, SimpleCallback const & cb, EventId eventMask = EV_ALL);
101         void do_remove(int fd, EventId eventMask = EV_ALL);
102
103         struct EventSpec 
104         {
105             SimpleCallback cb_read;
106             SimpleCallback cb_prio;
107             SimpleCallback cb_write;
108             SimpleCallback cb_hup;
109             SimpleCallback cb_err;
110
111             int epollMask() const;
112         };
113         
114         struct TimerSpec
115         {
116             TimerSpec() : timeout(), cb() {}
117             TimerSpec(unsigned long long timeout_, TimerCallback cb_)
118                 : timeout(timeout_), cb(cb_) {}
119
120             bool operator< (TimerSpec const & other) const
121                 { return timeout > other.timeout; }
122             
123             unsigned long long timeout;
124             TimerCallback cb;
125         };
126         
127         typedef std::map<int,EventSpec> FdTable;
128         typedef std::priority_queue<TimerSpec> TimerQueue;
129
130         FdTable fdTable_;
131         TimerQueue timerQueue_;
132         int epollFd_;
133         bool terminate_;
134     };
135
136     int retrieve_filehandle(int fd);
137
138 }}
139
140 ///////////////////////////////hh.e////////////////////////////////////////
141 #include "Scheduler.cci"
142 #include "Scheduler.ct"
143 #include "Scheduler.cti"
144 #endif
145
146 \f
147 // Local Variables:
148 // mode: c++
149 // c-file-style: "satcom"
150 // End: