-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: visualization service * fixed missing tokio features
- Loading branch information
Showing
8 changed files
with
197 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use std::{ | ||
collections::{HashMap, VecDeque}, | ||
time::Duration, | ||
}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use tokio::{select, time::Interval}; | ||
|
||
use crate::{now_ms, ErrorExt, PeerAddress}; | ||
|
||
use super::{P2pService, P2pServiceEvent}; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub enum VisualizationServiceEvent { | ||
PeerJoined(PeerAddress, Vec<(PeerAddress, u16)>), | ||
PeerUpdated(PeerAddress, Vec<(PeerAddress, u16)>), | ||
PeerLeaved(PeerAddress), | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
enum Message { | ||
Scan, | ||
Info(Vec<(PeerAddress, u16)>), | ||
} | ||
|
||
pub struct VisualizationService { | ||
service: P2pService, | ||
neighbours: HashMap<PeerAddress, u64>, | ||
ticker: Interval, | ||
collect_interval: Option<Duration>, | ||
collect_me: bool, | ||
outs: VecDeque<VisualizationServiceEvent>, | ||
} | ||
|
||
impl VisualizationService { | ||
pub fn new(collect_interval: Option<Duration>, collect_me: bool, service: P2pService) -> Self { | ||
let ticker = tokio::time::interval(collect_interval.unwrap_or(Duration::from_secs(100))); | ||
|
||
Self { | ||
ticker, | ||
collect_interval, | ||
collect_me, | ||
neighbours: HashMap::new(), | ||
outs: if collect_me { | ||
VecDeque::from([VisualizationServiceEvent::PeerJoined(service.router().local_address(), vec![])]) | ||
} else { | ||
VecDeque::new() | ||
}, | ||
service, | ||
} | ||
} | ||
|
||
pub async fn recv(&mut self) -> anyhow::Result<VisualizationServiceEvent> { | ||
loop { | ||
if let Some(out) = self.outs.pop_front() { | ||
return Ok(out); | ||
} | ||
|
||
select! { | ||
_ = self.ticker.tick() => { | ||
if let Some(interval) = self.collect_interval { | ||
if self.collect_me { | ||
// for update local node | ||
self.outs.push_back(VisualizationServiceEvent::PeerUpdated(self.service.router().local_address(), self.service.router().neighbours())); | ||
} | ||
|
||
let requester = self.service.requester(); | ||
tokio::spawn(async move { | ||
requester.send_broadcast(bincode::serialize(&Message::Scan).expect("should convert to buf")).await; | ||
}); | ||
|
||
let now = now_ms(); | ||
let mut timeout_peers = vec![]; | ||
for (peer, last_updated) in self.neighbours.iter() { | ||
if now >= *last_updated + interval.as_millis() as u64 * 2 { | ||
timeout_peers.push(*peer); | ||
self.outs.push_back(VisualizationServiceEvent::PeerLeaved(*peer)); | ||
} | ||
} | ||
|
||
for peer in timeout_peers { | ||
self.neighbours.remove(&peer); | ||
} | ||
} | ||
} | ||
event = self.service.recv() => match event.expect("should work") { | ||
P2pServiceEvent::Unicast(from, data) | P2pServiceEvent::Broadcast(from, data) => { | ||
if let Ok(msg) = bincode::deserialize::<Message>(&data) { | ||
match msg { | ||
Message::Scan => { | ||
let requester = self.service.requester(); | ||
let neighbours: Vec<(PeerAddress, u16)> = requester.router().neighbours(); | ||
tokio::spawn(async move { | ||
requester | ||
.send_unicast(from, bincode::serialize(&Message::Info(neighbours)).expect("should convert to buf")) | ||
.await | ||
.print_on_err("send neighbour info to visualization collector"); | ||
}); | ||
} | ||
Message::Info(neighbours) => { | ||
if self.neighbours.insert(from, now_ms()).is_none() { | ||
self.outs.push_back(VisualizationServiceEvent::PeerJoined(from, neighbours)); | ||
} else { | ||
self.outs.push_back(VisualizationServiceEvent::PeerUpdated(from, neighbours)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
P2pServiceEvent::Stream(..) => {} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::time::Duration; | ||
|
||
use test_log::test; | ||
|
||
use crate::visualization_service::{VisualizationService, VisualizationServiceEvent}; | ||
|
||
use super::create_random_node; | ||
|
||
#[test(tokio::test)] | ||
async fn discovery_new_node() { | ||
let (mut node1, addr1) = create_random_node(true).await; | ||
let mut service1 = VisualizationService::new(None, false, node1.create_service(0.into())); | ||
tokio::spawn(async move { while let Ok(_) = node1.recv().await {} }); | ||
tokio::spawn(async move { while let Ok(_) = service1.recv().await {} }); | ||
|
||
let (mut node2, addr2) = create_random_node(false).await; | ||
let mut service2 = VisualizationService::new(Some(Duration::from_secs(1)), false, node2.create_service(0.into())); | ||
let node2_requester = node2.requester(); | ||
tokio::spawn(async move { while let Ok(_) = node2.recv().await {} }); | ||
|
||
node2_requester.connect(addr1).await.expect("should connect success"); | ||
tokio::time::sleep(Duration::from_secs(1)).await; | ||
|
||
let mut events = vec![ | ||
tokio::time::timeout(Duration::from_secs(3), service2.recv()).await.unwrap().unwrap(), | ||
tokio::time::timeout(Duration::from_secs(3), service2.recv()).await.unwrap().unwrap(), | ||
]; | ||
|
||
for event in events.iter_mut() { | ||
match event { | ||
VisualizationServiceEvent::PeerJoined(_, neighbours) | VisualizationServiceEvent::PeerUpdated(_, neighbours) => { | ||
for (_, rtt) in neighbours.iter_mut() { | ||
*rtt = 0; | ||
} | ||
} | ||
VisualizationServiceEvent::PeerLeaved(_) => {} | ||
} | ||
} | ||
|
||
assert_eq!( | ||
events, | ||
vec![ | ||
VisualizationServiceEvent::PeerJoined(addr1, vec![(addr2, 0)]), | ||
VisualizationServiceEvent::PeerUpdated(addr1, vec![(addr2, 0)]), | ||
] | ||
); | ||
} |