DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::snbmodules::IPFormat Class Reference

Class that represents an IP address and a port TODO: should be replaced by something better ? More...

#include <ip_format.hpp>

Public Member Functions

 IPFormat (const std::string &ip="0.0.0.0", int port=0)
 Constructor that set the IP address and the port.
 
 IPFormat (const IPFormat &o)
 Copy constructor.
 
IPFormatoperator= (IPFormat const &)=default
 Defualt = operator.
 
bool operator== (IPFormat const &o) const
 == operator
 
bool operator< (IPFormat const &o) const
 < operator
 
std::string get_ip_port () const
 Get the IP address and the port in the format "ip:port".
 
bool is_default () const
 Check if the IP address and the port are set to the default values.
 
void set_port (int port)
 
char * strtok_s (char *s, const char *delim, char **context)
 
void set_ip (const std::string &ip)
 Set the IP address, must be called after set_port() if the port is specified in the IP address ex 0.0.0.0:1234.
 
std::string get_ip () const
 
int get_port () const
 

Private Attributes

std::string m_ip = "0.0.0.0"
 IP address.
 
int m_port = 0
 Port.
 

Detailed Description

Class that represents an IP address and a port TODO: should be replaced by something better ?

Definition at line 25 of file ip_format.hpp.

Constructor & Destructor Documentation

◆ IPFormat() [1/2]

dunedaq::snbmodules::IPFormat::IPFormat ( const std::string & ip = "0.0.0.0",
int port = 0 )
inlineexplicit

Constructor that set the IP address and the port.

Parameters
ipIP address
portPort

Definition at line 33 of file ip_format.hpp.

34 {
35 set_port(port);
36 set_ip(ip);
37 }
void set_ip(const std::string &ip)
Set the IP address, must be called after set_port() if the port is specified in the IP address ex 0....
Definition ip_format.hpp:80

◆ IPFormat() [2/2]

dunedaq::snbmodules::IPFormat::IPFormat ( const IPFormat & o)
inline

Copy constructor.

Definition at line 40 of file ip_format.hpp.

41 {
42 set_port(o.m_port);
43 set_ip(o.m_ip);
44 }

Member Function Documentation

◆ get_ip()

std::string dunedaq::snbmodules::IPFormat::get_ip ( ) const
inline

Definition at line 116 of file ip_format.hpp.

116{ return m_ip; }
std::string m_ip
IP address.

◆ get_ip_port()

std::string dunedaq::snbmodules::IPFormat::get_ip_port ( ) const
inline

Get the IP address and the port in the format "ip:port".

Returns
The IP address and the port in the format "ip:port"

Definition at line 57 of file ip_format.hpp.

57{ return m_ip + ":" + std::to_string(m_port); }

◆ get_port()

int dunedaq::snbmodules::IPFormat::get_port ( ) const
inline

Definition at line 117 of file ip_format.hpp.

117{ return m_port; }

◆ is_default()

bool dunedaq::snbmodules::IPFormat::is_default ( ) const
inline

Check if the IP address and the port are set to the default values.

Returns
True if the IP address and the port are set to the default values, false otherwise

Definition at line 61 of file ip_format.hpp.

61{ return m_ip == "0.0.0.0" && m_port == 0; }

◆ operator<()

bool dunedaq::snbmodules::IPFormat::operator< ( IPFormat const & o) const
inline

< operator

Definition at line 53 of file ip_format.hpp.

53{ return m_ip.compare(o.m_ip) || (m_ip == o.m_ip && m_port < o.m_port); }

◆ operator=()

IPFormat & dunedaq::snbmodules::IPFormat::operator= ( IPFormat const & )
default

Defualt = operator.

◆ operator==()

bool dunedaq::snbmodules::IPFormat::operator== ( IPFormat const & o) const
inline

== operator

Definition at line 50 of file ip_format.hpp.

50{ return m_ip == o.m_ip && m_port == o.m_port; }

◆ set_ip()

void dunedaq::snbmodules::IPFormat::set_ip ( const std::string & ip)
inline

Set the IP address, must be called after set_port() if the port is specified in the IP address ex 0.0.0.0:1234.

Definition at line 80 of file ip_format.hpp.

81 {
82 if (ip.empty()) {
83 throw std::invalid_argument("IP address cannot be empty");
84 }
85
86 // Splitting the string IP:PORT or IP
87 std::vector<std::string> ip_port_pair;
88 char* next_token = nullptr;
89
90 char* ip_char = new char[ip.length() + 1];
91 strcpy(ip_char, ip.c_str());
92
93 char* token = strtok_s(ip_char, ":", &next_token);
94 while (token != nullptr) {
95 ip_port_pair.emplace_back(std::string(token));
96 token = strtok_s(nullptr, ":", &next_token);
97 }
98
99 delete[] ip_char;
100
101 if (ip_port_pair.size() != 1 && ip_port_pair.size() != 2) {
102 throw std::invalid_argument("Invalid IP address format (IP:PORT or IP)");
103 }
104
105 // TODO Aug-14-2022 Leo Joly leo.vincent.andre.joly@cern.ch : Check if the IPv4 address is valid
106
107 // set values
108 if (ip_port_pair.size() == 2) {
109 set_port(std::stoi(ip_port_pair[1]));
110 }
111
112 m_ip = ip_port_pair[0];
113 }
char * strtok_s(char *s, const char *delim, char **context)
Definition ip_format.hpp:75

◆ set_port()

void dunedaq::snbmodules::IPFormat::set_port ( int port)
inline

Definition at line 64 of file ip_format.hpp.

65 {
66
67 if (port < 0 || port > 65535) {
68 throw std::invalid_argument("Port must be between 0 and 65535");
69 }
70
71 m_port = port;
72 }

◆ strtok_s()

char * dunedaq::snbmodules::IPFormat::strtok_s ( char * s,
const char * delim,
char ** context )
inline

Definition at line 75 of file ip_format.hpp.

75{ return strtok_r(s, delim, context); }

Member Data Documentation

◆ m_ip

std::string dunedaq::snbmodules::IPFormat::m_ip = "0.0.0.0"
private

IP address.

Definition at line 121 of file ip_format.hpp.

◆ m_port

int dunedaq::snbmodules::IPFormat::m_port = 0
private

Port.

Definition at line 123 of file ip_format.hpp.


The documentation for this class was generated from the following file: