aggregator
PullStreamAligner.hpp
Go to the documentation of this file.
1 #ifndef __AGGREGATORE_PULLSTREAMALIGNER__
2 #define __AGGREGATORE_PULLSTREAMALIGNER__
3 
4 #include <aggregator/StreamAligner.hpp>
5 
6 namespace aggregator
7 {
9  {
10  class PullStreamBase
11  {
12  public:
13  PullStreamBase() : has_data(false) {}
14  virtual ~PullStreamBase() {}
15  virtual void pull() = 0;
16  virtual void push() = 0;
17  virtual void copyState( const PullStreamBase& other ) = 0;
18 
19  base::Time lastTime() const { return last_ts; }
20  bool hasData() const { return has_data; }
21 
22  protected:
23  base::Time last_ts;
24  bool has_data;
25  };
26 
27  template <class T> class PullStream : public PullStreamBase
28  {
29  public:
30  typedef boost::function<bool (base::Time&, T&)> pull_callback_t;
31 
32  PullStream( pull_callback_t pull_callback, StreamAligner* sa, size_t stream_index )
33  : stream_idx( stream_index ), sa( sa ), pull_callback( pull_callback ) {}
34 
35  void pull()
36  {
37  has_data = pull_callback( last_ts, last_data );
38  }
39 
40  void push()
41  {
42  if( has_data )
43  sa->push( stream_idx, last_ts, last_data );
44 
45  has_data = false;
46  }
47 
48  void copyState( const PullStreamBase& other )
49  {
50  const PullStream<T> &pull_stream(static_cast<const PullStream<T>& >(other));
51  operator=( pull_stream );
52  }
53 
54  protected:
55  int stream_idx;
56  StreamAligner *sa;
57 
58  pull_callback_t pull_callback;
59  T last_data;
60  };
61 
62  static bool comparePullStreams( const PullStreamBase* b1, const PullStreamBase* b2 )
63  {
64  const base::Time &ts1( b1->lastTime() );
65  const base::Time &ts2( b2->lastTime() );
66  return ts1 < ts2 || !b2->hasData();
67  }
68 
69  public:
70  template <class T>
71  int registerStream( typename PullStream<T>::pull_callback_t pull_callback,
72  typename Stream<T>::callback_t callback, int bufferSize, base::Time period, int priority = -1 )
73  {
74  int idx = StreamAligner::registerStream<T>( callback, bufferSize, period, priority );
75  pull_streams.push_back( new PullStream<T>( pull_callback, this, idx ) );
76  return idx;
77  }
78 
79  bool pull()
80  {
81  for(pull_stream_vector::iterator it=pull_streams.begin();it != pull_streams.end();it++)
82  {
83  if( !(*it)->hasData() )
84  (*it)->pull();
85  }
86  std::sort( pull_streams.begin(), pull_streams.end(), &comparePullStreams );
87 
88  PullStreamBase *first = pull_streams.front();
89  if( first->hasData() )
90  {
91  first->push();
92  return true;
93  }
94  return false;
95  }
96 
97  void copyState(const PullStreamAligner& other)
98  {
99  StreamAligner::copyState( other );
100 
101  assert( pull_streams.size() == other.pull_streams.size() );
102  for(size_t i=0;i<pull_streams.size();i++)
103  {
104  pull_streams[i]->copyState( *other.pull_streams[i] );
105  }
106  }
107 
109  {
110  for(pull_stream_vector::iterator it=pull_streams.begin();it != pull_streams.end();it++)
111  delete *it;
112  }
113 
114  protected:
115  typedef std::vector<PullStreamBase*> pull_stream_vector;
116  pull_stream_vector pull_streams;
117  };
118 }
119 
120 #endif
pull_stream_vector pull_streams
Definition: PullStreamAligner.hpp:116
bool pull()
Definition: PullStreamAligner.hpp:79
void copyState(const PullStreamAligner &other)
Definition: PullStreamAligner.hpp:97
std::vector< PullStreamBase * > pull_stream_vector
Definition: PullStreamAligner.hpp:115
Definition: StreamAligner.hpp:18
~PullStreamAligner()
Definition: PullStreamAligner.hpp:108
int registerStream(typename PullStream< T >::pull_callback_t pull_callback, typename Stream< T >::callback_t callback, int bufferSize, base::Time period, int priority=-1)
Definition: PullStreamAligner.hpp:71
Definition: PullStreamAligner.hpp:8
void push(int idx, const base::Time &ts, const T &data)
Push new data into the stream.
Definition: StreamAligner.hpp:418
Definition: DetermineSampleTimestamp.hpp:8
Definition: StreamAligner.hpp:48
void copyState(const StreamAligner &other)
Definition: StreamAligner.hpp:252