Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New and improved qp2p API #206

Merged
merged 20 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6777e94
feature(default ip) Use localhost as IP.
dirvine Dec 30, 2020
30cacc2
chore(fmt) cargo fmt
dirvine Dec 30, 2020
590c2a4
chore(refactor) Simplify some statements.
dirvine Dec 31, 2020
3c89560
fix(example): dont use LocalHost in example
lionel-faber Jan 5, 2021
083be66
fix(igd): don't skip port forwarding if local IP address is specified
lionel-faber Jan 5, 2021
f2d27b2
fix(echo_service): prevent contacting the echo service multiple times
lionel-faber Jan 6, 2021
fcf1ea2
refactor(example): refactor echo service example to use StructOpt and
lionel-faber Jan 6, 2021
c9acf77
feat(api): add support for manual port forwarding by passing additional
lionel-faber Jan 12, 2021
17a7c61
feat(api): move all connection handling and message handling internally
lionel-faber Feb 1, 2021
aa02c96
feat(disconnects): add API for disconnection and fix tests
lionel-faber Feb 1, 2021
de24107
fix(test): refactor structure of test code and fix echo_service test
lionel-faber Feb 2, 2021
4e6e40b
fix(all): remove FIFO queues and use the mpsc channels directly
lionel-faber Feb 2, 2021
3cf5aee
feat(api): add API to open bi-directional stream that can be used to
lionel-faber Feb 2, 2021
9b402f1
remove(example): remove echo_service example
lionel-faber Feb 2, 2021
7dcba12
feat(echo_service): find if peer is externally reachable if external IP
lionel-faber Feb 2, 2021
631a19e
feat(echo_service): perform UPnP and/or echo_service when the endpoint
lionel-faber Feb 3, 2021
db83038
refactor(api): return the socket address of the connected peer after
lionel-faber Feb 4, 2021
860ea5b
refactor(comms): prevent early exit of control flow when sending an
lionel-faber Feb 4, 2021
bd8d21d
feat: makes QuicP2p::Endpoint cloneable so that it can more easily be…
dan-da Feb 8, 2021
4e02196
feat: adds a p2p node example that uses bidirectional streams
dan-da Feb 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ webpki = "~0.21.3"

[dependencies.tokio]
version = "~0.2.24"
features = [ "rt-core", "sync", "time", "macros", "io-std", "io-util" ]
features = [ "rt-threaded", "sync", "time", "macros", "io-std", "io-util" ]

[dev-dependencies]
anyhow = "1.0.36"
assert_matches = "1.3"
flexi_logger = "~0.16.1"
anyhow = "1.0.36"
rand = "~0.7.3"

[target."cfg(any(all(unix, not(any(target_os = \"android\", target_os = \"androideabi\", target_os = \"ios\"))), windows))".dependencies]
Expand Down
105 changes: 0 additions & 105 deletions examples/echo_service.rs

This file was deleted.

73 changes: 73 additions & 0 deletions examples/p2p_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! This example demonstrates accepting connections and messages
//! on a socket/port and replying on the same socket/port using a
//! bidirectional stream.
//!
//! We implement a simple P2P node that listens for incoming messages
//! from an arbitrary number of peers. If a peer sends us "marco" we reply
//! with "polo".
//!
//! Our node accepts a list of SocketAddr for peers on the command-line.
//! Upon startup, we send "marco" to each peer in the list and print
//! the reply. If the list is empty, we don't send any message.
//!
//! We then proceed to listening for new connections/messages.

use anyhow::Result;
use bytes::Bytes;
use qp2p::{Config, QuicP2p};
use std::env;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

#[tokio::main]
async fn main() -> Result<()> {
const MSG_MARCO: &str = "marco";
const MSG_POLO: &str = "polo";

// collect cli args
let args: Vec<String> = env::args().collect();

// instantiate QuicP2p with custom config
let qp2p = QuicP2p::with_config(
Some(Config {
local_ip: Some(IpAddr::V4(Ipv4Addr::LOCALHOST)),
idle_timeout_msec: Some(1000 * 3600), // 1 hour idle timeout.
..Default::default()
}),
Default::default(),
true,
)?;

// create an endpoint for us to listen on and send from.
let (node, _incoming_conns, mut incoming_messages, _disconnections) =
qp2p.new_endpoint().await?;

// if we received args then we parse them as SocketAddr and send a "marco" msg to each peer.
if args.len() > 1 {
for arg in args.iter().skip(1) {
let peer: SocketAddr = arg
.parse()
.expect("Invalid SocketAddr. Use the form 127.0.0.1:1234");
let msg = Bytes::from(MSG_MARCO);
println!("Sending to {:?} --> {:?}\n", peer, msg);
node.connect_to(&peer).await?;
node.send_message(msg.clone(), &peer).await?;
}
}

println!("\n---");
println!("Listening on: {:?}", node.socket_addr());
println!("---\n");

// loop over incoming messages
while let Some((socket_addr, bytes)) = incoming_messages.next().await {
println!("Received from {:?} --> {:?}", socket_addr, bytes);
if bytes == Bytes::from(MSG_MARCO) {
let reply = Bytes::from(MSG_POLO);
node.send_message(reply.clone(), &socket_addr).await?;
println!("Replied to {:?} --> {:?}", socket_addr, reply);
}
println!();
}

Ok(())
}
Loading