|
|
@@ -1,16 +1,18 @@
|
|
|
-use std::{fs::create_dir_all, sync::Arc};
|
|
|
+use std::{fs::create_dir_all, path::PathBuf, sync::Arc};
|
|
|
|
|
|
use async_executor::Executor;
|
|
|
-use clap::{IntoApp, Parser};
|
|
|
+use clap::Parser;
|
|
|
use simplelog::{ColorChoice, TermLogger, TerminalMode};
|
|
|
|
|
|
use darkfi::{
|
|
|
net::Settings as P2pSettings,
|
|
|
+ raft::Raft,
|
|
|
rpc::rpcserver::{listen_and_serve, RpcServerConfig},
|
|
|
util::{
|
|
|
cli::{log_config, spawn_config, Config},
|
|
|
expand_path,
|
|
|
path::get_config_path,
|
|
|
+ sleep,
|
|
|
},
|
|
|
Error, Result,
|
|
|
};
|
|
|
@@ -22,12 +24,13 @@ mod task_info;
|
|
|
mod util;
|
|
|
|
|
|
use crate::{
|
|
|
+ error::TaudResult,
|
|
|
jsonrpc::JsonRpcInterface,
|
|
|
task_info::TaskInfo,
|
|
|
util::{CliTaud, Settings, TauConfig, CONFIG_FILE_CONTENTS},
|
|
|
};
|
|
|
|
|
|
-async fn start(config: TauConfig, executor: Arc<Executor<'_>>) -> Result<()> {
|
|
|
+async fn start(config: TauConfig, args: CliTaud, executor: Arc<Executor<'_>>) -> Result<()> {
|
|
|
if config.dataset_path.is_empty() {
|
|
|
return Err(Error::ParseFailed("Failed to parse dataset_path"))
|
|
|
}
|
|
|
@@ -40,7 +43,23 @@ async fn start(config: TauConfig, executor: Arc<Executor<'_>>) -> Result<()> {
|
|
|
|
|
|
let settings = Settings { dataset_path };
|
|
|
|
|
|
- let _p2p_settings = P2pSettings::default();
|
|
|
+ let p2p_settings = P2pSettings {
|
|
|
+ inbound: args.accept,
|
|
|
+ outbound_connections: args.slots,
|
|
|
+ external_addr: args.accept,
|
|
|
+ peers: args.connect.clone(),
|
|
|
+ seeds: args.seed.clone(),
|
|
|
+ ..Default::default()
|
|
|
+ };
|
|
|
+
|
|
|
+ //
|
|
|
+ //Raft
|
|
|
+ //
|
|
|
+ let mut raft =
|
|
|
+ Raft::<TaskInfo>::new(p2p_settings.inbound, PathBuf::from(config.datastore_raft))?;
|
|
|
+
|
|
|
+ let raft_sender = raft.get_broadcast().clone();
|
|
|
+ let commits = raft.get_commits().clone();
|
|
|
|
|
|
//
|
|
|
// RPC
|
|
|
@@ -59,33 +78,54 @@ async fn start(config: TauConfig, executor: Arc<Executor<'_>>) -> Result<()> {
|
|
|
|
|
|
let recv_update_from_rpc: smol::Task<Result<()>> = executor.spawn(async move {
|
|
|
loop {
|
|
|
- let _task_info = rcv.recv().await?;
|
|
|
- // XXX
|
|
|
+ let task_info = rcv.recv().await?;
|
|
|
+ raft_sender.send(task_info).await?;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- listen_and_serve(server_config, rpc_interface, executor).await?;
|
|
|
+ let recv_update_from_raft: smol::Task<TaudResult<()>> = executor.spawn(async move {
|
|
|
+ loop {
|
|
|
+ // FIXME TODO
|
|
|
+ // this should update once receive rpc request from the tau-cli
|
|
|
+ sleep(1).await;
|
|
|
+ let recv_commits = commits.lock().await;
|
|
|
+
|
|
|
+ for task_info in recv_commits.iter() {
|
|
|
+ task_info.save()?;
|
|
|
+ if task_info.get_state() == "open" {
|
|
|
+ task_info.activate()?;
|
|
|
+ } else {
|
|
|
+ let mut mt = task_info.get_month_task()?;
|
|
|
+ mt.remove(&task_info.get_ref_id());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ let ex2 = executor.clone();
|
|
|
+ ex2.spawn(listen_and_serve(server_config, rpc_interface, executor.clone())).detach();
|
|
|
+
|
|
|
+ raft.start(p2p_settings.clone(), executor.clone()).await?;
|
|
|
|
|
|
recv_update_from_rpc.cancel().await;
|
|
|
+ recv_update_from_raft.cancel().await;
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
#[async_std::main]
|
|
|
async fn main() -> Result<()> {
|
|
|
let args = CliTaud::parse();
|
|
|
- let matches = CliTaud::command().get_matches();
|
|
|
|
|
|
- let verbosity_level = matches.occurrences_of("verbose");
|
|
|
- let (lvl, conf) = log_config(verbosity_level)?;
|
|
|
+ let (lvl, conf) = log_config(args.verbose.into())?;
|
|
|
TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
|
|
|
|
|
|
- let config_path = get_config_path(args.config, "taud_config.toml")?;
|
|
|
+ let config_path = get_config_path(args.config.clone(), "taud_config.toml")?;
|
|
|
spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
|
|
|
|
|
|
let config: TauConfig = Config::<TauConfig>::load(config_path)?;
|
|
|
|
|
|
let ex = Arc::new(Executor::new());
|
|
|
- smol::block_on(ex.run(start(config, ex.clone())))
|
|
|
+ smol::block_on(ex.run(start(config, args, ex.clone())))
|
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|