Kagome
Polkadot Runtime Engine in C++17
session.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_METRICS_SESSION_HPP
7 #define KAGOME_CORE_METRICS_SESSION_HPP
8 
9 #include <boost/asio/io_context.hpp>
10 #include <boost/asio/ip/tcp.hpp>
11 #include <boost/asio/streambuf.hpp>
12 #include <boost/beast/http/string_body.hpp>
13 
14 namespace kagome::metrics {
15 
19  class Session {
20  public:
21  using Body = boost::beast::http::string_body;
22  using Request = boost::beast::http::request<Body>;
23  using Response = boost::beast::http::response<Body>;
24  using Context = boost::asio::io_context;
25  using Socket = boost::asio::ip::tcp::socket;
26  using ErrorCode = boost::system::error_code;
27  using Streambuf = boost::asio::streambuf;
28  using Duration = boost::asio::steady_timer::duration;
29  using SessionId = uint64_t;
30 
31  private:
32  using OnRequestSignature = void(Request, std::shared_ptr<Session> session);
33  std::function<OnRequestSignature> on_request_;
34 
35  public:
36  struct Configuration {
37  static constexpr size_t kDefaultRequestSize = 10000u;
38  static constexpr Duration kDefaultTimeout = std::chrono::seconds(30);
39 
40  size_t max_request_size{kDefaultRequestSize};
41  Duration operation_timeout{kDefaultTimeout};
42  };
43 
44  virtual ~Session() = default;
45 
46  virtual void start() = 0;
47 
48  virtual Socket &socket() = 0;
49 
50  virtual SessionId id() const = 0;
51 
55  void connectOnRequest(std::function<OnRequestSignature> callback) {
56  on_request_ = std::move(callback);
57  }
58 
62  void processRequest(Request request, std::shared_ptr<Session> session) {
63  on_request_(request, std::move(session));
64  }
65 
69  virtual void respond(Response message) = 0;
70  };
71 
72 } // namespace kagome::metrics
73 
74 #endif // KAGOME_CORE_METRICS_SESSION_HPP
static constexpr Duration kDefaultTimeout
Definition: session.hpp:38
void connectOnRequest(std::function< OnRequestSignature > callback)
connects on request callback
Definition: session.hpp:55
void processRequest(Request request, std::shared_ptr< Session > session)
process request message
Definition: session.hpp:62
boost::beast::http::request< Body > Request
Definition: session.hpp:22
void(Request, std::shared_ptr< Session > session) OnRequestSignature
Definition: session.hpp:32
boost::beast::http::string_body Body
Definition: session.hpp:21
boost::system::error_code ErrorCode
Definition: session.hpp:26
session interface for OpenMetrics service
Definition: session.hpp:19
boost::asio::streambuf Streambuf
Definition: session.hpp:27
std::function< OnRequestSignature > on_request_
on request callback
Definition: session.hpp:33
virtual SessionId id() const =0
boost::asio::io_context Context
Definition: session.hpp:24
virtual void start()=0
virtual Socket & socket()=0
static constexpr size_t kDefaultRequestSize
Definition: session.hpp:37
boost::asio::steady_timer::duration Duration
Definition: session.hpp:28
boost::beast::http::response< Body > Response
Definition: session.hpp:23
virtual void respond(Response message)=0
send response message
boost::asio::ip::tcp::socket Socket
Definition: session.hpp:25
virtual ~Session()=default