21 #ifndef INCLUDE_OLA_IO_SELECTSERVER_H_
22 #define INCLUDE_OLA_IO_SELECTSERVER_H_
25 #include <ola/Clock.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>
37 class SelectServerTest;
43 using ola::thread::timeout_id;
44 using std::priority_queue;
56 enum Direction {READ, WRITE};
62 bool IsRunning()
const {
return !m_terminate; }
63 const TimeStamp *WakeUpTime()
const {
return &m_wake_up_time; }
67 void SetDefaultInterval(
const TimeInterval &poll_interval);
69 void RunOnce(
unsigned int delay_sec = POLL_INTERVAL_SECOND,
70 unsigned int delay_usec = POLL_INTERVAL_USECOND);
74 bool delete_on_close =
false);
81 timeout_id RegisterRepeatingTimeout(
unsigned int ms,
86 timeout_id RegisterSingleTimeout(
unsigned int ms,
90 void RemoveTimeout(timeout_id
id);
97 static const char K_READ_DESCRIPTOR_VAR[];
98 static const char K_WRITE_DESCRIPTOR_VAR[];
99 static const char K_CONNECTED_DESCRIPTORS_VAR[];
100 static const char K_TIMER_VAR[];
101 static const char K_LOOP_TIME[];
102 static const char K_LOOP_COUNT[];
111 : m_interval(interval) {
113 clock->CurrentTime(&now);
114 m_next = now + m_interval;
117 virtual bool Trigger() = 0;
120 m_next = now + m_interval;
123 TimeStamp NextTime()
const {
return m_next; }
131 class SingleEvent:
public Event {
136 Event(interval, clock),
140 virtual ~SingleEvent() {
163 class RepeatingEvent:
public Event {
168 Event(interval, clock),
177 return m_closure->Run();
186 bool delete_on_close;
187 } connected_descriptor_t;
189 struct connected_descriptor_t_lt {
190 bool operator()(
const connected_descriptor_t &c1,
191 const connected_descriptor_t &c2)
const {
192 return c1.descriptor->ReadDescriptor() <
193 c2.descriptor->ReadDescriptor();
198 bool operator()(Event *e1, Event *e2)
const {
199 return e1->NextTime() > e2->NextTime();
203 typedef set<ReadFileDescriptor*> ReadDescriptorSet;
204 typedef set<WriteFileDescriptor*> WriteDescriptorSet;
205 typedef set<connected_descriptor_t, connected_descriptor_t_lt>
206 ConnectedDescriptorSet;
207 typedef set<ola::Callback0<void>*> LoopClosureSet;
209 bool m_terminate, m_is_running;
211 unsigned int m_next_id;
212 ReadDescriptorSet m_read_descriptors;
213 ConnectedDescriptorSet m_connected_read_descriptors;
214 WriteDescriptorSet m_write_descriptors;
215 set<timeout_id> m_removed_timeouts;
218 typedef priority_queue<Event*, vector<Event*>, ltevent> event_queue_t;
219 event_queue_t m_events;
225 LoopClosureSet m_loop_closures;
226 std::queue<ola::BaseCallback0<void>*> m_incoming_queue;
233 void CheckDescriptors(fd_set *r_set, fd_set *w_set);
234 bool AddDescriptorsToSet(fd_set *r_set, fd_set *w_set,
int *max_sd);
236 void UnregisterAll();
237 void DrainAndExecute();
238 void SetTerminate() { m_terminate =
true; }
239 void SafeIncrement(
const string &var_name);
240 void SafeDecrement(
const string &var_name);
242 static const int K_MS_IN_SECOND = 1000;
243 static const int K_US_IN_SECOND = 1000000;
245 static const unsigned int POLL_INTERVAL_SECOND = 10;
246 static const unsigned int POLL_INTERVAL_USECOND = 0;
248 friend class ::SelectServerTest;
252 #endif // INCLUDE_OLA_IO_SELECTSERVER_H_