DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
dunedaq::restcmd::RestEndpoint Class Reference

#include <RestEndpoint.hpp>

Public Member Functions

 RestEndpoint (const std::string &, int port, std::function< void(const cmdobj_t &, cmdlib::cmd::CommandReply)> callback) noexcept
 
void init (size_t threads)
 
void start ()
 
void stop ()
 
void shutdown ()
 
void handleResponseCommand (const cmdobj_t &cmd, cmdlib::cmd::CommandReply &meta)
 
uint16_t getPort () const
 
std::shared_ptr< Pistache::Http::Client > getHttpClient () const
 

Private Member Functions

void createRouting ()
 
void createDescription ()
 
void handle_route_command (const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response)
 

Private Attributes

Pistache::Port port_
 
Pistache::Address address_
 
std::shared_ptr< Pistache::Http::Endpoint > http_endpoint_
 
Pistache::Rest::Description description_
 
Pistache::Rest::Router router_
 
Pistache::Http::Mime::MediaType accepted_mime_
 
std::shared_ptr< Pistache::Http::Client > http_client_
 
Pistache::Http::Client::Options http_client_options_
 
std::vector< Pistache::Async::Promise< Pistache::Http::Response > > http_client_responses_
 
std::function< void(const cmdobj_t &, cmdlib::cmd::CommandReply)> command_callback_
 
std::thread server_thread_
 

Detailed Description

Definition at line 39 of file RestEndpoint.hpp.

Constructor & Destructor Documentation

◆ RestEndpoint()

dunedaq::restcmd::RestEndpoint::RestEndpoint ( const std::string & ,
int port,
std::function< void(const cmdobj_t &, cmdlib::cmd::CommandReply)> callback )
inlineexplicitnoexcept

Definition at line 41 of file RestEndpoint.hpp.

43 : port_{ static_cast<uint16_t>(port) }
44 , address_{ Pistache::Ipv4::any(), port_ }
45 , http_endpoint_{ std::make_shared<Pistache::Http::Endpoint>( address_ ) }
46 , description_{ "DUNE DAQ cmdlib API", "0.1" }
47 , accepted_mime_{ MIME(Application, Json) }
48 , http_client_{ std::make_shared<Pistache::Http::Client>() }
49 , command_callback_{ callback }
50 { }
Pistache::Rest::Description description_
std::shared_ptr< Pistache::Http::Client > http_client_
std::shared_ptr< Pistache::Http::Endpoint > http_endpoint_
Pistache::Http::Mime::MediaType accepted_mime_
std::function< void(const cmdobj_t &, cmdlib::cmd::CommandReply)> command_callback_

Member Function Documentation

◆ createDescription()

void dunedaq::restcmd::RestEndpoint::createDescription ( )
private

◆ createRouting()

void RestEndpoint::createRouting ( )
private

Definition at line 54 of file RestEndpoint.cpp.

55{
56 using namespace Rest;
57 Routes::Post(router_, "/command", Routes::bind(&RestEndpoint::handle_route_command, this));
58}
Pistache::Rest::Router router_
void handle_route_command(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response)

◆ getHttpClient()

std::shared_ptr< Pistache::Http::Client > dunedaq::restcmd::RestEndpoint::getHttpClient ( ) const
inline

Definition at line 62 of file RestEndpoint.hpp.

62 {
63 return http_client_;
64 }

◆ getPort()

uint16_t dunedaq::restcmd::RestEndpoint::getPort ( ) const
inline

Definition at line 59 of file RestEndpoint.hpp.

59 {
60 return static_cast<uint16_t>(port_);
61 }

◆ handle_route_command()

void RestEndpoint::handle_route_command ( const Pistache::Rest::Request & ,
Pistache::Http::ResponseWriter response )
private

Definition at line 78 of file RestEndpoint.cpp.

79{
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}

◆ handleResponseCommand()

void RestEndpoint::handleResponseCommand ( const cmdobj_t & cmd,
cmdlib::cmd::CommandReply & meta )

Definition at line 96 of file RestEndpoint.cpp.

97{
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}
std::vector< Pistache::Async::Promise< Pistache::Http::Response > > http_client_responses_
#define TLOG(...)
Definition macro.hpp:22
void to_json(data_t &j, const Command &obj)
Definition Nljs.hpp:20

◆ init()

void RestEndpoint::init ( size_t threads)

Definition at line 20 of file RestEndpoint.cpp.

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);
31 http_client_options_ = Http::Client::options().threads(static_cast<int>(threads));
33}
Pistache::Http::Client::Options http_client_options_

◆ shutdown()

void RestEndpoint::shutdown ( )

Definition at line 47 of file RestEndpoint.cpp.

48{
49 http_endpoint_->shutdown();
50 //server_thread_.join();
51 http_client_->shutdown();
52}

◆ start()

void RestEndpoint::start ( )

Definition at line 35 of file RestEndpoint.cpp.

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}

◆ stop()

void dunedaq::restcmd::RestEndpoint::stop ( )

Member Data Documentation

◆ accepted_mime_

Pistache::Http::Mime::MediaType dunedaq::restcmd::RestEndpoint::accepted_mime_
private

Definition at line 80 of file RestEndpoint.hpp.

◆ address_

Pistache::Address dunedaq::restcmd::RestEndpoint::address_
private

Definition at line 76 of file RestEndpoint.hpp.

◆ command_callback_

std::function<void(const cmdobj_t&, cmdlib::cmd::CommandReply)> dunedaq::restcmd::RestEndpoint::command_callback_
private

Definition at line 88 of file RestEndpoint.hpp.

◆ description_

Pistache::Rest::Description dunedaq::restcmd::RestEndpoint::description_
private

Definition at line 78 of file RestEndpoint.hpp.

◆ http_client_

std::shared_ptr<Pistache::Http::Client> dunedaq::restcmd::RestEndpoint::http_client_
private

Definition at line 83 of file RestEndpoint.hpp.

◆ http_client_options_

Pistache::Http::Client::Options dunedaq::restcmd::RestEndpoint::http_client_options_
private

Definition at line 84 of file RestEndpoint.hpp.

◆ http_client_responses_

std::vector<Pistache::Async::Promise<Pistache::Http::Response> > dunedaq::restcmd::RestEndpoint::http_client_responses_
private

Definition at line 85 of file RestEndpoint.hpp.

◆ http_endpoint_

std::shared_ptr<Pistache::Http::Endpoint> dunedaq::restcmd::RestEndpoint::http_endpoint_
private

Definition at line 77 of file RestEndpoint.hpp.

◆ port_

Pistache::Port dunedaq::restcmd::RestEndpoint::port_
private

Definition at line 75 of file RestEndpoint.hpp.

◆ router_

Pistache::Rest::Router dunedaq::restcmd::RestEndpoint::router_
private

Definition at line 79 of file RestEndpoint.hpp.

◆ server_thread_

std::thread dunedaq::restcmd::RestEndpoint::server_thread_
private

Definition at line 91 of file RestEndpoint.hpp.


The documentation for this class was generated from the following files: