DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
RestEndpoint.hpp
Go to the documentation of this file.
1
11#ifndef RESTCMD_INCLUDE_RESTCMD_RESTENDPOINT_HPP_
12#define RESTCMD_INCLUDE_RESTCMD_RESTENDPOINT_HPP_
13
14#include "restcmd/Issues.hpp"
15
16#include "cmdlib/cmd/Nljs.hpp"
17
18#include <nlohmann/json.hpp>
19
20#include <pistache/client.h>
21#include <pistache/description.h>
22#include <pistache/endpoint.h>
23#include <pistache/http.h>
24#include <pistache/http_header.h>
25#include <pistache/mime.h>
26#include <pistache/router.h>
27
28#include <chrono>
29#include <future>
30#include <memory>
31#include <string>
32#include <thread>
33#include <vector>
34
36
37typedef nlohmann::json cmdobj_t;
38
40{
41public:
42 explicit RestEndpoint(const std::string& /*uri*/,
43 int port,
44 std::function<void(const cmdobj_t&, cmdlib::cmd::CommandReply)> callback) noexcept
45 : port_{ static_cast<uint16_t>(port) } // NOLINT(build/unsigned)
46 , address_{ Pistache::Ipv4::any(), port_ }
47 , http_endpoint_{ std::make_shared<Pistache::Http::Endpoint>(address_) }
48 , description_{ "DUNE DAQ cmdlib API", "0.1" }
49 , accepted_mime_{ MIME(Application, Json) }
50 , http_client_{ std::make_shared<Pistache::Http::Client>() }
51 , command_callback_{ callback }
52 {
53 }
54
55 void init(size_t threads);
56 void start();
57 void stop();
58 void shutdown();
59
60 // Client handler
62 uint16_t getPort() const { return static_cast<uint16_t>(port_); } // NOLINT(build/unsigned)
63 std::shared_ptr<Pistache::Http::Client> getHttpClient() const { return http_client_; }
64
65private:
66 void createRouting();
68 // void serveTask();
69
70 // Route handler
71 void handle_route_command(const Pistache::Rest::Request&, Pistache::Http::ResponseWriter response);
72
73 // REST
74 Pistache::Port port_;
75 Pistache::Address address_;
76 std::shared_ptr<Pistache::Http::Endpoint> http_endpoint_;
77 Pistache::Rest::Description description_;
78 Pistache::Rest::Router router_;
79 Pistache::Http::Mime::MediaType accepted_mime_;
80
81 // CLIENT
82 std::shared_ptr<Pistache::Http::Client> http_client_;
83 Pistache::Http::Client::Options http_client_options_;
84 std::vector<Pistache::Async::Promise<Pistache::Http::Response>> http_client_responses_;
85
86 // Function to call with received POST bodies
88
89 // Background server thread
90 std::thread server_thread_;
91};
92
93} // namespace dunedaq::restcmd
94
95#endif // RESTCMD_INCLUDE_RESTCMD_RESTENDPOINT_HPP_
RestEndpoint(const std::string &, int port, std::function< void(const cmdobj_t &, cmdlib::cmd::CommandReply)> callback) noexcept
std::shared_ptr< Pistache::Http::Client > getHttpClient() const
Pistache::Rest::Router router_
Pistache::Rest::Description description_
std::shared_ptr< Pistache::Http::Client > http_client_
void handle_route_command(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response)
std::shared_ptr< Pistache::Http::Endpoint > http_endpoint_
Pistache::Http::Client::Options http_client_options_
Pistache::Http::Mime::MediaType accepted_mime_
std::function< void(const cmdobj_t &, cmdlib::cmd::CommandReply)> command_callback_
void handleResponseCommand(const cmdobj_t &cmd, cmdlib::cmd::CommandReply &meta)
std::vector< Pistache::Async::Promise< Pistache::Http::Response > > http_client_responses_
nlohmann::json cmdobj_t