Quellcode durchsuchen

map: make node connections multiple and add logging

lunar-mining vor 4 Jahren
Ursprung
Commit
02d561ecf5
7 geänderte Dateien mit 70 neuen und 9 gelöschten Zeilen
  1. 2 0
      Cargo.lock
  2. 2 0
      bin/map/Cargo.toml
  3. 2 0
      bin/map/src/lib.rs
  4. 16 5
      bin/map/src/main.rs
  5. 43 0
      bin/map/src/options.rs
  6. 4 4
      bin/map/src/ui.rs
  7. 1 0
      src/cli/cli_config.rs

+ 2 - 0
Cargo.lock

@@ -2836,12 +2836,14 @@ version = "0.3.0"
 dependencies = [
  "async-channel",
  "async-std",
+ "clap 3.0.7",
  "darkfi",
  "easy-parallel",
  "log",
  "num_cpus",
  "rand 0.6.5",
  "serde_json",
+ "simplelog",
  "smol",
  "termion",
  "tui",

+ 2 - 0
bin/map/Cargo.toml

@@ -19,7 +19,9 @@ easy-parallel = "3.2.0"
 async-channel = "1.6.1"
 
 # Misc
+clap = "3.0.7"
 rand = "0.6.5"
+simplelog = "0.11.2"
 log = "0.4.14"
 num_cpus = "1.13.1"
 

+ 2 - 0
bin/map/src/lib.rs

@@ -1,7 +1,9 @@
 pub mod model;
+pub mod options;
 pub mod ui;
 pub mod view;
 
 pub use model::{IdList, InfoList, Model, NodeInfo};
+pub use options::ProgramOptions;
 pub use ui::ui;
 pub use view::{IdListView, InfoListView, View};

+ 16 - 5
bin/map/src/main.rs

@@ -12,8 +12,9 @@ use async_std::sync::Arc;
 use easy_parallel::Parallel;
 use log::debug;
 use serde_json::{json, Value};
+use simplelog::*;
 use smol::Executor;
-use std::{io, io::Read, path::PathBuf};
+use std::{fs::File, io, io::Read, path::PathBuf};
 use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
 use tui::{
     backend::{Backend, TermionBackend},
@@ -22,6 +23,7 @@ use tui::{
 
 use map::{
     model::{Connection, IdList, InfoList, NodeInfo},
+    options::ProgramOptions,
     ui,
     view::{IdListView, InfoListView},
     Model, View,
@@ -79,6 +81,11 @@ impl Map {
 
 #[async_std::main]
 async fn main() -> Result<()> {
+    let options = ProgramOptions::load()?;
+    let (lvl, cfg) = log_config(options.app.clone())?;
+
+    let file = File::create(&*options.log_path).unwrap();
+    WriteLogger::init(lvl, cfg, file)?;
     let config_path = join_config_path(&PathBuf::from("map_config.toml"))?;
 
     spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
@@ -119,10 +126,14 @@ async fn main() -> Result<()> {
 }
 
 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();
+    let mut rpc_vec = Vec::new();
+    for node in config.nodes.clone() {
+        rpc_vec.push(node);
+    }
+    for node in rpc_vec {
+        let client = Map::new(node.node_id);
+        ex.spawn(poll(client, model.clone())).detach();
+    }
 
     Ok(())
 }

+ 43 - 0
bin/map/src/options.rs

@@ -0,0 +1,43 @@
+use clap::{App, Arg, ArgMatches};
+
+use darkfi::Result;
+
+pub struct ProgramOptions {
+    pub log_path: Box<std::path::PathBuf>,
+    pub app: ArgMatches,
+}
+
+impl ProgramOptions {
+    pub fn load() -> Result<ProgramOptions> {
+        let app = App::new("dfi")
+            .version("0.1.0")
+            .author("lunar_mining")
+            .about("Map")
+            .arg(
+                Arg::new("LOG_PATH")
+                    .long("log")
+                    .value_name("LOG_PATH")
+                    .help("Logfile path")
+                    .takes_value(true),
+            )
+            .arg(
+                Arg::new("verbose")
+                    .short('v')
+                    .long("verbose")
+                    .multiple_occurrences(true)
+                    .help("Sets the level of verbosity"),
+            )
+            .get_matches();
+
+        let log_path = Box::new(
+            if let Some(log_path) = app.value_of("LOG_PATH") {
+                std::path::Path::new(log_path)
+            } else {
+                std::path::Path::new("/tmp/map.log")
+            }
+            .to_path_buf(),
+        );
+
+        Ok(ProgramOptions { log_path, app })
+    }
+}

+ 4 - 4
bin/map/src/ui.rs

@@ -65,12 +65,12 @@ fn render_info_left<B: Backend>(view: View, f: &mut Frame<'_, B>, index: usize)
     }
     let span = vec![
         Spans::from(format!("Outgoing connections:")),
-        Spans::from(format!("{}", iconnect_ids[0])),
-        Spans::from(format!("{}", iconnect_ids[1])),
+        Spans::from(format!("   {}", iconnect_ids[0])),
+        Spans::from(format!("   {}", iconnect_ids[1])),
         Spans::from(format!("")),
         Spans::from(format!("Incoming connections:")),
-        Spans::from(format!("{}", oconnect_ids[0])),
-        Spans::from(format!("{}", oconnect_ids[1])),
+        Spans::from(format!("   {}", oconnect_ids[0])),
+        Spans::from(format!("   {}", oconnect_ids[1])),
     ];
     let graph = Paragraph::new(span).block(Block::default().style(Style::default()));
     f.render_widget(graph, slice[0]);

+ 1 - 0
src/cli/cli_config.rs

@@ -150,6 +150,7 @@ pub struct MapConfig {
 #[derive(Clone, Serialize, Deserialize, Debug)]
 pub struct IrcNode {
     pub node_id: String,
+    //pub rpc_url: String,
 }
 
 pub fn spawn_config(path: &Path, contents: &[u8]) -> Result<()> {