Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
36 class SelectServerTest;
37 
38 namespace ola {
39 namespace io {
40 
64  public :
65  struct Options {
66  public:
67  Options()
68  : force_select(false),
69  export_map(NULL),
70  clock(NULL) {
71  }
72 
78 
83 
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 
164  unsigned int ms,
165  ola::Callback0<bool> *callback);
167  const ola::TimeInterval &interval,
168  ola::Callback0<bool> *callback);
169 
171  unsigned int ms,
174  const ola::TimeInterval &interval,
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  DISALLOW_COPY_AND_ASSIGN(SelectServer);
226 };
227 } // namespace io
228 } // namespace ola
229 #endif // INCLUDE_OLA_IO_SELECTSERVER_H_
Definition: Clock.h:118
Clock * clock
The Clock to use.
Definition: SelectServer.h:87
~SelectServer()
Clean up.
Definition: SelectServer.cpp:99
Definition: SelectServer.h:65
A 0 arg, single use callback that returns void.
Definition: Callback.h:157
void SetDefaultInterval(const TimeInterval &block_interval)
Set the duration to block for.
Definition: SelectServer.cpp:122
A container for the exported variables.
Definition: ExportMap.h:324
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition: Descriptor.h:282
A 0 argument callback which can be called multiple times.
Definition: Callback.h:129
void RemoveReadDescriptor(ReadFileDescriptor *descriptor)
Remove a ReadFileDescriptor for read-events.
Definition: SelectServer.cpp:171
void RemoveTimeout(ola::thread::timeout_id id)
Cancel an existing timeout.
Definition: SelectServer.cpp:245
void Terminate()
Exit from the Run() loop.
Definition: SelectServer.cpp:116
Export variables on the http server.
void Run()
Enter the event loop.
Definition: SelectServer.cpp:126
void * timeout_id
A timeout handle which can later be used to cancel a timeout.
Definition: SchedulerInterface.h:34
ola::thread::timeout_id RegisterRepeatingTimeout(unsigned int ms, ola::Callback0< bool > *callback)
Execute a callback periodically.
Definition: SelectServer.cpp:217
ola::thread::timeout_id RegisterSingleTimeout(unsigned int ms, ola::SingleUseCallback0< void > *callback)
Execute a callback after a certain time interval.
Definition: SelectServer.cpp:231
The interface for the SelectServer.
Definition: SelectServerInterface.h:42
void Execute(ola::BaseCallback0< void > *callback)
Execute the supplied callback at some point in the future.
Definition: SelectServer.cpp:253
Used to get the current time.
Definition: Clock.h:221
bool IsRunning() const
Checks if the SelectServer is running.
Definition: SelectServer.h:115
A single threaded I/O event management system.
Definition: SelectServer.h:63
A loopback descriptor.
Definition: Descriptor.h:398
Represents a file descriptor that supports reading data.
Definition: Descriptor.h:140
void RunInLoop(ola::Callback0< void > *callback)
Execute a callback on every event loop.
Definition: SelectServer.cpp:249
void DrainCallbacks()
Run all callbacks until there are none left.
Definition: SelectServer.cpp:268
bool AddReadDescriptor(ReadFileDescriptor *descriptor)
Register a ReadFileDescriptor for read-events.
Definition: SelectServer.cpp:153
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
ola::ExportMap * export_map
The export map to use.
Definition: SelectServer.h:82
bool AddWriteDescriptor(WriteFileDescriptor *descriptor)
Register a WriteFileDescriptor for write-events.
Definition: SelectServer.cpp:197
void RemoveWriteDescriptor(WriteFileDescriptor *descriptor)
Remove a WriteFileDescriptor for write-events.
Definition: SelectServer.cpp:205
bool force_select
Fall back to the select() implementation even if the flags are set for kqueue/epoll.
Definition: SelectServer.h:77
SelectServer(ola::ExportMap *export_map=NULL, Clock *clock=NULL)
Create a new SelectServer.
Definition: SelectServer.cpp:77
const TimeStamp * WakeUpTime() const
The time when this SelectServer was woken up.
Definition: SelectServer.cpp:108
Definition: Clock.h:171
void RunOnce()
Do a single pass through the event loop. Does not block.
Definition: SelectServer.cpp:143
Represents a file descriptor that supports writing data.
Definition: Descriptor.h:170
Definition: Mutex.h:35