Open Lighting Architecture  0.9.6
 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 
64  public :
65  struct Options {
66  public:
67  Options(): force_select(false) {}
68 
74  };
75 
82  SelectServer(ola::ExportMap *export_map = NULL,
83  Clock *clock = NULL,
84  const Options &options = Options());
85 
91  ~SelectServer();
92 
97  bool IsRunning() const { return m_is_running; }
98 
99  const TimeStamp *WakeUpTime() const;
100 
106  void Terminate();
107 
115  void SetDefaultInterval(const TimeInterval &block_interval);
116 
122  void Run();
123 
127  void RunOnce();
128 
134  void RunOnce(const TimeInterval &block_interval);
135 
136  bool AddReadDescriptor(ReadFileDescriptor *descriptor);
137  bool AddReadDescriptor(ConnectedDescriptor *descriptor,
138  bool delete_on_close = false);
139  void RemoveReadDescriptor(ReadFileDescriptor *descriptor);
140  void RemoveReadDescriptor(ConnectedDescriptor *descriptor);
141 
142  bool AddWriteDescriptor(WriteFileDescriptor *descriptor);
143  void RemoveWriteDescriptor(WriteFileDescriptor *descriptor);
144 
146  unsigned int ms,
147  ola::Callback0<bool> *callback);
149  const ola::TimeInterval &interval,
150  ola::Callback0<bool> *callback);
151 
153  unsigned int ms,
156  const ola::TimeInterval &interval,
159 
170  void RunInLoop(ola::Callback0<void> *callback);
171 
172  void Execute(ola::BaseCallback0<void> *callback);
173 
174  void DrainCallbacks();
175 
176  private:
177  typedef std::vector<ola::BaseCallback0<void>*> Callbacks;
178  typedef std::set<ola::Callback0<void>*> LoopClosureSet;
179 
180  ExportMap *m_export_map;
181  bool m_terminate, m_is_running;
182  TimeInterval m_poll_interval;
183  std::auto_ptr<class TimeoutManager> m_timeout_manager;
184  std::auto_ptr<class PollerInterface> m_poller;
185 
186  Clock *m_clock;
187  bool m_free_clock;
188  LoopClosureSet m_loop_callbacks;
189  Callbacks m_incoming_callbacks;
190  ola::thread::Mutex m_incoming_mutex;
191  LoopbackDescriptor m_incoming_descriptor;
192 
193  bool CheckForEvents(const TimeInterval &poll_interval);
194  void DrainAndExecute();
195  void RunCallbacks(Callbacks *callbacks);
196  void SetTerminate() { m_terminate = true; }
197 
198  // the maximum time we'll wait in the select call
199  static const unsigned int POLL_INTERVAL_SECOND = 10;
200  static const unsigned int POLL_INTERVAL_USECOND = 0;
201 
202  static const TimeStamp empty_time;
203 
204  friend class ::SelectServerTest;
205 
206  DISALLOW_COPY_AND_ASSIGN(SelectServer);
207 };
208 } // namespace io
209 } // namespace ola
210 #endif // INCLUDE_OLA_IO_SELECTSERVER_H_