crate::ix!();
pub struct BanMan {
pub cs_banned: ReentrantMutex<BanManInner>,
pub client_interface: Amo<ClientUIInterface>, pub ban_db: BanDB,
pub default_ban_time: OffsetDateTime,
}
impl Drop for BanMan {
fn drop(&mut self) {
self.dump_banlist();
}
}
fn translated(x: &str) -> String {
x.to_string()
}
impl BanMan {
pub fn new(
ban_file: Box<Path>,
client_interface: Amo<ClientUIInterface>,
default_ban_time: OffsetDateTime) -> Self {
let mut x = Self {
client_interface: client_interface,
ban_db: BanDB::new(ban_file.to_path_buf()),
default_ban_time: default_ban_time,
cs_banned: ReentrantMutex::new(BanManInner::default()),
};
if x.client_interface.is_some() {
x.client_interface
.get_mut()
.init_message(&translated("Loading banlist…"));
}
let n_start = Instant::now();
if x.ban_db.read(&mut x.cs_banned.get_mut().banned) {
x.sweep_banned();
log_print!(
bc_log::net,
"Loaded %d banned node addresses/subnets %dms\n",
x.cs_banned.lock().banned.len(),
Instant::now() - n_start
);
} else {
log_printf!("Recreating the banlist database\n");
x.cs_banned.get_mut().banned = Default::default();
x.cs_banned.get_mut().is_dirty = true;
}
x.dump_banlist();
x
}
pub fn sweep_banned(&mut self) {
let notify_ui = self.cs_banned.get_mut().do_sweep_banned();
self.notify_ui_on_sweep(notify_ui);
}
pub fn get_banned(&mut self, banmap: &mut BanMap) {
let notify_ui = self.cs_banned.get_mut().do_get_banned(banmap);
self.notify_ui_on_sweep(notify_ui);
}
pub fn notify_ui_on_sweep(&mut self, notify_ui: bool) {
if notify_ui && self.client_interface.is_some() {
self.client_interface
.get_mut()
.banned_list_changed();
}
}
}