libcyberradio  22.01.24
SimpleIpSetup.cpp
1 /***************************************************************************
2  * \file SimpleIpSetup.cpp
3  * \brief Defines a simple 1Gig data for NDR-class radios without 10Gig
4  * ports.
5  * \author DA
6  * \author NH
7  * \author MN
8  * \copyright (c) 2017 CyberRadio Solutions, Inc. All rights reserved.
9  *
10  ***************************************************************************/
11 
12 #include "LibCyberRadio/Driver/SimpleIpSetup.h"
13 #include "LibCyberRadio/Driver/RadioHandler.h"
14 #include "LibCyberRadio/Common/Pythonesque.h"
15 #include <boost/lexical_cast.hpp>
16 #include <sstream>
17 
18 
19 namespace LibCyberRadio
20 {
21 
22  namespace Driver
23  {
24 
25  SimpleIpSetup::SimpleIpSetup(const std::string& name,
26  RadioHandler* parent,
27  bool debug,
28  const std::string& sourceIP,
29  const std::string& destIP,
30  const std::string& destMAC) :
31  Configurable(name, debug),
32  _parent(parent),
33  _sourceIP(sourceIP),
34  _sourceMAC("00:00:00:00:00:00"),
35  _destIP(destIP),
36  _destMAC(destMAC)
37  {
39  }
40 
42  {
43  }
44 
46  Configurable(other),
47  _parent(other._parent),
48  _sourceIP(other._sourceIP),
49  _sourceMAC(other._sourceMAC),
50  _destIP(other._destIP),
51  _destMAC(other._destMAC)
52  {
53  }
54 
56  {
58  if ( this != &other )
59  {
60  _parent = other._parent;
61  _sourceIP = other._sourceIP;
62  _sourceMAC = other._sourceMAC;
63  _destIP = other._destIP;
64  _destMAC = other._destMAC;
65  }
66  return *this;
67  }
68 
70  {
71  this->debug("[SimpleIpSetup::setConfiguration] Called\n");
72  // Call the base-class version to modify the configuration dictionary
73  bool ret = Configurable::setConfiguration(cfg);
74  // Use the keys provided in the *incoming* dictionary to determine
75  // what needs to be changed.
76  if ( cfg.hasKey("sourceIP") && _config.hasKey("sourceIP") )
77  {
78  ret &= setSourceIP(cfg["sourceIP"]);
79  }
80  if ( cfg.hasKey("destIP") && _config.hasKey("destIP") )
81  {
82  ret &= setDestIPAddress(cfg["destIP"]);
83  }
84  if ( cfg.hasKey("destMAC") && _config.hasKey("destMAC") )
85  {
86  ret &= setDestMACAddress(cfg["destMAC"]);
87  }
88  this->debug("[SimpleIpSetup::setConfiguration] Returning\n");
89  return ret;
90  }
91 
93  {
94  this->debug("[SimpleIpSetup::queryConfiguration] Called\n");
95  // Query source and destination information
96  if ( _config.hasKey("sourceIP") )
97  executeSourceIPQuery(_sourceIP);
98  if ( _config.hasKey("sourceMAC") )
99  executeSourceMACQuery(_sourceMAC);
100  if ( _config.hasKey("destIP") )
101  executeDestIPQuery(_destIP);
102  if ( _config.hasKey("destMAC") )
103  executeDestMACQuery(_destMAC);
105  this->debug("[SimpleIpSetup::queryConfiguration] Returning\n");
106  }
107 
108  std::string SimpleIpSetup::getSourceMAC() const
109  {
110  return _sourceMAC;
111  }
112 
113  std::string SimpleIpSetup::getSourceIP() const
114  {
115  return _sourceIP;
116  }
117 
118  bool SimpleIpSetup::setSourceIP(const std::string& ipAddr)
119  {
120  bool ret = false;
121  if ( _config.hasKey("sourceIP") )
122  {
123  std::string adjSourceIP = ipAddr;
124  ret = executeSourceIPCommand(adjSourceIP);
125  if ( ret )
126  {
127  _sourceIP = adjSourceIP;
129  }
130  }
131  return ret;
132  }
133 
135  {
136  return _destMAC;
137  }
138 
140  {
141  return _destIP;
142  }
143 
144  bool SimpleIpSetup::setDestMACAddress(const std::string& macAddr)
145  {
146  bool ret = false;
147  if ( _config.hasKey("destMAC") )
148  {
149  std::string adjDestMAC = macAddr;
150  ret = executeDestMACCommand(adjDestMAC);
151  if ( ret )
152  {
153  _destMAC = adjDestMAC;
155  }
156  }
157  return ret;
158  }
159 
160  bool SimpleIpSetup::setDestIPAddress(const std::string& ipAddr)
161  {
162  bool ret = false;
163  if ( _config.hasKey("destIP") )
164  {
165  std::string adjDestIP = ipAddr;
166  ret = executeDestIPCommand(adjDestIP);
167  if ( ret )
168  {
169  _destIP = adjDestIP;
171  }
172  }
173  return ret;
174  }
175 
177  {
178  _config.clear();
179  //this->debug("[SimpleIpSetup::initConfigurationDict] Called\n");
180  _config["sourceIP"] = _sourceIP;
181  _config["sourceMAC"] = _sourceMAC;
182  _config["destIP"] = _destIP;
183  _config["destMAC"] = _destMAC;
184  //this->debug("[SimpleIpSetup::initConfigurationDict] Returning\n");
185  }
186 
188  {
189  this->debug("[SimpleIpSetup::updateConfigurationDict] Called\n");
190  if ( _config.hasKey("sourceIP") )
191  setConfigurationValue("sourceIP", _sourceIP);
192  if ( _config.hasKey("sourceMAC") )
193  setConfigurationValue("sourceMAC", _sourceMAC);
194  if ( _config.hasKey("destIP") )
195  setConfigurationValue("destIP", _destIP);
196  if ( _config.hasKey("destMAC") )
197  setConfigurationValue("destMAC", _destMAC);
198  this->debug("[SimpleIpSetup::updateConfigurationDict] Returning\n");
199  }
200 
201  // Default implementation uses the NDR472 pattern
202  bool SimpleIpSetup::executeSourceMACQuery(std::string& macAddr)
203  {
204  bool ret = false;
205  if ( (_parent != NULL) && (_parent->isConnected()) )
206  {
207  std::ostringstream oss;
208  oss << "#MAC?" << "\n";
209  BasicStringList rsp = _parent->sendCommand(oss.str(), 2.0);
210  if ( _parent->getLastCommandErrorInfo() == "" )
211  {
213  Pythonesque::Replace(rsp.front(), "#MAC ", ""),
214  ", ");
215  // vec[0] = Source MAC address
216  macAddr = vec[0];
217  ret = true;
218  }
219  }
220  return ret;
221  }
222 
223  // Default implementation uses the NDR472 pattern
224  bool SimpleIpSetup::executeSourceIPQuery(std::string& sourceIP)
225  {
226  bool ret = false;
227  if ( (_parent != NULL) && (_parent->isConnected()) )
228  {
229  std::ostringstream oss;
230  oss << "SIP?" << "\n";
231  BasicStringList rsp = _parent->sendCommand(oss.str(), 2.0);
232  if ( _parent->getLastCommandErrorInfo() == "" )
233  {
235  Pythonesque::Replace(rsp.front(), "SIP ", ""),
236  ", ");
237  // vec[0] = Source IP address
238  sourceIP = vec[0];
239  ret = true;
240  }
241  }
242  return ret;
243  }
244 
245  // Default implementation uses the NDR472 pattern
246  bool SimpleIpSetup::executeSourceIPCommand(std::string& sourceIP)
247  {
248  bool ret = false;
249  if ( (_parent != NULL) && (_parent->isConnected()) )
250  {
251  std::ostringstream oss;
252  oss << "SIP "
253  << sourceIP
254  << "\n";
255  BasicStringList rsp = _parent->sendCommand(oss.str(), 2.0);
256  if ( _parent->getLastCommandErrorInfo() == "" )
257  {
258  ret = true;
259  }
260  }
261  return ret;
262  }
263 
264  // Default implementation uses the NDR472 pattern
265  bool SimpleIpSetup::executeDestIPQuery(std::string& ipAddr)
266  {
267  bool ret = false;
268  if ( (_parent != NULL) && (_parent->isConnected()) )
269  {
270  std::ostringstream oss;
271  oss << "DIP?" << "\n";
272  BasicStringList rsp = _parent->sendCommand(oss.str(), 2.0);
273  if ( _parent->getLastCommandErrorInfo() == "" )
274  {
276  Pythonesque::Replace(rsp.front(), "DIP ", ""),
277  ", ");
278  // vec[0] = Destination IP address
279  ipAddr = vec[0];
280  ret = true;
281  }
282  }
283  return ret;
284  }
285 
286  // Default implementation uses the NDR472 pattern
287  bool SimpleIpSetup::executeDestIPCommand(std::string& ipAddr)
288  {
289  bool ret = false;
290  if ( (_parent != NULL) && (_parent->isConnected()) )
291  {
292  std::ostringstream oss;
293  oss << "DIP "
294  << ipAddr
295  << "\n";
296  BasicStringList rsp = _parent->sendCommand(oss.str(), 2.0);
297  if ( _parent->getLastCommandErrorInfo() == "" )
298  {
299  ret = true;
300  }
301  }
302  return ret;
303  }
304 
305  // Default implementation uses the NDR472 pattern
306  bool SimpleIpSetup::executeDestMACQuery(std::string& macAddr)
307  {
308  bool ret = false;
309  if ( (_parent != NULL) && (_parent->isConnected()) )
310  {
311  std::ostringstream oss;
312  oss << "TDMAC?" << "\n";
313  BasicStringList rsp = _parent->sendCommand(oss.str(), 2.0);
314  if ( _parent->getLastCommandErrorInfo() == "" )
315  {
317  Pythonesque::Replace(rsp.front(), "TDMAC ", ""),
318  ", ");
319  // vec[0] = Destination MAC address
320  macAddr = vec[0];
321  ret = true;
322  }
323  }
324  return ret;
325  }
326 
327  // Default implementation uses the NDR472 pattern
328  bool SimpleIpSetup::executeDestMACCommand(std::string& macAddr)
329  {
330  bool ret = false;
331  if ( (_parent != NULL) && (_parent->isConnected()) )
332  {
333  std::ostringstream oss;
334  oss << "TDMAC "
335  << macAddr
336  << "\n";
337  BasicStringList rsp = _parent->sendCommand(oss.str(), 2.0);
338  if ( _parent->getLastCommandErrorInfo() == "" )
339  {
340  ret = true;
341  }
342  }
343  return ret;
344  }
345 
346  } /* namespace Driver */
347 
348 } /* namespace LibCyberRadio */
349 
virtual std::string getSourceIP() const
Gets the source IP address.
virtual bool executeDestMACQuery(std::string &macAddr)
Executes the destination MAC query command.
virtual bool setConfiguration(ConfigurationDict &cfg)
Sets the configuration dictionary for this object.
virtual bool setDestMACAddress(const std::string &macAddr)
Sets the destination MAC address.
virtual bool hasKey(const std::string &key) const
Determines if the dictionary has the given key.
virtual SimpleIpSetup & operator=(const SimpleIpSetup &other)
Assignment operator for SimpleIpSetup objects.
virtual void initConfigurationDict()
Initializes the configuration dictionary, defining the allowed keys.
virtual bool isConnected() const
Gets whether or not the handler is connected.
virtual bool setSourceIP(const std::string &ipAddr)
Sets the source IP address.
static std::string Replace(const std::string &str, const std::string &oldstr, const std::string &newstr, int count=INT_MAX)
Replaces occurrences of one substring with another within the given string.
Definition: Pythonesque.cpp:62
virtual bool executeDestIPCommand(std::string &ipAddr)
Executes the destination IP set command.
SimpleIpSetup(const std::string &name="SIMPLEIPSETUP", RadioHandler *parent=NULL, bool debug=false, const std::string &sourceIP="0.0.0.0", const std::string &destIP="0.0.0.0", const std::string &destMAC="00:00:00:00:00:00")
Constructs a SimpleIpSetup object.
static BasicStringList Split(const std::string &str, const std::string &sep, int maxsplit=INT_MAX)
Splits the given string into a list of string tokens.
Definition: Pythonesque.cpp:77
Generic radio handler class.
Definition: RadioHandler.h:54
virtual int debug(const char *format,...)
Outputs debug information.
Definition: Debuggable.cpp:95
BASIC_LIST_CONTAINER< std::string > BasicStringList
Type representing a list of strings.
Definition: BasicList.h:25
virtual std::string getDestIPAddress() const
Gets the destination IP address.
virtual std::string getDestMACAddress() const
Gets the destination MAC address.
virtual std::string getSourceMAC() const
Gets the source MAC address.
Defines functionality for LibCyberRadio applications.
Definition: App.h:23
virtual bool executeDestIPQuery(std::string &ipAddr)
Executes the destination IP query command.
A configuration dictionary.
Definition: Configurable.h:51
virtual bool executeSourceMACQuery(std::string &macAddr)
Executes the source MAC query command.
virtual void updateConfigurationDict()
Updates the configuration dictionary from object settings.
virtual std::string getLastCommandErrorInfo() const
Gets the error message from the last command attempted.
Simple IP setup class for radios without 10GigE data ports.
Definition: SimpleIpSetup.h:46
virtual Configurable & operator=(const Configurable &other)
Assignment operator for Configurable objects.
virtual bool executeSourceIPCommand(std::string &ipAddr)
Executes the source IP set command.
virtual bool setConfiguration(ConfigurationDict &cfg)
Sets the configuration dictionary for this object.
virtual bool executeDestMACCommand(std::string &macAddr)
Executes the destination MAC set command.
virtual BasicStringList sendCommand(const std::string &cmdString, double timeout=-1)
Sends a command to the radio.
virtual void queryConfiguration()
Tells the object to create its configuration dictionary.
Base configurable object class.
Definition: Configurable.h:79
virtual bool setConfigurationValue(const std::string &key, const std::string &value)
Sets a named configuration value to a string.
virtual bool setDestIPAddress(const std::string &ipAddr)
Sets the destination IP address.
virtual ~SimpleIpSetup()
Destroys a SimpleIpSetup object.
virtual bool executeSourceIPQuery(std::string &ipAddr)
Executes the source IP query command.