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