DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
restcmd
src
RestEndpoint.cpp
Go to the documentation of this file.
1
8
#include "
restcmd/RestEndpoint.hpp
"
9
10
#include "
logging/Logging.hpp
"
11
12
#include <chrono>
13
#include <future>
14
#include <sstream>
15
#include <string>
16
#include <utility>
17
18
using namespace
dunedaq::restcmd
;
19
using namespace
Pistache;
20
21
void
22
RestEndpoint::init
(
size_t
threads)
23
{
24
auto
opts = Http::Endpoint::options()
25
.threads(
static_cast<
int
>
(threads))
26
.maxRequestSize(15728640)
// 15MB
27
.maxResponseSize(1048576)
// 1MB
28
.flags(Pistache::Tcp::Options::ReuseAddr)
29
.flags(Pistache::Tcp::Options::ReusePort);
30
31
http_endpoint_
->init(opts);
32
createRouting
();
33
http_client_options_
= Http::Client::options().threads(
static_cast<
int
>
(threads));
34
http_client_
->init(
http_client_options_
);
35
}
36
37
void
38
RestEndpoint::start
()
39
{
40
http_endpoint_
->setHandler(
router_
.handler());
41
http_endpoint_
->serveThreaded();
42
port_
=
http_endpoint_
->getPort();
43
TLOG
() <<
"REST server started on port "
<<
port_
;
44
}
45
46
// void RestEndpoint::serveTask()
47
// {
48
// }
49
50
void
51
RestEndpoint::shutdown
()
52
{
53
http_endpoint_
->shutdown();
54
// server_thread_.join();
55
http_client_
->shutdown();
56
}
57
58
void
59
RestEndpoint::createRouting
()
60
{
61
using namespace
Rest;
62
Routes::Post(
router_
,
"/command"
, Routes::bind(&
RestEndpoint::handle_route_command
,
this
));
63
}
64
65
inline
void
66
extendHeader
(Http::Header::Collection& headers)
67
{
68
headers.add<Http::Header::AccessControlAllowOrigin>(
"*"
);
69
headers.add<Http::Header::AccessControlAllowMethods>(
"POST,GET"
);
70
headers.add<Http::Header::ContentType>(MIME(Text, Plain));
71
}
72
73
inline
std::string
74
getClientAddress
(
const
Pistache::Rest::Request& request)
75
{
76
const
auto
xff = request.headers().tryGetRaw(
"X-Forwarded-For"
);
77
if
(!xff.isEmpty()) {
78
// TODO: Strip off after first comma (to handle chained proxies). // NOLINT
79
return
xff.get().value();
80
}
81
return
request.address().host();
82
}
83
84
void
85
RestEndpoint::handle_route_command
(
const
Rest::Request& request, Http::ResponseWriter response)
86
{
87
dunedaq::cmdlib::cmd::CommandReply
meta;
88
auto
addr = request.address();
89
auto
headers = request.headers();
90
auto
ct = headers.get<Http::Header::ContentType>();
91
if
(ct->mime() !=
accepted_mime_
) {
92
auto
res = response.send(Http::Code::Not_Acceptable,
"Not a JSON command!\n"
);
93
}
else
{
94
auto
ansport = headers.getRaw(
"X-Answer-Port"
);
// RS: FIXME reply using headers
95
auto
anshost = headers.tryGetRaw(
"X-Answer-Host"
);
// RS: FIXME reply using headers
96
meta.
data
[
"ans-port"
] = ansport.value();
97
meta.
data
[
"ans-host"
] = (!anshost.isEmpty() ? anshost.get().value() : addr.host());
98
command_callback_
(nlohmann::json::parse(request.body()), meta);
// RS: FIXME parse errors
99
auto
res = response.send(Http::Code::Accepted,
"Command received\n"
);
100
}
101
}
102
103
void
104
RestEndpoint::handleResponseCommand
(
const
cmdobj_t
& cmd,
dunedaq::cmdlib::cmd::CommandReply
& meta)
105
{
106
dunedaq::cmdlib::cmd::Command
command = cmd.get<
dunedaq::cmdlib::cmd::Command
>();
107
std::ostringstream addrstr;
108
addrstr << meta.
data
[
"ans-host"
].get<std::string>() <<
":"
<< meta.
data
[
"ans-port"
].get<std::string>() <<
"/response"
;
109
meta.
data
[
"cmdid"
] = command.
id
;
110
TLOG
() <<
"Sending POST request to "
<< addrstr.str();
111
112
nlohmann::json body_json;
113
dunedaq::cmdlib::cmd::to_json
(body_json, meta);
114
auto
response =
http_client_
->post(addrstr.str()).body(body_json.dump()).send();
115
response.then([&](Http::Response response) {
TLOG
() <<
"Response code = "
<< response.code(); },
116
[&](std::exception_ptr exc) {
117
// handle response failure
118
try
{
119
std::rethrow_exception(exc);
120
}
catch
(
const
std::exception& e) {
121
TLOG
() <<
"Exception thrown by Http::Client::post() call: \""
<< e.what()
122
<<
"\"; errno = "
<< errno;
123
}
124
});
125
http_client_responses_
.push_back(std::move(response));
126
}
getClientAddress
std::string getClientAddress(const Pistache::Rest::Request &request)
Definition
RestEndpoint.cpp:74
extendHeader
void extendHeader(Http::Header::Collection &headers)
Definition
RestEndpoint.cpp:66
RestEndpoint.hpp
dunedaq::restcmd::RestEndpoint::init
void init(size_t threads)
Definition
RestEndpoint.cpp:22
dunedaq::restcmd::RestEndpoint::port_
Pistache::Port port_
Definition
RestEndpoint.hpp:74
dunedaq::restcmd::RestEndpoint::start
void start()
Definition
RestEndpoint.cpp:38
dunedaq::restcmd::RestEndpoint::createRouting
void createRouting()
Definition
RestEndpoint.cpp:59
dunedaq::restcmd::RestEndpoint::router_
Pistache::Rest::Router router_
Definition
RestEndpoint.hpp:78
dunedaq::restcmd::RestEndpoint::http_client_
std::shared_ptr< Pistache::Http::Client > http_client_
Definition
RestEndpoint.hpp:82
dunedaq::restcmd::RestEndpoint::handle_route_command
void handle_route_command(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response)
Definition
RestEndpoint.cpp:85
dunedaq::restcmd::RestEndpoint::http_endpoint_
std::shared_ptr< Pistache::Http::Endpoint > http_endpoint_
Definition
RestEndpoint.hpp:76
dunedaq::restcmd::RestEndpoint::http_client_options_
Pistache::Http::Client::Options http_client_options_
Definition
RestEndpoint.hpp:83
dunedaq::restcmd::RestEndpoint::accepted_mime_
Pistache::Http::Mime::MediaType accepted_mime_
Definition
RestEndpoint.hpp:79
dunedaq::restcmd::RestEndpoint::command_callback_
std::function< void(const cmdobj_t &, cmdlib::cmd::CommandReply)> command_callback_
Definition
RestEndpoint.hpp:87
dunedaq::restcmd::RestEndpoint::handleResponseCommand
void handleResponseCommand(const cmdobj_t &cmd, cmdlib::cmd::CommandReply &meta)
Definition
RestEndpoint.cpp:104
dunedaq::restcmd::RestEndpoint::http_client_responses_
std::vector< Pistache::Async::Promise< Pistache::Http::Response > > http_client_responses_
Definition
RestEndpoint.hpp:84
dunedaq::restcmd::RestEndpoint::shutdown
void shutdown()
Definition
RestEndpoint.cpp:51
Logging.hpp
TLOG
#define TLOG(...)
Definition
macro.hpp:21
dunedaq::cmdlib::cmd::to_json
void to_json(data_t &j, const Command &obj)
Definition
Nljs.hpp:20
dunedaq::restcmd
Definition
RestEndpoint.hpp:35
dunedaq::restcmd::cmdobj_t
nlohmann::json cmdobj_t
Definition
RestEndpoint.hpp:37
dunedaq::cmdlib::cmd::CommandReply
Definition
Structs.hpp:45
dunedaq::cmdlib::cmd::CommandReply::data
Data data
Definition
Structs.hpp:57
dunedaq::cmdlib::cmd::Command
Definition
Structs.hpp:28
dunedaq::cmdlib::cmd::Command::id
CmdId id
Definition
Structs.hpp:31
Generated on
for DUNE-DAQ by
1.18.0