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