main.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. use std::{fs::create_dir_all, sync::Arc};
  2. use async_executor::Executor;
  3. use clap::{IntoApp, Parser};
  4. use simplelog::{ColorChoice, TermLogger, TerminalMode};
  5. use darkfi::{
  6. net::Settings as P2pSettings,
  7. rpc::rpcserver::{listen_and_serve, RpcServerConfig},
  8. util::{
  9. cli::{log_config, spawn_config, Config},
  10. expand_path,
  11. path::get_config_path,
  12. },
  13. Error, Result,
  14. };
  15. mod crdt;
  16. mod error;
  17. mod jsonrpc;
  18. mod month_tasks;
  19. mod task_info;
  20. mod util;
  21. use crate::{
  22. crdt::Node,
  23. jsonrpc::JsonRpcInterface,
  24. task_info::TaskInfo,
  25. util::{CliTaud, Settings, TauConfig, CONFIG_FILE_CONTENTS},
  26. };
  27. async fn start(config: TauConfig, executor: Arc<Executor<'_>>) -> Result<()> {
  28. if config.dataset_path.is_empty() {
  29. return Err(Error::ParseFailed("Failed to parse dataset_path"))
  30. }
  31. let dataset_path = expand_path(&config.dataset_path)?;
  32. // mkdir dataset_path if not exists
  33. create_dir_all(dataset_path.join("month"))?;
  34. create_dir_all(dataset_path.join("task"))?;
  35. let settings = Settings { dataset_path };
  36. //
  37. // Crdt
  38. //
  39. let p2p_settings = P2pSettings::default();
  40. let (node_snd, node_rcv) = async_channel::unbounded::<Vec<u8>>();
  41. let node = Node::new("node", p2p_settings, node_snd).await;
  42. let ex2 = executor.clone();
  43. let node2 = node.clone();
  44. let crdt_task = executor.spawn(node2.start(ex2.clone()));
  45. //
  46. // RPC
  47. //
  48. let server_config = RpcServerConfig {
  49. socket_addr: config.rpc_listener_url.url.parse()?,
  50. use_tls: false,
  51. // this is all random filler that is meaningless bc tls is disabled
  52. identity_path: Default::default(),
  53. identity_pass: Default::default(),
  54. };
  55. let (snd, rcv) = async_channel::unbounded::<TaskInfo>();
  56. let rpc_interface = Arc::new(JsonRpcInterface::new(snd, settings));
  57. let node2 = node.clone();
  58. let recv_update_from_rpc: smol::Task<Result<()>> = executor.spawn(async move {
  59. loop {
  60. let task_info = rcv.recv().await?;
  61. node2.clone().send_event(task_info).await?;
  62. }
  63. });
  64. let recv_update_from_node: smol::Task<Result<()>> = executor.spawn(async move {
  65. loop {
  66. let payload = node_rcv.recv().await?;
  67. // XXX
  68. }
  69. });
  70. listen_and_serve(server_config, rpc_interface, executor).await?;
  71. crdt_task.cancel().await;
  72. recv_update_from_rpc.cancel().await;
  73. recv_update_from_node.cancel().await;
  74. Ok(())
  75. }
  76. #[async_std::main]
  77. async fn main() -> Result<()> {
  78. let args = CliTaud::parse();
  79. let matches = CliTaud::command().get_matches();
  80. let verbosity_level = matches.occurrences_of("verbose");
  81. let (lvl, conf) = log_config(verbosity_level)?;
  82. TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  83. let config_path = get_config_path(args.config, "taud_config.toml")?;
  84. spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  85. let config: TauConfig = Config::<TauConfig>::load(config_path)?;
  86. let ex = Arc::new(Executor::new());
  87. smol::block_on(ex.run(start(config, ex.clone())))
  88. }
  89. #[cfg(test)]
  90. mod tests {
  91. use std::{
  92. fs::{create_dir_all, remove_dir_all},
  93. path::PathBuf,
  94. };
  95. use super::*;
  96. use crate::{
  97. error::TaudResult, month_tasks::MonthTasks, task_info::TaskInfo, util::get_current_time,
  98. };
  99. const TEST_DATA_PATH: &str = "/tmp/test_tau_data";
  100. fn get_path() -> Result<PathBuf> {
  101. remove_dir_all(TEST_DATA_PATH).ok();
  102. let path = PathBuf::from(TEST_DATA_PATH);
  103. // mkdir dataset_path if not exists
  104. create_dir_all(path.join("month"))?;
  105. create_dir_all(path.join("task"))?;
  106. Ok(path)
  107. }
  108. #[test]
  109. fn load_and_save_tasks() -> TaudResult<()> {
  110. let settings = Settings { dataset_path: get_path()? };
  111. // load and save TaskInfo
  112. ///////////////////////
  113. let mut task = TaskInfo::new("test_title", "test_desc", None, 0.0, &settings)?;
  114. task.save()?;
  115. let t_load = TaskInfo::load(&task.get_ref_id(), &settings)?;
  116. assert_eq!(task, t_load);
  117. task.set_title("test_title_2");
  118. task.save()?;
  119. let t_load = TaskInfo::load(&task.get_ref_id(), &settings)?;
  120. assert_eq!(task, t_load);
  121. // load and save MonthTasks
  122. ///////////////////////
  123. let task_tks = vec![];
  124. let mut mt = MonthTasks::new(&task_tks, &settings);
  125. mt.save()?;
  126. let mt_load = MonthTasks::load_or_create(&get_current_time(), &settings)?;
  127. assert_eq!(mt, mt_load);
  128. mt.add(&task.get_ref_id());
  129. mt.save()?;
  130. let mt_load = MonthTasks::load_or_create(&get_current_time(), &settings)?;
  131. assert_eq!(mt, mt_load);
  132. // activate task
  133. ///////////////////////
  134. let task = TaskInfo::new("test_title_3", "test_desc", None, 0.0, &settings)?;
  135. task.save()?;
  136. let mt_load = MonthTasks::load_or_create(&get_current_time(), &settings)?;
  137. assert!(!mt_load.get_task_tks().contains(&task.get_ref_id()));
  138. task.activate()?;
  139. let mt_load = MonthTasks::load_or_create(&get_current_time(), &settings)?;
  140. assert!(mt_load.get_task_tks().contains(&task.get_ref_id()));
  141. remove_dir_all(TEST_DATA_PATH).ok();
  142. Ok(())
  143. }
  144. }