diff options
-rw-r--r-- | examples/fullmesh.rs | 15 | ||||
-rw-r--r-- | src/peering/fullmesh.rs | 20 |
2 files changed, 30 insertions, 5 deletions
diff --git a/examples/fullmesh.rs b/examples/fullmesh.rs index f5b307b..afc4deb 100644 --- a/examples/fullmesh.rs +++ b/examples/fullmesh.rs @@ -1,4 +1,5 @@ use std::io::Write; +use std::net::SocketAddr; use log::info; @@ -65,6 +66,11 @@ async fn main() { info!("Node private key: {}", hex::encode(&privkey)); info!("Node public key: {}", hex::encode(&privkey.public_key())); + let public_addr = opt.public_addr.map(|x| x.parse().unwrap()); + let listen_addr: SocketAddr = opt.listen_addr.parse().unwrap(); + info!("Node public address: {:?}", public_addr); + info!("Node listen address: {}", listen_addr); + let netapp = NetApp::new(netid.clone(), privkey.clone()); let mut bootstrap_peers = vec![]; @@ -72,9 +78,11 @@ async fn main() { bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address")); } - let peering = FullMeshPeeringStrategy::new(netapp.clone(), bootstrap_peers); - - let listen_addr = opt.listen_addr.parse().unwrap(); + let peering = FullMeshPeeringStrategy::new( + netapp.clone(), + bootstrap_peers, + public_addr.map(|a| SocketAddr::new(a, listen_addr.port())), + ); info!("Add more peers to this mesh by running: fullmesh -n {} -l 127.0.0.1:$((1000 + $RANDOM)) -b {}@{}", hex::encode(&netid), @@ -83,7 +91,6 @@ async fn main() { let watch_cancel = netapp::util::watch_ctrl_c(); - let public_addr = opt.public_addr.map(|x| x.parse().unwrap()); tokio::join!( netapp.listen(listen_addr, public_addr, watch_cancel.clone()), peering.run(watch_cancel), diff --git a/src/peering/fullmesh.rs b/src/peering/fullmesh.rs index 5f17718..22657dd 100644 --- a/src/peering/fullmesh.rs +++ b/src/peering/fullmesh.rs @@ -163,7 +163,11 @@ impl FullMeshPeeringStrategy { /// The strategy will not be run until `.run()` is called and awaited. /// Once that happens, the peering strategy will try to connect /// to all of the nodes specified in the bootstrap list. - pub fn new(netapp: Arc<NetApp>, bootstrap_list: Vec<(NodeID, SocketAddr)>) -> Arc<Self> { + pub fn new( + netapp: Arc<NetApp>, + bootstrap_list: Vec<(NodeID, SocketAddr)>, + our_addr: Option<SocketAddr>, + ) -> Arc<Self> { let mut known_hosts = KnownHosts::new(); for (id, addr) in bootstrap_list { if id != netapp.id { @@ -179,6 +183,18 @@ impl FullMeshPeeringStrategy { } } + if let Some(addr) = our_addr { + known_hosts.list.insert( + netapp.id, + PeerInfoInternal { + addr, + state: PeerConnState::Ourself, + last_seen: None, + ping: VecDeque::new(), + }, + ); + } + let strat = Arc::new(Self { netapp: netapp.clone(), known_hosts: RwLock::new(known_hosts), @@ -188,6 +204,8 @@ impl FullMeshPeeringStrategy { peer_list_endpoint: netapp.endpoint("__netapp/peering/fullmesh.rs/PeerList".into()), }); + strat.update_public_peer_list(&strat.known_hosts.read().unwrap()); + strat.ping_endpoint.set_handler(strat.clone()); strat.peer_list_endpoint.set_handler(strat.clone()); |