Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Remove time crate
Browse files Browse the repository at this point in the history
  • Loading branch information
canndrew committed Apr 15, 2016
1 parent b247c43 commit 2b282a5
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 89 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ rand = "~0.3.14"
rustc-serialize = "~0.3.18"
socket_addr = "~0.1.0"
sodiumoxide = "~0.0.9"
time = "~0.1.34"
void = "1.0.1"
w_result = "~0.1.1"
byteorder = "~0.5.0"
Expand Down
5 changes: 3 additions & 2 deletions examples/simple-tcp-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@

extern crate nat_traversal;
extern crate w_result;
extern crate time;

use std::time::{Instant, Duration};

use nat_traversal::{MappingContext, SimpleTcpHolePunchServer};
use w_result::{WOk, WErr};
Expand All @@ -61,7 +62,7 @@ fn main() {
};

// Now we create the server.
let deadline = time::SteadyTime::now() + time::Duration::seconds(3);
let deadline = Instant::now() + Duration::from_secs(3);
let simple_server = match SimpleTcpHolePunchServer::new(Box::new(mapping_context), deadline) {
WOk(simple_server, warnings) => {
for warning in warnings {
Expand Down
5 changes: 3 additions & 2 deletions examples/simple-udp-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@

extern crate nat_traversal;
extern crate w_result;
extern crate time;

use std::time::{Instant, Duration};

use nat_traversal::{MappingContext, SimpleUdpHolePunchServer};
use w_result::{WOk, WErr};
Expand All @@ -61,7 +62,7 @@ fn main() {
};

// Now we create the server.
let deadline = time::SteadyTime::now() + time::Duration::seconds(3);
let deadline = Instant::now() + Duration::from_secs(3);
let simple_server = match SimpleUdpHolePunchServer::new(Box::new(mapping_context), deadline) {
WOk(simple_server, warnings) => {
for warning in warnings {
Expand Down
6 changes: 3 additions & 3 deletions examples/tcp-hole-punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ extern crate nat_traversal;
extern crate w_result;
extern crate rustc_serialize;
extern crate socket_addr;
extern crate time;

use std::net::ToSocketAddrs;
use std::io::{Read, Write};
use std::time::{Instant, Duration};

use socket_addr::SocketAddr;
use nat_traversal::{MappingContext, gen_rendezvous_info, MappedTcpSocket, tcp_punch_hole};
Expand Down Expand Up @@ -110,7 +110,7 @@ fn main() {
}

// Now we use our context to create a mapped tcp socket.
let deadline = time::SteadyTime::now() + time::Duration::seconds(5);
let deadline = Instant::now() + Duration::from_secs(5);
let mapped_socket = match MappedTcpSocket::new(&mapping_context, deadline) {
WOk(mapped_socket, warnings) => {
for warning in warnings {
Expand Down Expand Up @@ -175,7 +175,7 @@ fn main() {
// complete the connection.
let mut stream = match tcp_punch_hole(
socket, our_priv_info, their_pub_info,
time::SteadyTime::now() + time::Duration::seconds(5)
Instant::now() + Duration::from_secs(5)
) {
WOk(punched_socket, warnings) => {
for warning in warnings {
Expand Down
6 changes: 3 additions & 3 deletions examples/udp-hole-punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ extern crate nat_traversal;
extern crate w_result;
extern crate rustc_serialize;
extern crate socket_addr;
extern crate time;

use std::net::ToSocketAddrs;
use std::time::{Instant, Duration};

use socket_addr::SocketAddr;
use nat_traversal::{MappingContext, gen_rendezvous_info, MappedUdpSocket, PunchedUdpSocket};
Expand Down Expand Up @@ -111,7 +111,7 @@ fn main() {
// Now we use our context to create a mapped udp socket.
let mapped_socket = match MappedUdpSocket::new(
&mapping_context,
time::SteadyTime::now() + time::Duration::seconds(5)
Instant::now() + Duration::from_secs(5)
) {
WOk(mapped_socket, warnings) => {
for warning in warnings {
Expand Down Expand Up @@ -174,7 +174,7 @@ fn main() {

// Now we use the socket, our private rendezvous info and their public rendezvous info to
// complete the connection.
let deadline = time::SteadyTime::now() + time::Duration::seconds(5);
let deadline = Instant::now() + Duration::from_secs(5);
let punched_socket = match PunchedUdpSocket::punch_hole(socket, our_priv_info,
their_pub_info, deadline)
{
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ extern crate byteorder;
extern crate net2;
extern crate rand;
extern crate rustc_serialize;
extern crate time;
extern crate void;
#[macro_use]
extern crate maidsafe_utilities;
Expand Down
34 changes: 15 additions & 19 deletions src/mapped_tcp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::net;
use std::net::{IpAddr, Ipv4Addr, TcpStream};
use std::io;
use std::io::{Read, Write};
use std::time::Duration;
use std::time::{Instant, Duration};
use std::thread;
use std::str;
use std::sync::mpsc;
Expand All @@ -35,7 +35,6 @@ use net2;
use socket_addr::SocketAddr;
use w_result::{WResult, WErr, WOk};
use maidsafe_utilities::serialisation::{deserialise, SerialisationError};
use time::SteadyTime;
use rand::random;
use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian};

Expand All @@ -46,7 +45,6 @@ use rendezvous_info;
use socket_utils;
use mapping_context;
use listener_message;
use utils;
use utils::DisplaySlice;

/// A tcp socket for which we know our external endpoints.
Expand Down Expand Up @@ -272,7 +270,7 @@ pub fn new_reusably_bound_tcp_socket(local_addr: &net::SocketAddr) -> Result<net
impl MappedTcpSocket {
/// Map an existing tcp socket. The socket must bound but not connected. It must have been
/// bound with SO_REUSEADDR and SO_REUSEPORT options (or equivalent) set.
pub fn map(socket: net2::TcpBuilder, mc: &MappingContext, deadline: SteadyTime)
pub fn map(socket: net2::TcpBuilder, mc: &MappingContext, deadline: Instant)
-> WResult<MappedTcpSocket, MappedTcpSocketMapWarning, MappedTcpSocketMapError>
{
let mut endpoints = Vec::new();
Expand Down Expand Up @@ -450,10 +448,9 @@ impl MappedTcpSocket {
}));
}
let timeout_thread = thread!("MappedTcpSocket::map timeout", move || {
let now = SteadyTime::now();
let now = Instant::now();
if deadline > now {
let timeout = deadline - now;
let timeout = utils::time_duration_to_std_duration(timeout);
thread::park_timeout(timeout);
}
let _ = results_tx.send(None);
Expand Down Expand Up @@ -484,7 +481,7 @@ impl MappedTcpSocket {
}

/// Create a new `MappedTcpSocket`
pub fn new(mc: &MappingContext, deadline: SteadyTime)
pub fn new(mc: &MappingContext, deadline: Instant)
-> WResult<MappedTcpSocket, MappedTcpSocketMapWarning, MappedTcpSocketNewError>
{
let unspec_addr = net::SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
Expand Down Expand Up @@ -602,7 +599,7 @@ impl From<TcpPunchHoleError> for io::Error {
pub fn tcp_punch_hole(socket: net2::TcpBuilder,
our_priv_rendezvous_info: PrivRendezvousInfo,
their_pub_rendezvous_info: PubRendezvousInfo,
deadline: SteadyTime)
deadline: Instant)
-> WResult<TcpStream, TcpPunchHoleWarning, TcpPunchHoleError> {
// In order to do tcp hole punching we connect to all of their endpoints in parallel while
// simultaneously listening. All the sockets we use must be bound to the same local address. As
Expand Down Expand Up @@ -689,13 +686,12 @@ pub fn tcp_punch_hole(socket: net2::TcpBuilder,
Ok(stream)
};
loop {
let now = SteadyTime::now();
let now = Instant::now();
if now >= deadline || shutdown_clone.load(Ordering::SeqCst) {
break;
}
else {
let timeout = deadline - now;
let timeout = utils::time_duration_to_std_duration(timeout);
match f(timeout) {
Ok(stream) => {
let _ = results_tx_clone.send(Some(Ok((stream, addr))));
Expand Down Expand Up @@ -743,13 +739,12 @@ pub fn tcp_punch_hole(socket: net2::TcpBuilder,
// Spawn a new thread here to prevent someone from connecting then not sending any data
// and preventing us from accepting any more connections.
let results_tx_clone = results_tx_clone.clone();
let now = SteadyTime::now();
let now = Instant::now();
if now >= deadline {
break;
};
let _ = thread!("tcp_punch_hole listen handshake", move || {
let timeout = deadline - now;
let timeout = utils::time_duration_to_std_duration(timeout);
match stream.set_write_timeout(Some(timeout)) {
Ok(()) => (),
Err(e) => {
Expand Down Expand Up @@ -815,10 +810,11 @@ pub fn tcp_punch_hole(socket: net2::TcpBuilder,
// TODO(canndrew): We won't need to do this one this is fixed: https://github.com/rust-lang/rfcs/issues/962
let results_tx_clone = results_tx.clone();
let timeout_thread = thread!("tcp_punch_hole timeout", move || {
let now = SteadyTime::now();
let timeout = deadline - now;
let timeout = utils::time_duration_to_std_duration(timeout);
thread::park_timeout(timeout);
let now = Instant::now();
if deadline > now {
let timeout = deadline - now;
thread::park_timeout(timeout);
}
let _ = results_tx_clone.send(None);
});
let timeout_thread_handle = timeout_thread.thread();
Expand Down Expand Up @@ -956,14 +952,14 @@ mod test {
use super::*;

use std::io::{Read, Write};
use time;
use std::time::{Instant, Duration};

use mapping_context::MappingContext;
use rendezvous_info::gen_rendezvous_info;

#[test]
fn two_peers_tcp_hole_punch_over_loopback() {
let deadline = time::SteadyTime::now() + time::Duration::seconds(5);
let deadline = Instant::now() + Duration::from_secs(5);
let mapping_context = unwrap_result!(MappingContext::new().result_log());

let mapped_socket_0 = unwrap_result!(MappedTcpSocket::new(&mapping_context, deadline).result_log());
Expand All @@ -976,7 +972,7 @@ mod test {
let endpoints_1 = mapped_socket_1.endpoints;
let (priv_info_1, pub_info_1) = gen_rendezvous_info(endpoints_1);

let deadline = time::SteadyTime::now() + time::Duration::seconds(5);
let deadline = Instant::now() + Duration::from_secs(5);
let thread_0 = thread!("two_peers_tcp_hole_punch_over_loopback_0", move || {
let mut stream = unwrap_result!(tcp_punch_hole(socket_0, priv_info_0, pub_info_1, deadline).result_log());
let mut data = [0u8; 4];
Expand Down
14 changes: 6 additions & 8 deletions src/mapped_udp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ use std::io;
use std::net::UdpSocket;
use std::net;
use std::net::IpAddr;
use std::time::Duration;
use std::time::{Instant, Duration};
use std::collections::HashSet;

use igd;
use time;
use maidsafe_utilities::serialisation::deserialise;
use socket_addr::SocketAddr;
use w_result::{WResult, WOk, WErr};
use time::SteadyTime;

use listener_message;
use mapping_context;
Expand Down Expand Up @@ -165,7 +163,7 @@ impl From<MappedUdpSocketNewError> for io::Error {

impl MappedUdpSocket {
/// Map an existing `UdpSocket`.
pub fn map(socket: UdpSocket, mc: &MappingContext, deadline: SteadyTime)
pub fn map(socket: UdpSocket, mc: &MappingContext, deadline: Instant)
-> WResult<MappedUdpSocket, MappedUdpSocketMapWarning, MappedUdpSocketMapError>
{
let mut endpoints = Vec::new();
Expand Down Expand Up @@ -292,11 +290,11 @@ impl MappedUdpSocket {
.into_iter().collect();

// Ping all the simple servers and waiting for a response.
let start_time = SteadyTime::now();
let start_time = Instant::now();
let mut recv_deadline = start_time;
let mut deadline = deadline;
while recv_deadline < deadline && simple_servers.len() > 0 {
recv_deadline = recv_deadline + time::Duration::milliseconds(250);
recv_deadline = recv_deadline + Duration::from_millis(250);

// TODO(canndrew): We should limit the number of servers that we send to. If the user
// has added two thousand servers we really don't want to be pinging all of them. We
Expand Down Expand Up @@ -332,7 +330,7 @@ impl MappedUdpSocket {
// let is_global = recv_addr.is_global();
let is_global = false;
if is_global {
let now = SteadyTime::now();
let now = Instant::now();
if deadline > now {
deadline = now + (now - deadline) / 2;
}
Expand Down Expand Up @@ -360,7 +358,7 @@ impl MappedUdpSocket {
}

/// Create a new `MappedUdpSocket`
pub fn new(mc: &MappingContext, deadline: SteadyTime)
pub fn new(mc: &MappingContext, deadline: Instant)
-> WResult<MappedUdpSocket, MappedUdpSocketMapWarning, MappedUdpSocketNewError>
{
// Sometimes we might bind a socket to a random port then find that we have an IGD gateway
Expand Down
23 changes: 10 additions & 13 deletions src/punched_udp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
use maidsafe_utilities::serialisation::{deserialise, SerialisationError, serialise};
use std::io;
use std::net::UdpSocket;
use std;
use std::time::{Instant, Duration};
use std::thread;

use time;
use time::SteadyTime;
use socket_addr::SocketAddr;
use w_result::{WResult, WOk, WErr};

Expand Down Expand Up @@ -133,7 +131,7 @@ impl PunchedUdpSocket {
pub fn punch_hole(socket: UdpSocket,
our_priv_rendezvous_info: PrivRendezvousInfo,
their_pub_rendezvous_info: PubRendezvousInfo,
deadline: SteadyTime)
deadline: Instant)
-> WResult<PunchedUdpSocket, UdpPunchHoleWarning, UdpPunchHoleError>
{
let mut warnings = Vec::new();
Expand Down Expand Up @@ -207,11 +205,11 @@ impl PunchedUdpSocket {
// Spend TOTAL_TIMEOUT_MS trying to get their actual address that we can
// communicate with.

const DELAY_BETWEEN_RESENDS_MS: i64 = 600;
const DELAY_BETWEEN_RESENDS_MS: u64 = 600;

let mut recv_deadline = SteadyTime::now();
let mut recv_deadline = Instant::now();
while recv_deadline < deadline {
recv_deadline = recv_deadline + time::Duration::milliseconds(DELAY_BETWEEN_RESENDS_MS);
recv_deadline = recv_deadline + Duration::from_millis(DELAY_BETWEEN_RESENDS_MS);
let mut i = 0;
while i < endpoints.len() {
// TODO(canndrew): How should we handle partial write?
Expand Down Expand Up @@ -260,7 +258,7 @@ impl PunchedUdpSocket {
let mut attempts = 0;
let mut successful_attempts = 0;
let mut error = None;
while attempts < 2 || SteadyTime::now() < deadline {
while attempts < 2 || Instant::now() < deadline {
attempts += 1;
match socket.send_to(&send_data[..], &*addr) {
Ok(n) => {
Expand All @@ -277,7 +275,7 @@ impl PunchedUdpSocket {
}
}
};
thread::sleep(std::time::Duration::from_millis(100));
thread::sleep(Duration::from_millis(100));
}
if successful_attempts == 0 {
let ret = match error {
Expand Down Expand Up @@ -334,9 +332,8 @@ pub fn filter_udp_hole_punch_packet(data: &[u8]) -> Option<&[u8]> {
mod tests {
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use std::time::{Instant, Duration};
use rand;
use time;

use mapping_context::MappingContext;
use mapped_udp_socket::MappedUdpSocket;
Expand All @@ -345,7 +342,7 @@ mod tests {

#[test]
fn two_peers_udp_hole_punch_over_loopback() {
let deadline = time::SteadyTime::now() + time::Duration::seconds(3);
let deadline = Instant::now() + Duration::from_secs(3);
let mapping_context = unwrap_result!(MappingContext::new().result_discard());
let mapped_socket_0 = unwrap_result!(MappedUdpSocket::new(&mapping_context, deadline).result_discard());
let mapped_socket_1 = unwrap_result!(MappedUdpSocket::new(&mapping_context, deadline).result_discard());
Expand All @@ -358,7 +355,7 @@ mod tests {
let (tx_0, rx_0) = mpsc::channel();
let (tx_1, rx_1) = mpsc::channel();

let deadline = time::SteadyTime::now() + time::Duration::seconds(3);
let deadline = Instant::now() + Duration::from_secs(3);
let jh_0 = thread!("two_peers_hole_punch_over_loopback punch socket 0", move || {
let res = PunchedUdpSocket::punch_hole(socket_0,
priv_info_0,
Expand Down
Loading

0 comments on commit 2b282a5

Please sign in to comment.