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_SRC_RESTENDPOINT_HPP_
12#define RESTCMD_SRC_RESTENDPOINT_HPP_
13
14#include <nlohmann/json.hpp>
15
16#include <pistache/http.h>
17#include <pistache/http_header.h>
18#include <pistache/description.h>
19#include <pistache/router.h>
20#include <pistache/endpoint.h>
21#include <pistache/client.h>
22#include <pistache/mime.h>
23
24#include "cmdlib/cmd/Nljs.hpp"
25
26#include "restcmd/Issues.hpp"
27
28#include <thread>
29#include <chrono>
30#include <future>
31#include <memory>
32#include <string>
33
34namespace dunedaq {
35namespace restcmd {
36
37typedef nlohmann::json cmdobj_t;
38
40public:
41 explicit RestEndpoint(const std::string& /*uri*/, int port,
42 std::function<void(const cmdobj_t&, cmdlib::cmd::CommandReply)> callback) noexcept
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 { }
51
52 void init(size_t threads);
53 void start();
54 void stop();
55 void shutdown();
56
57 // Client handler
59 uint16_t getPort() const {
60 return static_cast<uint16_t>(port_);
61 }
62 std::shared_ptr<Pistache::Http::Client> getHttpClient() const {
63 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
74 // REST
75 Pistache::Port port_;
76 Pistache::Address address_;
77 std::shared_ptr<Pistache::Http::Endpoint> http_endpoint_;
78 Pistache::Rest::Description description_;
79 Pistache::Rest::Router router_;
80 Pistache::Http::Mime::MediaType accepted_mime_;
81
82 // CLIENT
83 std::shared_ptr<Pistache::Http::Client> http_client_;
84 Pistache::Http::Client::Options http_client_options_;
85 std::vector<Pistache::Async::Promise<Pistache::Http::Response>> http_client_responses_;
86
87 // Function to call with received POST bodies
89
90 // Background server thread
91 std::thread server_thread_;
92
93};
94
95} // namespace restcmd
96} // namespace dunedaq
97
98#endif // RESTCMD_SRC_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
Including Qt Headers.