File: SelectPoller.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (427 lines) | stat: -rw-r--r-- 13,195 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * SelectPoller.cpp
 * A Poller which uses select()
 * Copyright (C) 2013 Simon Newton
 */

#ifdef _WIN32
// Pull in fd_set and related definitions.
#include <ola/win/CleanWinSock2.h>
#endif  // _WIN32

#include "common/io/SelectPoller.h"

#include <string.h>
#include <errno.h>

#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <utility>

#include "ola/Clock.h"
#include "ola/Logging.h"
#include "ola/base/Macro.h"
#include "ola/io/Descriptor.h"
#include "ola/stl/STLUtils.h"

namespace ola {
namespace io {

using std::pair;
using std::map;
using std::string;

/**
 * @brief Insert a descriptor into one of the descriptor maps.
 * @param descriptor_map the descriptor_map to insert into.
 * @param fd the FD to use as the key
 * @param value the value to associate with the key
 * @param type the name of the map, used for logging if the fd already exists
 *   in the map.
 * @returns true if the descriptor was inserted, false if it was already in the
 *   map.
 *
 * There are three possibilities:
 *  - The fd does not already exist in the map
 *  - The fd exists and the value is NULL.
 *  - The fd exists and is not NULL.
 */
template <typename T>
bool InsertIntoDescriptorMap(map<int, T*> *descriptor_map, int fd, T *value,
                             const string &type) {
  typedef map<int, T*> MapType;
  pair<typename MapType::iterator, bool> p = descriptor_map->insert(
      typename MapType::value_type(fd, value));

  if (!p.second) {
    // already in map
    if (p.first->second == NULL) {
      p.first->second = value;
    } else {
      OLA_WARN << "FD " << fd << " was already in the " << type
          << " descriptor map: " << p.first->second << " : " << value;
      return false;
    }
  }
  return true;
}

/**
 * @brief Remove a FD from a descriptor map by setting the value to NULL.
 * @returns true if the FD was removed from the map, false if it didn't exist
 *   in the map.
 */
template <typename T>
bool RemoveFromDescriptorMap(map<int, T*> *descriptor_map, int fd) {
  T **ptr = STLFind(descriptor_map, fd);
  if (ptr) {
    *ptr = NULL;
    return true;
  }
  return false;
}

SelectPoller::SelectPoller(ExportMap *export_map, Clock* clock)
    : m_export_map(export_map),
      m_loop_iterations(NULL),
      m_loop_time(NULL),
      m_clock(clock) {
  if (m_export_map) {
    m_loop_time = m_export_map->GetCounterVar(K_LOOP_TIME);
    m_loop_iterations = m_export_map->GetCounterVar(K_LOOP_COUNT);
  }
}

SelectPoller::~SelectPoller() {
  ConnectedDescriptorMap::iterator iter = m_connected_read_descriptors.begin();
  for (; iter != m_connected_read_descriptors.end(); ++iter) {
    if (iter->second) {
      if (iter->second->delete_on_close) {
        delete iter->second->descriptor;
      }
      delete iter->second;
    }
  }
  m_read_descriptors.clear();
  m_connected_read_descriptors.clear();
  m_write_descriptors.clear();
}

bool SelectPoller::AddReadDescriptor(class ReadFileDescriptor *descriptor) {
  if (!descriptor->ValidReadDescriptor()) {
    OLA_WARN << "AddReadDescriptor called with invalid descriptor";
    return false;
  }

  return InsertIntoDescriptorMap(&m_read_descriptors,
      descriptor->ReadDescriptor(), descriptor, "read");
}

bool SelectPoller::AddReadDescriptor(class ConnectedDescriptor *descriptor,
                                     bool delete_on_close) {
  if (!descriptor->ValidReadDescriptor()) {
    OLA_WARN << "AddReadDescriptor called with invalid descriptor";
    return false;
  }

  connected_descriptor_t *cd = new connected_descriptor_t();
  cd->descriptor = descriptor;
  cd->delete_on_close = delete_on_close;

  bool ok = InsertIntoDescriptorMap(&m_connected_read_descriptors,
      descriptor->ReadDescriptor(), cd, "connected");
  if (!ok) {
    delete cd;
  }
  return ok;
}

bool SelectPoller::RemoveReadDescriptor(class ReadFileDescriptor *descriptor) {
  if (!descriptor->ValidReadDescriptor()) {
    OLA_WARN << "Removing an invalid ReadDescriptor";
    return false;
  }

  return RemoveFromDescriptorMap(&m_read_descriptors,
                                 descriptor->ReadDescriptor());
}

bool SelectPoller::RemoveReadDescriptor(class ConnectedDescriptor *descriptor) {
  if (!descriptor->ValidReadDescriptor()) {
    OLA_WARN << "Removing an invalid ConnectedDescriptor";
    return false;
  }

  connected_descriptor_t **ptr = STLFind(
      &m_connected_read_descriptors, descriptor->ReadDescriptor());
  if (ptr && *ptr) {
    delete *ptr;
    *ptr = NULL;
    return true;
  }
  return false;
}

bool SelectPoller::AddWriteDescriptor(class WriteFileDescriptor *descriptor) {
  if (!descriptor->ValidWriteDescriptor()) {
    OLA_WARN << "AddWriteDescriptor called with invalid descriptor";
    return false;
  }

  return InsertIntoDescriptorMap(&m_write_descriptors,
      descriptor->WriteDescriptor(), descriptor, "write");
}

bool SelectPoller::RemoveWriteDescriptor(
    class WriteFileDescriptor *descriptor) {
  if (!descriptor->ValidWriteDescriptor()) {
    OLA_WARN << "Removing an invalid WriteDescriptor";
    return false;
  }

  return RemoveFromDescriptorMap(&m_write_descriptors,
                                 descriptor->WriteDescriptor());
}

bool SelectPoller::Poll(TimeoutManager *timeout_manager,
                        const TimeInterval &poll_interval) {
  int maxsd;
  fd_set r_fds, w_fds;
  TimeStamp now;
  TimeInterval sleep_interval = poll_interval;
  struct timeval tv;

  maxsd = 0;
  FD_ZERO(&r_fds);
  FD_ZERO(&w_fds);
  m_clock->CurrentMonotonicTime(&now);

  TimeInterval next_event_in = timeout_manager->ExecuteTimeouts(&now);
  if (!next_event_in.IsZero()) {
    sleep_interval = std::min(next_event_in, sleep_interval);
  }

  // Adding descriptors should be the last thing we do, they may have changed
  // due to timeouts above.
  bool closed_descriptors = AddDescriptorsToSet(&r_fds, &w_fds, &maxsd);
  // If there are closed descriptors, set the timeout to something
  // very small (1ms). This ensures we at least make a pass through the
  // descriptors.
  if (closed_descriptors) {
    sleep_interval = std::min(sleep_interval, TimeInterval(0, 1000));
  }

  // take care of stats accounting
  if (m_wake_up_time.IsSet()) {
    TimeInterval loop_time = now - m_wake_up_time;
    OLA_DEBUG << "ss process time was " << loop_time.ToString();
    if (m_loop_time)
      (*m_loop_time) += loop_time.AsInt();
    if (m_loop_iterations)
      (*m_loop_iterations)++;
  }

  sleep_interval.AsTimeval(&tv);
  switch (select(maxsd + 1, &r_fds, &w_fds, NULL, &tv)) {
    case 0:
      // timeout
      m_clock->CurrentMonotonicTime(&m_wake_up_time);
      timeout_manager->ExecuteTimeouts(&m_wake_up_time);

      if (closed_descriptors) {
        // there were closed descriptors before the select() we need to deal
        // with them.
        FD_ZERO(&r_fds);
        FD_ZERO(&w_fds);
        CheckDescriptors(&r_fds, &w_fds);
      }
      return true;
    case -1:
      if (errno == EINTR)
        return true;
      OLA_WARN << "select() error, " << strerror(errno);
      return false;
    default:
      m_clock->CurrentMonotonicTime(&m_wake_up_time);
      CheckDescriptors(&r_fds, &w_fds);
      m_clock->CurrentMonotonicTime(&m_wake_up_time);
      timeout_manager->ExecuteTimeouts(&m_wake_up_time);
  }

  return true;
}

/*
 * Add all the descriptors to the FD_SET.
 * @returns true if there are descriptors that have been closed.
 *
 * This also takes care of removing any entries from the maps where the value
 * is NULL. This is safe because we don't execute any callbacks from within
 * this method.
 */
bool SelectPoller::AddDescriptorsToSet(fd_set *r_set,
                                       fd_set *w_set,
                                       int *max_sd) {
  bool closed_descriptors = false;

  ReadDescriptorMap::iterator iter = m_read_descriptors.begin();
  while (iter != m_read_descriptors.end()) {
    ReadDescriptorMap::iterator this_iter = iter;
    iter++;

    ReadFileDescriptor *descriptor = this_iter->second;
    if (!descriptor) {
      // This one was removed.
      m_read_descriptors.erase(this_iter);
      continue;
    }

    if (descriptor->ValidReadDescriptor()) {
      *max_sd = std::max(*max_sd, descriptor->ReadDescriptor());
      FD_SET(descriptor->ReadDescriptor(), r_set);
    } else {
      // The descriptor was probably closed without removing it from the select
      // server
      if (m_export_map) {
        (*m_export_map->GetIntegerVar(K_READ_DESCRIPTOR_VAR))--;
      }
      m_read_descriptors.erase(this_iter);
      OLA_WARN << "Removed a inactive descriptor from the select server";
    }
  }

  ConnectedDescriptorMap::iterator con_iter =
      m_connected_read_descriptors.begin();
  while (con_iter != m_connected_read_descriptors.end()) {
    ConnectedDescriptorMap::iterator this_iter = con_iter;
    con_iter++;

    if (!this_iter->second) {
      // This one was removed.
      m_connected_read_descriptors.erase(this_iter);
      continue;
    }

    if (this_iter->second->descriptor->ValidReadDescriptor()) {
      *max_sd = std::max(
          *max_sd, this_iter->second->descriptor->ReadDescriptor());
      FD_SET(this_iter->second->descriptor->ReadDescriptor(), r_set);
    } else {
      closed_descriptors = true;
    }
  }

  WriteDescriptorMap::iterator write_iter = m_write_descriptors.begin();
  while (write_iter != m_write_descriptors.end()) {
    WriteDescriptorMap::iterator this_iter = write_iter;
    write_iter++;

    WriteFileDescriptor *descriptor = this_iter->second;
    if (!descriptor) {
      // This one was removed.
      m_write_descriptors.erase(this_iter);
      continue;
    }

    if (descriptor->ValidWriteDescriptor()) {
      *max_sd = std::max(*max_sd, descriptor->WriteDescriptor());
      FD_SET(descriptor->WriteDescriptor(), w_set);
    } else {
      // The descriptor was probably closed without removing it from the select
      // server
      if (m_export_map) {
        (*m_export_map->GetIntegerVar(K_WRITE_DESCRIPTOR_VAR))--;
      }
      m_write_descriptors.erase(this_iter);
      OLA_WARN << "Removed a disconnected descriptor from the select server";
    }
  }
  return closed_descriptors;
}


/*
 * Check all the registered descriptors:
 *  - Execute the callback for descriptors with data
 *  - Execute OnClose if a remote end closed the connection
 */
void SelectPoller::CheckDescriptors(fd_set *r_set, fd_set *w_set) {
  // Remember the add / remove methods above may be called during
  // PerformRead(), PerformWrite() or the on close handler. Our iterators are
  // safe because we only ever call erase from within AddDescriptorsToSet(),
  // which isn't called from any of the Add / Remove methods.
  ReadDescriptorMap::iterator iter = m_read_descriptors.begin();
  for (; iter != m_read_descriptors.end(); ++iter) {
    if (iter->second && FD_ISSET(iter->second->ReadDescriptor(), r_set)) {
      iter->second->PerformRead();
    }
  }

  ConnectedDescriptorMap::iterator con_iter =
      m_connected_read_descriptors.begin();
  for (; con_iter != m_connected_read_descriptors.end(); ++con_iter) {
    if (!con_iter->second) {
      continue;
    }

    connected_descriptor_t *cd = con_iter->second;
    ConnectedDescriptor *descriptor = cd->descriptor;

    bool closed = false;
    if (!descriptor->ValidReadDescriptor()) {
      closed = true;
    } else if (FD_ISSET(descriptor->ReadDescriptor(), r_set)) {
      if (descriptor->IsClosed()) {
        closed = true;
      } else {
        descriptor->PerformRead();
      }
    }

    if (closed) {
      ConnectedDescriptor::OnCloseCallback *on_close =
        descriptor->TransferOnClose();
      bool delete_on_close = cd->delete_on_close;

      delete con_iter->second;
      con_iter->second = NULL;
      if (m_export_map) {
        (*m_export_map->GetIntegerVar(K_CONNECTED_DESCRIPTORS_VAR))--;
      }

      if (on_close)
        on_close->Run();

      if (delete_on_close)
        delete descriptor;
    }
  }

  // Check the write sockets. These may have changed since the start of the
  // method due to running callbacks.
  WriteDescriptorMap::iterator write_iter = m_write_descriptors.begin();
  for (; write_iter != m_write_descriptors.end(); write_iter++) {
    if (write_iter->second &&
        FD_ISSET(write_iter->second->WriteDescriptor(), w_set)) {
      write_iter->second->PerformWrite();
    }
  }
}
}  // namespace io
}  // namespace ola