lighthouse-pulse/lighthouse/client/mod.rs

84 lines
2.5 KiB
Rust
Raw Normal View History

2018-09-14 05:52:49 +00:00
use std::sync::Arc;
2018-08-16 04:17:28 +00:00
use std::thread;
use super::db::{ DiskDB };
2018-08-16 04:17:28 +00:00
use super::config::LighthouseConfig;
use super::futures::sync::mpsc::{
unbounded,
};
use super::network_libp2p::service::listen as network_listen;
use super::network_libp2p::state::NetworkState;
use super::slog::Logger;
2018-09-09 14:36:00 +00:00
use super::sync::run_sync_future;
2018-08-16 04:17:28 +00:00
use super::db::ClientDB;
2018-08-23 05:11:02 +00:00
/// Represents the co-ordination of the
/// networking, syncing and RPC (not-yet-implemented) threads.
2018-08-16 04:17:28 +00:00
pub struct Client {
pub db: Arc<ClientDB>,
2018-08-23 05:11:02 +00:00
pub network_thread: thread::JoinHandle<()>,
pub sync_thread: thread::JoinHandle<()>,
2018-08-16 04:17:28 +00:00
}
impl Client {
2018-08-23 05:11:02 +00:00
/// Instantiates a new "Client".
///
/// Presently, this means starting network and sync threads
/// and plumbing them together.
2018-09-21 22:17:31 +00:00
pub fn new(config: &LighthouseConfig,
log: &Logger)
2018-08-16 04:17:28 +00:00
-> Self
{
// Open the local db
let db = {
2018-09-18 05:59:44 +00:00
let db = DiskDB::open(&config.data_dir, None);
2018-09-14 05:52:49 +00:00
Arc::new(db)
2018-08-16 04:17:28 +00:00
};
// Start the network thread
let network_state = NetworkState::new(
&config.data_dir,
&config.p2p_listen_port,
2018-08-23 05:11:02 +00:00
&log).expect("Network setup failed"); let (network_thread, network_tx, network_rx) = {
2018-08-16 04:17:28 +00:00
let (message_sender, message_receiver) = unbounded();
let (event_sender, event_receiver) = unbounded();
let network_log = log.new(o!());
let thread = thread::spawn(move || {
network_listen(
network_state,
event_sender,
message_receiver,
network_log,
);
});
(thread, message_sender, event_receiver)
};
// Start the sync thread
let (sync_thread, _sync_tx, _sync_rx) = {
let (sync_out_sender, sync_out_receiver) = unbounded();
let (sync_in_sender, sync_in_receiver) = unbounded();
let sync_log = log.new(o!());
2018-09-14 05:52:49 +00:00
let sync_db = db.clone();
2018-08-16 04:17:28 +00:00
let thread = thread::spawn(move || {
2018-09-09 14:36:00 +00:00
run_sync_future(
2018-08-16 04:17:28 +00:00
sync_db,
network_tx.clone(),
network_rx,
2018-09-21 22:17:31 +00:00
&sync_out_sender,
&sync_in_receiver,
2018-08-16 04:17:28 +00:00
sync_log,
);
});
(thread, sync_in_sender, sync_out_receiver)
};
// Return the client struct
Self {
2018-09-21 22:17:31 +00:00
db,
2018-08-23 05:11:02 +00:00
network_thread,
sync_thread,
2018-08-16 04:17:28 +00:00
}
}
}