Skip to content

Commit

Permalink
refactor(request-response): revise public API to follow naming conven…
Browse files Browse the repository at this point in the history
…tion (#3159)
  • Loading branch information
jxs authored Dec 13, 2022
1 parent cbf0a27 commit f828db6
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 228 deletions.
27 changes: 12 additions & 15 deletions examples/file-sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ mod network {
use libp2p::kad::record::store::MemoryStore;
use libp2p::kad::{GetProvidersOk, Kademlia, KademliaEvent, QueryId, QueryResult};
use libp2p::multiaddr::Protocol;
use libp2p::request_response::{
ProtocolSupport, RequestId, RequestResponse, RequestResponseCodec, RequestResponseEvent,
RequestResponseMessage, ResponseChannel,
};
use libp2p::request_response::{self, ProtocolSupport, RequestId, ResponseChannel};
use libp2p::swarm::{ConnectionHandlerUpgrErr, NetworkBehaviour, Swarm, SwarmEvent};
use std::collections::{hash_map, HashMap, HashSet};
use std::iter;
Expand Down Expand Up @@ -254,7 +251,7 @@ mod network {
libp2p::development_transport(id_keys).await?,
ComposedBehaviour {
kademlia: Kademlia::new(peer_id, MemoryStore::new(peer_id)),
request_response: RequestResponse::new(
request_response: request_response::Behaviour::new(
FileExchangeCodec(),
iter::once((FileExchangeProtocol(), ProtocolSupport::Full)),
Default::default(),
Expand Down Expand Up @@ -459,9 +456,9 @@ mod network {
)) => {}
SwarmEvent::Behaviour(ComposedEvent::Kademlia(_)) => {}
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
RequestResponseEvent::Message { message, .. },
request_response::Event::Message { message, .. },
)) => match message {
RequestResponseMessage::Request {
request_response::Message::Request {
request, channel, ..
} => {
self.event_sender
Expand All @@ -472,7 +469,7 @@ mod network {
.await
.expect("Event receiver not to be dropped.");
}
RequestResponseMessage::Response {
request_response::Message::Response {
request_id,
response,
} => {
Expand All @@ -484,7 +481,7 @@ mod network {
}
},
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
RequestResponseEvent::OutboundFailure {
request_response::Event::OutboundFailure {
request_id, error, ..
},
)) => {
Expand All @@ -495,7 +492,7 @@ mod network {
.send(Err(Box::new(error)));
}
SwarmEvent::Behaviour(ComposedEvent::RequestResponse(
RequestResponseEvent::ResponseSent { .. },
request_response::Event::ResponseSent { .. },
)) => {}
SwarmEvent::NewListenAddr { address, .. } => {
let local_peer_id = *self.swarm.local_peer_id();
Expand Down Expand Up @@ -604,18 +601,18 @@ mod network {
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "ComposedEvent")]
struct ComposedBehaviour {
request_response: RequestResponse<FileExchangeCodec>,
request_response: request_response::Behaviour<FileExchangeCodec>,
kademlia: Kademlia<MemoryStore>,
}

#[derive(Debug)]
enum ComposedEvent {
RequestResponse(RequestResponseEvent<FileRequest, FileResponse>),
RequestResponse(request_response::Event<FileRequest, FileResponse>),
Kademlia(KademliaEvent),
}

impl From<RequestResponseEvent<FileRequest, FileResponse>> for ComposedEvent {
fn from(event: RequestResponseEvent<FileRequest, FileResponse>) -> Self {
impl From<request_response::Event<FileRequest, FileResponse>> for ComposedEvent {
fn from(event: request_response::Event<FileRequest, FileResponse>) -> Self {
ComposedEvent::RequestResponse(event)
}
}
Expand Down Expand Up @@ -682,7 +679,7 @@ mod network {
}

#[async_trait]
impl RequestResponseCodec for FileExchangeCodec {
impl request_response::Codec for FileExchangeCodec {
type Protocol = FileExchangeProtocol;
type Request = FileRequest;
type Response = FileResponse;
Expand Down
30 changes: 15 additions & 15 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ use libp2p_core::{
connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr, PeerId,
};
use libp2p_request_response::{
ProtocolSupport, RequestId, RequestResponse, RequestResponseConfig, RequestResponseEvent,
RequestResponseMessage, ResponseChannel,
self as request_response, ProtocolSupport, RequestId, ResponseChannel,
};
use libp2p_swarm::{
behaviour::{
Expand Down Expand Up @@ -167,7 +166,7 @@ pub struct Behaviour {
local_peer_id: PeerId,

// Inner behaviour for sending requests and receiving the response.
inner: RequestResponse<AutoNatCodec>,
inner: request_response::Behaviour<AutoNatCodec>,

config: Config,

Expand Down Expand Up @@ -218,9 +217,9 @@ pub struct Behaviour {
impl Behaviour {
pub fn new(local_peer_id: PeerId, config: Config) -> Self {
let protocols = iter::once((AutoNatProtocol, ProtocolSupport::Full));
let mut cfg = RequestResponseConfig::default();
let mut cfg = request_response::Config::default();
cfg.set_request_timeout(config.timeout);
let inner = RequestResponse::new(AutoNatCodec, protocols, cfg);
let inner = request_response::Behaviour::new(AutoNatCodec, protocols, cfg);
Self {
local_peer_id,
inner,
Expand Down Expand Up @@ -419,7 +418,8 @@ impl Behaviour {
}

impl NetworkBehaviour for Behaviour {
type ConnectionHandler = <RequestResponse<AutoNatCodec> as NetworkBehaviour>::ConnectionHandler;
type ConnectionHandler =
<request_response::Behaviour<AutoNatCodec> as NetworkBehaviour>::ConnectionHandler;
type OutEvent = Event;

fn poll(&mut self, cx: &mut Context<'_>, params: &mut impl PollParameters) -> Poll<Action> {
Expand All @@ -432,21 +432,21 @@ impl NetworkBehaviour for Behaviour {
match self.inner.poll(cx, params) {
Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)) => {
let (mut events, action) = match event {
RequestResponseEvent::Message {
message: RequestResponseMessage::Response { .. },
request_response::Event::Message {
message: request_response::Message::Response { .. },
..
}
| RequestResponseEvent::OutboundFailure { .. } => {
| request_response::Event::OutboundFailure { .. } => {
self.as_client().handle_event(params, event)
}
RequestResponseEvent::Message {
message: RequestResponseMessage::Request { .. },
request_response::Event::Message {
message: request_response::Message::Request { .. },
..
}
| RequestResponseEvent::InboundFailure { .. } => {
| request_response::Event::InboundFailure { .. } => {
self.as_server().handle_event(params, event)
}
RequestResponseEvent::ResponseSent { .. } => (VecDeque::new(), None),
request_response::Event::ResponseSent { .. } => (VecDeque::new(), None),
};
self.pending_out_events.append(&mut events);
if let Some(action) = action {
Expand Down Expand Up @@ -542,12 +542,12 @@ type Action = NetworkBehaviourAction<
<Behaviour as NetworkBehaviour>::ConnectionHandler,
>;

// Trait implemented for `AsClient` as `AsServer` to handle events from the inner [`RequestResponse`] Protocol.
// Trait implemented for `AsClient` and `AsServer` to handle events from the inner [`request_response::Behaviour`] Protocol.
trait HandleInnerEvent {
fn handle_event(
&mut self,
params: &mut impl PollParameters,
event: RequestResponseEvent<DialRequest, DialResponse>,
event: request_response::Event<DialRequest, DialResponse>,
) -> (VecDeque<Event>, Option<Action>);
}

Expand Down
14 changes: 6 additions & 8 deletions protocols/autonat/src/behaviour/as_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ use futures::FutureExt;
use futures_timer::Delay;
use instant::Instant;
use libp2p_core::{connection::ConnectionId, Multiaddr, PeerId};
use libp2p_request_response::{
OutboundFailure, RequestId, RequestResponse, RequestResponseEvent, RequestResponseMessage,
};
use libp2p_request_response::{self as request_response, OutboundFailure, RequestId};
use libp2p_swarm::{AddressScore, NetworkBehaviourAction, PollParameters};
use rand::{seq::SliceRandom, thread_rng};
use std::{
Expand Down Expand Up @@ -83,7 +81,7 @@ pub enum OutboundProbeEvent {

/// View over [`super::Behaviour`] in a client role.
pub struct AsClient<'a> {
pub inner: &'a mut RequestResponse<AutoNatCodec>,
pub inner: &'a mut request_response::Behaviour<AutoNatCodec>,
pub local_peer_id: PeerId,
pub config: &'a Config,
pub connected: &'a HashMap<PeerId, HashMap<ConnectionId, Option<Multiaddr>>>,
Expand All @@ -105,15 +103,15 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
fn handle_event(
&mut self,
params: &mut impl PollParameters,
event: RequestResponseEvent<DialRequest, DialResponse>,
event: request_response::Event<DialRequest, DialResponse>,
) -> (VecDeque<Event>, Option<Action>) {
let mut events = VecDeque::new();
let mut action = None;
match event {
RequestResponseEvent::Message {
request_response::Event::Message {
peer,
message:
RequestResponseMessage::Response {
request_response::Message::Response {
request_id,
response,
},
Expand Down Expand Up @@ -160,7 +158,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> {
}
}
}
RequestResponseEvent::OutboundFailure {
request_response::Event::OutboundFailure {
peer,
error,
request_id,
Expand Down
13 changes: 6 additions & 7 deletions protocols/autonat/src/behaviour/as_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use super::{
use instant::Instant;
use libp2p_core::{connection::ConnectionId, multiaddr::Protocol, Multiaddr, PeerId};
use libp2p_request_response::{
InboundFailure, RequestId, RequestResponse, RequestResponseEvent, RequestResponseMessage,
ResponseChannel,
self as request_response, InboundFailure, RequestId, ResponseChannel,
};
use libp2p_swarm::{
dial_opts::{DialOpts, PeerCondition},
Expand Down Expand Up @@ -75,7 +74,7 @@ pub enum InboundProbeEvent {

/// View over [`super::Behaviour`] in a server role.
pub struct AsServer<'a> {
pub inner: &'a mut RequestResponse<AutoNatCodec>,
pub inner: &'a mut request_response::Behaviour<AutoNatCodec>,
pub config: &'a Config,
pub connected: &'a HashMap<PeerId, HashMap<ConnectionId, Option<Multiaddr>>>,
pub probe_id: &'a mut ProbeId,
Expand All @@ -98,15 +97,15 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
fn handle_event(
&mut self,
_params: &mut impl PollParameters,
event: RequestResponseEvent<DialRequest, DialResponse>,
event: request_response::Event<DialRequest, DialResponse>,
) -> (VecDeque<Event>, Option<Action>) {
let mut events = VecDeque::new();
let mut action = None;
match event {
RequestResponseEvent::Message {
request_response::Event::Message {
peer,
message:
RequestResponseMessage::Request {
request_response::Message::Request {
request_id,
request,
channel,
Expand Down Expand Up @@ -161,7 +160,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
}
}
}
RequestResponseEvent::InboundFailure {
request_response::Event::InboundFailure {
peer,
error,
request_id,
Expand Down
4 changes: 2 additions & 2 deletions protocols/autonat/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::structs_proto;
use async_trait::async_trait;
use futures::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use libp2p_core::{upgrade, Multiaddr, PeerId};
use libp2p_request_response::{ProtocolName, RequestResponseCodec};
use libp2p_request_response::{self as request_response, ProtocolName};
use prost::Message;
use std::{convert::TryFrom, io};

Expand All @@ -42,7 +42,7 @@ impl ProtocolName for AutoNatProtocol {
pub struct AutoNatCodec;

#[async_trait]
impl RequestResponseCodec for AutoNatCodec {
impl request_response::Codec for AutoNatCodec {
type Protocol = AutoNatProtocol;
type Request = DialRequest;
type Response = DialResponse;
Expand Down
10 changes: 10 additions & 0 deletions protocols/request-response/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# 0.24.0 [unreleased]

- Rename types as per [discussion 2174].
`RequestResponse` has been renamed to `Behaviour`.
The `RequestResponse` prefix has been removed from various types like `RequestResponseEvent`.
Users should prefer importing the request_response protocol as a module (`use libp2p::request_response;`),
and refer to its types via `request_response::`. For example: `request_response::Behaviour` or `request_response::Event`.
See [PR 3159].

- Update to `libp2p-swarm` `v0.42.0`.

[discussion 2174]: https://github.com/libp2p/rust-libp2p/discussions/2174
[PR 3159]: https://github.com/libp2p/rust-libp2p/pull/3159

# 0.23.0

- Update to `libp2p-core` `v0.38.0`.
Expand Down
Loading

0 comments on commit f828db6

Please sign in to comment.