Open Lighting Architecture  0.9.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
63  public :
69  SelectServer(ola::ExportMap *export_map = NULL,
70  Clock *clock = NULL);
71 
77  ~SelectServer();
78 
83  bool IsRunning() const { return m_is_running; }
84 
85  const TimeStamp *WakeUpTime() const;
86 
92  void Terminate();
93 
101  void SetDefaultInterval(const TimeInterval &block_interval);
102 
108  void Run();
109 
113  void RunOnce();
114 
120  void RunOnce(const TimeInterval &block_interval);
121 
122  bool AddReadDescriptor(ReadFileDescriptor *descriptor);
123  bool AddReadDescriptor(ConnectedDescriptor *descriptor,
124  bool delete_on_close = false);
125  void RemoveReadDescriptor(ReadFileDescriptor *descriptor);
126  void RemoveReadDescriptor(ConnectedDescriptor *descriptor);
127 
128  bool AddWriteDescriptor(WriteFileDescriptor *descriptor);
129  void RemoveWriteDescriptor(WriteFileDescriptor *descriptor);
130 
132  unsigned int ms,
133  ola::Callback0<bool> *callback);
135  const ola::TimeInterval &interval,
136  ola::Callback0<bool> *callback);
137 
139  unsigned int ms,
142  const ola::TimeInterval &interval,
145 
156  void RunInLoop(ola::Callback0<void> *callback);
157 
158  void Execute(ola::BaseCallback0<void> *callback);
159 
160  void DrainCallbacks();
161 
162  private:
163  typedef std::vector<ola::BaseCallback0<void>*> Callbacks;
164  typedef std::set<ola::Callback0<void>*> LoopClosureSet;
165 
166  ExportMap *m_export_map;
167  bool m_terminate, m_is_running;
168  TimeInterval m_poll_interval;
169  std::auto_ptr<class TimeoutManager> m_timeout_manager;
170  std::auto_ptr<class PollerInterface> m_poller;
171 
172  Clock *m_clock;
173  bool m_free_clock;
174  LoopClosureSet m_loop_callbacks;
175  Callbacks m_incoming_callbacks;
176  ola::thread::Mutex m_incoming_mutex;
177  LoopbackDescriptor m_incoming_descriptor;
178 
179  bool CheckForEvents(const TimeInterval &poll_interval);
180  void DrainAndExecute();
181  void RunCallbacks(Callbacks *callbacks);
182  void SetTerminate() { m_terminate = true; }
183 
184  // the maximum time we'll wait in the select call
185  static const unsigned int POLL_INTERVAL_SECOND = 10;
186  static const unsigned int POLL_INTERVAL_USECOND = 0;
187 
188  static const TimeStamp empty_time;
189 
190  friend class ::SelectServerTest;
191 
192  DISALLOW_COPY_AND_ASSIGN(SelectServer);
193 };
194 } // namespace io
195 } // namespace ola
196 #endif // INCLUDE_OLA_IO_SELECTSERVER_H_