Kagome
Polkadot Runtime Engine in C++17
protocol_base_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_NETWORK_PROTOCOLBASEIMPL
7 #define KAGOME_NETWORK_PROTOCOLBASEIMPL
8 
10 
11 #include <memory>
12 #include <optional>
13 #include <string>
14 
15 #include <libp2p/host/host.hpp>
16 #include <libp2p/peer/stream_protocols.hpp>
17 
19 #include "utils/box.hpp"
20 #include "utils/non_copyable.hpp"
21 
22 namespace kagome::network {
25  using Protocols = libp2p::StreamProtocols;
28  using Host = libp2p::Host;
29  using ProtocolName = std::string;
30 
32  public:
33  ProtocolBaseImpl() = delete;
34  ~ProtocolBaseImpl() = default;
35 
37  Protocols const &protocols,
38  std::string const &log_section)
39  : host_{host},
40  protocols_{std::move(protocols)},
41  log_{log::createLogger(log_section, "kagome_protocols")} {}
42 
43  template <typename T>
44  bool start(std::weak_ptr<T> wptr) {
45  host_.setProtocolHandler(
46  protocols_,
47  [log{logger()}, wp(std::move(wptr))](auto &&stream_and_proto) {
48  network::streamReadBuffer(stream_and_proto);
49  if (auto peer_id = stream_and_proto.stream->remotePeerId()) {
50  SL_TRACE(log,
51  "Handled {} protocol stream from: {}",
52  stream_and_proto.protocol,
53  peer_id.value().toBase58());
54  if (auto self = wp.lock()) {
55  self->onIncomingStream(std::move(stream_and_proto.stream));
56  return;
57  }
58  } else {
59  log->warn("Handled {} protocol stream from unknown peer",
60  stream_and_proto.protocol);
61  }
62  stream_and_proto.stream->close(
63  [stream{stream_and_proto.stream}](auto &&) {});
64  });
65  return true;
66  }
67 
68  bool stop() {
69  return true;
70  }
71 
72  Protocols const &protocolIds() const {
73  return protocols_;
74  }
75 
76  Host &host() {
77  return host_;
78  }
79 
80  log::Logger const &logger() const {
81  return log_;
82  }
83 
84  template <typename T>
85  void closeStream(std::weak_ptr<T> wptr, std::shared_ptr<Stream> stream) {
86  BOOST_ASSERT(stream);
87  stream->close([log{logger()}, wptr, stream](auto &&result) {
88  if (auto self = wptr.lock()) {
89  if (!result) {
90  SL_WARN(log,
91  "Stream {} was not closed successfully with {}",
92  self->protocolName(),
93  stream->remotePeerId().value());
94 
95  } else {
96  SL_VERBOSE(log,
97  "Stream {} with {} was closed.",
98  self->protocolName(),
99  stream->remotePeerId().value());
100  }
101  }
102  });
103  }
104 
105  private:
109  };
110 
111 } // namespace kagome::network
112 
113 #endif // KAGOME_NETWORK_PROTOCOLBASEIMPL
log::Logger const & logger() const
void closeStream(std::weak_ptr< T > wptr, std::shared_ptr< Stream > stream)
void streamReadBuffer(libp2p::StreamAndProtocol &result)
libp2p::peer::PeerInfo PeerInfo
libp2p::peer::Protocol Protocol
libp2p::StreamProtocols Protocols
libp2p::peer::PeerId PeerId
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
ProtocolBaseImpl(libp2p::Host &host, Protocols const &protocols, std::string const &log_section)
Protocols const & protocolIds() const
std::string ProtocolName
libp2p::connection::Stream Stream
bool start(std::weak_ptr< T > wptr)
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112