reannounce persisted data

This commit is contained in:
Alex 2023-03-09 16:58:16 +01:00
parent c96aec520d
commit 65e979de80

View file

@ -192,13 +192,6 @@ impl Daemon {
let socket = UdpSocket::bind(SocketAddr::new("0.0.0.0".parse()?, config.gossip_port))?;
socket.set_broadcast(true)?;
let gossip = config
.persist_file
.as_ref()
.and_then(|x| std::fs::read(x).ok())
.and_then(|x| bincode::deserialize(&x).ok())
.unwrap_or_default();
Ok(Daemon {
config,
gossip_key,
@ -207,7 +200,7 @@ impl Daemon {
socket,
state: Mutex::new(State {
peers: HashMap::new(),
gossip,
gossip: HashMap::new(),
}),
})
}
@ -217,6 +210,10 @@ impl Daemon {
error!("Error while initializing Wireguard configuration: {}", e);
}
if let Err(e) = self.load_persisted_data() {
error!("Error while loading/reannouncing persisted data: {}", e);
}
let request = self.make_packet(&Gossip::Request)?;
for peer in self.config.peers.iter() {
let addr = SocketAddr::new(peer.address, self.config.gossip_port);
@ -242,6 +239,17 @@ impl Daemon {
Ok(())
}
fn load_persisted_data(&self) -> Result<()> {
if let Some(file) = &self.config.persist_file {
let mut state = self.state.lock().unwrap();
let persisted: HashMap<_, _> = bincode::deserialize(&std::fs::read(file)?)?;
for (pubkey, addrs) in persisted.into_iter() {
state.handle_announce(self, pubkey, addrs)?;
}
}
Ok(())
}
fn wg_loop(&self) -> ! {
let mut i = 0;
loop {