Kagome
Polkadot Runtime Engine in C++17
protocol_req_collation.cpp
Go to the documentation of this file.
1 
7 
8 #include "network/common.hpp"
10 #include "utils/non_copyable.hpp"
11 
12 namespace kagome::network {
13 
15  : RequestResponseProtocol<CollationFetchingRequest,
16  CollationFetchingResponse,
17  ScaleMessageReadWriter>,
19  NonMovable {
21  application::AppConfiguration const &app_config,
22  application::ChainSpec const &chain_spec,
23  std::shared_ptr<ReqCollationObserver> observer)
29  "ReqCollationProtocol"},
30  observer_{std::move(observer)} {}
31 
32  protected:
33  outcome::result<CollationFetchingResponse> onRxRequest(
35  std::shared_ptr<Stream> /*stream*/) override {
36  BOOST_ASSERT(observer_);
37  return observer_->OnCollationRequest(std::move(request));
38  }
39 
40  void onTxRequest(CollationFetchingRequest const &request) override {
41  if (base().logger()->level() >= log::Level::DEBUG) {
42  base().logger()->debug("Requesting collation");
43  }
44  }
45 
46  private:
47  std::shared_ptr<ReqCollationObserver> observer_;
48  };
49 
51  libp2p::Host &host,
52  application::AppConfiguration const &app_config,
53  application::ChainSpec const &chain_spec,
54  std::shared_ptr<ReqCollationObserver> observer)
55  : impl_{std::make_shared<ReqCollationProtocolImpl>(
56  host, app_config, chain_spec, std::move(observer))} {}
57 
59  BOOST_ASSERT(impl_ && !!"ReqCollationProtocolImpl must be initialized!");
60  return impl_->protocolName();
61  }
62 
64  BOOST_ASSERT(impl_ && !!"ReqCollationProtocolImpl must be initialized!");
65  return impl_->start();
66  }
67 
69  BOOST_ASSERT(impl_ && !!"ReqCollationProtocolImpl must be initialized!");
70  return impl_->stop();
71  }
72 
73  void ReqCollationProtocol::onIncomingStream(std::shared_ptr<Stream> stream) {
74  BOOST_ASSERT(!"Must not be called!");
75  }
76 
78  const PeerInfo &peer_info,
79  std::function<void(outcome::result<std::shared_ptr<Stream>>)> &&cb) {
80  BOOST_ASSERT(!"Must not be called!");
81  }
82 
84  const PeerId &peer_id,
86  std::function<void(outcome::result<CollationFetchingResponse>)>
87  &&response_handler) {
88  BOOST_ASSERT(impl_ && !!"ReqCollationProtocolImpl must be initialized!");
89  return impl_->doRequest(
90  peer_id, std::move(request), std::move(response_handler));
91  }
92 
93 } // namespace kagome::network
std::shared_ptr< ReqCollationProtocolImpl > impl_
void request(const PeerId &peer_id, CollationFetchingRequest request, std::function< void(outcome::result< CollationFetchingResponse >)> &&response_handler) override
void onIncomingStream(std::shared_ptr< Stream > stream) override
log::Logger const & logger() const
libp2p::peer::PeerInfo PeerInfo
libp2p::peer::Protocol Protocol
libp2p::peer::PeerId PeerId
void newOutgoingStream(const PeerInfo &peer_info, std::function< void(outcome::result< std::shared_ptr< Stream >>)> &&cb) override
const libp2p::peer::Protocol kReqCollationProtocol
Definition: common.hpp:28
ReqCollationProtocolImpl(libp2p::Host &host, application::AppConfiguration const &app_config, application::ChainSpec const &chain_spec, std::shared_ptr< ReqCollationObserver > observer)
const Protocol & protocolName() const override
virtual outcome::result< ResponseType > onRxRequest(RequestType request, std::shared_ptr< Stream > stream)=0