|
|
|
@ -1,22 +1,21 @@
|
|
|
|
|
use crate::data::node_data::NodeData;
|
|
|
|
|
use crate::modules::nodes_refresh::settings::NodesRefreshSettings;
|
|
|
|
|
use crate::modules::Module;
|
|
|
|
|
use vented::result::VentedResult;
|
|
|
|
|
use vented::server::VentedServer;
|
|
|
|
|
use crate::server::tick_context::TickContext;
|
|
|
|
|
use crate::utils::result::SnekcloudResult;
|
|
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
|
use vented::server::server_events::{NodeListPayload, NODE_LIST_REQUEST_EVENT};
|
|
|
|
|
use vented::server::data::Node;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use crate::utils::settings::get_settings;
|
|
|
|
|
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::Arc;
|
|
|
|
|
use std::time::{Duration, Instant, UNIX_EPOCH};
|
|
|
|
|
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::server::data::Node;
|
|
|
|
|
use vented::server::server_events::{NodeListPayload, NODE_LIST_REQUEST_EVENT};
|
|
|
|
|
use vented::server::VentedServer;
|
|
|
|
|
|
|
|
|
|
pub mod settings;
|
|
|
|
|
|
|
|
|
@ -28,12 +27,15 @@ pub struct NodesRefreshModule {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Module for NodesRefreshModule {
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> 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();
|
|
|
|
|
for node in server.nodes() {
|
|
|
|
@ -50,12 +52,15 @@ impl Module for NodesRefreshModule {
|
|
|
|
|
|
|
|
|
|
for node in event.get_payload::<NodeListPayload>().ok()?.nodes {
|
|
|
|
|
if !nodes.contains_key(&node.id) {
|
|
|
|
|
nodes.insert(node.id.clone(), Node {
|
|
|
|
|
nodes.insert(
|
|
|
|
|
node.id.clone(),
|
|
|
|
|
Node {
|
|
|
|
|
id: node.id,
|
|
|
|
|
trusted: false,
|
|
|
|
|
public_key: PublicKey::from(node.public_key),
|
|
|
|
|
address: node.address,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
new_nodes = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -73,13 +78,18 @@ impl Module for NodesRefreshModule {
|
|
|
|
|
move || {
|
|
|
|
|
if update_required.load(Ordering::Relaxed) {
|
|
|
|
|
let nodes_folder = get_settings().node_data_dir;
|
|
|
|
|
nodes.lock().values().cloned().map(|node| {
|
|
|
|
|
nodes
|
|
|
|
|
.lock()
|
|
|
|
|
.values()
|
|
|
|
|
.cloned()
|
|
|
|
|
.map(|node| {
|
|
|
|
|
if let Some(address) = node.address {
|
|
|
|
|
NodeData::with_addresses(node.id, vec![address], node.public_key)
|
|
|
|
|
} else {
|
|
|
|
|
NodeData::new(node.id, node.public_key)
|
|
|
|
|
}
|
|
|
|
|
}).for_each(|data| {
|
|
|
|
|
})
|
|
|
|
|
.for_each(|data| {
|
|
|
|
|
let mut path = nodes_folder.clone();
|
|
|
|
|
path.push(PathBuf::from(format!("{}.toml", data.id)));
|
|
|
|
|
|
|
|
|
@ -98,9 +108,17 @@ impl Module for NodesRefreshModule {
|
|
|
|
|
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() {
|
|
|
|
|
context.nodes().iter().filter(|node| node.trusted).for_each(|node| {
|
|
|
|
|
context
|
|
|
|
|
.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();
|
|
|
|
@ -117,7 +135,7 @@ impl NodesRefreshModule {
|
|
|
|
|
nodes: Arc::new(Mutex::new(HashMap::new())),
|
|
|
|
|
settings: get_settings().modules.nodes_refresh,
|
|
|
|
|
last_request: null_time,
|
|
|
|
|
update_required: Arc::new(AtomicBool::new(false))
|
|
|
|
|
update_required: Arc::new(AtomicBool::new(false)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|