소스 검색

map: created config file and removed duplicate index write

lunar-mining 4 년 전
부모
커밋
0d0cb05c9a
4개의 변경된 파일31개의 추가작업 그리고 11개의 파일을 삭제
  1. 1 1
      bin/map/Cargo.toml
  2. 19 9
      bin/map/src/main.rs
  3. 10 0
      src/cli/cli_config.rs
  4. 1 1
      src/cli/mod.rs

+ 1 - 1
bin/map/Cargo.toml

@@ -5,7 +5,7 @@ edition = "2021"
 
 [dependencies.darkfi]
 path = "../../"
-features = ["rpc"]
+features = ["rpc", "cli"]
 
 [dependencies]
 # Tui

+ 19 - 9
bin/map/src/main.rs

@@ -1,7 +1,11 @@
 use darkfi::{
+    cli::{
+        cli_config::{log_config, spawn_config},
+        Config, MapConfig,
+    },
     error::{Error, Result},
     rpc::{jsonrpc, jsonrpc::JsonResult},
-    util::async_util,
+    util::{async_util, join_config_path},
 };
 
 use async_std::sync::Arc;
@@ -9,7 +13,7 @@ use easy_parallel::Parallel;
 use log::debug;
 use serde_json::{json, Value};
 use smol::Executor;
-use std::{io, io::Read};
+use std::{io, io::Read, path::PathBuf};
 use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
 use tui::{
     backend::{Backend, TermionBackend},
@@ -23,6 +27,8 @@ use map::{
     Model, View,
 };
 
+const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../map_config.toml");
+
 struct Map {
     url: String,
 }
@@ -73,6 +79,13 @@ impl Map {
 
 #[async_std::main]
 async fn main() -> Result<()> {
+    let config_path = join_config_path(&PathBuf::from("map_config.toml"))?;
+
+    // Spawn config file if it's not in place already.
+    spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
+
+    let config = Config::<MapConfig>::load(config_path)?;
+
     let stdout = io::stdout().into_raw_mode()?;
     let backend = TermionBackend::new(stdout);
     let mut terminal = Terminal::new(backend)?;
@@ -133,7 +146,7 @@ async fn main() -> Result<()> {
         // Run the main future on the current thread.
         .finish(|| {
             smol::future::block_on(async move {
-                run_rpc(ex2.clone(), model.clone()).await?;
+                run_rpc(&config, ex2.clone(), model.clone()).await?;
                 render(&mut terminal, model.clone()).await?;
                 drop(signal);
                 Ok::<(), darkfi::Error>(())
@@ -143,8 +156,9 @@ async fn main() -> Result<()> {
     result
 }
 
-async fn run_rpc(ex: Arc<Executor<'_>>, model: Arc<Model>) -> Result<()> {
-    let client = Map::new("tcp://127.0.0.1:8000".to_string());
+async fn run_rpc(config: &MapConfig, ex: Arc<Executor<'_>>, model: Arc<Model>) -> Result<()> {
+    // TODO: listen to multiple nodes
+    let client = Map::new(config.nodes[0].node_id.to_string());
 
     ex.spawn(poll(client, model)).detach();
 
@@ -186,10 +200,6 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
             ];
 
             for node in infos {
-                // update the index
-                let mut index = model.info_list.index.lock().await;
-                *index += 1;
-
                 // write nodes
                 model.info_list.infos.lock().await.push(node.clone());
                 // write node id

+ 10 - 0
src/cli/cli_config.rs

@@ -142,6 +142,16 @@ pub struct CashierdConfig {
     pub networks: Vec<FeatureNetwork>,
 }
 
+#[derive(Clone, Serialize, Deserialize, Debug)]
+pub struct MapConfig {
+    pub nodes: Vec<IrcNode>,
+}
+
+#[derive(Clone, Serialize, Deserialize, Debug)]
+pub struct IrcNode {
+    pub node_id: String,
+}
+
 pub fn spawn_config(path: &Path, contents: &[u8]) -> Result<()> {
     if !path.exists() {
         if let Some(parent) = path.parent() {

+ 1 - 1
src/cli/mod.rs

@@ -1,7 +1,7 @@
 pub mod cli_config;
 pub mod cli_parser;
 
-pub use cli_config::{CashierdConfig, Config, DarkfidConfig, DrkConfig, GatewaydConfig};
+pub use cli_config::{CashierdConfig, Config, DarkfidConfig, DrkConfig, GatewaydConfig, MapConfig};
 
 pub use cli_parser::{
     CliCashierd, CliDao, CliDaoSubCommands, CliDarkfid, CliDrk, CliDrkSubCommands, CliGatewayd,