Scheduler: TimerEvent doku
[senf.git] / Scheduler / FdDispatcher.hh
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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 /** \file
24     \brief FdDispatcher public header */
25
26 #ifndef HH_FdDispatcher_
27 #define HH_FdDispatcher_ 1
28
29 // Custom includes
30 #include <map>
31 #include "FdManager.hh"
32 #include "FIFORunner.hh"
33
34 //#include "FdDispatcher.mpp"
35 #include "FdDispatcher.ih"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39 namespace scheduler {
40
41     /** \brief Scheduler dispatcher managing poll-able file descriptors
42
43         File descriptors are added directly to the event loop.
44       */
45     class FdDispatcher
46     {
47     public:
48         ///////////////////////////////////////////////////////////////////////////
49         // Types
50
51         typedef boost::function<void (int)> Callback;
52
53         enum Events { 
54             EV_READ = FdManager::EV_READ, EV_PRIO = FdManager::EV_PRIO, EV_WRITE = FdManager::EV_WRITE,
55             EV_HUP = FdManager::EV_HUP, EV_ERR = FdManager::EV_ERR,
56             EV_ALL = FdManager::EV_READ | FdManager::EV_WRITE | FdManager::EV_PRIO
57         };
58
59         ///////////////////////////////////////////////////////////////////////////
60         ///\name Structors and default members
61         ///@{
62         
63         FdDispatcher(FdManager & manager, FIFORunner & runner);
64         ~FdDispatcher();
65
66         ///@}
67         ///////////////////////////////////////////////////////////////////////////
68
69         bool add(std::string const & name, int fd, Callback const & cb, int events = EV_ALL);
70                                         ///< Add file descriptor callback
71                                         /**< There is always one active callback for each
72                                              combination of file descriptor and event. Registering a
73                                              new callback will overwrite the old callback.
74                                              \param[in] name descriptive name
75                                              \param[in] fd file descriptor
76                                              \param[in] cb callback
77                                              \param[in] events Events to call \a cb for */
78
79         void remove(int fd, int events = EV_ALL); ///< Remove callback
80                                         /**< \param[in] fd file descriptor
81                                              \param[in] events Events for which to remove the
82                                                  callback */
83         
84         bool empty() const;             ///< \c true, if no file descriptors are registered.
85
86     protected:
87
88     private:
89         /// Internal: File descriptor event
90         struct FdEvent 
91             : public detail::FdTask<0, FdEvent>,
92               public detail::FdTask<1, FdEvent>,
93               public detail::FdTask<2, FdEvent>,
94               public FdManager::Event
95         {
96             typedef detail::FdTask<0, FdEvent> ReadTask;
97             typedef detail::FdTask<1, FdEvent> PrioTask;
98             typedef detail::FdTask<2, FdEvent> WriteTask;
99
100             explicit FdEvent(std::string const & name)
101                 : ReadTask (name), PrioTask (name), WriteTask (name) {}
102
103             virtual void signal(int events);
104             int activeEvents() const;
105             int events;
106         };
107
108         typedef std::map<int, FdEvent> FdMap;
109
110         FdMap fds_;
111         FdManager & manager_;
112         FIFORunner & runner_;
113     };
114
115 }}
116
117 ///////////////////////////////hh.e////////////////////////////////////////
118 #include "FdDispatcher.cci"
119 #include "FdDispatcher.ct"
120 #include "FdDispatcher.cti"
121 #endif
122
123 \f
124 // Local Variables:
125 // mode: c++
126 // fill-column: 100
127 // comment-column: 40
128 // c-file-style: "senf"
129 // indent-tabs-mode: nil
130 // ispell-local-dictionary: "american"
131 // compile-command: "scons -u test"
132 // End: