nidas  v1.2-1520
SampleBuffer.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  ** 2009, 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_SAMPLEBUFFER_H
28 #define NIDAS_CORE_SAMPLEBUFFER_H
29 
30 #include <iostream>
31 #include <deque>
32 
33 #include "SampleThread.h"
34 #include "SampleSourceSupport.h"
35 
36 namespace nidas { namespace core {
37 
43 class SampleBuffer : public SampleThread
44 {
45 public:
46 
50  SampleBuffer(const std::string& name,bool raw);
51 
52  virtual ~SampleBuffer();
53 
54  void setKeepStats(bool val)
55  {
56  _source.setKeepStats(val);
57  }
58 
59  bool getKeepStats() const
60  {
61  return _source.getKeepStats();
62  }
63 
65  {
66  return _source.getRawSampleSource();
67  }
68 
70  {
72  }
73 
78  void addSampleTag(const SampleTag* tag) throw()
79  {
80  _source.addSampleTag(tag);
81  }
82 
83  void removeSampleTag(const SampleTag* tag) throw()
84  {
86  }
87 
91  std::list<const SampleTag*> getSampleTags() const
92  {
93  return _source.getSampleTags();
94  }
95 
100  {
101  return _source.getSampleTagIterator();
102  }
103 
107  void addSampleClient(SampleClient* client) throw()
108  {
109  _source.addSampleClient(client);
110  }
111 
112  void removeSampleClient(SampleClient* client) throw()
113  {
114  _source.removeSampleClient(client);
115  }
116 
121  void addSampleClientForTag(SampleClient* client,const SampleTag* tag) throw()
122  {
123  _source.addSampleClientForTag(client,tag);
124  }
125 
126  void removeSampleClientForTag(SampleClient* client,const SampleTag* tag) throw()
127  {
128  _source.removeSampleClientForTag(client,tag);
129  }
130 
131  int getClientCount() const throw()
132  {
133  return _source.getClientCount();
134  }
135 
137  {
138  return _source.getSampleStats();
139  }
140 
144  void flush() throw();
145 
146  void interrupt();
147 
162  bool receive(const Sample *s) throw();
163 
173  size_t size() const;
174 
175  void setLengthSecs(float)
176  {
177  }
178 
179  float getLengthSecs() const
180  {
181  return 0.0;
182  }
183 
188  void setHeapMax(size_t val) { _heapMax = val; }
189 
190  size_t getHeapMax() const { return _heapMax; }
191 
195  size_t getHeapSize() const { return _heapSize; }
196 
204  void setHeapBlock(bool val) { _heapBlock = val; }
205 
206  bool getHeapBlock() const { return _heapBlock; }
207 
208  // void setDebug(bool val) { debug = val; }
209 
214  size_t getNumDiscardedSamples() const
215  {
216  return _discardedSamples;
217  }
218 
223  size_t getNumFutureSamples() const
224  {
225  return _realTimeFutureSamples;
226  }
227 
233  void setRealTime(bool val)
234  {
235  _realTime = val;
236  }
237 
238  bool getRealTime() const
239  {
240  return _realTime;
241  }
242 
243  void setLateSampleCacheSize(unsigned int)
244  {
245  }
246 
247  unsigned int getLateSampleCacheSize() const
248  {
249  return 0;
250  }
251 
252 private:
253 
257  int run() throw(nidas::util::Exception);
258 
259 // #define USE_DEQUE
260 #ifdef USE_DEQUE
261  std::deque<const Sample*> _sampleBuf;
262 #else
263  std::vector<const Sample*> _sampleBufs[2];
264 
265  std::vector<const Sample*>* _inserterBuf;
266 
267  std::vector<const Sample*>* _consumerBuf;
268 #endif
269 
271 
277  void heapDecrement(size_t bytes);
278 
280 
282 
287  size_t _heapMax;
288 
292  size_t _heapSize;
293 
306 
308 
314 
320 
325 
326  bool _doFlush;
327 
328  bool _flushed;
329 
335  bool _realTime;
336 
337  size_t sizeNoLock() const;
338 
339  bool emptyNoLock() const;
340 
341 
345  SampleBuffer(const SampleBuffer&);
346 
351 
352 };
353 
354 }} // namespace nidas namespace core
355 
356 #endif
void setKeepStats(bool val)
Definition: SampleBuffer.h:54
std::vector< const Sample * > * _inserterBuf
Definition: SampleBuffer.h:265
void setKeepStats(bool val)
Definition: SampleSourceSupport.h:154
size_t _heapMax
Limit on the maximum size of memory to use while buffering samples.
Definition: SampleBuffer.h:287
void addSampleClientForTag(SampleClient *client, const SampleTag *tag)
Add a Client for a given SampleTag.
Definition: SampleBuffer.h:121
SampleBuffer & operator=(const SampleBuffer &)
No assignment.
void removeSampleClient(SampleClient *client)
Remove a SampleClient from this SampleSource.
Definition: SampleBuffer.h:112
size_t _discardedSamples
Number of samples discarded because of _heapSize &gt; _heapMax and heapBlock == true.
Definition: SampleBuffer.h:313
void removeSampleClientForTag(SampleClient *client, const SampleTag *tag)
Remove a SampleClient for a given SampleTag from this SampleSource.
Definition: SampleBuffer.h:126
void setHeapBlock(bool val)
Definition: SampleBuffer.h:204
void setRealTime(bool val)
Is this sorter running in real-time? If so then we can screen for bad time-tags by checking against t...
Definition: SampleBuffer.h:233
Interface for a Thread for buffering samples.
Definition: SampleThread.h:52
float getLengthSecs() const
Definition: SampleBuffer.h:179
bool getRealTime() const
Definition: SampleBuffer.h:238
SampleSourceSupport _source
Definition: SampleBuffer.h:270
SampleSource * getProcessedSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SampleBuffer.h:69
void removeSampleTag(const SampleTag *tag)
Definition: SampleSourceSupport.cc:67
size_t sizeNoLock() const
Definition: SampleBuffer.cc:91
int _discardWarningCount
How often to log warnings about discardedSamples.
Definition: SampleBuffer.h:324
bool _doFlush
Definition: SampleBuffer.h:326
std::list< const SampleTag * > getSampleTags() const
Implementation of SampleSource::getSampleTags().
Definition: SampleBuffer.h:91
Pure virtual interface for a source of Samples.
Definition: SampleSource.h:48
SampleTagIterator getSampleTagIterator() const
Definition: SampleSourceSupport.cc:75
void removeSampleTag(const SampleTag *tag)
Definition: SampleBuffer.h:83
nidas::util::Cond _heapCond
Definition: SampleBuffer.h:307
void addSampleClient(SampleClient *client)
Implementation of SampleSource::addSampleClient().
Definition: SampleBuffer.h:107
A source of samples.
Definition: SampleSourceSupport.h:47
void heapDecrement(size_t bytes)
Utility function to decrement the heap size after writing one or more samples.
Definition: SampleBuffer.cc:399
void removeSampleClient(SampleClient *c)
Remove a SampleClient from this SampleSource This will also remove a SampleClient if it has been adde...
Definition: SampleSourceSupport.cc:88
bool getKeepStats() const
Definition: SampleSourceSupport.h:159
bool _realTime
Is this sorter running in real-time? If so then we can screen for bad time-tags by checking against t...
Definition: SampleBuffer.h:335
size_t getHeapMax() const
Definition: SampleBuffer.h:190
bool getHeapBlock() const
Definition: SampleBuffer.h:206
void removeSampleClientForTag(SampleClient *c, const SampleTag *)
Add a SampleClient to this SampleSource.
Definition: SampleSourceSupport.cc:121
void setHeapMax(size_t val)
Set the maximum amount of heap memory to use for sorting samples.
Definition: SampleBuffer.h:188
void addSampleClient(SampleClient *c)
Add a SampleClient to this SampleSource.
Definition: SampleSourceSupport.cc:80
Pure virtual interface of a client of Samples.
Definition: SampleClient.h:38
size_t getNumFutureSamples() const
Number of samples discarded because their timetags were in the future.
Definition: SampleBuffer.h:223
size_t size() const
Current number of samples in the buffer.
Definition: SampleBuffer.cc:85
size_t getHeapSize() const
Get the current amount of heap being used for sorting.
Definition: SampleBuffer.h:195
virtual ~SampleBuffer()
Definition: SampleBuffer.cc:67
const SampleStats & getSampleStats() const
Definition: SampleSourceSupport.h:149
std::vector< const Sample * > * _consumerBuf
Definition: SampleBuffer.h:267
std::vector< const Sample * > _sampleBufs[2]
Definition: SampleBuffer.h:263
void addSampleTag(const SampleTag *tag)
How to tell this SampleBuffer what sample tags it will be sorting.
Definition: SampleBuffer.h:78
nidas::util::Cond _flushCond
Definition: SampleBuffer.h:281
SampleBuffer(const std::string &name, bool raw)
Constructor.
Definition: SampleBuffer.cc:43
void setLateSampleCacheSize(unsigned int)
Definition: SampleBuffer.h:243
A SampleClient that buffers its received samples, using a pair of STL vectors, and then sends the buf...
Definition: SampleBuffer.h:43
SampleTagIterator getSampleTagIterator() const
Implementation of SampleSource::getSampleTagIterator().
Definition: SampleBuffer.h:99
bool _heapBlock
_heapBlock controls what happens when the number of bytes in _samples exceeds _heapMax.
Definition: SampleBuffer.h:305
Interface to a data sample.
Definition: Sample.h:189
int run()
Thread run function.
Definition: SampleBuffer.cc:112
bool emptyNoLock() const
Definition: SampleBuffer.cc:100
void setLengthSecs(float)
Definition: SampleBuffer.h:175
int getClientCount() const
How many SampleClients are currently in my list.
Definition: SampleBuffer.h:131
SampleSource * getRawSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SampleSourceSupport.h:60
Class for iterating over the SampleTags of a Project, Site, DSMConfig, or a SampleSource.
Definition: NidsIterators.h:217
bool _flushed
Definition: SampleBuffer.h:328
Class describing a group of variables that are sampled and handled together.
Definition: SampleTag.h:87
void addSampleClientForTag(SampleClient *c, const SampleTag *)
Add a SampleClient to this SampleSource.
Definition: SampleSourceSupport.cc:101
size_t _heapSize
Current heap size, in bytes.
Definition: SampleBuffer.h:292
A source of samples.
Definition: SampleStats.h:41
size_t getNumDiscardedSamples() const
Number of samples discarded because of _heapSize &gt; _heapMax and heapBlock == true.
Definition: SampleBuffer.h:214
A wrapper class for a Posix condition variable.
Definition: ThreadSupport.h:245
const SampleStats & getSampleStats() const
Definition: SampleBuffer.h:136
void interrupt()
Interrupt this thread.
Definition: SampleBuffer.cc:374
bool receive(const Sample *s)
Insert a sample in the buffer, where it is then passed on to SampleClients.
Definition: SampleBuffer.cc:534
void flush()
flush all samples from buffer, distributing them to SampleClients.
Definition: SampleBuffer.cc:548
int getClientCount() const
How many SampleClients are currently in my list.
Definition: SampleSourceSupport.cc:136
size_t _realTimeFutureSamples
Number of samples discarded because getRealTime() is true and the samples have timetags later than th...
Definition: SampleBuffer.h:319
std::list< const SampleTag * > getSampleTags() const
What SampleTags am I a SampleSource for?
Definition: SampleSourceSupport.cc:54
bool getKeepStats() const
Definition: SampleBuffer.h:59
SampleSource * getProcessedSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SampleSourceSupport.h:66
unsigned int getLateSampleCacheSize() const
Definition: SampleBuffer.h:247
nidas::util::Cond _sampleBufCond
Definition: SampleBuffer.h:279
void addSampleTag(const SampleTag *tag)
Add a SampleTag to this SampleSource.
Definition: SampleSourceSupport.cc:60
SampleSource * getRawSampleSource()
Several objects in NIDAS can be both a SampleSource of raw Samples and processed Samples.
Definition: SampleBuffer.h:64