Skip to content

Commit

Permalink
feat(echo_service): perform UPnP and/or echo_service when the endpoint
Browse files Browse the repository at this point in the history
is created

- this simplifies the Endpoint::socket_addr() API removing the need of
a mutable borrow
  • Loading branch information
lionel-faber committed Feb 3, 2021
1 parent 330ddd2 commit 5ea6745
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl QuicP2p {
/// config.local_port = Some(3000);
/// let mut quic_p2p = QuicP2p::with_config(Some(config.clone()), Default::default(), true)?;
/// let (mut endpoint, _, _, _) = quic_p2p.new_endpoint().await?;
/// let peer_addr = endpoint.socket_addr().await?;
/// let peer_addr = endpoint.socket_addr();
///
/// config.local_port = Some(3001);
/// let mut quic_p2p = QuicP2p::with_config(Some(config), &[peer_addr], true)?;
Expand Down
8 changes: 4 additions & 4 deletions src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,11 @@ mod tests {
)?;

// Create Endpoint
let (mut peer1, mut peer1_connections, _, _) = qp2p.new_endpoint().await?;
let peer1_addr = peer1.socket_addr().await?;
let (peer1, mut peer1_connections, _, _) = qp2p.new_endpoint().await?;
let peer1_addr = peer1.socket_addr();

let (mut peer2, _, _, _) = qp2p.new_endpoint().await?;
let peer2_addr = peer2.socket_addr().await?;
let (peer2, _, _, _) = qp2p.new_endpoint().await?;
let peer2_addr = peer2.socket_addr();

peer2.connect_to(&peer1_addr).await?;

Expand Down
14 changes: 6 additions & 8 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Endpoint {
let (connection_tx, connection_rx) = mpsc::unbounded_channel();
let (disconnection_tx, disconnection_rx) = mpsc::unbounded_channel();

let endpoint = Self {
let mut endpoint = Self {
local_addr,
public_addr,
quic_endpoint,
Expand Down Expand Up @@ -157,6 +157,8 @@ impl Endpoint {
} else {
warn!("Public IP address not verified since bootstrap contacts are empty");
}
} else {
endpoint.public_addr = Some(endpoint.fetch_public_address().await?);
}
listen_for_incoming_connections(
quic_incoming,
Expand All @@ -179,12 +181,8 @@ impl Endpoint {
}

/// Returns the socket address of the endpoint
pub async fn socket_addr(&mut self) -> Result<SocketAddr> {
if cfg!(test) || !self.qp2p_config.forward_port {
Ok(self.local_addr())
} else {
self.public_addr().await
}
pub fn socket_addr(&self) -> SocketAddr {
self.public_addr.unwrap_or(self.local_addr)
}

/// Get our connection adddress to give to others for them to connect to us.
Expand All @@ -194,7 +192,7 @@ impl Endpoint {
/// simply build our connection info by querying the underlying bound socket for our address.
/// Note that if such an obtained address is of unspecified category we will ignore that as
/// such an address cannot be reached and hence not useful.
pub async fn public_addr(&mut self) -> Result<SocketAddr> {
async fn fetch_public_address(&mut self) -> Result<SocketAddr> {
// Skip port forwarding
if self.local_addr.ip().is_loopback() {
return Ok(self.local_addr);
Expand Down
58 changes: 27 additions & 31 deletions src/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ async fn successful_connection() -> Result<()> {
utils::init_logging();

let qp2p = new_qp2p()?;
let (mut peer1, mut peer1_incoming_connections, _, _) = qp2p.new_endpoint().await?;
let peer1_addr = peer1.socket_addr().await?;
let (peer1, mut peer1_incoming_connections, _, _) = qp2p.new_endpoint().await?;
let peer1_addr = peer1.socket_addr();

let (mut peer2, _, _, _) = qp2p.new_endpoint().await?;
let (peer2, _, _, _) = qp2p.new_endpoint().await?;
peer2.connect_to(&peer1_addr).await?;
let peer2_addr = peer2.socket_addr().await?;
let peer2_addr = peer2.socket_addr();

if let Some(connecting_peer) = peer1_incoming_connections.next().await {
assert_eq!(connecting_peer, peer2_addr);
Expand All @@ -31,12 +31,12 @@ async fn single_message() -> Result<()> {
utils::init_logging();

let qp2p = new_qp2p()?;
let (mut peer1, mut peer1_incoming_connections, mut peer1_incoming_messages, _) =
let (peer1, mut peer1_incoming_connections, mut peer1_incoming_messages, _) =
qp2p.new_endpoint().await?;
let peer1_addr = peer1.socket_addr().await?;
let peer1_addr = peer1.socket_addr();

let (mut peer2, _, _, _) = qp2p.new_endpoint().await?;
let peer2_addr = peer2.socket_addr().await?;
let (peer2, _, _, _) = qp2p.new_endpoint().await?;
let peer2_addr = peer2.socket_addr();

// Peer 2 connects and sends a message
peer2.connect_to(&peer1_addr).await?;
Expand Down Expand Up @@ -67,12 +67,12 @@ async fn reuse_outgoing_connection() -> Result<()> {
utils::init_logging();

let qp2p = new_qp2p()?;
let (mut alice, _, _, _) = qp2p.new_endpoint().await?;
let alice_addr = alice.socket_addr().await?;
let (alice, _, _, _) = qp2p.new_endpoint().await?;
let alice_addr = alice.socket_addr();

let (mut bob, mut bob_incoming_connections, mut bob_incoming_messages, _) =
let (bob, mut bob_incoming_connections, mut bob_incoming_messages, _) =
qp2p.new_endpoint().await?;
let bob_addr = bob.socket_addr().await?;
let bob_addr = bob.socket_addr();

// Connect for the first time and send a message.
alice.connect_to(&bob_addr).await?;
Expand Down Expand Up @@ -119,13 +119,13 @@ async fn reuse_incoming_connection() -> Result<()> {
utils::init_logging();

let qp2p = new_qp2p()?;
let (mut alice, mut alice_incoming_connections, mut alice_incoming_messages, _) =
let (alice, mut alice_incoming_connections, mut alice_incoming_messages, _) =
qp2p.new_endpoint().await?;
let alice_addr = alice.socket_addr().await?;
let alice_addr = alice.socket_addr();

let (mut bob, mut bob_incoming_connections, mut bob_incoming_messages, _) =
let (bob, mut bob_incoming_connections, mut bob_incoming_messages, _) =
qp2p.new_endpoint().await?;
let bob_addr = bob.socket_addr().await?;
let bob_addr = bob.socket_addr();

// Connect for the first time and send a message.
alice.connect_to(&bob_addr).await?;
Expand Down Expand Up @@ -179,11 +179,11 @@ async fn disconnection() -> Result<()> {
_alice_incoming_messages,
mut alice_disconnections,
) = qp2p.new_endpoint().await?;
let alice_addr = alice.socket_addr().await?;
let alice_addr = alice.socket_addr();

let (mut bob, mut bob_incoming_connections, _bob_incoming_messages, mut bob_disconnections) =
let (bob, mut bob_incoming_connections, _bob_incoming_messages, mut bob_disconnections) =
qp2p.new_endpoint().await?;
let bob_addr = bob.socket_addr().await?;
let bob_addr = bob.socket_addr();

// Alice connects to Bob who should receive an incoming connection.
alice.connect_to(&bob_addr).await?;
Expand Down Expand Up @@ -231,16 +231,16 @@ async fn simultaneous_incoming_and_outgoing_connections() -> Result<()> {

let qp2p = new_qp2p()?;
let (
mut alice,
alice,
mut alice_incoming_connections,
mut alice_incoming_messages,
mut alice_disconnections,
) = qp2p.new_endpoint().await?;
let alice_addr = alice.socket_addr().await?;
let alice_addr = alice.socket_addr();

let (mut bob, mut bob_incoming_connections, mut bob_incoming_messages, _bob_disconnections) =
qp2p.new_endpoint().await?;
let bob_addr = bob.socket_addr().await?;
let bob_addr = bob.socket_addr();

future::try_join(alice.connect_to(&bob_addr), bob.connect_to(&alice_addr)).await?;

Expand Down Expand Up @@ -312,17 +312,13 @@ async fn multiple_concurrent_connects_to_the_same_peer() -> Result<()> {
utils::init_logging();

let qp2p = new_qp2p()?;
let (
mut alice,
mut alice_incoming_connections,
mut alice_incoming_messages,
_alice_disconnections,
) = qp2p.new_endpoint().await?;
let alice_addr = alice.socket_addr().await?;
let (alice, mut alice_incoming_connections, mut alice_incoming_messages, _alice_disconnections) =
qp2p.new_endpoint().await?;
let alice_addr = alice.socket_addr();

let (mut bob, _bob_incoming_connections, mut bob_incoming_messages, _bob_disconnections) =
let (bob, _bob_incoming_connections, mut bob_incoming_messages, _bob_disconnections) =
qp2p.new_endpoint().await?;
let bob_addr = bob.socket_addr().await?;
let bob_addr = bob.socket_addr();

// Try to establish two connections to the same peer at the same time.
let ((), ()) =
Expand Down

0 comments on commit 5ea6745

Please sign in to comment.