DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
snbmodules
include
snbmodules
ip_format.hpp
Go to the documentation of this file.
1
8
9
#ifndef SNBMODULES_INCLUDE_SNBMODULES_IP_FORMAT_HPP_
10
#define SNBMODULES_INCLUDE_SNBMODULES_IP_FORMAT_HPP_
11
12
// #define _CRT_SECURE_NO_WARNINGS
13
14
#include <cstring>
15
#include <filesystem>
16
#include <iostream>
17
#include <stdexcept>
18
#include <string>
19
#include <utility>
20
#include <vector>
21
22
namespace
dunedaq::snbmodules
{
25
class
IPFormat
26
{
27
28
public
:
29
// Constructors
33
explicit
IPFormat
(
const
std::string& ip =
"0.0.0.0"
,
int
port = 0)
34
{
35
set_port
(port);
36
set_ip
(ip);
37
}
38
40
IPFormat
(
const
IPFormat
& o)
41
{
42
set_port
(o.
m_port
);
43
set_ip
(o.
m_ip
);
44
}
45
47
IPFormat
&
operator=
(
IPFormat
const
&) =
default
;
48
50
bool
operator==
(
IPFormat
const
& o)
const
{
return
m_ip
== o.
m_ip
&&
m_port
== o.
m_port
; }
51
53
bool
operator<
(
IPFormat
const
& o)
const
{
return
m_ip
.compare(o.
m_ip
) || (
m_ip
== o.
m_ip
&&
m_port
< o.
m_port
); }
54
57
std::string
get_ip_port
()
const
{
return
m_ip
+
":"
+ std::to_string(
m_port
); }
58
61
inline
bool
is_default
()
const
{
return
m_ip
==
"0.0.0.0"
&&
m_port
== 0; }
62
63
// Setters
64
void
set_port
(
int
port)
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
}
73
74
#ifndef _MSC_VER
75
inline
char
*
strtok_s
(
char
* s,
const
char
* delim,
char
** context) {
return
strtok_r(s, delim, context); }
76
#endif
// !_MSC_VER
77
80
void
set_ip
(
const
std::string& ip)
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
}
114
115
// Getters
116
std::string
get_ip
()
const
{
return
m_ip
; }
117
int
get_port
()
const
{
return
m_port
; }
118
119
private
:
121
std::string
m_ip
=
"0.0.0.0"
;
123
int
m_port
= 0;
124
};
125
}
// namespace dunedaq::snbmodules
126
#endif
// SNBMODULES_INCLUDE_SNBMODULES_IP_FORMAT_HPP_
dunedaq::snbmodules::IPFormat::set_port
void set_port(int port)
Definition
ip_format.hpp:64
dunedaq::snbmodules::IPFormat::IPFormat
IPFormat(const IPFormat &o)
Copy constructor.
Definition
ip_format.hpp:40
dunedaq::snbmodules::IPFormat::get_ip_port
std::string get_ip_port() const
Get the IP address and the port in the format "ip:port".
Definition
ip_format.hpp:57
dunedaq::snbmodules::IPFormat::get_port
int get_port() const
Definition
ip_format.hpp:117
dunedaq::snbmodules::IPFormat::m_port
int m_port
Port.
Definition
ip_format.hpp:123
dunedaq::snbmodules::IPFormat::operator<
bool operator<(IPFormat const &o) const
< operator
Definition
ip_format.hpp:53
dunedaq::snbmodules::IPFormat::strtok_s
char * strtok_s(char *s, const char *delim, char **context)
Definition
ip_format.hpp:75
dunedaq::snbmodules::IPFormat::get_ip
std::string get_ip() const
Definition
ip_format.hpp:116
dunedaq::snbmodules::IPFormat::is_default
bool is_default() const
Check if the IP address and the port are set to the default values.
Definition
ip_format.hpp:61
dunedaq::snbmodules::IPFormat::IPFormat
IPFormat(const std::string &ip="0.0.0.0", int port=0)
Constructor that set the IP address and the port.
Definition
ip_format.hpp:33
dunedaq::snbmodules::IPFormat::operator==
bool operator==(IPFormat const &o) const
== operator
Definition
ip_format.hpp:50
dunedaq::snbmodules::IPFormat::set_ip
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
dunedaq::snbmodules::IPFormat::operator=
IPFormat & operator=(IPFormat const &)=default
Defualt = operator.
dunedaq::snbmodules::IPFormat::m_ip
std::string m_ip
IP address.
Definition
ip_format.hpp:121
dunedaq::snbmodules
Definition
bookkeeper.hpp:30
Generated on
for DUNE-DAQ by
1.17.0