Skip to content

Commit 4172755

Browse files
committed
rename Provider trait to Gateway
1 parent cb2dbbc commit 4172755

File tree

5 files changed

+32
-51
lines changed

5 files changed

+32
-51
lines changed

protocols/upnp/src/behaviour.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::{
3333
};
3434

3535
use crate::{
36-
provider::{Protocol, Provider},
36+
gateway::{Gateway, Protocol},
3737
Config,
3838
};
3939
use futures::{future::BoxFuture, stream::FuturesUnordered, Future, FutureExt, StreamExt};
@@ -51,8 +51,8 @@ const MAPPING_DURATION: u64 = 3600;
5151
const MAPPING_TIMEOUT: u64 = MAPPING_DURATION / 2;
5252

5353
/// Map a port on the gateway.
54-
async fn map_port<P: Provider + 'static>(
55-
gateway: Arc<P::Gateway>,
54+
async fn map_port<P: Gateway + 'static>(
55+
gateway: Arc<P>,
5656
mapping: Mapping,
5757
permanent: bool,
5858
) -> Event {
@@ -72,17 +72,14 @@ async fn map_port<P: Provider + 'static>(
7272
}
7373

7474
/// Remove a port mapping on the gateway.
75-
async fn remove_port_mapping<P: Provider + 'static>(
76-
gateway: Arc<P::Gateway>,
77-
mapping: Mapping,
78-
) -> Event {
75+
async fn remove_port_mapping<P: Gateway + 'static>(gateway: Arc<P>, mapping: Mapping) -> Event {
7976
match P::remove_port(gateway, mapping.protocol, mapping.internal_addr.port()).await {
8077
Ok(()) => Event::Removed(mapping),
8178
Err(err) => Event::RemovalFailure(mapping, err),
8279
}
8380
}
8481

85-
/// A [`Provider::Gateway`] event.
82+
/// A [`Gateway`] event.
8683
#[derive(Debug)]
8784
enum Event {
8885
/// Port was successfully mapped.
@@ -136,18 +133,18 @@ enum MappingState {
136133
Permanent,
137134
}
138135

139-
/// Current state of the UPnP [`Provider::Gateway`].
140-
enum GatewayState<P: Provider> {
141-
Searching(BoxFuture<'static, Result<(P::Gateway, Ipv4Addr), Box<dyn std::error::Error>>>),
142-
Available((Arc<P::Gateway>, Ipv4Addr)),
136+
/// Current state of the UPnP [`Gateway`].
137+
enum GatewayState<P: Gateway> {
138+
Searching(BoxFuture<'static, Result<(P, Ipv4Addr), Box<dyn std::error::Error>>>),
139+
Available((Arc<P>, Ipv4Addr)),
143140
GatewayNotFound,
144141
}
145142

146143
/// A `NetworkBehaviour` for UPnP port mapping. Automatically tries to map the external port
147144
/// to an internal address on the gateway on a `FromSwarm::NewListenAddr`.
148145
pub struct Behaviour<P>
149146
where
150-
P: Provider,
147+
P: Gateway,
151148
{
152149
/// Gateway config.
153150
config: Config,
@@ -163,7 +160,7 @@ where
163160

164161
impl<P> Default for Behaviour<P>
165162
where
166-
P: Provider + 'static,
163+
P: Gateway + 'static,
167164
{
168165
fn default() -> Self {
169166
Self::new(Config::default())
@@ -172,13 +169,13 @@ where
172169

173170
impl<P> Behaviour<P>
174171
where
175-
P: Provider + 'static,
172+
P: Gateway + 'static,
176173
{
177174
/// Builds a new `UPnP` behaviour.
178175
pub fn new(config: Config) -> Self {
179176
Self {
180177
config,
181-
state: GatewayState::Searching(P::search_gateway(config).boxed()),
178+
state: GatewayState::Searching(P::search(config).boxed()),
182179
mappings: Default::default(),
183180
pending_events: Default::default(),
184181
}
@@ -187,7 +184,7 @@ where
187184

188185
impl<P> NetworkBehaviour for Behaviour<P>
189186
where
190-
P: Provider + 'static,
187+
P: Gateway + 'static,
191188
{
192189
type ConnectionHandler = dummy::ConnectionHandler;
193190

protocols/upnp/src/provider.rs renamed to protocols/upnp/src/gateway.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,19 @@ impl fmt::Display for Protocol {
5353

5454
/// The interface for non-blocking UPnP I/O providers.
5555
#[async_trait]
56-
pub trait Provider {
57-
/// The gateway of obtained from [`Provider::search_gateway`].
58-
type Gateway: Send + Sync;
59-
56+
pub trait Gateway: Sized + Send + Sync {
6057
/// Search for the gateway endpoint on the local network.
61-
async fn search_gateway(config: Config) -> Result<(Self::Gateway, Ipv4Addr), Box<dyn Error>>;
58+
async fn search(config: Config) -> Result<(Self, Ipv4Addr), Box<dyn Error>>;
6259

6360
/// Add the input port mapping on the gateway so that traffic is forwarded to the input address.
6461
async fn add_port(
65-
_: Arc<Self::Gateway>,
62+
_: Arc<Self>,
6663
protocol: Protocol,
6764
addr: SocketAddrV4,
6865
duration: Duration,
6966
) -> Result<(), Box<dyn Error>>;
7067

7168
/// Remove port mapping on the gateway.
72-
async fn remove_port(
73-
_: Arc<Self::Gateway>,
74-
protocol: Protocol,
75-
port: u16,
76-
) -> Result<(), Box<dyn Error>>;
69+
async fn remove_port(_: Arc<Self>, protocol: Protocol, port: u16)
70+
-> Result<(), Box<dyn Error>>;
7771
}

protocols/upnp/src/provider/async_std.rs renamed to protocols/upnp/src/gateway/async_std.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ use igd_async_std::{
3434
PortMappingProtocol, SearchOptions,
3535
};
3636

37-
#[doc(hidden)]
38-
pub struct Provider;
39-
4037
#[async_trait]
41-
impl super::Provider for Provider {
42-
type Gateway = Gateway;
43-
44-
async fn search_gateway(config: Config) -> Result<(Self::Gateway, Ipv4Addr), Box<dyn Error>> {
38+
impl super::Gateway for Gateway {
39+
async fn search(config: Config) -> Result<(Self, Ipv4Addr), Box<dyn Error>> {
4540
let options = SearchOptions {
4641
bind_addr: config.bind_addr,
4742
broadcast_address: config.broadcast_addr,
@@ -53,7 +48,7 @@ impl super::Provider for Provider {
5348
}
5449

5550
async fn add_port(
56-
gateway: Arc<Self::Gateway>,
51+
gateway: Arc<Self>,
5752
protocol: Protocol,
5853
addr: SocketAddrV4,
5954
duration: Duration,
@@ -75,7 +70,7 @@ impl super::Provider for Provider {
7570
}
7671

7772
async fn remove_port(
78-
gateway: Arc<Self::Gateway>,
73+
gateway: Arc<Self>,
7974
protocol: Protocol,
8075
port: u16,
8176
) -> Result<(), Box<dyn Error>> {
@@ -91,4 +86,4 @@ impl super::Provider for Provider {
9186
}
9287

9388
/// The type of a [`Behaviour`] using the `async-io` implementation.
94-
pub type Behaviour = crate::behaviour::Behaviour<Provider>;
89+
pub type Behaviour = crate::behaviour::Behaviour<Gateway>;

protocols/upnp/src/provider/tokio.rs renamed to protocols/upnp/src/gateway/tokio.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ use igd::{
3434
PortMappingProtocol, SearchOptions,
3535
};
3636

37-
#[doc(hidden)]
38-
pub struct Provider;
39-
4037
#[async_trait]
41-
impl super::Provider for Provider {
42-
type Gateway = Gateway;
43-
44-
async fn search_gateway(config: Config) -> Result<(Self::Gateway, Ipv4Addr), Box<dyn Error>> {
38+
impl super::Gateway for Gateway {
39+
async fn search(config: Config) -> Result<(Self, Ipv4Addr), Box<dyn Error>> {
4540
let options = SearchOptions {
4641
bind_addr: config.bind_addr,
4742
broadcast_address: config.broadcast_addr,
@@ -53,7 +48,7 @@ impl super::Provider for Provider {
5348
}
5449

5550
async fn add_port(
56-
gateway: Arc<Self::Gateway>,
51+
gateway: Arc<Self>,
5752
protocol: Protocol,
5853
addr: SocketAddrV4,
5954
duration: Duration,
@@ -75,7 +70,7 @@ impl super::Provider for Provider {
7570
}
7671

7772
async fn remove_port(
78-
gateway: Arc<Self::Gateway>,
73+
gateway: Arc<Self>,
7974
protocol: Protocol,
8075
port: u16,
8176
) -> Result<(), Box<dyn Error>> {
@@ -91,4 +86,4 @@ impl super::Provider for Provider {
9186
}
9287

9388
/// The type of a [`Behaviour`] using the `tokio` implementation.
94-
pub type Behaviour = crate::behaviour::Behaviour<Provider>;
89+
pub type Behaviour = crate::behaviour::Behaviour<Gateway>;

protocols/upnp/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@
2828
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
2929

3030
mod behaviour;
31-
mod provider;
31+
mod gateway;
3232

3333
use std::{
3434
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
3535
time::Duration,
3636
};
3737

3838
#[cfg(feature = "async-std")]
39-
pub use provider::async_std;
39+
pub use gateway::async_std;
4040
#[cfg(feature = "tokio")]
41-
pub use provider::tokio;
41+
pub use gateway::tokio;
4242

4343
/// The configuration for UPnP capabilities for libp2p.
4444
#[derive(Clone, Copy, Debug)]

0 commit comments

Comments
 (0)