Open Lighting Architecture  0.9.2
 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 <queue>
34 #include <set>
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  private :
161  typedef std::set<ola::Callback0<void>*> LoopClosureSet;
162 
163  ExportMap *m_export_map;
164  bool m_terminate, m_is_running;
165  TimeInterval m_poll_interval;
166  std::auto_ptr<class TimeoutManager> m_timeout_manager;
167  std::auto_ptr<class PollerInterface> m_poller;
168 
169  Clock *m_clock;
170  bool m_free_clock;
171  LoopClosureSet m_loop_callbacks;
172  std::queue<ola::BaseCallback0<void>*> m_incoming_queue;
173  ola::thread::Mutex m_incoming_mutex;
174  LoopbackDescriptor m_incoming_descriptor;
175 
176  bool CheckForEvents(const TimeInterval &poll_interval);
177  void DrainAndExecute();
178  void SetTerminate() { m_terminate = true; }
179 
180  // the maximum time we'll wait in the select call
181  static const unsigned int POLL_INTERVAL_SECOND = 10;
182  static const unsigned int POLL_INTERVAL_USECOND = 0;
183 
184  static const TimeStamp empty_time;
185 
186  friend class ::SelectServerTest;
187 
188  DISALLOW_COPY_AND_ASSIGN(SelectServer);
189 };
190 } // namespace io
191 } // namespace ola
192 #endif // INCLUDE_OLA_IO_SELECTSERVER_H_