git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@129 270642c3-0616-0410...
[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 <boost/function.hpp>
29 #include <boost/utility.hpp>
30 #include <boost/call_traits.hpp>
31
32 //#include "scheduler.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace satcom {
36 namespace lib {
37
38     /** \brief Singleton class to manage the event loop
39
40         This class manages a single select() type event loop. A
41         customer of this class may register any number of file
42         descriptiors with this class and pass callback functions to be
43         called on input, output or error. This functions are specified
44         using boost::function objects
45       */
46     class Scheduler
47         : boost::noncopyable
48     {
49     public:
50         ///////////////////////////////////////////////////////////////////////////
51         // Types
52
53         enum EventId { EV_NONE=0, 
54                        EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16, 
55                        EV_ALL=31 };
56
57         template <class Handle>
58         struct GenericCallback {
59             typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
60                                           EventId) > Callback;
61         };
62         typedef GenericCallback<int>::Callback Callback;
63
64         ///////////////////////////////////////////////////////////////////////////
65         ///\name Structors and default members
66         ///@{
67
68         // private default constructor
69         // no copy constructor
70         // no copy assignment
71         // default destructor
72         // no conversion constructors
73
74         static Scheduler & instance();
75
76         ///@}
77         ///////////////////////////////////////////////////////////////////////////
78
79         void add(int fd, Callback const & cb, EventId eventMask = EV_ALL);
80         void remove(int fd, EventId eventMask = EV_ALL);
81
82         template <class Handle>
83         void add(Handle const & handle, 
84                  typename GenericCallback<Handle>::Callback const & cb,
85                  EventId eventMask = EV_ALL);
86         template <class Handle>
87         void remove(Handle const & handle, EventId eventMask = EV_ALL);
88
89         void process();
90
91         void terminate();
92
93     protected:
94
95     private:
96         Scheduler();
97
98         struct EventSpec 
99         {
100             Callback cb_read;
101             Callback cb_prio;
102             Callback cb_write;
103             Callback cb_hup;
104             Callback cb_err;
105
106             int epollMask() const;
107         };
108         
109         typedef std::map<int,EventSpec> FdTable;
110
111         FdTable fdTable_;
112         int epollFd_;
113         bool terminate_;
114     };
115
116 }}
117
118 ///////////////////////////////hh.e////////////////////////////////////////
119 #include "Scheduler.cci"
120 #include "Scheduler.ct"
121 #include "Scheduler.cti"
122 #endif
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // c-file-style: "satcom"
128 // End: