Kagome
Polkadot Runtime Engine in C++17
session_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_METRICS_IMPL_SESSION_IMPL_HPP
7 #define KAGOME_CORE_METRICS_IMPL_SESSION_IMPL_HPP
8 
9 #include <string_view>
10 
11 #include <boost/asio/strand.hpp>
12 #include <boost/beast.hpp>
13 #include "log/logger.hpp"
14 #include "metrics/session.hpp"
15 
16 namespace kagome::metrics {
17 
18  class SessionImpl : public Session,
19  public std::enable_shared_from_this<SessionImpl> {
20  using Body = boost::beast::http::string_body;
21  using Request = boost::beast::http::request<Body>;
22  using Response = boost::beast::http::response<Body>;
23  using Parser = boost::beast::http::request_parser<Body>;
24  using HttpField = boost::beast::http::field;
25  using HttpError = boost::beast::http::error;
26 
27  public:
33  SessionImpl(Context &context, Configuration config);
34  ~SessionImpl() override = default;
35 
36  Socket &socket() override {
37  return stream_.socket();
38  }
39 
43  void start() override;
44 
49  void respond(Response response) override;
50 
55  SessionId id() const override {
56  return 0ull;
57  }
58 
59  private:
63  void stop();
64 
70  void handleRequest(Request &&request);
71 
75  void asyncRead();
76 
82  void asyncWrite(Response message);
83 
87  void onRead(boost::system::error_code ec, std::size_t size);
88 
92  void onWrite(bool close, boost::system::error_code ec, std::size_t);
93 
99  void reportError(boost::system::error_code ec, std::string_view message);
100 
101  static constexpr boost::string_view kServerName = "Kagome";
102 
104  boost::asio::strand<boost::asio::io_context::executor_type> strand_;
105 
107  boost::beast::tcp_stream stream_;
108  boost::beast::flat_buffer buffer_;
109  std::shared_ptr<void> res_;
110 
115  std::unique_ptr<Parser> parser_;
117  };
118 
119 } // namespace kagome::metrics
120 
121 #endif // KAGOME_CORE_METRICS_IMPL_SESSION_IMPL_HPP
SessionImpl(Context &context, Configuration config)
constructor
boost::beast::http::error HttpError
boost::beast::http::field HttpField
boost::beast::flat_buffer buffer_
read buffer
SessionId id() const override
method to get id of the session
void reportError(boost::system::error_code ec, std::string_view message)
reports error code and message
void asyncRead()
asynchronously read http message
boost::beast::http::string_body Body
void start() override
starts session
session interface for OpenMetrics service
Definition: session.hpp:19
boost::beast::http::request_parser< Body > Parser
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Strand to ensure the connection&#39;s handlers are not called concurrently.
boost::beast::tcp_stream stream_
stream
static constexpr boost::string_view kServerName
boost::asio::io_context Context
Definition: session.hpp:24
void respond(Response response) override
sends response wrapped by http message
boost::beast::http::response< Body > Response
void asyncWrite(Response message)
sends http message
Socket & socket() override
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
~SessionImpl() override=default
std::unique_ptr< Parser > parser_
request parser type
void stop()
stops session
std::shared_ptr< void > res_
boost::beast::http::request< Body > Request
void onWrite(bool close, boost::system::error_code ec, std::size_t)
write completion callback
void onRead(boost::system::error_code ec, std::size_t size)
read completion callback
boost::asio::ip::tcp::socket Socket
Definition: session.hpp:25
Configuration config_
session configuration
void handleRequest(Request &&request)
process http request, compose and execute response