mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-16 14:47:25 +08:00
Introduce foreigner network (#25)
* support network identity for instance * introduce foreign network foreign network allow a node serving as one public node. other nodes can connect to this node to discover peers and exchange route info.
This commit is contained in:
@@ -51,6 +51,10 @@ impl PeerMap {
|
||||
self.peer_map.get(peer_id).map(|v| v.clone())
|
||||
}
|
||||
|
||||
pub fn has_peer(&self, peer_id: &PeerId) -> bool {
|
||||
self.peer_map.contains_key(peer_id)
|
||||
}
|
||||
|
||||
pub async fn send_msg_directly(
|
||||
&self,
|
||||
msg: Bytes,
|
||||
@@ -97,6 +101,10 @@ impl PeerMap {
|
||||
}
|
||||
}
|
||||
|
||||
if gateway_peer_id.is_none() && self.has_peer(dst_peer_id) {
|
||||
gateway_peer_id = Some(*dst_peer_id);
|
||||
}
|
||||
|
||||
let Some(gateway_peer_id) = gateway_peer_id else {
|
||||
log::error!("no gateway for dst_peer_id: {}", dst_peer_id);
|
||||
return Ok(());
|
||||
@@ -117,6 +125,10 @@ impl PeerMap {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.peer_map.is_empty()
|
||||
}
|
||||
|
||||
pub async fn list_peers(&self) -> Vec<PeerId> {
|
||||
let mut ret = Vec::new();
|
||||
for item in self.peer_map.iter() {
|
||||
@@ -175,4 +187,19 @@ impl PeerMap {
|
||||
let mut routes = self.routes.write().await;
|
||||
routes.insert(0, route);
|
||||
}
|
||||
|
||||
pub async fn clean_peer_without_conn(&self) {
|
||||
let mut to_remove = vec![];
|
||||
|
||||
for peer_id in self.list_peers().await {
|
||||
let conns = self.list_peer_conns(&peer_id).await;
|
||||
if conns.is_none() || conns.as_ref().unwrap().is_empty() {
|
||||
to_remove.push(peer_id);
|
||||
}
|
||||
}
|
||||
|
||||
for peer_id in to_remove {
|
||||
self.close_peer(&peer_id).await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user