Tidy client struct

This commit is contained in:
Paul Hauner 2018-08-23 15:11:02 +10:00
parent 9689142883
commit 121bff725e
2 changed files with 12 additions and 7 deletions

View File

@ -10,12 +10,19 @@ use super::network_libp2p::state::NetworkState;
use super::slog::Logger; use super::slog::Logger;
use super::sync::start_sync; use super::sync::start_sync;
/// Represents the co-ordination of the
/// networking, syncing and RPC (not-yet-implemented) threads.
pub struct Client { pub struct Client {
pub db: Arc<RwLock<DB>>, pub db: Arc<RwLock<DB>>,
pub threads: Vec<thread::JoinHandle<()>> pub network_thread: thread::JoinHandle<()>,
pub sync_thread: thread::JoinHandle<()>,
} }
impl Client { impl Client {
/// Instantiates a new "Client".
///
/// Presently, this means starting network and sync threads
/// and plumbing them together.
pub fn new(config: LighthouseConfig, pub fn new(config: LighthouseConfig,
log: Logger) log: Logger)
-> Self -> Self
@ -30,8 +37,7 @@ impl Client {
let network_state = NetworkState::new( let network_state = NetworkState::new(
&config.data_dir, &config.data_dir,
&config.p2p_listen_port, &config.p2p_listen_port,
&log).expect("Network setup failed"); &log).expect("Network setup failed"); let (network_thread, network_tx, network_rx) = {
let (network_thread, network_tx, network_rx) = {
let (message_sender, message_receiver) = unbounded(); let (message_sender, message_receiver) = unbounded();
let (event_sender, event_receiver) = unbounded(); let (event_sender, event_receiver) = unbounded();
let network_log = log.new(o!()); let network_log = log.new(o!());
@ -68,7 +74,8 @@ impl Client {
// Return the client struct // Return the client struct
Self { Self {
db: db, db: db,
threads: vec![sync_thread, network_thread] network_thread,
sync_thread,
} }
} }
} }

View File

@ -66,9 +66,7 @@ fn main() {
"port" => &config.p2p_listen_port); "port" => &config.p2p_listen_port);
let client = Client::new(config, log.new(o!())); let client = Client::new(config, log.new(o!()));
for thread in client.threads { client.sync_thread.join().unwrap();
thread.join().unwrap();
}
info!(log, "Exiting."); info!(log, "Exiting.");
} }