Open Lighting Architecture: include/ola/io/SelectServer.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
SelectServer.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 * SelectServer.h
17 * The select server interface
18 * Copyright (C) 2005 Simon Newton
19 */
20
21#ifndef INCLUDE_OLA_IO_SELECTSERVER_H_
22#define INCLUDE_OLA_IO_SELECTSERVER_H_
23
24#include <ola/Callback.h>
25#include <ola/Clock.h>
26#include <ola/ExportMap.h>
27#include <ola/io/Descriptor.h>
28#include <ola/io/SelectServerInterface.h>
29#include <ola/network/Socket.h>
30#include <ola/thread/Thread.h>
31
32#include <memory>
33#include <set>
34#include <vector>
35
36class SelectServerTest;
37
38namespace ola {
39namespace io {
40
63 class SelectServer: public SelectServerInterface {
64 public :
65 struct Options {
66 public:
67 Options()
68 : force_select(false),
69 export_map(NULL),
70 clock(NULL) {
71 }
72
77 bool force_select;
78
82 ola::ExportMap *export_map;
83
87 Clock *clock;
88 };
89
95 SelectServer(ola::ExportMap *export_map = NULL,
96 Clock *clock = NULL);
97
102 explicit SelectServer(const Options &options);
103
109 ~SelectServer();
110
115 bool IsRunning() const { return m_is_running; }
116
117 const TimeStamp *WakeUpTime() const;
118
124 void Terminate();
125
133 void SetDefaultInterval(const TimeInterval &block_interval);
134
140 void Run();
141
145 void RunOnce();
146
152 void RunOnce(const TimeInterval &block_interval);
153
154 bool AddReadDescriptor(ReadFileDescriptor *descriptor);
155 bool AddReadDescriptor(ConnectedDescriptor *descriptor,
156 bool delete_on_close = false);
157 void RemoveReadDescriptor(ReadFileDescriptor *descriptor);
158 void RemoveReadDescriptor(ConnectedDescriptor *descriptor);
159
160 bool AddWriteDescriptor(WriteFileDescriptor *descriptor);
161 void RemoveWriteDescriptor(WriteFileDescriptor *descriptor);
162
163 ola::thread::timeout_id RegisterRepeatingTimeout(
164 unsigned int ms,
165 ola::Callback0<bool> *callback);
166 ola::thread::timeout_id RegisterRepeatingTimeout(
167 const ola::TimeInterval &interval,
168 ola::Callback0<bool> *callback);
169
170 ola::thread::timeout_id RegisterSingleTimeout(
171 unsigned int ms,
172 ola::SingleUseCallback0<void> *callback);
173 ola::thread::timeout_id RegisterSingleTimeout(
174 const ola::TimeInterval &interval,
175 ola::SingleUseCallback0<void> *callback);
176 void RemoveTimeout(ola::thread::timeout_id id);
177
188 void RunInLoop(ola::Callback0<void> *callback);
189
190 void Execute(ola::BaseCallback0<void> *callback);
191
192 void DrainCallbacks();
193
194 private:
195 typedef std::vector<ola::BaseCallback0<void>*> Callbacks;
196 typedef std::set<ola::Callback0<void>*> LoopClosureSet;
197
198 ExportMap *m_export_map;
199 bool m_terminate, m_is_running;
200 TimeInterval m_poll_interval;
201 std::auto_ptr<class TimeoutManager> m_timeout_manager;
202 std::auto_ptr<class PollerInterface> m_poller;
203
204 Clock *m_clock;
205 bool m_free_clock;
206 LoopClosureSet m_loop_callbacks;
207 Callbacks m_incoming_callbacks;
208 ola::thread::Mutex m_incoming_mutex;
209 LoopbackDescriptor m_incoming_descriptor;
210
211 void Init(const Options &options);
212 bool CheckForEvents(const TimeInterval &poll_interval);
213 void DrainAndExecute();
214 void RunCallbacks(Callbacks *callbacks);
215 void SetTerminate() { m_terminate = true; }
216
217 // the maximum time we'll wait in the select call
218 static const unsigned int POLL_INTERVAL_SECOND = 10;
219 static const unsigned int POLL_INTERVAL_USECOND = 0;
220
221 static const TimeStamp empty_time;
222
223 friend class ::SelectServerTest;
224
225 SelectServer(const SelectServer &) = delete;
226 const SelectServer &operator=(const SelectServer &) = delete;
227};
228} // namespace io
229} // namespace ola
230#endif // INCLUDE_OLA_IO_SELECTSERVER_H_
Callback.h
ExportMap.h
Export variables on the http server.
ola::BaseCallback0
The base class for all 0 argument callbacks.
Definition Callback.h:119
ola::Callback0
A 0 argument callback which can be called multiple times.
Definition Callback.h:129
ola::Clock
Used to get the current time.
Definition Clock.h:230
ola::ExportMap
A container for the exported variables.
Definition ExportMap.h:324
ola::SingleUseCallback0
A 0 argument callback which deletes itself after it's run.
Definition Callback.h:141
ola::TimeInterval
Definition Clock.h:126
ola::TimeStamp
Definition Clock.h:179
ola::client::SelectServer::SelectServer
SelectServer(ola::ExportMap *export_map=NULL, Clock *clock=NULL)
Create a new SelectServer.
Definition SelectServer.cpp:77
ola::io::ConnectedDescriptor
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition Descriptor.h:283
ola::io::LoopbackDescriptor
A loopback descriptor.
Definition Descriptor.h:399
ola::io::ReadFileDescriptor
Represents a file descriptor that supports reading data.
Definition Descriptor.h:140
ola::io::SelectServer
A single threaded I/O event management system.
Definition SelectServer.h:63
ola::io::SelectServer::SetDefaultInterval
void SetDefaultInterval(const TimeInterval &block_interval)
Set the duration to block for.
Definition SelectServer.cpp:122
ola::io::SelectServer::IsRunning
bool IsRunning() const
Checks if the SelectServer is running.
Definition SelectServer.h:115
ola::io::SelectServer::RegisterSingleTimeout
ola::thread::timeout_id RegisterSingleTimeout(unsigned int ms, ola::SingleUseCallback0< void > *callback)
Execute a callback after a certain time interval.
Definition SelectServer.cpp:231
ola::io::SelectServer::SelectServer
SelectServer(ola::ExportMap *export_map=NULL, Clock *clock=NULL)
Create a new SelectServer.
Definition SelectServer.cpp:77
ola::io::SelectServer::RegisterRepeatingTimeout
ola::thread::timeout_id RegisterRepeatingTimeout(unsigned int ms, ola::Callback0< bool > *callback)
Execute a callback periodically.
Definition SelectServer.cpp:217
ola::io::SelectServer::RemoveTimeout
void RemoveTimeout(ola::thread::timeout_id id)
Cancel an existing timeout.
Definition SelectServer.cpp:245
ola::io::SelectServer::DrainCallbacks
void DrainCallbacks()
Run all callbacks until there are none left.
Definition SelectServer.cpp:268
ola::io::SelectServer::WakeUpTime
const TimeStamp * WakeUpTime() const
The time when this SelectServer was woken up.
Definition SelectServer.cpp:108
ola::io::SelectServer::RunInLoop
void RunInLoop(ola::Callback0< void > *callback)
Execute a callback on every event loop.
Definition SelectServer.cpp:249
ola::io::SelectServer::Execute
void Execute(ola::BaseCallback0< void > *callback)
Execute the supplied callback at some point in the future.
Definition SelectServer.cpp:253
ola::io::SelectServer::Terminate
void Terminate()
Exit from the Run() loop.
Definition SelectServer.cpp:116
ola::io::SelectServer::RunOnce
void RunOnce()
Do a single pass through the event loop. Does not block.
Definition SelectServer.cpp:143
ola::io::SelectServer::AddWriteDescriptor
bool AddWriteDescriptor(WriteFileDescriptor *descriptor)
Register a WriteFileDescriptor for write-events.
Definition SelectServer.cpp:197
ola::io::SelectServer::RemoveReadDescriptor
void RemoveReadDescriptor(ReadFileDescriptor *descriptor)
Remove a ReadFileDescriptor for read-events.
Definition SelectServer.cpp:171
ola::io::SelectServer::Run
void Run()
Enter the event loop.
Definition SelectServer.cpp:126
ola::io::SelectServer::~SelectServer
~SelectServer()
Clean up.
Definition SelectServer.cpp:99
ola::io::SelectServer::RemoveWriteDescriptor
void RemoveWriteDescriptor(WriteFileDescriptor *descriptor)
Remove a WriteFileDescriptor for write-events.
Definition SelectServer.cpp:205
ola::io::SelectServer::AddReadDescriptor
bool AddReadDescriptor(ReadFileDescriptor *descriptor)
Register a ReadFileDescriptor for read-events.
Definition SelectServer.cpp:153
ola::io::WriteFileDescriptor
Represents a file descriptor that supports writing data.
Definition Descriptor.h:170
ola::thread::Mutex
Definition Mutex.h:41
Descriptor.h
ola::io
Classes for general I/O and event handling.
Definition Descriptor.cpp:49
ola::thread::timeout_id
void * timeout_id
A timeout handle which can later be used to cancel a timeout.
Definition SchedulerInterface.h:34
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
ola::io::SelectServer::Options
Definition SelectServer.h:65
ola::io::SelectServer::Options::clock
Clock * clock
The Clock to use.
Definition SelectServer.h:87
ola::io::SelectServer::Options::force_select
bool force_select
Fall back to the select() implementation even if the flags are set for kqueue/epoll.
Definition SelectServer.h:77
ola::io::SelectServer::Options::export_map
ola::ExportMap * export_map
The export map to use.
Definition SelectServer.h:82