diff --git a/src/engine/device_thread.rs b/src/engine/device_thread.rs index 4bfebe2..7938219 100644 --- a/src/engine/device_thread.rs +++ b/src/engine/device_thread.rs @@ -90,8 +90,8 @@ impl DeviceThread Ok(()), Ok(_) => Err(Error::Socket("Sent out truncated packet")), Err(e) => Err(Error::SocketIo("IOError when sending", e)), diff --git a/src/engine/shared.rs b/src/engine/shared.rs index 4b13b87..2e5d829 100644 --- a/src/engine/shared.rs +++ b/src/engine/shared.rs @@ -21,45 +21,53 @@ use super::common::PeerData; #[derive(Clone)] pub struct SharedPeerCrypto { peers: Arc>, Hash>>>, - //TODO: local hashmap as cache + cache: HashMap>, Hash>, //TODO: local hashmap as cache } impl SharedPeerCrypto { pub fn new() -> Self { - SharedPeerCrypto { peers: Arc::new(Mutex::new(HashMap::default())) } + SharedPeerCrypto { peers: Arc::new(Mutex::new(HashMap::default())), cache: HashMap::default() } } - pub fn encrypt_for(&self, peer: SocketAddr, data: &mut MsgBuffer) -> Result<(), Error> { - //TODO: use cache first - let mut peers = self.peers.lock(); - match peers.get_mut(&peer) { - None => Err(Error::InvalidCryptoState("No crypto found for peer")), - Some(None) => Ok(()), - Some(Some(crypto)) => { - crypto.encrypt(data); - Ok(()) + pub fn encrypt_for(&mut self, peer: SocketAddr, data: &mut MsgBuffer) -> Result<(), Error> { + let crypto = match self.cache.get(&peer) { + Some(crypto) => crypto, + None => { + let peers = self.peers.lock(); + if let Some(crypto) = peers.get(&peer) { + self.cache.insert(peer, crypto.clone()); + self.cache.get(&peer).unwrap() + } else { + return Err(Error::InvalidCryptoState("No crypto found for peer")); + } } + }; + if let Some(crypto) = crypto { + crypto.encrypt(data); } + Ok(()) } - pub fn store(&self, data: &HashMap) { - //TODO: store in shared and in cache + pub fn store(&mut self, data: &HashMap) { + self.cache.clear(); + self.cache.extend(data.iter().map(|(k, v)| (*k, v.crypto.get_core()))); let mut peers = self.peers.lock(); peers.clear(); - peers.extend(data.iter().map(|(k, v)| (*k, v.crypto.get_core()))); + peers.extend(self.cache.iter().map(|(k, v)| (*k, v.clone()))); } pub fn load(&mut self) { - // TODO sync if needed + let peers = self.peers.lock(); + self.cache.clear(); + self.cache.extend(peers.iter().map(|(k, v)| (*k, v.clone()))); } - pub fn get_snapshot(&self) -> HashMap>, Hash> { - //TODO: return local cache - self.peers.lock().clone() + pub fn get_snapshot(&mut self) -> &HashMap>, Hash> { + &self.cache } pub fn count(&self) -> usize { - self.peers.lock().len() + self.cache.len() } }