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