Open Lighting Architecture  0.9.0
 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <set>
30 
31 #include "common/io/PollerInterface.h"
32 #include "common/io/TimeoutManager.h"
33 
34 namespace ola {
35 namespace io {
36 
41 class SelectPoller : public PollerInterface {
42  public :
48  SelectPoller(ExportMap *export_map, Clock *clock);
49 
50  ~SelectPoller();
51 
52  bool AddReadDescriptor(class ReadFileDescriptor *descriptor);
53  bool AddReadDescriptor(class ConnectedDescriptor *descriptor,
54  bool delete_on_close);
55  bool RemoveReadDescriptor(class ReadFileDescriptor *descriptor);
56  bool RemoveReadDescriptor(class ConnectedDescriptor *descriptor);
57 
58  bool AddWriteDescriptor(class WriteFileDescriptor *descriptor);
59  bool RemoveWriteDescriptor(class WriteFileDescriptor *descriptor);
60 
61  const TimeStamp *WakeUpTime() const { return &m_wake_up_time; }
62 
63  bool Poll(TimeoutManager *timeout_manager,
64  const TimeInterval &poll_interval);
65 
66  private:
67  typedef struct {
68  ConnectedDescriptor *descriptor;
69  bool delete_on_close;
70  } connected_descriptor_t;
71 
72  struct connected_descriptor_t_lt {
73  bool operator()(const connected_descriptor_t &c1,
74  const connected_descriptor_t &c2) const {
75  return c1.descriptor->ReadDescriptor() <
76  c2.descriptor->ReadDescriptor();
77  }
78  };
79 
80  typedef std::set<ReadFileDescriptor*> ReadDescriptorSet;
81  typedef std::set<WriteFileDescriptor*> WriteDescriptorSet;
82  typedef std::set<connected_descriptor_t, connected_descriptor_t_lt>
83  ConnectedDescriptorSet;
84 
85  ExportMap *m_export_map;
86  CounterVariable *m_loop_iterations;
87  CounterVariable *m_loop_time;
88  Clock *m_clock;
89  TimeStamp m_wake_up_time;
90 
91  ReadDescriptorSet m_read_descriptors;
92  ConnectedDescriptorSet m_connected_read_descriptors;
93  WriteDescriptorSet m_write_descriptors;
94 
95  void CheckDescriptors(fd_set *r_set, fd_set *w_set);
96  bool AddDescriptorsToSet(fd_set *r_set, fd_set *w_set, int *max_sd);
97 
98  DISALLOW_COPY_AND_ASSIGN(SelectPoller);
99 };
100 } // namespace io
101 } // namespace ola
102 #endif // COMMON_IO_SELECTPOLLER_H_