nidas v1.2.3
SensorHandler.h
Go to the documentation of this file.
1// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2// vim: set shiftwidth=4 softtabstop=4 expandtab:
3/*
4 ********************************************************************
5 ** NIDAS: NCAR In-situ Data Acquistion Software
6 **
7 ** 2004, Copyright University Corporation for Atmospheric Research
8 **
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** The LICENSE.txt file accompanying this software contains
20 ** a copy of the GNU General Public License. If it is not found,
21 ** write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 **
24 ********************************************************************
25*/
26
27#ifndef NIDAS_CORE_PORTSELECTOR_H
28#define NIDAS_CORE_PORTSELECTOR_H
29
30#include <nidas/Config.h>
31
32#include "DSMSensor.h"
33#include "Polled.h"
34#include "SensorOpener.h"
36#include <nidas/util/Thread.h>
39
40#include <sys/time.h>
41
42#include <vector>
43#include <set>
44
55#if (((POLLING_METHOD == POLL_EPOLL_ET || POLLING_METHOD == POLL_EPOLL_LT) && !defined(HAVE_EPOLL_PWAIT)) || (POLLING_METHOD == POLL_POLL && !defined(HAVE_PPOLL)))
56#define USE_NOTIFY_PIPE
57#endif
58
59namespace nidas { namespace core {
60
88{
89public:
90
93
99 SensorHandler(unsigned short rserialPort = 0);
100
102
108 void signalHandler(int sig, siginfo_t*);
109
114 void addSensor(DSMSensor * sensor);
115
119 void closeSensor(DSMSensor * sensor);
120
126 void sensorIsOpen(DSMSensor * sensor) throw();
127
134
142 {
144 }
150 {
152 }
153
157 void handleRemoteSerial(int fd, DSMSensor * sensor);
158
164 int run();
165
166 std::list<DSMSensor*> getAllSensors() const;
167
168 std::list<DSMSensor*> getOpenedSensors() const;
169
173 void interrupt();
174
180 int join();
181
182#if POLLING_METHOD == POLL_EPOLL_ET || POLLING_METHOD == POLL_EPOLL_LT
183 int getEpollFd() const { return _epollfd; }
184#endif
185
190 void incrementFullBufferReads(const DSMSensor* sensor);
191
192
197 {
198 _pollingChanged = true;
199 }
200
201private:
202
203 class PolledDSMSensor : public Polled
204 {
205 public:
209 PolledDSMSensor(DSMSensor* sensor,SensorHandler* handler);
210
215
219 bool handlePollEvents(uint32_t events) throw();
220
222
223 int getFd() const { return _sensor->getReadFd(); }
224
225 const std::string getName() const { return _sensor->getName(); }
226
227 int getTimeoutMsecs() const { return _sensor->getTimeoutMsecs(); }
228
241 void setupTimeouts(int checkIntervalMsecs);
242
254 bool checkTimeout();
255
262 void close();
263
264 private:
266
268
274
279
286
287 // no copying
289
290 // no assignment
292
293 };
294
295#ifdef USE_NOTIFY_PIPE
296 class NotifyPipe: public Polled
297 {
298 public:
299 NotifyPipe(SensorHandler* handler);
300
301 ~NotifyPipe();
302
303 bool handlePollEvents(uint32_t events);
304
305 void close();
306
312 void notify();
313
314 int getFd() const { return _fds[0]; }
315
316 private:
317 int _fds[2];
319
320 // no copying
322
323 // no assignment
325 };
326#endif
327
333 void add(DSMSensor* sensor) throw();
334
340 void remove(PolledDSMSensor* sensor) throw();
341
346 void scheduleClose(PolledDSMSensor*) throw();
347
351 void scheduleReopen(PolledDSMSensor*) throw();
352
357 void add(RemoteSerialConnection *) throw();
358
359 void remove(RemoteSerialConnection *) throw();
360
365 void scheduleAdd(RemoteSerialConnection*) throw();
366
368
374 void handlePollingChange();
375
377
378 void setupTimeouts(int sensorCheckIntervalMsecs);
379
381
385 std::list<DSMSensor*> _allSensors;
386
388
393
398 bool getPollingChanged();
399
405 void setPollingChanged();
406
410 std::list<DSMSensor*> _openedSensors;
411
415 std::list<PolledDSMSensor*> _polledSensors;
416
421 std::list<DSMSensor*> _newOpenedSensors;
422
428 std::set<PolledDSMSensor*> _pendingSensorClosures;
429
434 std::set<PolledDSMSensor*> _pendingSensorReopens;
435
437
439
444 std::list<RemoteSerialConnection*> _newRserials;
445
450 std::set<RemoteSerialConnection*> _pendingRserialClosures;
451
452 std::list<RemoteSerialConnection*> _activeRserialConns;
453
454#if POLLING_METHOD == POLL_EPOLL_ET || POLLING_METHOD == POLL_EPOLL_LT
459
460 struct epoll_event* _events;
461
463
464#elif POLLING_METHOD == POLL_PSELECT
465
466 fd_set _rfdset;
467
468 fd_set _efdset;
469
473 int _nselect;
474
478 int* _fds;
479
480#elif POLLING_METHOD == POLL_POLL
481
482 struct pollfd* _fds;
483
484#endif
485
486#if POLLING_METHOD == POLL_PSELECT || POLLING_METHOD == POLL_POLL
490 Polled** _polled;
491
492#if POLLING_METHOD == POLL_POLL
493 nfds_t _nfds;
494#else
495 unsigned int _nfds;
496#endif
497
498 unsigned int _nAllocFds;
499
500 struct timespec _timeout;
501
502#endif
503
504#ifdef USE_NOTIFY_PIPE
506#endif
507
509
511
517
522
528
530
536 std::map<const DSMSensor*,unsigned int> _fullBufferReads;
537
539
542
545};
546
547}} // namespace nidas namespace core
548
549#endif
DSMSensor provides the basic support for reading, processing and distributing samples from a sensor a...
Definition DSMSensor.h:88
virtual int getTimeoutMsecs() const
Definition DSMSensor.h:531
virtual int getReadFd() const
Definition DSMSensor.h:460
virtual std::string getName() const
Return a name that should fully identify this sensor.
Definition DSMSensor.h:183
Interface for objects with a file descriptor, providing a virtual method to be called when system cal...
Definition Polled.h:110
Definition RemoteSerialConnection.h:42
Definition RemoteSerialListener.h:42
Definition SensorHandler.h:297
void notify()
Used in public methods of SensorHandler which are called from other threads to notify the SensorHandl...
Definition SensorHandler.cc:413
SensorHandler * _handler
Definition SensorHandler.h:318
bool handlePollEvents(uint32_t events)
Definition SensorHandler.cc:389
NotifyPipe & operator=(const NotifyPipe &)
NotifyPipe(SensorHandler *handler)
Definition SensorHandler.cc:328
int _fds[2]
Definition SensorHandler.h:317
~NotifyPipe()
Definition SensorHandler.cc:355
int getFd() const
Definition SensorHandler.h:314
void close()
Definition SensorHandler.cc:365
Definition SensorHandler.h:204
void close()
Remove this DSMSensor from those being polled, then call its close() method.
Definition SensorHandler.cc:211
PolledDSMSensor(DSMSensor *sensor, SensorHandler *handler)
Definition SensorHandler.cc:180
void setupTimeouts(int checkIntervalMsecs)
SensorHandler implements a fairly crude way to detect timeouts on data read from sensors.
Definition SensorHandler.cc:299
bool checkTimeout()
The SensorHandler will call this method of each opened sensor at the interval specified previously in...
Definition SensorHandler.cc:275
~PolledDSMSensor()
Destructor does not close().
Definition SensorHandler.h:214
SensorHandler * _handler
Definition SensorHandler.h:267
PolledDSMSensor & operator=(const PolledDSMSensor &)
DSMSensor * _sensor
Definition SensorHandler.h:265
DSMSensor * getDSMSensor()
Definition SensorHandler.h:221
PolledDSMSensor(const PolledDSMSensor &)
int _lastCheckInterval
What the previous timeout check interval was.
Definition SensorHandler.h:285
int getFd() const
Definition SensorHandler.h:223
int getTimeoutMsecs() const
Definition SensorHandler.h:227
bool handlePollEvents(uint32_t events)
Definition SensorHandler.cc:233
int _nTimeoutChecks
How many times this sensor has been checked for timeouts since the last time data was reads.
Definition SensorHandler.h:273
const std::string getName() const
Definition SensorHandler.h:225
int _nTimeoutChecksMax
How many timeout checks constitute an timeout on this sensor.
Definition SensorHandler.h:278
SensorHandler implements a DSMSensor event loop.
Definition SensorHandler.h:88
SensorHandler(const SensorHandler &)
No copy.
void addSensor(DSMSensor *sensor)
Add an unopened sensor to the SensorHandler.
Definition SensorHandler.cc:744
dsm_time_t _sensorStatsTime
Definition SensorHandler.h:510
RemoteSerialListener * _rserial
Definition SensorHandler.h:438
SensorOpener _opener
Definition SensorHandler.h:529
void interrupt()
Interrupt polling.
Definition SensorHandler.cc:720
dsm_time_t _sensorCheckTime
Definition SensorHandler.h:508
void scheduleAdd(RemoteSerialConnection *)
Schedule this remote serial connection to be closed when convenient.
Definition SensorHandler.cc:863
std::set< PolledDSMSensor * > _pendingSensorClosures
Sensors to be closed, probably due to a read error or a timeout.
Definition SensorHandler.h:428
void add(DSMSensor *sensor)
Internal private method to create a PolledDSMSensor from a DSMSensor and add it to the list of curren...
Definition SensorHandler.cc:815
std::list< DSMSensor * > getOpenedSensors() const
Definition SensorHandler.cc:174
std::list< DSMSensor * > _allSensors
The collection of DSMSensors to be handled.
Definition SensorHandler.h:385
void signalHandler(int sig, siginfo_t *)
Override default implementation of Thread::signalHandler().
Definition SensorHandler.cc:125
std::list< DSMSensor * > _openedSensors
Collection of DSMSensors which have been opened.
Definition SensorHandler.h:410
void handleRemoteSerial(int fd, DSMSensor *sensor)
struct epoll_event * _events
Definition SensorHandler.h:460
std::set< RemoteSerialConnection * > _pendingRserialClosures
RemoteSerialConnections to be closed, probably because the socket connection closed.
Definition SensorHandler.h:450
int _nevents
Definition SensorHandler.h:462
void setSensorStatsInterval(int val)
Set the sensor statistics calculation period.
Definition SensorHandler.h:141
~SensorHandler()
Close any remaining sensors.
Definition SensorHandler.cc:90
nidas::util::Mutex _pollingMutex
Definition SensorHandler.h:387
std::list< RemoteSerialConnection * > _newRserials
Newly opened RemoteSerialConnections, which are to be added to the list of file descriptors to be pol...
Definition SensorHandler.h:444
void scheduleClose(PolledDSMSensor *)
Schedule this PolledDSMSensor to be closed when convenient.
Definition SensorHandler.cc:786
bool _pollingChanged
A change in the polling file descriptors needs to be handled.
Definition SensorHandler.h:392
void checkSensors(dsm_time_t)
Check on each sensor.
SensorHandler(unsigned short rserialPort=0)
Constructor.
Definition SensorHandler.cc:44
std::map< const DSMSensor *, unsigned int > _fullBufferReads
For each sensor, number of times that the input buffer was filled in a read, i.e.
Definition SensorHandler.h:536
unsigned int _sensorCheckIntervalUsecs
Same as _sensorCheckIntervalMsecs, but in microseconds.
Definition SensorHandler.h:521
SensorHandler & operator=(const SensorHandler &)
No assignment.
std::list< PolledDSMSensor * > _polledSensors
Those DSMSensors currently being polled.
Definition SensorHandler.h:415
int run()
Thread function.
Definition SensorHandler.cc:426
void scheduleReopen(PolledDSMSensor *)
Schedule this PolledDSMSensor to be closed and reopened.
Definition SensorHandler.cc:802
unsigned short _remoteSerialSocketPort
Definition SensorHandler.h:436
int getEpollFd() const
Definition SensorHandler.h:183
int _sensorCheckIntervalMsecs
Interval for checking for timeouts on each sensor, in milliseconds.
Definition SensorHandler.h:516
void calcStatistics(dsm_time_t)
Definition SensorHandler.cc:130
int getSensorStatsInterval() const
Get the sensor check period.
Definition SensorHandler.h:149
std::list< RemoteSerialConnection * > _activeRserialConns
Definition SensorHandler.h:452
void updateTimeouts()
Tell the SensorHandler that one or more sensor timeouts have changed.
Definition SensorHandler.h:196
void handlePollingChange()
Called when something has changed in our collection of sensors.
Definition SensorHandler.cc:994
bool getPollingChanged()
Lock the polling mutex, read the state of the _pollingChanged flag, then return it.
Definition SensorHandler.cc:982
int _epollfd
epoll file descriptor.
Definition SensorHandler.h:458
void setupTimeouts(int sensorCheckIntervalMsecs)
Definition SensorHandler.cc:966
bool _acceptingOpens
Definition SensorHandler.h:538
unsigned int _sensorStatsInterval
Interval for calculcating through-put statistics on each sensor, in microseconds.
Definition SensorHandler.h:527
void checkTimeouts(dsm_time_t)
Definition SensorHandler.cc:150
void sensorIsOpen(DSMSensor *sensor)
After SensorOpener has opened the sensor, it will notify SensorHandler via this method that the senso...
Definition SensorHandler.cc:756
void incrementFullBufferReads(const DSMSensor *sensor)
Called by a DSMSensor to indicate that a read did not consume all available data.
Definition SensorHandler.cc:225
std::set< PolledDSMSensor * > _pendingSensorReopens
Sensors to be closed and then reopened.
Definition SensorHandler.h:434
std::list< DSMSensor * > _newOpenedSensors
Newly opened DSMSensors, which are to be added to the list of file descriptors to be polled.
Definition SensorHandler.h:421
int join()
Join this thread and join the SensorOpener.
Definition SensorHandler.cc:733
void closeSensor(DSMSensor *sensor)
Request that SensorHandler close the sensor.
void setPollingChanged()
Lock the polling mutex and set the _pollingChanged flag.
Definition SensorHandler.cc:988
void remove(PolledDSMSensor *sensor)
Internal private method to close the PolledDSMSensor.
Definition SensorHandler.cc:834
std::list< DSMSensor * > getAllSensors() const
Definition SensorHandler.cc:167
NotifyPipe * _notifyPipe
Definition SensorHandler.h:505
Thread which opens DSMSensors.
Definition SensorOpener.h:42
A C++ wrapper for a POSIX mutex.
Definition ThreadSupport.h:161
Definition Thread.h:83
Sample * getSample(sampleType type, unsigned int len)
A convienence method for getting a sample of an enumerated type from a pool.
Definition Sample.cc:70
long long dsm_time_t
Posix time in microseconds, the number of non-leap microseconds since 1970 Jan 1 00:00 UTC.
Definition Sample.h:62
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
int fd
Definition twod.c:56
#define USECS_PER_MSEC
Definition types.h:115