Skip to content
Snippets Groups Projects
Commit 76c92f0b authored by Éloïs's avatar Éloïs
Browse files

Merge branch '101-rooter-create-conf-thread-to-write-config-changes' into 'dev'

Resolve "Rooter: create conf thread to write config changes"

Closes #101

See merge request !87
parents 2b19a920 49fcd889
No related branches found
No related tags found
1 merge request!87Resolve "Rooter: create conf thread to write config changes"
......@@ -287,15 +287,17 @@ impl<'a, 'b: 'a> DuniterCore<'b, 'a, DuRsConf> {
self.user_command = Some(UserCommand::ListModules(ListModulesOpt::from_clap(matches)));
// Start rooter thread
self.rooter_sender = Some(rooter::start_rooter::<DuRsConf>(0, vec![]));
self.rooter_sender = Some(rooter::start_rooter(0, profile.clone(), conf, vec![]));
true
} else if let Some(_matches) = cli_args.subcommand_matches("start") {
// Store user command
self.user_command = Some(UserCommand::Start());
// Start rooter thread
self.rooter_sender = Some(rooter::start_rooter::<DuRsConf>(
self.rooter_sender = Some(rooter::start_rooter(
self.run_duration_in_secs,
profile.clone(),
conf,
external_followers,
));
true
......@@ -314,7 +316,7 @@ impl<'a, 'b: 'a> DuniterCore<'b, 'a, DuRsConf> {
verif_hashs: opts.unsafe_mode,
}));
// Start rooter thread
self.rooter_sender = Some(rooter::start_rooter::<DuRsConf>(0, vec![]));
self.rooter_sender = Some(rooter::start_rooter(0, profile.clone(), conf, vec![]));
true
} else if let Some(matches) = cli_args.subcommand_matches("sync_ts") {
let opts = SyncTsOpt::from_clap(matches);
......
......@@ -15,6 +15,8 @@
//! Relay messages between durs modules.
use duniter_conf;
use duniter_conf::DuRsConf;
use duniter_message::*;
use duniter_module::*;
use std::collections::HashMap;
......@@ -176,6 +178,29 @@ fn start_broadcasting_thread(
}
}
/// Start conf thread
fn start_conf_thread(
profile: &str,
conf: &mut DuRsConf,
receiver: &mpsc::Receiver<DursMsgContent>,
) {
loop {
match receiver.recv() {
Ok(msg) => {
if let DursMsgContent::SaveNewModuleConf(module_static_name, new_json_conf) = msg {
conf.set_module_conf(module_static_name.to_string(), new_json_conf);
duniter_conf::write_conf_file(&profile, conf)
.expect("Fail to write new module conf in conf file ! ");
}
}
Err(_) => {
info!("Conf thread stops.");
break;
}
}
}
}
/// Send msg to several receivers
fn send_msg_to_several_receivers(
msg: DursMsg,
......@@ -226,8 +251,10 @@ fn store_msg_in_pool(
}
/// Start rooter thread
pub fn start_rooter<DC: DuniterConf>(
pub fn start_rooter(
run_duration_in_secs: u64,
profile: String,
conf: DuRsConf,
external_followers: Vec<mpsc::Sender<DursMsgContent>>,
) -> mpsc::Sender<RooterThreadMessage<DursMsg>> {
let start_time = SystemTime::now();
......@@ -256,6 +283,17 @@ pub fn start_rooter<DC: DuniterConf>(
);
});
// Create conf thread channel
let (conf_sender, conf_receiver): (
mpsc::Sender<DursMsgContent>,
mpsc::Receiver<DursMsgContent>,
) = mpsc::channel();
// Create conf thread
thread::spawn(move || {
start_conf_thread(&profile, &mut conf.clone(), &conf_receiver);
});
// Define variables
let mut modules_senders: HashMap<ModuleStaticName, mpsc::Sender<DursMsg>> = HashMap::new();
let mut pool_msgs: HashMap<ModuleStaticName, Vec<DursMsgContent>> = HashMap::new();
......@@ -320,9 +358,21 @@ pub fn start_rooter<DC: DuniterConf>(
break;
}
}
DursMsgReceiver::Role(_module_role) => broadcasting_sender
DursMsgReceiver::Role(role) =>
// If the message is intended for role "ChangeConf", forward it to the conf thread
{
if let ModuleRole::ChangeConf = role {
conf_sender
.send(msg.1)
.expect("Fail to reach conf thread !");
} else {
broadcasting_sender
.send(RooterThreadMessage::ModuleMessage(msg))
.expect("Fail to relay message to broadcasting thread !"),
.expect(
"Fail to relay message to broadcasting thread !",
);
}
}
DursMsgReceiver::Event(_module_event) => broadcasting_sender
.send(RooterThreadMessage::ModuleMessage(msg))
.expect("Fail to relay message to broadcasting thread !"),
......
......@@ -75,7 +75,7 @@ pub enum DursMsgContent {
/// Brut binary message
Binary(Vec<u8>),
/// New configuration of a module to save
SaveNewModuleConf(ModuleName, serde_json::Value),
SaveNewModuleConf(ModuleStaticName, serde_json::Value),
/// Response of DALRequest
DALResponse(Box<DALResponse>),
/// Blockchain event
......
......@@ -105,7 +105,9 @@ impl ToString for ModuleReqFullId {
}
/// Duniter configuration
pub trait DuniterConf: Clone + Debug + Default + PartialEq + Serialize + DeserializeOwned {
pub trait DuniterConf:
Clone + Debug + Default + PartialEq + Serialize + DeserializeOwned + Send + ToOwned
{
/// Get conf version profile
fn version(&self) -> usize;
/// Get currency
......@@ -155,16 +157,18 @@ pub enum ModuleRole {
BlockValidation,
/// Generates the content of the next block
BlockGeneration,
/// Manage pending data for the wot
WotPool,
/// Change configuration file
ChangeConf,
/// Communicates with client software
ClientsNetwork,
/// Manage pending data for the currency (transactions and scripts)
CurrencyPool,
/// Manages the network between nodes implementing the DUP protocol
InterNodesNetwork,
/// Communicates with client software
ClientsNetwork,
/// Communicates with the node user
UserInterface,
/// Manage pending data for the wot
WotPool,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment