2020-03-10 00:07:36 +00:00
|
|
|
use lazy_static::*;
|
2020-03-22 13:47:21 +00:00
|
|
|
use std::{
|
|
|
|
net::SocketAddr,
|
2020-07-09 07:58:21 +00:00
|
|
|
sync::atomic::{AtomicU16, Ordering},
|
2020-03-22 13:47:21 +00:00
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
2020-03-10 00:07:36 +00:00
|
|
|
use tracing::*;
|
|
|
|
use tracing_subscriber::EnvFilter;
|
2020-07-09 11:42:38 +00:00
|
|
|
use veloren_network::{Network, Participant, Pid, ProtocolAddr, Stream, PROMISES_NONE};
|
2020-03-10 00:07:36 +00:00
|
|
|
|
2020-05-27 15:58:57 +00:00
|
|
|
#[allow(dead_code)]
|
2020-03-22 13:47:21 +00:00
|
|
|
pub fn setup(tracing: bool, mut sleep: u64) -> (u64, u64) {
|
2020-03-10 00:07:36 +00:00
|
|
|
if tracing {
|
|
|
|
sleep += 1000
|
|
|
|
}
|
|
|
|
if sleep > 0 {
|
|
|
|
thread::sleep(Duration::from_millis(sleep));
|
|
|
|
}
|
|
|
|
|
|
|
|
let _subscriber = if tracing {
|
|
|
|
let filter = EnvFilter::from_default_env()
|
|
|
|
.add_directive("trace".parse().unwrap())
|
2020-04-08 14:26:42 +00:00
|
|
|
.add_directive("async_std::task::block_on=warn".parse().unwrap())
|
2020-03-10 00:07:36 +00:00
|
|
|
.add_directive("veloren_network::tests=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::controller=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::channel=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::message=trace".parse().unwrap())
|
|
|
|
.add_directive("veloren_network::metrics=trace".parse().unwrap())
|
2020-04-08 14:26:42 +00:00
|
|
|
.add_directive("veloren_network::types=trace".parse().unwrap());
|
2020-03-10 00:07:36 +00:00
|
|
|
|
|
|
|
Some(
|
|
|
|
tracing_subscriber::FmtSubscriber::builder()
|
|
|
|
// all spans/events with a level higher than TRACE (e.g, info, warn, etc.)
|
|
|
|
// will be written to stdout.
|
|
|
|
.with_max_level(Level::TRACE)
|
|
|
|
.with_env_filter(filter)
|
|
|
|
// sets this to be the default, global subscriber for this application.
|
|
|
|
.try_init(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-03-22 13:47:21 +00:00
|
|
|
(0, 0)
|
|
|
|
}
|
|
|
|
|
2020-05-27 15:58:57 +00:00
|
|
|
#[allow(dead_code)]
|
2020-04-08 14:26:42 +00:00
|
|
|
pub async fn network_participant_stream(
|
2020-07-09 11:42:38 +00:00
|
|
|
addr: ProtocolAddr,
|
2020-07-09 07:58:21 +00:00
|
|
|
) -> (Network, Participant, Stream, Network, Participant, Stream) {
|
2020-07-14 23:34:41 +00:00
|
|
|
let (n_a, f_a) = Network::new(Pid::fake(1));
|
2020-06-08 09:47:39 +00:00
|
|
|
std::thread::spawn(f_a);
|
2020-07-14 23:34:41 +00:00
|
|
|
let (n_b, f_b) = Network::new(Pid::fake(2));
|
2020-06-08 09:47:39 +00:00
|
|
|
std::thread::spawn(f_b);
|
2020-04-08 14:26:42 +00:00
|
|
|
|
|
|
|
n_a.listen(addr.clone()).await.unwrap();
|
|
|
|
let p1_b = n_b.connect(addr).await.unwrap();
|
|
|
|
let p1_a = n_a.connected().await.unwrap();
|
|
|
|
|
|
|
|
let s1_a = p1_a.open(10, PROMISES_NONE).await.unwrap();
|
|
|
|
let s1_b = p1_b.opened().await.unwrap();
|
|
|
|
|
|
|
|
(n_a, p1_a, s1_a, n_b, p1_b, s1_b)
|
|
|
|
}
|
|
|
|
|
2020-05-27 15:58:57 +00:00
|
|
|
#[allow(dead_code)]
|
2020-07-09 11:42:38 +00:00
|
|
|
pub fn tcp() -> veloren_network::ProtocolAddr {
|
2020-03-22 13:47:21 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref PORTS: AtomicU16 = AtomicU16::new(5000);
|
|
|
|
}
|
|
|
|
let port = PORTS.fetch_add(1, Ordering::Relaxed);
|
2020-07-09 11:42:38 +00:00
|
|
|
veloren_network::ProtocolAddr::Tcp(SocketAddr::from(([127, 0, 0, 1], port)))
|
2020-03-10 00:07:36 +00:00
|
|
|
}
|
2020-04-08 14:26:42 +00:00
|
|
|
|
2020-05-27 15:58:57 +00:00
|
|
|
#[allow(dead_code)]
|
2020-07-09 11:42:38 +00:00
|
|
|
pub fn udp() -> veloren_network::ProtocolAddr {
|
2020-04-08 14:26:42 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref PORTS: AtomicU16 = AtomicU16::new(5000);
|
|
|
|
}
|
|
|
|
let port = PORTS.fetch_add(1, Ordering::Relaxed);
|
2020-07-09 11:42:38 +00:00
|
|
|
veloren_network::ProtocolAddr::Udp(SocketAddr::from(([127, 0, 0, 1], port)))
|
2020-04-08 14:26:42 +00:00
|
|
|
}
|