nidas  v1.2-1520
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 
31 #include <nidas/util/IOException.h>
33 
34 namespace nidas { namespace core {
35 
36 class DSMSensor;
37 
74 {
75 public:
76 
77  SampleScanner(int bufsize=8192);
78 
79  virtual ~SampleScanner();
80 
86  virtual void init();
87 
91  virtual void setMessageParameters(unsigned int len, const std::string& val, bool eom)
93 
97  virtual const std::string& getMessageSeparator() const
98  {
99  return _emptyString;
100  }
101 
105  virtual const std::string getBackslashedMessageSeparator() const
106  {
107  return _emptyString;
108  }
109 
110  virtual bool getMessageSeparatorAtEOM() const
111  {
112  return false;
113  }
114 
118  virtual unsigned int getMessageLength() const
119  {
120  return 0;
121  }
122 
127  virtual bool getNullTerminate() const
128  {
129  return false;
130  }
131 
132  void setUsecsPerByte(int val)
133  {
134  _usecsPerByte = val;
135  }
136 
137  int getUsecsPerByte() const
138  {
139  return _usecsPerByte;
140  }
141 
146  virtual size_t readBuffer(DSMSensor* sensor, bool& exhausted)
148 
155  virtual size_t readBuffer(DSMSensor* sensor, bool& exhausted,int msecTimeout)
157 
158  virtual void clearBuffer();
159 
164  virtual Sample* nextSample(DSMSensor* sensor) = 0;
165 
166  unsigned int getBytesInBuffer() const { return _bufhead - _buftail; }
167 
168  virtual void resetStatistics();
169 
177  virtual void calcStatistics(unsigned int periodUsec);
178 
179  unsigned int getMaxSampleLength() const
180  { return _maxSampleLength[_reportIndex]; }
181 
182  unsigned int getMinSampleLength() const
183  {
184  // if max is 0 then we haven't gotten any data
185  if (_maxSampleLength[_reportIndex] == 0) return 0;
187  }
188 
189  unsigned int getBadTimeTagCount() const
190  {
191  return _badTimeTags;
192  }
193 
194  float getObservedSamplingRate() const;
195 
196  float getObservedDataRate() const;
197 
198  void addNumBytesToStats(size_t val) { _nbytes += val; }
199 
200  void addSampleToStats(unsigned int val)
201  {
202  _nsamples++;
204  std::min(_minSampleLength[_currentIndex],val);
206  std::max(_maxSampleLength[_currentIndex],val);
207  }
208 
210  {
211  _badTimeTags++;
212  }
213 
214 protected:
215 
219  const unsigned int BUFSIZE;
220 
221  char* _buffer;
222 
223  unsigned int _bufhead;
224 
225  unsigned int _buftail;
226 
228 
230 
231  unsigned int _outSampRead;
232 
233  unsigned int _outSampToRead;
234 
236 
237  std::string _messageSeparator;
238 
240 
242 
246  char* _separator;
247 
252 
253 private:
254 
255  std::string _emptyString;
256 
258 
259  unsigned int _minSampleLength[2];
260 
261  unsigned int _maxSampleLength[2];
262 
264 
266 
267  size_t _nsamples;
268 
269  size_t _nbytes;
270 
271  unsigned int _badTimeTags;
272 
277 
279 
281 
286 
291 };
292 
301 {
302 public:
303 
304  DriverSampleScanner(int bufsize=8192);
305 
310  void setMessageParameters(unsigned int, const std::string&, bool)
311  throw(nidas::util::InvalidParameterException)
312  {
314  "setMessageSeparator not supported");
315  }
316 
321  Sample* nextSample(DSMSensor* sensor);
322 
323 private:
324 
325 };
326 
333 {
334 public:
335 
336  MessageSampleScanner(int bufsize=8192);
337 
379  void setMessageParameters(unsigned int len, const std::string& val, bool eom)
381 
386  const std::string& getMessageSeparator() const
387  {
388  return _messageSeparator;
389  }
390 
394  const std::string getBackslashedMessageSeparator() const;
395 
401  {
402  return _separatorAtEOM;
403  }
404 
405  unsigned int getMessageLength() const
406  {
407  return _messageLength;
408  }
409 
410 private:
411 
412 };
413 
424 {
425 public:
426 
427  MessageStreamScanner(int bufsize=4096);
428 
429  void setMessageParameters(unsigned int len, const std::string& val, bool eom)
431 
436  const std::string& getMessageSeparator() const
437  {
438  return _messageSeparator;
439  }
440 
444  const std::string getBackslashedMessageSeparator() const;
445 
447  {
448  return _separatorAtEOM;
449  }
450 
451  unsigned int getMessageLength() const
452  {
453  return _messageLength;
454  }
455 
456  void setNullTerminate(bool val)
457  {
458  _nullTerminate = val;
459  }
460 
461  bool getNullTerminate() const
462  {
463  return _nullTerminate;
464  }
465 
467  {
468  return (this->*_nextSampleFunc)(sensor);
469  }
470 
471  size_t readBuffer(DSMSensor* sensor, bool& exhausted)
473 
474  size_t readBuffer(DSMSensor* sensor, bool& exhausted,int msecTimeout)
476 
480  void warnNonIncrTimeTag(const DSMSensor *sensor,
481  dsm_time_t badtt, dsm_time_t cortt, unsigned int nbad);
482 
483  void warnBackwardsStepTimeTag( const DSMSensor *sensor,
484  dsm_time_t badtt, unsigned int nbad);
485 
486 protected:
487 
493  virtual Sample* nextSampleSepBOM(DSMSensor* sensor);
494 
500  virtual Sample* nextSampleSepEOM(DSMSensor* sensor);
501 
507 
508  /* ptr to setXXX member function for setting an attribute of this
509  * class, based on the value of the tag from the IOStream.
510  */
512 
524  Sample* requestBiggerSample(unsigned int nc);
525 
526 protected:
527 
529 
531 
533 
534  const unsigned int MAX_MESSAGE_STREAM_SAMPLE_SIZE;
535 
537 
539 
541 
545  unsigned int _sampleLengthAlloc;
546 
548 
553 
557  unsigned int _outSampLengthAlloc;
558 
564 
569  unsigned int _stepBackTimeTag;
570 
577  unsigned int _nonIncrTimeTag;
578 
579 };
580 
582 {
583 public:
584 
585  DatagramSampleScanner(int bufsize=16384);
586 
591  void setMessageParameters(unsigned int, const std::string&, bool)
592  throw(nidas::util::InvalidParameterException)
593  {
595  "setMessageParameters not supported");
596  }
597 
602  size_t readBuffer(DSMSensor* sensor, bool& exhausted)
604 
609  Sample* nextSample(DSMSensor* sensor);
610 
618  void setNullTerminate(bool val)
619  {
620  _nullTerminate = val;
621  }
622 
623  bool getNullTerminate() const
624  {
625  return _nullTerminate;
626  }
627 
628 
629 private:
630  std::list<int> _packetLengths;
631 
632  std::list<dsm_time_t> _packetTimes;
633 
635 
636 };
637 
638 }} // namespace nidas namespace core
639 
640 #endif
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:590
virtual bool getNullTerminate() const
Should the SampleScanner append a null character, &#39;\0&#39;, to the messages.
Definition: SampleScanner.h:127
virtual void calcStatistics(unsigned int periodUsec)
Update the sensor sampling statistics: samples/sec, bytes/sec, min/max sample size, that can be accessed via getObservedSamplingRate(), getObservedDataRate() etc.
Definition: SampleScanner.cc:193
std::list< int > _packetLengths
Definition: SampleScanner.h:630
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:379
const std::string getBackslashedMessageSeparator() const
Get message separator with backslash sequences added back.
Definition: SampleScanner.cc:283
bool _separatorAtEOM
Definition: SampleScanner.h:241
dsm_time_t _lastSampleTime
Definition: SampleScanner.h:532
unsigned int _stepBackTimeTag
Number of time tags that were not monotonically increasing, due to backward step changes.
Definition: SampleScanner.h:569
void warnBackwardsStepTimeTag(const DSMSensor *sensor, dsm_time_t badtt, unsigned int nbad)
Definition: SampleScanner.cc:458
MessageStreamScanner(int bufsize=4096)
Definition: SampleScanner.cc:301
virtual void resetStatistics()
Definition: SampleScanner.cc:179
virtual unsigned int getMessageLength() const
Returns 0.
Definition: SampleScanner.h:118
virtual bool getMessageSeparatorAtEOM() const
Definition: SampleScanner.h:110
int _currentIndex
Definition: SampleScanner.h:263
SampleScanner(int bufsize=8192)
Definition: SampleScanner.cc:53
size_t readBuffer(DSMSensor *sensor, bool &exhausted)
Read from the sensor into the internal buffer of this SampleScanner.
Definition: SampleScanner.cc:414
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:61
MessageSampleScanner(int bufsize=8192)
Definition: SampleScanner.cc:278
float getObservedSamplingRate() const
Definition: SampleScanner.cc:208
unsigned int getMessageLength() const
Returns 0.
Definition: SampleScanner.h:451
int _sampleOverflows
Definition: SampleScanner.h:540
void setMessageParameters(unsigned int len, const std::string &val, bool eom)
Set the parameters which delineate a message for this scanner.
Definition: SampleScanner.cc:288
dsm_time_t _lastBufferTime
Definition: SampleScanner.h:530
unsigned int _buftail
Definition: SampleScanner.h:225
size_t readBuffer(DSMSensor *sensor, bool &exhausted)
Read from the sensor into the internal buffer of this SampleScanner.
Definition: SampleScanner.cc:887
bool _nullTerminate
Definition: SampleScanner.h:634
float getObservedDataRate() const
Definition: SampleScanner.cc:216
void setUsecsPerByte(int val)
Definition: SampleScanner.h:132
unsigned int _sampleLengthAlloc
Size of samples to allocate.
Definition: SampleScanner.h:545
time_t _initialTimeSecs
Definition: SampleScanner.h:257
size_t _nsamples
Definition: SampleScanner.h:267
bool getMessageSeparatorAtEOM() const
Is the message separator at the end of the message (true), or at the beginning (false)?
Definition: SampleScanner.h:400
Definition: SampleScanner.h:581
unsigned int getBytesInBuffer() const
Definition: SampleScanner.h:166
unsigned int _outSampRead
Definition: SampleScanner.h:231
float _sampleRateObs
Observed number of samples per second.
Definition: SampleScanner.h:276
const std::string & getMessageSeparator() const
Get message separator string.
Definition: SampleScanner.h:436
int getUsecsPerByte() const
Definition: SampleScanner.h:137
dsm_time_t _tfirstchar
Definition: SampleScanner.h:528
struct dsm_sample _header
Definition: SampleScanner.h:229
DatagramSampleScanner(int bufsize=16384)
Definition: SampleScanner.cc:880
unsigned int _nonIncrTimeTag
Number of time tags that were not monotonically increasing, not because of backward step changes...
Definition: SampleScanner.h:577
unsigned int getMessageLength() const
Returns 0.
Definition: SampleScanner.h:405
unsigned int _maxSampleLength[2]
Definition: SampleScanner.h:261
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:447
float _dataRateObs
Definition: SampleScanner.h:278
dsm_time_t _bomtt
Definition: SampleScanner.h:538
char * _buffer
Definition: SampleScanner.h:221
unsigned int _bufhead
Definition: SampleScanner.h:223
Sample *(MessageStreamScanner::* _nextSampleFunc)(DSMSensor *)
Definition: SampleScanner.h:511
unsigned int getMaxSampleLength() const
Definition: SampleScanner.h:179
bool getNullTerminate() const
Should the SampleScanner append a null character, &#39;\0&#39;, to the messages.
Definition: SampleScanner.h:461
bool getMessageSeparatorAtEOM() const
Definition: SampleScanner.h:446
virtual void setMessageParameters(unsigned int len, const std::string &val, bool eom)=0
Set the parameters associated with scanning of character messages.
int _messageLength
Definition: SampleScanner.h:239
unsigned int _badTimeTags
Definition: SampleScanner.h:271
int len
Definition: sing.cc:934
Interface to a data sample.
Definition: Sample.h:189
Sample * nextSample(DSMSensor *sensor)
Extract the next sample from the buffer.
Definition: SampleScanner.cc:228
int _nsmallSamples
Count of number of consecutive samples smaller than _sampleLengthAlloc.
Definition: SampleScanner.h:552
virtual void clearBuffer()
Definition: SampleScanner.cc:174
SampleScanner & operator=(const SampleScanner &)
No assignment (could be added if needed).
DSMSensor provides the basic support for reading, processing and distributing samples from a sensor a...
Definition: DSMSensor.h:87
const unsigned int BUFSIZE
Buffer size for reading from sensor.
Definition: SampleScanner.h:219
std::list< dsm_time_t > _packetTimes
Definition: SampleScanner.h:632
void addSampleToStats(unsigned int val)
Definition: SampleScanner.h:200
size_t _nbytes
Definition: SampleScanner.h:269
Definition: IOException.h:37
std::string _messageSeparator
Definition: SampleScanner.h:237
void setMessageParameters(unsigned int len, const std::string &val, bool eom)
Set the parameters associated with scanning of character messages.
Definition: SampleScanner.cc:320
char * _outSampDataPtr
Definition: SampleScanner.h:235
A DriverSampleScanner which supports the set/get of message separation parameters.
Definition: SampleScanner.h:332
void incrementBadTimeTags()
Definition: SampleScanner.h:209
bool getNullTerminate() const
Should the SampleScanner append a null character, &#39;\0&#39;, to the messages.
Definition: SampleScanner.h:623
void setNullTerminate(bool val)
User of DatagramSampleScanner should specify if they want the samples to be null terminated.
Definition: SampleScanner.h:618
unsigned int getMinSampleLength() const
Definition: SampleScanner.h:182
A scanner of sample data.
Definition: SampleScanner.h:73
unsigned int getBadTimeTagCount() const
Definition: SampleScanner.h:189
virtual ~SampleScanner()
Definition: SampleScanner.cc:70
A SampleScanner for reading samples that have been pre-formatted by a device driver - they already ha...
Definition: SampleScanner.h:300
unsigned int _minSampleLength[2]
Definition: SampleScanner.h:259
Sample * nextSample(DSMSensor *sensor)
Extract the next sample from the buffer.
Definition: SampleScanner.h:466
Definition: types.h:82
virtual Sample * nextSample(DSMSensor *sensor)=0
Extract the next sample from the buffer.
std::string _emptyString
Definition: SampleScanner.h:255
int _separatorLen
Length of messageSeparator.
Definition: SampleScanner.h:251
DriverSampleScanner(int bufsize=8192)
Definition: SampleScanner.cc:223
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:467
void setMessageParameters(unsigned int, const std::string &, bool)
setMessageSeparator is not implemented in DriverSampleScanner.
Definition: SampleScanner.h:310
const std::string & getMessageSeparator() const
Get message separator string.
Definition: SampleScanner.h:386
char * _separator
messageSeparator in a C string.
Definition: SampleScanner.h:246
int _separatorCnt
Definition: SampleScanner.h:536
int _reportIndex
Definition: SampleScanner.h:265
void addNumBytesToStats(size_t val)
Definition: SampleScanner.h:198
virtual const std::string getBackslashedMessageSeparator() const
Returns an empty string.
Definition: SampleScanner.h:105
A SampleScanner which supports the set/get of message separation parameters and extracts samples from...
Definition: SampleScanner.h:423
void setMessageParameters(unsigned int, const std::string &, bool)
setMessageSeparator is not implemented in DatagramSampleScanner.
Definition: SampleScanner.h:591
int _usecsPerByte
Definition: SampleScanner.h:280
bool _nullTerminate
Definition: SampleScanner.h:547
Sample * nextSampleByLength(DSMSensor *sensor)
Method to read input and break it into samples strictly by record length.
Definition: SampleScanner.cc:816
virtual size_t readBuffer(DSMSensor *sensor, bool &exhausted)
Read from the sensor into the internal buffer of this SampleScanner.
Definition: SampleScanner.cc:82
const std::string getBackslashedMessageSeparator() const
Get message separator with backslash sequences added back.
Definition: SampleScanner.cc:315
virtual const std::string & getMessageSeparator() const
Returns an empty string.
Definition: SampleScanner.h:97
virtual void init()
Initialize the scanner.
Definition: SampleScanner.cc:77
void setNullTerminate(bool val)
Definition: SampleScanner.h:456
unsigned int _outSampLengthAlloc
Number of bytes allocated in data portion of current output sample.
Definition: SampleScanner.h:557
Sample * nextSample(DSMSensor *sensor)
Extract the next sample from the buffer.
Definition: SampleScanner.cc:944
const unsigned int MAX_MESSAGE_STREAM_SAMPLE_SIZE
Definition: SampleScanner.h:534
Definition: InvalidParameterException.h:35
Sample * _osamp
Definition: SampleScanner.h:227
unsigned int _outSampToRead
Definition: SampleScanner.h:233
bool _stepBackwards
If the system time of a buffer read is earlier than the previous read.
Definition: SampleScanner.h:563