HARDT - The Ham Radio DSP Toolkit
hnetworkprocessor.h
1 #ifndef __HNETWORKPROCESSOR_H
2 #define __HNETWORKPROCESSOR_H
3 
4 #include <netinet/in.h>
5 
6 #include "hnetworkreader.h"
7 #include "hnetworkwriter.h"
8 
19 template <class T>
20 class HNetworkProcessor : public HProcessor<T>
21 {
22  private:
23 
24  int _dataPort;
25  int _serverSocket;
26  int _commandPort;
27  int _commandSocket;
28  int _clientSocket;
29  struct sockaddr_in _address;
30  const char* _server;
31  HNetworkReader<T> _networkReader;
32  HNetworkWriter<T> _networkWriter;
33  bool* _terminated;
34 
35  bool _isServer;
36  bool _isWriter;
37 
38  HReader<T>* _reader;
39  HWriter<T>* _writer;
40 
41  void InitServer();
42  void InitServerDataPort();
43  void InitServerCommandPort();
44  void InitClient();
45  void RunServer(long unsigned int blocks);
46  void RunClient(long unsigned int blocks);
47  void RunProcessor(long unsigned int blocks);
48 
49  bool SendCommand(HCommand* command);
50  void ReceiveCommands();
51  void ReadCommand(int socket);
52 
53  bool Command(HCommand* cmd)
54  {
55  return true;
56  }
57 
58  public:
59 
62  HNetworkProcessor(const char* address, int dataPort, int commandPort, HWriter<T>* writer, int blocksize, bool* terminationToken);
63 
66  HNetworkProcessor(const char* address, int dataPort, int commandPort, int blocksize, bool* terminationToken);
67 
70  HNetworkProcessor(const char* address, int dataPort, int commandPort, HReader<T>* reader, int blocksize, bool* terminationToken);
71 
74  HNetworkProcessor(int dataPort, int commandPort, HWriter<T>* writer, int blocksize, bool* terminationToken);
75 
78  HNetworkProcessor(int dataPort, int commandPort, int blocksize, bool* terminationToken);
79 
82  HNetworkProcessor(int dataPort, int commandPort, HReader<T>* reader, int blocksize, bool* terminationToken);
83 
86 
88  void Run(long unsigned int blocks = 0);
89 
91  void Halt();
92 
94  bool Command(H_COMMAND_CLASS commandClass, H_COMMAND_OPCODE commandOpcode, int16_t length, HCommandData data)
95  {
96  // A processor acting as a server can not send any commands. Commands is always initiated at the client
97  if( _isServer ) {
98  HLog("Refusing to send command since we are a server networkprocessor");
99  return false;
100  }
101 
102  // Create the command
103  HLog("Creating command with class %d and opcode %d with length %d", commandClass, commandOpcode, length);
104  HCommand cmd = {static_cast<int16_t>(commandClass), static_cast<int16_t>(commandOpcode), length, data};
105 
106  // Send the command to the locally connected reader or writer
107  if( HProcessor<T>::_reader != nullptr )
108  {
109  HLog("Sending command to reader chain");
110  if( !HProcessor<T>::_reader->Command(&cmd) )
111  {
112  HError("Error when sending command to reader chain");
113  return false;
114  }
115  }
116  if( HProcessor<T>::_writer != nullptr )
117  {
118  HLog("Sending command to writer chain");
119  if( !HProcessor<T>::_writer->Command(&cmd) )
120  {
121  HError("Error when sending command to writer chain");
122  return false;
123  }
124  }
125 
126  // Send the command to the remote end (the server)
127  HLog("Sending command to remote (server)");
128  if( !SendCommand(&cmd) )
129  {
130  HError("ERror when sending command to rmote end (the server)");
131  return false;
132  }
133 
134  // Done
135  HLog("Command sent successfully");
136  return true;
137  }
138 };
139 
140 #endif
HProcessor
Definition: hprocessor.h:24
HWriter
Definition: hwriter.h:10
HNetworkProcessor::Run
void Run(long unsigned int blocks=0)
Definition: hnetworkprocessor.cpp:278
HNetworkReader
Definition: hnetworkreader.h:10
HNetworkProcessor::HNetworkProcessor
HNetworkProcessor(const char *address, int dataPort, int commandPort, HWriter< T > *writer, int blocksize, bool *terminationToken)
Definition: hnetworkprocessor.cpp:24
HReader
Definition: hreader.h:24
HNetworkProcessor::Command
bool Command(H_COMMAND_CLASS commandClass, H_COMMAND_OPCODE commandOpcode, int16_t length, HCommandData data)
Definition: hnetworkprocessor.h:94
HCommand
Definition: hcommand.h:81
HNetworkProcessor::~HNetworkProcessor
~HNetworkProcessor()
Definition: hnetworkprocessor.cpp:138
HCommandData
Definition: hcommand.h:68
HNetworkProcessor::Halt
void Halt()
Definition: hnetworkprocessor.cpp:413
HNetworkWriter
Definition: hnetworkwriter.h:12
HNetworkProcessor
Definition: hnetworkprocessor.h:20