|
|
@ -1,24 +1,23 @@
|
|
|
|
|
|
|
|
use crate::data::node_data::NodeData;
|
|
|
|
|
|
|
|
use crate::modules::nodes_refresh::settings::NodesRefreshSettings;
|
|
|
|
use crate::modules::Module;
|
|
|
|
use crate::modules::Module;
|
|
|
|
use vented::result::VentedResult;
|
|
|
|
use crate::server::tick_context::TickContext;
|
|
|
|
use vented::server::VentedServer;
|
|
|
|
|
|
|
|
use crate::utils::result::SnekcloudResult;
|
|
|
|
use crate::utils::result::SnekcloudResult;
|
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
use crate::utils::settings::get_settings;
|
|
|
|
use vented::server::server_events::{NodeListPayload, NODE_LIST_REQUEST_EVENT};
|
|
|
|
|
|
|
|
use vented::server::data::Node;
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
use std::collections::{HashMap};
|
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use std::time::{Duration, Instant, UNIX_EPOCH};
|
|
|
|
use vented::crypto::PublicKey;
|
|
|
|
use vented::crypto::PublicKey;
|
|
|
|
use std::time::{Instant, Duration, UNIX_EPOCH};
|
|
|
|
|
|
|
|
use crate::utils::settings::get_settings;
|
|
|
|
|
|
|
|
use crate::data::node_data::NodeData;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use crate::modules::nodes_refresh::settings::NodesRefreshSettings;
|
|
|
|
|
|
|
|
use crate::server::tick_context::TickContext;
|
|
|
|
|
|
|
|
use vented::event::Event;
|
|
|
|
use vented::event::Event;
|
|
|
|
|
|
|
|
use vented::server::data::Node;
|
|
|
|
|
|
|
|
use vented::server::server_events::{NodeListPayload, NODE_LIST_REQUEST_EVENT};
|
|
|
|
|
|
|
|
use vented::server::VentedServer;
|
|
|
|
|
|
|
|
|
|
|
|
pub mod settings;
|
|
|
|
pub mod settings;
|
|
|
|
|
|
|
|
|
|
|
|
pub struct NodesRefreshModule {
|
|
|
|
pub struct NodesRefreshModule {
|
|
|
|
nodes: Arc<Mutex<HashMap<String, Node>>>,
|
|
|
|
nodes: Arc<Mutex<HashMap<String, Node>>>,
|
|
|
@ -28,12 +27,15 @@ pub struct NodesRefreshModule {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Module for NodesRefreshModule {
|
|
|
|
impl Module for NodesRefreshModule {
|
|
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> String {
|
|
|
|
fn name(&self) -> String {
|
|
|
|
"node_list_refresh".to_string()
|
|
|
|
"node_list_refresh".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn init(&mut self, server: &mut VentedServer, pool: &mut ScheduledThreadPool) -> SnekcloudResult<()> {
|
|
|
|
fn init(
|
|
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
|
|
server: &mut VentedServer,
|
|
|
|
|
|
|
|
pool: &mut ScheduledThreadPool,
|
|
|
|
|
|
|
|
) -> SnekcloudResult<()> {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let mut node_list = self.nodes.lock();
|
|
|
|
let mut node_list = self.nodes.lock();
|
|
|
|
for node in server.nodes() {
|
|
|
|
for node in server.nodes() {
|
|
|
@ -50,12 +52,15 @@ impl Module for NodesRefreshModule {
|
|
|
|
|
|
|
|
|
|
|
|
for node in event.get_payload::<NodeListPayload>().ok()?.nodes {
|
|
|
|
for node in event.get_payload::<NodeListPayload>().ok()?.nodes {
|
|
|
|
if !nodes.contains_key(&node.id) {
|
|
|
|
if !nodes.contains_key(&node.id) {
|
|
|
|
nodes.insert(node.id.clone(), Node {
|
|
|
|
nodes.insert(
|
|
|
|
id: node.id,
|
|
|
|
node.id.clone(),
|
|
|
|
trusted: false,
|
|
|
|
Node {
|
|
|
|
public_key: PublicKey::from(node.public_key),
|
|
|
|
id: node.id,
|
|
|
|
address: node.address,
|
|
|
|
trusted: false,
|
|
|
|
});
|
|
|
|
public_key: PublicKey::from(node.public_key),
|
|
|
|
|
|
|
|
address: node.address,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
new_nodes = true;
|
|
|
|
new_nodes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -73,20 +78,25 @@ impl Module for NodesRefreshModule {
|
|
|
|
move || {
|
|
|
|
move || {
|
|
|
|
if update_required.load(Ordering::Relaxed) {
|
|
|
|
if update_required.load(Ordering::Relaxed) {
|
|
|
|
let nodes_folder = get_settings().node_data_dir;
|
|
|
|
let nodes_folder = get_settings().node_data_dir;
|
|
|
|
nodes.lock().values().cloned().map(|node| {
|
|
|
|
nodes
|
|
|
|
if let Some(address) = node.address {
|
|
|
|
.lock()
|
|
|
|
NodeData::with_addresses(node.id, vec![address], node.public_key)
|
|
|
|
.values()
|
|
|
|
} else {
|
|
|
|
.cloned()
|
|
|
|
NodeData::new(node.id, node.public_key)
|
|
|
|
.map(|node| {
|
|
|
|
}
|
|
|
|
if let Some(address) = node.address {
|
|
|
|
}).for_each(|data| {
|
|
|
|
NodeData::with_addresses(node.id, vec![address], node.public_key)
|
|
|
|
let mut path = nodes_folder.clone();
|
|
|
|
} else {
|
|
|
|
path.push(PathBuf::from(format!("{}.toml", data.id)));
|
|
|
|
NodeData::new(node.id, node.public_key)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.for_each(|data| {
|
|
|
|
|
|
|
|
let mut path = nodes_folder.clone();
|
|
|
|
|
|
|
|
path.push(PathBuf::from(format!("{}.toml", data.id)));
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(e) = data.write_to_file(path) {
|
|
|
|
if let Err(e) = data.write_to_file(path) {
|
|
|
|
log::error!("Failed to write updated node data: {}", e);
|
|
|
|
log::error!("Failed to write updated node data: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -98,11 +108,19 @@ impl Module for NodesRefreshModule {
|
|
|
|
Box::new(self)
|
|
|
|
Box::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn tick(&mut self, mut context: TickContext, _: &mut ScheduledThreadPool) -> VentedResult<()> {
|
|
|
|
fn tick(
|
|
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
|
|
mut context: TickContext,
|
|
|
|
|
|
|
|
_: &mut ScheduledThreadPool,
|
|
|
|
|
|
|
|
) -> SnekcloudResult<()> {
|
|
|
|
if self.last_request.elapsed() > self.settings.update_interval() {
|
|
|
|
if self.last_request.elapsed() > self.settings.update_interval() {
|
|
|
|
context.nodes().iter().filter(|node| node.trusted).for_each(|node| {
|
|
|
|
context
|
|
|
|
context.emit(node.id.clone(), Event::new(NODE_LIST_REQUEST_EVENT));
|
|
|
|
.nodes()
|
|
|
|
});
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.filter(|node| node.trusted)
|
|
|
|
|
|
|
|
.for_each(|node| {
|
|
|
|
|
|
|
|
context.emit(node.id.clone(), Event::new(NODE_LIST_REQUEST_EVENT));
|
|
|
|
|
|
|
|
});
|
|
|
|
self.last_request = Instant::now();
|
|
|
|
self.last_request = Instant::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -117,7 +135,7 @@ impl NodesRefreshModule {
|
|
|
|
nodes: Arc::new(Mutex::new(HashMap::new())),
|
|
|
|
nodes: Arc::new(Mutex::new(HashMap::new())),
|
|
|
|
settings: get_settings().modules.nodes_refresh,
|
|
|
|
settings: get_settings().modules.nodes_refresh,
|
|
|
|
last_request: null_time,
|
|
|
|
last_request: null_time,
|
|
|
|
update_required: Arc::new(AtomicBool::new(false))
|
|
|
|
update_required: Arc::new(AtomicBool::new(false)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|