use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use crate::custom_traits::IMacVendorCache;
type Vendor = String;
type MacAddress = String;
#[derive(Clone)]
pub struct MacVendorCache {
cache: Arc<Mutex<HashMap<MacAddress, Vendor>>>,
}
impl IMacVendorCache for MacVendorCache {
fn new() -> Self {
MacVendorCache {
cache: Arc::new(Mutex::new(HashMap::new())),
}
}
fn add(&self, mac_addr: MacAddress, vendor_name: Vendor) {
let mut cache = self.cache.lock().unwrap();
cache.insert(mac_addr, vendor_name);
}
fn contains(&self, mac_addr: &MacAddress) -> bool {
let cache = self.cache.lock().unwrap();
cache.contains_key(mac_addr)
}
fn get(&self, mac_addr: &MacAddress) -> String {
let cache = self.cache.lock().unwrap();
cache
.get(mac_addr)
.unwrap_or(&"Unknown".to_string())
.to_owned()
}
fn length(&self) -> usize {
self.cache.lock().unwrap().len()
}
}