use crate::connection::connection::{Connection};
use crate::inventory::hosts::Host;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;
use std::collections::HashMap;
pub struct ConnectionCache {
connections: HashMap<String, Arc<Mutex<dyn Connection>>>
}
impl ConnectionCache {
pub fn new() -> Self {
Self {
connections: HashMap::new()
}
}
pub fn add_connection(&mut self, host:&Arc<RwLock<Host>>, connection: &Arc<Mutex<dyn Connection>>) {
let host2 = host.read().expect("host read");
self.connections.insert(host2.name.clone(), Arc::clone(connection));
}
pub fn has_connection(&self, host: &Arc<RwLock<Host>>) -> bool {
let host2 = host.read().expect("host read");
return self.connections.contains_key(&host2.name.clone());
}
pub fn get_connection(&self, host: &Arc<RwLock<Host>>) -> Arc<Mutex<dyn Connection>> {
let host2 = host.read().expect("host read");
return Arc::clone(self.connections.get(&host2.name.clone()).unwrap());
}
pub fn clear(&mut self) {
self.connections.clear();
}
}