Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #41 from maufl/update-hyper
Browse files Browse the repository at this point in the history
Update tokio and hyper dependencies
  • Loading branch information
sbstp committed Jan 17, 2020
2 parents b64094a + 0596dcc commit b4b8117
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 266 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "igd"
version = "0.9.1"
edition = "2018"
authors = ["Simon Bernier St-Pierre <sbernierstpierre@gmail.com>"]
description = "Internet Gateway Protocol client"
homepage = "https://github.com/sbstp/rust-igd"
Expand All @@ -18,15 +19,14 @@ xmltree = "0.8"
rand = "0.4"
attohttpc = { version = "0.4", default-features = false }
url = "1"
futures = { version = "0.1", optional = true }
tokio = { version = "0.1", optional = true }
tokio-retry = { version = "0.2", optional = true }
futures = { version = "0.3", optional = true }
tokio = { version = "0.2", optional = true , features = [ "udp", "macros" ]}
log = { version = "0.4", optional = true }
bytes = { version = "0.4", optional = true }
http = {version = "0.1", optional = true }
http = {version = "0.2", optional = true }

[dependencies.hyper]
version = "0.12"
version = "0.13"
default-features = false
features = [ "runtime" ]
optional = true
Expand All @@ -36,7 +36,7 @@ simplelog = "0.5"

[features]
default = []
aio = ["futures", "tokio", "tokio-retry", "hyper", "bytes", "log", "http"]
aio = ["futures", "tokio", "hyper", "bytes", "log", "http"]

[[example]]
name = "add_any_port"
Expand Down
85 changes: 28 additions & 57 deletions examples/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,51 @@
//! If everything works fine, 2 port mappings are added, 1 removed and we're left with single
//! port mapping: External 1234 ---> 4321 Internal

extern crate igd;
extern crate futures;
extern crate tokio;
extern crate simplelog;

use std::env;
use std::net::SocketAddrV4;

use igd::aio::{search_gateway};
use igd::PortMappingProtocol;
use futures::future::Future;
use simplelog::{SimpleLogger, LevelFilter, Config as LogConfig};

fn main() {
#[tokio::main]
async fn main() {
let ip = match env::args().nth(1) {
Some(ip) => ip,
None => {
println!("Local socket address is missing!");
println!("This example requires a socket address representing the local machine and the port to bind to as an argument");
println!("Example: target/debug/examples/async 192.168.0.198:4321");
println!("Example: cargo run --features async --example async -- 192.168.0.198:4321");
println!("Example: target/debug/examples/io 192.168.0.198:4321");
println!("Example: cargo run --features aio --example aio -- 192.168.0.198:4321");
return;
}
};
let ip: SocketAddrV4 = ip.parse().expect("Invalid socket address");

let _ = SimpleLogger::init(LevelFilter::Debug, LogConfig::default());

let f = futures::lazy(move || {
search_gateway(Default::default())
.map_err(|e| panic!("Failed to find IGD: {}", e))
.and_then(move |gateway| gateway.get_external_ip()
.map_err(|e| panic!("Failed to get external IP: {}", e))
.and_then(|ip| Ok((gateway, ip)))
)
.and_then(|(gateway, pub_ip)| {
println!("Our public IP: {}", pub_ip);
Ok(gateway)
})
.and_then(move |gateway| {
gateway.add_port(
PortMappingProtocol::TCP,
1234,
ip,
120,
"rust-igd-async-example",
)
.map_err(|e| panic!("Failed to add port mapping: {}", e))
.and_then(|_| {
println!("New port mapping was successfully added.");
Ok(gateway)
})
})
.and_then(move |gateway| {
gateway.add_port(
PortMappingProtocol::TCP,
2345,
ip,
120,
"rust-igd-async-example",
)
.map_err(|e| panic!("Failed to add port mapping: {}", e))
.and_then(|_| {
println!("New port mapping was successfully added.");
Ok(gateway)
})
})
.and_then(|gateway| gateway.remove_port(PortMappingProtocol::TCP, 2345))
.and_then(|_| {
println!("Port was removed.");
Ok(())
})

}).map(|_| () ).map_err(|_| () );

tokio::run(f);
let gateway = match search_gateway(Default::default()).await {
Ok(g) => g,
Err(err) => return println!("Faild to find IGD: {}", err)
};
let pub_ip = match gateway.get_external_ip().await {
Ok(ip) => ip,
Err(err) => return println!("Failed to get external IP: {}", err)
};
println!("Our public IP is {}", pub_ip);
if let Err(e) = gateway.add_port(PortMappingProtocol::TCP, 1234, ip, 120, "rust-igd-async-example").await {
println!("Failed to add port mapping: {}", e);
}
println!("New port mapping was successfully added.");

if let Err(e) = gateway.add_port(PortMappingProtocol::TCP, 2345, ip, 120, "rust-igd-async-example").await {
println!("Failed to add port mapping: {}", e);
}
println!("New port mapping was successfully added.");

if gateway.remove_port(PortMappingProtocol::TCP, 2345).await.is_err() {
println!("Port mapping was not successfully removed");
} else {
println!("Port was removed.");
}
}
Loading

0 comments on commit b4b8117

Please sign in to comment.