3ba1cebe1e33d490d5082ae4dc76ec3b3a8605d4
[senf.git] / senf / Scheduler / Poller.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 Poller public header */
25
26 #ifndef HH_SENF_Scheduler_Poller_
27 #define HH_SENF_Scheduler_Poller_ 1
28
29 // Custom includes
30 #include <sys/epoll.h>
31 #include <boost/utility.hpp>
32 #include <boost/iterator/transform_iterator.hpp>
33 #include <boost/range/iterator_range.hpp>
34
35 //#include "Poller.mpp"
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 namespace senf {
39 namespace scheduler {
40 namespace detail {
41
42     /** \brief Epoll abstraction
43
44         This class provides a more convenient interface to the epoll() API. File descriptors are
45         registered with pointers to a parameterized event type. After waiting for an event, the
46         Poller allows to iterate over the event instances for all posted events.
47
48         \tparam Value Event type
49       */
50     template <class Value>
51     class Poller
52         : boost::noncopyable
53     {
54         struct GetPollResult
55         {
56             typedef std::pair<int, Value*> result_type;
57             result_type operator()(epoll_event const &) const;
58         };
59
60         static int const NumEvents = 8;
61
62     public:
63         //-////////////////////////////////////////////////////////////////////////
64         // Types
65
66         typedef Value value_type;
67         typedef boost::transform_iterator<GetPollResult, epoll_event*> iterator;
68         typedef boost::iterator_range<iterator> range;
69
70         enum Events {
71             EV_READ = EPOLLIN, EV_PRIO = EPOLLPRI, EV_WRITE = EPOLLOUT,
72             EV_HUP = EPOLLHUP, EV_ERR = EPOLLERR
73         };
74
75         //-////////////////////////////////////////////////////////////////////////
76         ///\name Structors and default members
77         //\{
78
79         Poller();
80         ~Poller();
81
82         //\}
83         //-////////////////////////////////////////////////////////////////////////
84
85         bool set(int fd, int events, Value * data); ///< Set file descriptor event data and mask
86                                         /**< The Poller does \e not own \a data. The value is owned
87                                              by some external entity (the dispatcher to be more
88                                              precise). */
89         void remove(int fd);            ///< Remove file descriptor
90         range wait();                   ///< Wait for one event
91                                         /**< \returns a range of iterators which iterate over the
92                                              data values registered with the event */
93
94         void timeout(int t);            ///< Set event timeout to \a t milliseconds
95         int timeout() const;            ///< Current event timeout
96
97     private:
98         int epollFd_;
99         int timeout_;
100     };
101
102
103 }}}
104
105 //-/////////////////////////////////////////////////////////////////////////////////////////////////
106 //#include "Poller.cci"
107 #include "Poller.ct"
108 #include "Poller.cti"
109 #endif
110
111 \f
112 // Local Variables:
113 // mode: c++
114 // fill-column: 100
115 // comment-column: 40
116 // c-file-style: "senf"
117 // indent-tabs-mode: nil
118 // ispell-local-dictionary: "american"
119 // compile-command: "scons -u test"
120 // End: