Open Lighting Architecture  0.9.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SelectPoller.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * SelectPoller.h
17  * A Poller which uses select()
18  * Copyright (C) 2013 Simon Newton
19  */
20 
21 #ifndef COMMON_IO_SELECTPOLLER_H_
22 #define COMMON_IO_SELECTPOLLER_H_
23 
24 #include <ola/Clock.h>
25 #include <ola/ExportMap.h>
26 #include <ola/base/Macro.h>
27 #include <ola/io/Descriptor.h>
28 
29 #include <map>
30 #include <set>
31 
32 #include "common/io/PollerInterface.h"
33 #include "common/io/TimeoutManager.h"
34 
35 namespace ola {
36 namespace io {
37 
43 class SelectPoller : public PollerInterface {
44  public :
50  SelectPoller(ExportMap *export_map, Clock *clock);
51 
52  ~SelectPoller();
53 
54  bool AddReadDescriptor(class ReadFileDescriptor *descriptor);
55  bool AddReadDescriptor(class ConnectedDescriptor *descriptor,
56  bool delete_on_close);
57  bool RemoveReadDescriptor(class ReadFileDescriptor *descriptor);
58  bool RemoveReadDescriptor(class ConnectedDescriptor *descriptor);
59 
60  bool AddWriteDescriptor(class WriteFileDescriptor *descriptor);
61  bool RemoveWriteDescriptor(class WriteFileDescriptor *descriptor);
62 
63  const TimeStamp *WakeUpTime() const { return &m_wake_up_time; }
64 
65  bool Poll(TimeoutManager *timeout_manager,
66  const TimeInterval &poll_interval);
67 
68  private:
69  typedef struct {
70  ConnectedDescriptor *descriptor;
71  bool delete_on_close;
72  } connected_descriptor_t;
73 
74  typedef std::map<int, ReadFileDescriptor*> ReadDescriptorMap;
75  typedef std::map<int, WriteFileDescriptor*> WriteDescriptorMap;
76  typedef std::map<int, connected_descriptor_t*> ConnectedDescriptorMap;
77 
78  ExportMap *m_export_map;
79  CounterVariable *m_loop_iterations;
80  CounterVariable *m_loop_time;
81  Clock *m_clock;
82  TimeStamp m_wake_up_time;
83 
84  ReadDescriptorMap m_read_descriptors;
85  WriteDescriptorMap m_write_descriptors;
86  ConnectedDescriptorMap m_connected_read_descriptors;
87 
88  void CheckDescriptors(fd_set *r_set, fd_set *w_set);
89  bool AddDescriptorsToSet(fd_set *r_set, fd_set *w_set, int *max_sd);
90 
91  DISALLOW_COPY_AND_ASSIGN(SelectPoller);
92 };
93 } // namespace io
94 } // namespace ola
95 #endif // COMMON_IO_SELECTPOLLER_H_