DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
daphnemodules
src
DaphneV3Interface.cpp
Go to the documentation of this file.
1
11
12
#include "
DaphneV3Interface.hpp
"
13
14
#include "
logging/Logging.hpp
"
15
#include <fmt/format.h>
16
17
#include <regex>
18
#include <string>
19
#include <utility>
20
21
using namespace
dunedaq::daphnemodules
;
22
using namespace
daphne
;
23
24
DaphneV3Interface::DaphneV3Interface
(std::string
address
, std::string routing, std::chrono::milliseconds timeout)
25
:
m_context
(1)
26
,
m_socket
(
m_context
,
zmq
::socket_type::dealer)
27
,
m_timeout
(timeout)
28
{
29
30
m_socket
.set(zmq::sockopt::routing_id, routing);
31
auto
value = (int)
m_timeout
.count();
// NOLINT
32
TLOG
() << routing <<
" timeout set to "
<< value <<
" ms"
;
33
m_socket
.set(zmq::sockopt::rcvtimeo, value);
34
m_socket
.set(zmq::sockopt::sndtimeo, value);
35
m_socket
.set(zmq::sockopt::immediate, 1);
// Don't queue messages to incomplete connections
36
37
// find out if the address has a port with a regex
38
static
const
std::regex ip_with_port(R
"(^([^\/\s:]+)(?::(\d{1,5}))?$)");
39
std::smatch string_values;
40
if
(!std::regex_match(
address
, string_values, ip_with_port)) {
41
throw
InvalidIPAddress
(
ERS_HERE
,
address
);
42
}
43
44
m_connection
= string_values[2].matched ? fmt::format(
"tcp://{}"
,
address
)
45
: fmt::format(
"tcp://{}:{}"
,
address
,
s_default_control_port
);
46
TLOG
() <<
"Connecting to: "
<<
m_connection
;
47
48
m_socket
.connect(
m_connection
);
49
50
auto
add = string_values[1];
51
auto
port = string_values[2].matched ? std::stoi(string_values[2]) :
s_default_control_port
;
52
53
try
{
54
if
(!
validate_connection
()) {
55
auto
add = string_values[1];
56
auto
port = string_values[2].matched ? std::stoi(string_values[2]) :
s_default_control_port
;
57
throw
FailedPing(
ERS_HERE
, add, port);
58
}
59
}
catch
(
const
ers::Issue
& e) {
60
throw
FailedPing(
ERS_HERE
, add, port, e);
61
}
62
}
63
64
void
65
DaphneV3Interface::close
()
66
{
67
68
const
std::lock_guard<std::mutex> lock(
m_access_mutex
);
69
m_socket
.set(zmq::sockopt::linger, 0);
70
m_socket
.close();
71
}
72
73
ControlEnvelopeV2
74
DaphneV3Interface::send
(std::string&& message,
daphne::MessageTypeV2
type)
75
{
76
77
const
std::lock_guard<std::mutex> lock(
m_access_mutex
);
78
79
_send
(std::move(message), type);
80
81
return
_receive
();
82
}
83
84
void
85
DaphneV3Interface::_send
(std::string&& message,
daphne::MessageTypeV2
type)
86
{
87
88
ControlEnvelopeV2
env
;
89
env
.set_version(2);
90
env
.set_dir(
DIR_REQUEST
);
91
92
env
.set_type(type);
93
env
.set_payload(message);
94
95
// additional information
96
env
.set_msg_id(
m_message_counter
++);
97
env
.set_timestamp_ns(
98
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count());
99
100
std::string bytes =
env
.SerializeAsString();
101
102
if
(!
m_socket
.send(zmq::buffer(bytes), zmq::send_flags::none)) {
103
throw
FailedSend
(
ERS_HERE
,
MessageTypeV2_Name
(type));
104
}
105
}
106
107
ControlEnvelopeV2
108
DaphneV3Interface::_receive
()
109
{
110
111
zmq::message_t reply;
112
if
(!
m_socket
.recv(reply, zmq::recv_flags::none)) {
113
// timeout or EAGAIN
114
throw
FailedReceive(
ERS_HERE
,
m_connection
);
115
}
116
if
(reply.size() <= 0) {
117
throw
EmptyPayload(
ERS_HERE
,
m_connection
);
118
}
119
120
ControlEnvelopeV2
rep;
121
if
(!rep.ParseFromArray(reply.data(),
static_cast<
int
>
(reply.size()))) {
122
throw
FailedDecoding(
ERS_HERE
, rep.GetTypeName(), reply.to_string());
123
}
124
125
if
(rep.
version
() != 2 || rep.
dir
() !=
DIR_RESPONSE
) {
126
ers::warning
(UnexpectedDirection(
ERS_HERE
, rep.
version
(),
Direction_Name
(rep.
dir
()), reply.to_string()));
127
}
128
129
return
rep;
130
}
131
132
bool
133
DaphneV3Interface::validate_connection
()
134
{
135
static
const
uint64_t good_value = 0xdeadbeef;
// NOLINT
136
137
TestRegRequest
req;
// empty
138
auto
reply =
send<TestRegResponse>
(
139
req.SerializeAsString(),
MessageTypeV2::MT2_READ_TEST_REG_REQ
,
MessageTypeV2::MT2_READ_TEST_REG_RESP
);
140
141
return
reply.value() == good_value;
142
}
DaphneV3Interface.hpp
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:127
daphne::ControlEnvelopeV2
Definition
daphne_control_high.pb.h:5899
daphne::ControlEnvelopeV2::version
::uint32_t version() const
Definition
daphne_control_high.pb.h:9817
daphne::ControlEnvelopeV2::dir
::daphne::Direction dir() const
Definition
daphne_control_high.pb.h:9839
daphne::TestRegRequest
Definition
daphne_control_high.pb.h:5086
dunedaq::daphnemodules::DaphneV3Interface::m_socket
zmq::socket_t m_socket
Definition
DaphneV3Interface.hpp:102
dunedaq::daphnemodules::DaphneV3Interface::close
void close()
Definition
DaphneV3Interface.cpp:65
dunedaq::daphnemodules::DaphneV3Interface::s_default_control_port
static const size_t s_default_control_port
Definition
DaphneV3Interface.hpp:111
dunedaq::daphnemodules::DaphneV3Interface::m_access_mutex
std::mutex m_access_mutex
Definition
DaphneV3Interface.hpp:103
dunedaq::daphnemodules::DaphneV3Interface::DaphneV3Interface
DaphneV3Interface(std::string address, std::string routing, std::chrono::milliseconds timeout=std::chrono::milliseconds(500))
Definition
DaphneV3Interface.cpp:24
dunedaq::daphnemodules::DaphneV3Interface::send
daphne::ControlEnvelopeV2 send(std::string &&message, daphne::MessageTypeV2)
Definition
DaphneV3Interface.cpp:74
dunedaq::daphnemodules::DaphneV3Interface::m_context
zmq::context_t m_context
Definition
DaphneV3Interface.hpp:100
dunedaq::daphnemodules::DaphneV3Interface::m_timeout
std::chrono::milliseconds m_timeout
Definition
DaphneV3Interface.hpp:107
dunedaq::daphnemodules::DaphneV3Interface::_send
void _send(std::string &&message, daphne::MessageTypeV2)
Definition
DaphneV3Interface.cpp:85
dunedaq::daphnemodules::DaphneV3Interface::validate_connection
bool validate_connection()
Definition
DaphneV3Interface.cpp:133
dunedaq::daphnemodules::DaphneV3Interface::m_message_counter
uint64_t m_message_counter
Definition
DaphneV3Interface.hpp:109
dunedaq::daphnemodules::DaphneV3Interface::m_connection
std::string m_connection
Definition
DaphneV3Interface.hpp:105
dunedaq::daphnemodules::DaphneV3Interface::_receive
daphne::ControlEnvelopeV2 _receive()
Definition
DaphneV3Interface.cpp:108
ers::Issue
Base class for any user define issue.
Definition
Issue.hpp:76
Logging.hpp
TLOG
#define TLOG(...)
Definition
macro.hpp:21
daphne::zmq
Definition
ControlClient.cpp:4
daphne
Definition
ControlClient.cpp:4
daphne::MessageTypeV2
MessageTypeV2
Definition
daphne_control_high.pb.h:248
daphne::MT2_READ_TEST_REG_RESP
@ MT2_READ_TEST_REG_RESP
Definition
daphne_control_high.pb.h:317
daphne::MT2_READ_TEST_REG_REQ
@ MT2_READ_TEST_REG_REQ
Definition
daphne_control_high.pb.h:316
daphne::MessageTypeV2_Name
const std::string & MessageTypeV2_Name(T value)
Definition
daphne_control_high.pb.h:335
daphne::Direction_Name
const std::string & Direction_Name(T value)
Definition
daphne_control_high.pb.h:232
daphne::DIR_REQUEST
@ DIR_REQUEST
Definition
daphne_control_high.pb.h:216
daphne::DIR_RESPONSE
@ DIR_RESPONSE
Definition
daphne_control_high.pb.h:217
dunedaq::daphnemodules
Definition
DaphneV2Interface.hpp:53
dunedaq.env
Definition
env.py:1
dunedaq::FailedSend
FailedSend
Definition
DaphneV3Interface.hpp:37
dunedaq::InvalidIPAddress
InvalidIPAddress
Definition
CommonIssues.hpp:26
dunedaq::address
Invalid address
Definition
CommonIssues.hpp:26
ers::warning
void warning(const Issue &issue)
Definition
ers.hpp:150
Generated on
for DUNE-DAQ by
1.18.0