DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
SchemaUtils.hpp
Go to the documentation of this file.
1
11#ifndef IOMANAGER_INCLUDE_IOMANAGER_SCHEMAUTILS_HPP_
12#define IOMANAGER_INCLUDE_IOMANAGER_SCHEMAUTILS_HPP_
13
16#include "confmodel/Service.hpp"
17
18#include <cerrno> // NOLINT(runtime/output_format)
19#include <functional>
20#include <ifaddrs.h>
21#include <netdb.h>
22#include <regex>
23#include <sstream>
24#include <string>
25
26namespace dunedaq::iomanager {
27
29{
30 std::string uid{ "" };
31 std::string data_type{ "" };
32 std::string tag{ "" };
33 std::string session{ "" };
34
36
37 ConnectionId(std::string uid, std::string data_type, std::string tag = "", std::string session = "")
38 : uid(uid)
40 , tag(tag)
42 {
43 }
44
46 : uid(cfg->UID())
47 , data_type(cfg->get_data_type())
48 {
49 }
50};
51
52inline bool
53operator<(ConnectionId const& l, ConnectionId const& r)
54{
55 if (l.session == r.session || l.session == "" || r.session == "") {
56 if (l.data_type == r.data_type) {
57 if (l.uid == r.uid) {
58 return l.tag < r.tag;
59 }
60 return l.uid < r.uid;
61 }
62 return l.data_type < r.data_type;
63 }
64 return l.session < r.session;
65}
66inline bool
68{
69 return (l.session == "" || r.session == "" || l.session == r.session) && l.uid == r.uid && l.tag == r.tag &&
70 l.data_type == r.data_type;
71}
72
73inline bool
75{
76 if (search.data_type != check.data_type)
77 return false;
78
79 if (search.session != check.session && search.session != "" && check.session != "")
80 return false;
81
82 std::regex search_ex(search.uid);
83 return std::regex_match(check.uid, search_ex);
84}
85
86inline std::string
87to_string(const ConnectionId& conn_id)
88{
89 if (conn_id.session != "") {
90 return conn_id.session + "/" + conn_id.uid + (conn_id.tag != "" ? "+" + conn_id.tag : "") + "@@" +
91 conn_id.data_type;
92 }
93 return conn_id.uid + (conn_id.tag != "" ? "+" + conn_id.tag : "") + "@@" + conn_id.data_type;
94}
95
96inline std::string
97get_host_ip(std::string eth_device_name)
98{
99 std::string ipaddr = "0.0.0.0";
100 char hostname[NI_MAXHOST]; // NOLINT This is a char array for interfacing with the C networking API
101 if (gethostname(&hostname[0], NI_MAXHOST) == 0) {
102 ipaddr = std::string(hostname);
103 }
104 if (eth_device_name != "") {
105 // Work out which ip address goes with this device
106 struct ifaddrs* ifaddr = nullptr;
107 getifaddrs(&ifaddr);
108
109 for (auto ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
110 if (ifa->ifa_addr == nullptr || std::string(ifa->ifa_name) != eth_device_name) {
111 continue;
112 }
113
114 char ip[NI_MAXHOST]; // NOLINT This is a char array for interfacing with the C networking API
115 int status = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), ip, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
116 if (status != 0) {
117 continue;
118 }
119 ipaddr = std::string(ip);
120 break;
121 }
122 freeifaddrs(ifaddr);
123 }
124 return ipaddr;
125}
126
127inline std::string
129{
130 std::string uri = "";
131 if (netCon) {
132 TLOG_DEBUG(45) << "Getting URI for network connection " << netCon->UID();
133 auto service = netCon->get_associated_service();
134 auto protocol = service->get_protocol();
135 if (protocol == "tcp") {
136
137 std::string port = "*";
138 if (service->get_port() && service->get_port() != 0) {
139 port = std::to_string(service->get_port());
140 }
141 auto iface = service->get_eth_device_name();
142 uri = std::string(service->get_protocol() + "://" + get_host_ip(iface) + ":" + port);
143 } else if (protocol == "inproc") {
144 uri = std::string(service->get_protocol() + "://" + service->get_path());
145 }
146 }
147 return uri;
148}
149
150} // namespace dunedaq::iomanager
151
152namespace std {
153
154template<>
155struct hash<dunedaq::iomanager::ConnectionId>
156{
157 std::size_t operator()(const dunedaq::iomanager::ConnectionId& conn_id) const
158 {
159 return std::hash<std::string>()(conn_id.session + conn_id.uid + conn_id.tag + conn_id.data_type);
160 }
161};
162
163} // namespace std
164
165#endif // IOMANAGER_INCLUDE_IOMANAGER_SCHEMAUTILS_HPP_
static void check(std::vector< conffwk::ConfigurationChange * > &changes, const InheritanceData &inheritance, const std::set< std::string > &class_names, const OksConfiguration::SMap &objects, const std::string &obj_class, const std::string &obj_id, const char action)
const std::string & UID() const noexcept
const dunedaq::confmodel::Service * get_associated_service() const
Get "associated_service" relationship value. Service provided by this connection.
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
bool is_match(ConnectionId const &search, ConnectionId const &check)
std::string to_string(const ConnectionId &conn_id)
std::string get_host_ip(std::string eth_device_name)
bool operator<(ConnectionRegistration const &l, ConnectionRegistration const &r)
std::string get_uri_for_connection(const confmodel::NetworkConnection *netCon)
bool operator==(ConnectionId const &l, ConnectionId const &r)
The DUNE-DAQ namespace.
Definition DataStore.hpp:57
Unsupported std::string uri Execution of command std::string error Failed to create CommandFacility uri
Definition Issues.hpp:77
ConnectionId(std::string uid, std::string data_type, std::string tag="", std::string session="")
ConnectionId(const confmodel::Connection *cfg)
std::size_t operator()(const dunedaq::iomanager::ConnectionId &conn_id) const