main.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 jsonrpc;
  17. mod month_tasks;
  18. mod task_info;
  19. mod util;
  20. use crate::{
  21. crdt::Node,
  22. jsonrpc::JsonRpcInterface,
  23. task_info::TaskInfo,
  24. util::{CliTaud, Settings, TauConfig, CONFIG_FILE_CONTENTS},
  25. };
  26. async fn start(config: TauConfig, executor: Arc<Executor<'_>>) -> Result<()> {
  27. if config.dataset_path.is_empty() {
  28. return Err(Error::ParseFailed("Failed to parse dataset_path"))
  29. }
  30. let dataset_path = expand_path(&config.dataset_path)?;
  31. // mkdir dataset_path if not exists
  32. create_dir_all(dataset_path.join("month"))?;
  33. create_dir_all(dataset_path.join("task"))?;
  34. let settings = Settings { dataset_path };
  35. //
  36. // Crdt
  37. //
  38. let p2p_settings = P2pSettings::default();
  39. let node = Node::new("node", p2p_settings).await;
  40. let ex2 = executor.clone();
  41. let node2 = node.clone();
  42. let crdt_task = executor.spawn(node2.start(ex2.clone()));
  43. //
  44. // RPC
  45. //
  46. let server_config = RpcServerConfig {
  47. socket_addr: config.rpc_listener_url.url.parse()?,
  48. use_tls: false,
  49. // this is all random filler that is meaningless bc tls is disabled
  50. identity_path: Default::default(),
  51. identity_pass: Default::default(),
  52. };
  53. let (snd, _rcv) = async_channel::unbounded::<TaskInfo>();
  54. let rpc_interface = Arc::new(JsonRpcInterface::new(snd, settings));
  55. listen_and_serve(server_config, rpc_interface, executor).await?;
  56. crdt_task.cancel().await;
  57. Ok(())
  58. }
  59. #[async_std::main]
  60. async fn main() -> Result<()> {
  61. let args = CliTaud::parse();
  62. let matches = CliTaud::command().get_matches();
  63. let verbosity_level = matches.occurrences_of("verbose");
  64. let (lvl, conf) = log_config(verbosity_level)?;
  65. TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  66. let config_path = get_config_path(args.config, "taud_config.toml")?;
  67. spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  68. let config: TauConfig = Config::<TauConfig>::load(config_path)?;
  69. let ex = Arc::new(Executor::new());
  70. smol::block_on(ex.run(start(config, ex.clone())))
  71. }
  72. #[cfg(test)]
  73. mod tests {
  74. use std::{
  75. fs::{create_dir_all, remove_dir_all},
  76. path::PathBuf,
  77. };
  78. use crate::{month_tasks::MonthTasks, task_info::TaskInfo, util::get_current_time};
  79. use super::*;
  80. fn get_path() -> Result<PathBuf> {
  81. remove_dir_all("/tmp/test_tau_data").ok();
  82. let path = PathBuf::from("/tmp/test_tau_data");
  83. // mkdir dataset_path if not exists
  84. create_dir_all(path.join("month"))?;
  85. create_dir_all(path.join("task"))?;
  86. Ok(path)
  87. }
  88. #[test]
  89. fn load_and_save_tasks() -> Result<()> {
  90. let settings = Settings { dataset_path: get_path()? };
  91. // load and save TaskInfo
  92. ///////////////////////
  93. let mut task = TaskInfo::new("test_title", "test_desc", None, 0, &settings)?;
  94. task.save()?;
  95. let t_load = TaskInfo::load(&task.get_ref_id(), &settings)?;
  96. assert_eq!(task, t_load);
  97. task.set_title("test_title_2");
  98. task.save()?;
  99. let t_load = TaskInfo::load(&task.get_ref_id(), &settings)?;
  100. assert_eq!(task, t_load);
  101. // load and save MonthTasks
  102. ///////////////////////
  103. let task_tks = vec![];
  104. let mut mt = MonthTasks::new(&task_tks, &settings);
  105. mt.save()?;
  106. let mt_load = MonthTasks::load_or_create(&get_current_time(), &settings)?;
  107. assert_eq!(mt, mt_load);
  108. mt.add(&task.get_ref_id());
  109. mt.save()?;
  110. let mt_load = MonthTasks::load_or_create(&get_current_time(), &settings)?;
  111. assert_eq!(mt, mt_load);
  112. // activate task
  113. ///////////////////////
  114. let task = TaskInfo::new("test_title_3", "test_desc", None, 0, &settings)?;
  115. task.save()?;
  116. let mt_load = MonthTasks::load_or_create(&get_current_time(), &settings)?;
  117. assert!(!mt_load.get_task_tks().contains(&task.get_ref_id()));
  118. task.activate()?;
  119. let mt_load = MonthTasks::load_or_create(&get_current_time(), &settings)?;
  120. assert!(mt_load.get_task_tks().contains(&task.get_ref_id()));
  121. Ok(())
  122. }
  123. }