nidas v1.2.3
SampleScanner.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 ** 2006, 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#ifndef NIDAS_CORE_SAMPLESCANNER_H
27#define NIDAS_CORE_SAMPLESCANNER_H
28
29#include "SampleClock.h"
30
33
34namespace nidas { namespace core {
35
36class DSMSensor;
37
74{
75public:
76
77 SampleScanner(int bufsize=8192);
78
79 virtual ~SampleScanner();
80
86 virtual void init();
87
93 virtual void setMessageParameters(unsigned int len, const std::string& val,
94 bool eom) = 0;
95
99 virtual const std::string& getMessageSeparator() const
100 {
101 return _emptyString;
102 }
103
107 virtual const std::string getBackslashedMessageSeparator() const
108 {
109 return _emptyString;
110 }
111
112 virtual bool getMessageSeparatorAtEOM() const
113 {
114 return false;
115 }
116
120 virtual unsigned int getMessageLength() const
121 {
122 return 0;
123 }
124
129 virtual bool getNullTerminate() const
130 {
131 return false;
132 }
133
134 void setUsecsPerByte(int val)
135 {
137 }
138
139 int getUsecsPerByte() const
140 {
141 return _usecsPerByte;
142 }
143
150 virtual size_t readBuffer(DSMSensor* sensor, bool& exhausted);
151
160 virtual size_t readBuffer(DSMSensor* sensor, bool& exhausted, int msecTimeout);
161
162 virtual void clearBuffer();
163
168 virtual Sample* nextSample(DSMSensor* sensor) = 0;
169
170 unsigned int getBytesInBuffer() const { return _bufhead - _buftail; }
171
172 virtual void resetStatistics();
173
181 virtual void calcStatistics(unsigned int periodUsec);
182
183 unsigned int getMaxSampleLength() const
184 { return _maxSampleLength[_reportIndex]; }
185
186 unsigned int getMinSampleLength() const
187 {
188 // if max is 0 then we haven't gotten any data
189 if (_maxSampleLength[_reportIndex] == 0) return 0;
191 }
192
193 unsigned int getBadTimeTagCount() const
194 {
195 return _badTimeTags;
196 }
197
198 float getObservedSamplingRate() const;
199
200 float getObservedDataRate() const;
201
202 void addNumBytesToStats(size_t val) { _nbytes += val; }
203
204 void addSampleToStats(unsigned int val)
205 {
206 _nsamples++;
208 std::min(_minSampleLength[_currentIndex],val);
210 std::max(_maxSampleLength[_currentIndex],val);
211 }
212
214 {
215 _badTimeTags++;
216 }
217
218protected:
219
223 const unsigned int BUFSIZE;
224
225 char* _buffer;
226
227 unsigned int _bufhead;
228
229 unsigned int _buftail;
230
232
234
235 unsigned int _outSampRead;
236
237 unsigned int _outSampToRead;
238
240
241 std::string _messageSeparator;
242
244
246
251
256
257private:
258
259 std::string _emptyString;
260
262
263 unsigned int _minSampleLength[2];
264
265 unsigned int _maxSampleLength[2];
266
268
270
271 size_t _nsamples;
272
273 size_t _nbytes;
274
275 unsigned int _badTimeTags;
276
281
283
285
290
295};
296
305{
306public:
307
308 DriverSampleScanner(int bufsize=8192);
309
316 void setMessageParameters(unsigned int, const std::string&, bool)
317 {
319 "setMessageSeparator not supported");
320 }
321
326 Sample* nextSample(DSMSensor* sensor);
327
328private:
329
330};
331
338{
339public:
340
341 MessageSampleScanner(int bufsize=8192);
342
386 void setMessageParameters(unsigned int len, const std::string& val, bool eom);
387
392 const std::string& getMessageSeparator() const
393 {
394 return _messageSeparator;
395 }
396
400 const std::string getBackslashedMessageSeparator() const;
401
407 {
408 return _separatorAtEOM;
409 }
410
411 unsigned int getMessageLength() const
412 {
413 return _messageLength;
414 }
415
416private:
417
418};
419
430{
431public:
432
433 MessageStreamScanner(int bufsize=4096);
434
438 void setMessageParameters(unsigned int len, const std::string& val, bool eom);
439
444 const std::string& getMessageSeparator() const
445 {
446 return _messageSeparator;
447 }
448
452 const std::string getBackslashedMessageSeparator() const;
453
455 {
456 return _separatorAtEOM;
457 }
458
459 unsigned int getMessageLength() const
460 {
461 return _messageLength;
462 }
463
464 void setNullTerminate(bool val)
465 {
467 }
468
469 bool getNullTerminate() const
470 {
471 return _nullTerminate;
472 }
473
475 {
476 return (this->*_nextSampleFunc)(sensor);
477 }
478
482 size_t readBuffer(DSMSensor* sensor, bool& exhausted);
483
487 size_t readBuffer(DSMSensor* sensor, bool& exhausted,int msecTimeout);
488
492 void warnNonIncrTimeTag(const DSMSensor *sensor,
493 dsm_time_t badtt, dsm_time_t cortt, unsigned int nbad);
494
495 void warnBackwardsStepTimeTag( const DSMSensor *sensor,
496 dsm_time_t badtt, unsigned int nbad);
497
498protected:
499
505 virtual Sample* nextSampleSepBOM(DSMSensor* sensor);
506
512 virtual Sample* nextSampleSepEOM(DSMSensor* sensor);
513
519
520 /* ptr to setXXX member function for setting an attribute of this
521 * class, based on the value of the tag from the IOStream.
522 */
524
536 Sample* requestBiggerSample(unsigned int nc);
537
538protected:
539
541
543
545
547
549
551
553
557 unsigned int _sampleLengthAlloc;
558
560
565
570
576
581 unsigned int _stepBackTimeTag;
582
589 unsigned int _nonIncrTimeTag;
590
591};
592
594{
595public:
596
597 DatagramSampleScanner(int bufsize=16384);
598
605 void setMessageParameters(unsigned int, const std::string&, bool)
606 {
608 "setMessageParameters not supported");
609 }
610
617 size_t readBuffer(DSMSensor* sensor, bool& exhausted);
618
623 Sample* nextSample(DSMSensor* sensor);
624
632 void setNullTerminate(bool val)
633 {
635 }
636
637 bool getNullTerminate() const
638 {
639 return _nullTerminate;
640 }
641
642
643private:
644 std::list<int> _packetLengths;
645
646 std::list<dsm_time_t> _packetTimes;
647
649
650};
651
652}} // namespace nidas namespace core
653
654#endif
DSMSensor provides the basic support for reading, processing and distributing samples from a sensor a...
Definition DSMSensor.h:88
Definition SampleScanner.h:594
std::list< int > _packetLengths
Definition SampleScanner.h:644
bool getNullTerminate() const
Should the SampleScanner append a null character, '\0', to the messages.
Definition SampleScanner.h:637
void setNullTerminate(bool val)
User of DatagramSampleScanner should specify if they want the samples to be null terminated.
Definition SampleScanner.h:632
Sample * nextSample(DSMSensor *sensor)
Extract the next sample from the buffer.
Definition SampleScanner.cc:945
bool _nullTerminate
Definition SampleScanner.h:648
DatagramSampleScanner(int bufsize=16384)
Definition SampleScanner.cc:882
void setMessageParameters(unsigned int, const std::string &, bool)
setMessageSeparator is not implemented in DatagramSampleScanner.
Definition SampleScanner.h:605
size_t readBuffer(DSMSensor *sensor, bool &exhausted)
Read from the sensor into the internal buffer of this SampleScanner.
Definition SampleScanner.cc:889
std::list< dsm_time_t > _packetTimes
Definition SampleScanner.h:646
A SampleScanner for reading samples that have been pre-formatted by a device driver - they already ha...
Definition SampleScanner.h:305
Sample * nextSample(DSMSensor *sensor)
Extract the next sample from the buffer.
Definition SampleScanner.cc:231
DriverSampleScanner(int bufsize=8192)
Definition SampleScanner.cc:226
void setMessageParameters(unsigned int, const std::string &, bool)
setMessageSeparator is not implemented in DriverSampleScanner.
Definition SampleScanner.h:316
A DriverSampleScanner which supports the set/get of message separation parameters.
Definition SampleScanner.h:338
unsigned int getMessageLength() const
Returns 0.
Definition SampleScanner.h:411
bool getMessageSeparatorAtEOM() const
Is the message separator at the end of the message (true), or at the beginning (false)?
Definition SampleScanner.h:406
const std::string & getMessageSeparator() const
Get message separator string.
Definition SampleScanner.h:392
const std::string getBackslashedMessageSeparator() const
Get message separator with backslash sequences added back.
Definition SampleScanner.cc:286
MessageSampleScanner(int bufsize=8192)
Definition SampleScanner.cc:281
void setMessageParameters(unsigned int len, const std::string &val, bool eom)
Set the parameters which delineate a message for this scanner.
Definition SampleScanner.cc:291
A SampleScanner which supports the set/get of message separation parameters and extracts samples from...
Definition SampleScanner.h:430
Sample * nextSample(DSMSensor *sensor)
Extract the next sample from the buffer.
Definition SampleScanner.h:474
unsigned int _outSampLengthAlloc
Number of bytes allocated in data portion of current output sample.
Definition SampleScanner.h:569
size_t readBuffer(DSMSensor *sensor, bool &exhausted)
Definition SampleScanner.cc:417
unsigned int _sampleLengthAlloc
Size of samples to allocate.
Definition SampleScanner.h:557
dsm_time_t _lastBufferTime
Definition SampleScanner.h:542
MessageStreamScanner(int bufsize=4096)
Definition SampleScanner.cc:304
void warnNonIncrTimeTag(const DSMSensor *sensor, dsm_time_t badtt, dsm_time_t cortt, unsigned int nbad)
Issue warning log message about a non-forward time tag.
Definition SampleScanner.cc:449
Sample * requestBiggerSample(unsigned int nc)
If a new sample can be allocated without exceeding MAX_MESSAGE_STREAM_SAMPLE_SIZE then get a new,...
Definition SampleScanner.cc:382
const std::string & getMessageSeparator() const
Get message separator string.
Definition SampleScanner.h:444
bool _stepBackwards
If the system time of a buffer read is earlier than the previous read.
Definition SampleScanner.h:575
unsigned int getMessageLength() const
Returns 0.
Definition SampleScanner.h:459
void warnBackwardsStepTimeTag(const DSMSensor *sensor, dsm_time_t badtt, unsigned int nbad)
Definition SampleScanner.cc:460
bool getMessageSeparatorAtEOM() const
Definition SampleScanner.h:454
Sample *(MessageStreamScanner::* _nextSampleFunc)(DSMSensor *)
Definition SampleScanner.h:523
bool getNullTerminate() const
Should the SampleScanner append a null character, '\0', to the messages.
Definition SampleScanner.h:469
dsm_time_t _bomtt
Definition SampleScanner.h:550
int _sampleOverflows
Definition SampleScanner.h:552
Sample * nextSampleByLength(DSMSensor *sensor)
Method to read input and break it into samples strictly by record length.
Definition SampleScanner.cc:818
const unsigned int MAX_MESSAGE_STREAM_SAMPLE_SIZE
Definition SampleScanner.h:546
virtual Sample * nextSampleSepBOM(DSMSensor *sensor)
Method to read input and break it into samples where the message separator occurs at the beginning of...
Definition SampleScanner.cc:592
void setMessageParameters(unsigned int len, const std::string &val, bool eom)
Definition SampleScanner.cc:323
int _nsmallSamples
Count of number of consecutive samples smaller than _sampleLengthAlloc.
Definition SampleScanner.h:564
const std::string getBackslashedMessageSeparator() const
Get message separator with backslash sequences added back.
Definition SampleScanner.cc:318
bool _nullTerminate
Definition SampleScanner.h:559
unsigned int _stepBackTimeTag
Number of time tags that were not monotonically increasing, due to backward step changes.
Definition SampleScanner.h:581
void setNullTerminate(bool val)
Definition SampleScanner.h:464
virtual Sample * nextSampleSepEOM(DSMSensor *sensor)
Method to read input and break it into samples where the message separator occurs at the end of the m...
Definition SampleScanner.cc:469
dsm_time_t _lastSampleTime
Definition SampleScanner.h:544
int _separatorCnt
Definition SampleScanner.h:548
dsm_time_t _tfirstchar
Definition SampleScanner.h:540
unsigned int _nonIncrTimeTag
Number of time tags that were not monotonically increasing, not because of backward step changes,...
Definition SampleScanner.h:589
A scanner of sample data.
Definition SampleScanner.h:74
unsigned int getBytesInBuffer() const
Definition SampleScanner.h:170
int getUsecsPerByte() const
Definition SampleScanner.h:139
virtual bool getNullTerminate() const
Should the SampleScanner append a null character, '\0', to the messages.
Definition SampleScanner.h:129
SampleScanner & operator=(const SampleScanner &)
No assignment (could be added if needed).
virtual void setMessageParameters(unsigned int len, const std::string &val, bool eom)=0
Set the parameters associated with scanning of character messages.
Sample * _osamp
Definition SampleScanner.h:231
unsigned int getMaxSampleLength() const
Definition SampleScanner.h:183
char * _buffer
Definition SampleScanner.h:225
std::string _emptyString
Definition SampleScanner.h:259
int _separatorLen
Length of messageSeparator.
Definition SampleScanner.h:255
float getObservedSamplingRate() const
Definition SampleScanner.cc:211
bool _separatorAtEOM
Definition SampleScanner.h:245
unsigned int _buftail
Definition SampleScanner.h:229
size_t _nsamples
Definition SampleScanner.h:271
struct dsm_sample _header
Definition SampleScanner.h:233
virtual const std::string getBackslashedMessageSeparator() const
Returns an empty string.
Definition SampleScanner.h:107
float _sampleRateObs
Observed number of samples per second.
Definition SampleScanner.h:280
int _reportIndex
Definition SampleScanner.h:269
char * _outSampDataPtr
Definition SampleScanner.h:239
virtual void calcStatistics(unsigned int periodUsec)
Update the sensor sampling statistics: samples/sec, bytes/sec, min/max sample size,...
Definition SampleScanner.cc:196
int _messageLength
Definition SampleScanner.h:243
virtual void init()
Initialize the scanner.
Definition SampleScanner.cc:77
unsigned int _maxSampleLength[2]
Definition SampleScanner.h:265
unsigned int getMinSampleLength() const
Definition SampleScanner.h:186
virtual void clearBuffer()
Definition SampleScanner.cc:177
SampleScanner(int bufsize=8192)
Definition SampleScanner.cc:53
float _dataRateObs
Definition SampleScanner.h:282
unsigned int _bufhead
Definition SampleScanner.h:227
std::string _messageSeparator
Definition SampleScanner.h:241
unsigned int _badTimeTags
Definition SampleScanner.h:275
int _usecsPerByte
Definition SampleScanner.h:284
void incrementBadTimeTags()
Definition SampleScanner.h:213
void addNumBytesToStats(size_t val)
Definition SampleScanner.h:202
int _currentIndex
Definition SampleScanner.h:267
unsigned int _outSampToRead
Definition SampleScanner.h:237
unsigned int _minSampleLength[2]
Definition SampleScanner.h:263
size_t _nbytes
Definition SampleScanner.h:273
SampleScanner(const SampleScanner &)
No copy (could be added if needed).
unsigned int getBadTimeTagCount() const
Definition SampleScanner.h:193
unsigned int _outSampRead
Definition SampleScanner.h:235
virtual unsigned int getMessageLength() const
Returns 0.
Definition SampleScanner.h:120
virtual size_t readBuffer(DSMSensor *sensor, bool &exhausted)
Read from the sensor into the internal buffer of this SampleScanner.
Definition SampleScanner.cc:82
virtual const std::string & getMessageSeparator() const
Returns an empty string.
Definition SampleScanner.h:99
virtual bool getMessageSeparatorAtEOM() const
Definition SampleScanner.h:112
time_t _initialTimeSecs
Definition SampleScanner.h:261
virtual ~SampleScanner()
Definition SampleScanner.cc:70
char * _separator
messageSeparator in a C string.
Definition SampleScanner.h:250
virtual Sample * nextSample(DSMSensor *sensor)=0
Extract the next sample from the buffer.
void setUsecsPerByte(int val)
Definition SampleScanner.h:134
virtual void resetStatistics()
Definition SampleScanner.cc:182
float getObservedDataRate() const
Definition SampleScanner.cc:219
void addSampleToStats(unsigned int val)
Definition SampleScanner.h:204
const unsigned int BUFSIZE
Buffer size for reading from sensor.
Definition SampleScanner.h:223
Interface to a data sample.
Definition Sample.h:190
Definition InvalidParameterException.h:35
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 len
Definition sing.cc:948
Definition types.h:82