Просмотр исходного кода

map: moved id_list, info_list, node_info into model.rs

lunar-mining 4 лет назад
Родитель
Сommit
f63783baae
6 измененных файлов с 125 добавлено и 103 удалено
  1. 0 52
      bin/map/src/id_list.rs
  2. 0 27
      bin/map/src/info_list.rs
  3. 1 4
      bin/map/src/lib.rs
  4. 22 19
      bin/map/src/main.rs
  5. 102 1
      bin/map/src/model.rs
  6. 0 0
      bin/map/src/old/node_info.rs

+ 0 - 52
bin/map/src/id_list.rs

@@ -1,52 +0,0 @@
-//TODO: made node_id into a hashset across project
-//use std::collections::HashSet
-use tui::widgets::ListState;
-
-#[derive(Clone)]
-pub struct IdList {
-    pub state: ListState,
-    // todo: mutex
-    pub node_id: Vec<String>,
-}
-
-impl IdList {
-    pub fn new(node_id: Vec<String>) -> IdList {
-        IdList { state: ListState::default(), node_id }
-    }
-
-    pub fn next(&mut self) {
-        let i = match self.state.selected() {
-            Some(i) => {
-                if i >= self.node_id.len() - 1 {
-                    0
-                } else {
-                    i + 1
-                }
-            }
-            None => 0,
-        };
-        self.state.select(Some(i));
-    }
-
-    pub fn previous(&mut self) {
-        let i = match self.state.selected() {
-            Some(i) => {
-                if i == 0 {
-                    self.node_id.len() - 1
-                } else {
-                    i - 1
-                }
-            }
-            None => 0,
-        };
-        self.state.select(Some(i));
-    }
-
-    pub fn unselect(&mut self) {
-        self.state.select(None);
-    }
-}
-
-//pub async fn add_seen(&self, id: u32) {
-//    self.privmsg_ids.lock().await.insert(id);
-//}

+ 0 - 27
bin/map/src/info_list.rs

@@ -1,27 +0,0 @@
-use crate::node_info::NodeInfo;
-
-#[derive(Clone)]
-pub struct InfoList {
-    pub index: usize,
-    pub infos: Vec<NodeInfo>,
-}
-
-impl InfoList {
-    pub fn new(infos: Vec<NodeInfo>) -> InfoList {
-        let index = 0;
-
-        InfoList { index, infos }
-    }
-
-    pub async fn next(&mut self) {
-        self.index = (self.index + 1) % self.infos.len();
-    }
-
-    pub async fn previous(&mut self) {
-        if self.index > 0 {
-            self.index -= 1;
-        } else {
-            self.index = self.infos.len() - 1;
-        }
-    }
-}

+ 1 - 4
bin/map/src/lib.rs

@@ -1,9 +1,6 @@
-pub mod id_list;
-pub mod info_list;
 pub mod model;
 pub mod model;
-pub mod node_info;
 pub mod types;
 pub mod types;
 pub mod ui;
 pub mod ui;
 
 
-pub use model::Model;
+pub use model::{IdList, InfoList, Model, NodeInfo};
 pub use ui::ui;
 pub use ui::ui;

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

@@ -16,7 +16,10 @@ use tui::{
     Terminal,
     Terminal,
 };
 };
 
 
-use map::{id_list::IdList, info_list::InfoList, node_info::NodeInfo, ui, Model};
+use map::{
+    model::{IdList, InfoList, NodeInfo},
+    ui, Model,
+};
 
 
 struct Map {
 struct Map {
     url: String,
     url: String,
@@ -119,8 +122,8 @@ async fn main() -> Result<()> {
 
 
     let id_list = IdList::new(ids);
     let id_list = IdList::new(ids);
 
 
-    //let app = Arc::new(Model::new(id_list, info_list));
-    let app = Model::new(id_list, info_list);
+    //let model = Arc::new(Model::new(id_list, info_list));
+    let model = Model::new(id_list, info_list);
 
 
     let nthreads = num_cpus::get();
     let nthreads = num_cpus::get();
     let (signal, shutdown) = async_channel::unbounded::<()>();
     let (signal, shutdown) = async_channel::unbounded::<()>();
@@ -133,8 +136,8 @@ async fn main() -> Result<()> {
         // Run the main future on the current thread.
         // Run the main future on the current thread.
         .finish(|| {
         .finish(|| {
             smol::future::block_on(async move {
             smol::future::block_on(async move {
-                run_rpc(ex2.clone(), app.clone()).await?;
-                run_app(&mut terminal, app.clone()).await?;
+                run_rpc(ex2.clone(), model.clone()).await?;
+                run_model(&mut terminal, model.clone()).await?;
                 drop(signal);
                 drop(signal);
                 Ok::<(), darkfi::Error>(())
                 Ok::<(), darkfi::Error>(())
             })
             })
@@ -143,15 +146,15 @@ async fn main() -> Result<()> {
     result
     result
 }
 }
 
 
-async fn run_rpc(ex: Arc<Executor<'_>>, app: Model) -> Result<()> {
+async fn run_rpc(ex: Arc<Executor<'_>>, model: Model) -> Result<()> {
     let client = Map::new("tcp://127.0.0.1:8000".to_string());
     let client = Map::new("tcp://127.0.0.1:8000".to_string());
 
 
-    ex.spawn(poll(client, app)).detach();
+    ex.spawn(poll(client, model)).detach();
 
 
     Ok(())
     Ok(())
 }
 }
 
 
-async fn poll(client: Map, _app: Model) -> Result<()> {
+async fn poll(client: Map, _model: Model) -> Result<()> {
     loop {
     loop {
         let reply = client.get_info().await?;
         let reply = client.get_info().await?;
 
 
@@ -169,7 +172,7 @@ async fn poll(client: Map, _app: Model) -> Result<()> {
                 last_message: node3["message"].to_string(),
                 last_message: node3["message"].to_string(),
             }];
             }];
 
 
-            //app.lock().await.update(infos).await;
+            //model.lock().await.update(infos).await;
         } else {
         } else {
             // TODO: error handling
             // TODO: error handling
             println!("Reply is an error");
             println!("Reply is an error");
@@ -179,36 +182,36 @@ async fn poll(client: Map, _app: Model) -> Result<()> {
     }
     }
 }
 }
 
 
-async fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: Model) -> io::Result<()> {
+async fn run_model<B: Backend>(terminal: &mut Terminal<B>, mut model: Model) -> io::Result<()> {
     let mut asi = async_stdin();
     let mut asi = async_stdin();
 
 
     terminal.clear()?;
     terminal.clear()?;
 
 
-    app.id_list.state.select(Some(0));
+    model.id_list.state.select(Some(0));
 
 
-    app.info_list.index = 0;
+    model.info_list.index = 0;
 
 
     // acquire the mutex
     // acquire the mutex
-    // let mut app = app.lock();
+    // let mut model = model.lock();
 
 
     loop {
     loop {
         // clone everything
         // clone everything
         terminal.draw(|f| {
         terminal.draw(|f| {
-            ui::ui(f, app.clone());
+            ui::ui(f, model.clone());
         })?;
         })?;
         for k in asi.by_ref().keys() {
         for k in asi.by_ref().keys() {
             match k.unwrap() {
             match k.unwrap() {
                 Key::Char('q') => {
                 Key::Char('q') => {
                     terminal.clear()?;
                     terminal.clear()?;
-                    return Ok(());
+                    return Ok(())
                 }
                 }
                 Key::Char('j') => {
                 Key::Char('j') => {
-                    app.id_list.next();
-                    app.info_list.next().await;
+                    model.id_list.next();
+                    model.info_list.next().await;
                 }
                 }
                 Key::Char('k') => {
                 Key::Char('k') => {
-                    app.id_list.previous();
-                    app.info_list.previous().await;
+                    model.id_list.previous();
+                    model.info_list.previous().await;
                 }
                 }
                 _ => (),
                 _ => (),
             }
             }

+ 102 - 1
bin/map/src/model.rs

@@ -1,4 +1,4 @@
-use crate::{id_list::IdList, info_list::InfoList, node_info::NodeInfo};
+use tui::widgets::ListState;
 //use async_std::sync::Mutex;
 //use async_std::sync::Mutex;
 //use smol::Timer;
 //use smol::Timer;
 //use std::{collections::HashMap, time::Duration};
 //use std::{collections::HashMap, time::Duration};
@@ -46,8 +46,109 @@ impl Model {
     }
     }
 }
 }
 
 
+#[derive(Clone)]
+pub struct IdList {
+    pub state: ListState,
+    pub node_id: Vec<String>,
+}
+
+impl IdList {
+    pub fn new(node_id: Vec<String>) -> IdList {
+        IdList { state: ListState::default(), node_id }
+    }
+
+    pub fn next(&mut self) {
+        let i = match self.state.selected() {
+            Some(i) => {
+                if i >= self.node_id.len() - 1 {
+                    0
+                } else {
+                    i + 1
+                }
+            }
+            None => 0,
+        };
+        self.state.select(Some(i));
+    }
+
+    pub fn previous(&mut self) {
+        let i = match self.state.selected() {
+            Some(i) => {
+                if i == 0 {
+                    self.node_id.len() - 1
+                } else {
+                    i - 1
+                }
+            }
+            None => 0,
+        };
+        self.state.select(Some(i));
+    }
+
+    pub fn unselect(&mut self) {
+        self.state.select(None);
+    }
+}
+
+#[derive(Clone)]
+pub struct InfoList {
+    pub index: usize,
+    pub infos: Vec<NodeInfo>,
+}
+
+impl InfoList {
+    pub fn new(infos: Vec<NodeInfo>) -> InfoList {
+        let index = 0;
+
+        InfoList { index, infos }
+    }
+
+    pub async fn next(&mut self) {
+        self.index = (self.index + 1) % self.infos.len();
+    }
+
+    pub async fn previous(&mut self) {
+        if self.index > 0 {
+            self.index -= 1;
+        } else {
+            self.index = self.infos.len() - 1;
+        }
+    }
+}
+
 //impl Default for Model {
 //impl Default for Model {
 //    fn default() -> Self {
 //    fn default() -> Self {
 //        Self::new()
 //        Self::new()
 //    }
 //    }
 //}
 //}
+
+//TODO: made node_id into a HashSet(u32)
+// wrap NodeInfo and NodeId in a Mutex
+
+pub type NodeId = u32;
+
+#[derive(Clone)]
+pub struct NodeInfo {
+    pub id: String,
+    pub connections: usize,
+    pub is_active: bool,
+    pub last_message: String,
+}
+
+impl NodeInfo {
+    pub fn new() -> NodeInfo {
+        let connections = 0;
+        let is_active = false;
+        NodeInfo { id: String::new(), connections, is_active, last_message: String::new() }
+    }
+}
+
+impl Default for NodeInfo {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+//pub async fn add_seen(&self, id: u32) {
+//    self.privmsg_ids.lock().await.insert(id);
+//}

+ 0 - 0
bin/map/src/node_info.rs → bin/map/src/old/node_info.rs