HARDT - The Ham Radio DSP Toolkit
hmux.h
1 #ifndef __HMUX_H
2 #define __HMUX_H
3 
4 #include "hreader.h"
5 #include "hwriter.h"
6 #include "hwriterconsumer.h"
7 #include "hprobe.h"
8 
31 template <class T>
32 class HMux : public HReader<T>, public HWriter<T>, public HWriterConsumer<T>
33 {
34  private:
35 
36  int _blocksize;
37 
38  std::vector< HReader<T>* > _readers;
39 
40  int _writers;
41  HWriter<T>* _writer;
42  int _received;
43  T* _output;
44 
45  T** _buffers;
46  int _bufferCount;
47 
48  bool _duplex;
49 
50  public:
51 
61  HMux( std::vector< HReader<T>* > readers, size_t blocksize, bool duplex = false);
62 
73  HMux( HWriter<T>* writer, int writers, size_t blocksize, bool duplex = false);
74 
84  HMux( std::vector< HWriterConsumer<T>* > consumers, size_t blocksize, bool duplex = false);
85 
87  ~HMux();
88 
90  int Read(T* dest, size_t blocksize);
91 
93  int Write(T* src, size_t blocksize);
94 
96  bool Command(HCommand* command) {
97  if( _readers.size() != 0 ) {
98  for (int i = 0; i < _readers.size(); i++) {
99  if (!_readers[i]->Command(command)) {
100  return false;
101  }
102  }
103  return true;
104  }
105  else if( _writer != nullptr ) {
106  return _writer->Command(command);
107  }
108  return true;
109  }
110 
112  void SetWriter(HWriter<T>* writer)
113  {
114  _writer = writer;
115  }
116 
118  bool Start()
119  {
120  if( _writer != nullptr ) {
121  return _writer->Start();
122  }
123  if( _readers.size() > 0 ) {
124  for (int i = 0; i < _readers.size(); i++) {
125  if (!_readers[i]->Start()) {
126  return false;
127  }
128  }
129  }
130  return true;
131  }
132 
134  bool Stop()
135  {
136  if( _writer != nullptr ) {
137  return _writer->Stop();
138  }
139  if( _readers.size() > 0 ) {
140  for (int i = 0; i < _readers.size(); i++) {
141  if (!_readers[i]->Stop()) {
142  return false;
143  }
144  }
145  }
146  return true;
147  }
148 };
149 
150 #endif
HMux::HMux
HMux(std::vector< HReader< T > * > readers, size_t blocksize, bool duplex=false)
Definition: hmux.cpp:7
HMux::~HMux
~HMux()
Definition: hmux.cpp:78
HMux::SetWriter
void SetWriter(HWriter< T > *writer)
Definition: hmux.h:112
HMux::Read
int Read(T *dest, size_t blocksize)
Definition: hmux.cpp:93
HMux::Command
bool Command(HCommand *command)
Definition: hmux.h:96
HWriter
Definition: hwriter.h:10
HMux::Stop
bool Stop()
Definition: hmux.h:134
HMux
Definition: hmux.h:32
HMux::Start
bool Start()
Definition: hmux.h:118
HWriter::Command
virtual bool Command(HCommand *command)=0
HReader
Definition: hreader.h:24
HMux::Write
int Write(T *src, size_t blocksize)
Definition: hmux.cpp:136
HWriter::Start
virtual bool Start()
Definition: hwriter.h:21
HCommand
Definition: hcommand.h:81
HWriter::Stop
virtual bool Stop()
Definition: hwriter.h:27
HWriterConsumer
Definition: hwriterconsumer.h:8