Skip to content

Commit 859a249

Browse files
authored
StreamProtocols are passed to Identify (#268)
* StreamProtocols are passed to Identify * Revert "StreamProtocols are passed to Identify" This reverts commit af9cc1a. * Config for Identify
1 parent 8032923 commit 859a249

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright Quadrivium LLC
3+
* All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <libp2p/peer/stream_protocols.hpp>
10+
11+
namespace {
12+
const std::string kIdentifyProto = "/ipfs/id/1.0.0";
13+
} // namespace
14+
15+
namespace libp2p::protocol {
16+
17+
struct IdentifyConfig {
18+
IdentifyConfig() = default;
19+
StreamProtocols protocols = {::kIdentifyProto};
20+
};
21+
22+
} // namespace libp2p::protocol

include/libp2p/protocol/identify/identify.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <libp2p/event/bus.hpp>
1313
#include <libp2p/protocol/base_protocol.hpp>
14+
#include <libp2p/protocol/identify/config.hpp>
1415
#include <libp2p/protocol/identify/identify_msg_processor.hpp>
1516

1617
namespace libp2p::multi {
@@ -32,7 +33,8 @@ namespace libp2p::protocol {
3233
* @param msg_processor to work with Identify messages
3334
* @param event_bus - bus, over which the events arrive
3435
*/
35-
Identify(Host &host,
36+
Identify(const IdentifyConfig &config,
37+
Host &host,
3638
std::shared_ptr<IdentifyMessageProcessor> msg_processor,
3739
event::Bus &event_bus);
3840

@@ -81,7 +83,7 @@ namespace libp2p::protocol {
8183
std::shared_ptr<IdentifyMessageProcessor> msg_processor_;
8284
event::Bus &bus_;
8385
event::Handle sub_; // will unsubscribe during destruction by itself
84-
86+
StreamProtocols protocols_;
8587
bool started_ = false;
8688
};
8789
} // namespace libp2p::protocol

src/protocol/identify/identify.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
*/
66

77
#include <libp2p/protocol/identify/identify.hpp>
8-
98
#include <string>
109
#include <tuple>
1110

1211
#include <boost/assert.hpp>
1312

14-
namespace {
15-
const std::string kIdentifyProto = "/ipfs/id/1.0.0";
16-
} // namespace
17-
1813
namespace libp2p::protocol {
19-
Identify::Identify(Host &host,
14+
Identify::Identify(const IdentifyConfig &config,
15+
Host &host,
2016
std::shared_ptr<IdentifyMessageProcessor> msg_processor,
2117
event::Bus &event_bus)
22-
: host_{host}, msg_processor_{std::move(msg_processor)}, bus_{event_bus} {
18+
: host_{host},
19+
msg_processor_{std::move(msg_processor)},
20+
bus_{event_bus},
21+
protocols_{config.protocols} {
2322
BOOST_ASSERT(msg_processor_);
2423
}
2524

@@ -38,7 +37,7 @@ namespace libp2p::protocol {
3837
}
3938

4039
peer::ProtocolName Identify::getProtocolId() const {
41-
return kIdentifyProto;
40+
return protocols_.empty() ? peer::ProtocolName{} : protocols_.front();
4241
}
4342

4443
void Identify::handle(StreamAndProtocol stream) {
@@ -50,7 +49,7 @@ namespace libp2p::protocol {
5049
BOOST_ASSERT(!started_);
5150
started_ = true;
5251

53-
host_.setProtocolHandler({kIdentifyProto},
52+
host_.setProtocolHandler(protocols_,
5453
[wp = weak_from_this()](StreamAndProtocol stream) {
5554
if (auto self = wp.lock()) {
5655
self->handle(std::move(stream));
@@ -86,9 +85,7 @@ namespace libp2p::protocol {
8685
std::move(remote_peer_addr_res.value())}};
8786

8887
msg_processor_->getHost().newStream(
89-
peer_info,
90-
{kIdentifyProto},
91-
[self{shared_from_this()}](auto &&stream_res) {
88+
peer_info, protocols_, [self{shared_from_this()}](auto &&stream_res) {
9289
if (!stream_res) {
9390
return;
9491
}

test/libp2p/protocol/identify_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ class IdentifyTest : public testing::Test {
8080

8181
id_msg_processor_ = std::make_shared<IdentifyMessageProcessor>(
8282
host_, conn_manager_, id_manager_, key_marshaller_);
83-
identify_ = std::make_shared<Identify>(host_, id_msg_processor_, bus_);
83+
IdentifyConfig config;
84+
config.protocols = {kIdentifyProto};
85+
identify_ = std::make_shared<Identify>(config, host_, id_msg_processor_, bus_);
8486
}
8587

8688
HostMock host_;

0 commit comments

Comments
 (0)