Open Lighting Architecture  0.9.1
 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 
47  public :
48  enum Direction {READ, WRITE};
49 
50  SelectServer(ola::ExportMap *export_map = NULL,
51  Clock *clock = NULL);
52  ~SelectServer();
53 
54  bool IsRunning() const { return !m_terminate; }
55  const TimeStamp *WakeUpTime() const;
56 
57  void Terminate();
58 
59  void SetDefaultInterval(const TimeInterval &poll_interval);
60  void Run();
61  void RunOnce(unsigned int delay_sec = POLL_INTERVAL_SECOND,
62  unsigned int delay_usec = POLL_INTERVAL_USECOND);
63 
64  bool AddReadDescriptor(ReadFileDescriptor *descriptor);
65  bool AddReadDescriptor(ConnectedDescriptor *descriptor,
66  bool delete_on_close = false);
67  bool RemoveReadDescriptor(ReadFileDescriptor *descriptor);
68  bool RemoveReadDescriptor(ConnectedDescriptor *descriptor);
69 
70  bool AddWriteDescriptor(WriteFileDescriptor *descriptor);
71  bool RemoveWriteDescriptor(WriteFileDescriptor *descriptor);
72 
73  ola::thread::timeout_id RegisterRepeatingTimeout(
74  unsigned int ms,
75  ola::Callback0<bool> *closure);
76  ola::thread::timeout_id RegisterRepeatingTimeout(
77  const ola::TimeInterval &interval,
78  ola::Callback0<bool> *closure);
79 
80  ola::thread::timeout_id RegisterSingleTimeout(
81  unsigned int ms,
83  ola::thread::timeout_id RegisterSingleTimeout(
84  const ola::TimeInterval &interval,
86  void RemoveTimeout(ola::thread::timeout_id id);
87 
88  void RunInLoop(ola::Callback0<void> *closure);
89 
90  void Execute(ola::BaseCallback0<void> *closure);
91 
92  private :
93  typedef std::set<ola::Callback0<void>*> LoopClosureSet;
94 
95  ExportMap *m_export_map;
96  bool m_terminate, m_is_running;
97  TimeInterval m_poll_interval;
98  std::auto_ptr<class TimeoutManager> m_timeout_manager;
99  std::auto_ptr<class PollerInterface> m_poller;
100 
101  Clock *m_clock;
102  bool m_free_clock;
103  LoopClosureSet m_loop_closures;
104  std::queue<ola::BaseCallback0<void>*> m_incoming_queue;
105  ola::thread::Mutex m_incoming_mutex;
106  LoopbackDescriptor m_incoming_descriptor;
107 
108  bool CheckForEvents(const TimeInterval &poll_interval);
109  void DrainAndExecute();
110  void SetTerminate() { m_terminate = true; }
111 
112  // the maximum time we'll wait in the select call
113  static const unsigned int POLL_INTERVAL_SECOND = 10;
114  static const unsigned int POLL_INTERVAL_USECOND = 0;
115 
116  static const TimeStamp empty_time;
117 
118  friend class ::SelectServerTest;
119 
120  DISALLOW_COPY_AND_ASSIGN(SelectServer);
121 };
122 } // namespace io
123 } // namespace ola
124 #endif // INCLUDE_OLA_IO_SELECTSERVER_H_