Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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_
A time interval, with usecond accuracy.
Definition: Clock.h:130
Manages timer events.
Definition: TimeoutManager.h:45
A container for the exported variables.
Definition: ExportMap.h:324
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition: Descriptor.h:282
bool RemoveWriteDescriptor(class WriteFileDescriptor *descriptor)
Unregister a WriteFileDescriptor for write events.
Definition: SelectPoller.cpp:191
Definition: ExportMap.h:176
Export variables on the http server.
bool Poll(TimeoutManager *timeout_manager, const TimeInterval &poll_interval)
Poll the Descriptors for events and execute any callbacks.
Definition: SelectPoller.cpp:202
Used to get the current time.
Definition: Clock.h:233
Represents a file descriptor that supports reading data.
Definition: Descriptor.h:140
Helper macros.
bool RemoveReadDescriptor(class ReadFileDescriptor *descriptor)
Unregister a ReadFileDescriptor for read events.
Definition: SelectPoller.cpp:155
An implementation of PollerInterface that uses select().
Definition: SelectPoller.h:43
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
SelectPoller(ExportMap *export_map, Clock *clock)
Create a new SelectPoller.
Definition: SelectPoller.cpp:100
bool AddWriteDescriptor(class WriteFileDescriptor *descriptor)
Register a WriteFileDescriptor to receive ready-to-write events.
Definition: SelectPoller.cpp:181
The interface for the Poller classes.
Definition: PollerInterface.h:70
Represents a point in time with microsecond accuracy.
Definition: Clock.h:183
Represents a file descriptor that supports writing data.
Definition: Descriptor.h:170
bool AddReadDescriptor(class ReadFileDescriptor *descriptor)
Register a ReadFileDescriptor for read events.
Definition: SelectPoller.cpp:126