Przeglądaj źródła

create new data structures to enable addr sub-list

lunar-mining 4 lat temu
rodzic
commit
ba574bd75c

+ 2 - 2
bin/dnetview/src/lib.rs

@@ -5,7 +5,7 @@ pub mod ui;
 pub mod view;
 
 pub use config::{DnvConfig, CONFIG_FILE_CONTENTS};
-pub use model::{IdList, InfoList, Model, NodeInfo};
+pub use model::{AddrInfo, AddrList, IdList, InfoList, Model, NodeInfo};
 pub use options::ProgramOptions;
 pub use ui::ui;
-pub use view::{IdListView, InfoListView, View};
+pub use view::{AddrListView, IdListView, InfoListView, View};

+ 30 - 7
bin/dnetview/src/main.rs

@@ -27,10 +27,13 @@ use darkfi::{
 
 use dnetview::{
     config::{DnvConfig, CONFIG_FILE_CONTENTS},
-    model::{Channel, IdList, InboundInfo, InfoList, ManualInfo, NodeInfo, OutboundInfo, Slot},
+    model::{
+        AddrInfo, AddrList, Channel, IdList, InboundInfo, InfoList, ManualInfo, NodeInfo,
+        OutboundInfo, Slot,
+    },
     options::ProgramOptions,
     ui,
-    view::{IdListView, InfoListView},
+    view::{AddrListView, IdListView, InfoListView},
     Model, View,
 };
 
@@ -110,8 +113,9 @@ async fn main() -> Result<()> {
     let info_list = InfoList::new();
     let ids = FxHashSet::default();
     let id_list = IdList::new(ids);
+    let addr_list = AddrList::new();
 
-    let model = Arc::new(Model::new(id_list, info_list));
+    let model = Arc::new(Model::new(id_list, info_list, addr_list));
 
     let nthreads = num_cpus::get();
     let (signal, shutdown) = async_channel::unbounded::<()>();
@@ -160,6 +164,8 @@ async fn poll(client: DNetView, model: Arc<Model>) -> Result<()> {
             let mut mconnects = Vec::new();
             let mut oconnects = Vec::new();
             let mut slots = Vec::new();
+            let mut addrs = Vec::new();
+            let mut msgs = Vec::new();
 
             // parse inbound connection data
             let i_connected = &inbound_obj["connected"];
@@ -181,10 +187,12 @@ async fn poll(client: DNetView, model: Arc<Model>) -> Result<()> {
                     let msg = node.unwrap().get("last_msg").unwrap().as_str().unwrap().to_string();
                     let status =
                         node.unwrap().get("last_status").unwrap().as_str().unwrap().to_string();
-                    let channel = Channel::new(msg, status);
+                    let channel = Channel::new(msg.clone(), status);
                     let is_empty = false;
                     let iinfo = InboundInfo::new(is_empty, addr.clone(), channel);
                     iconnects.push(iinfo);
+                    addrs.push(addr);
+                    msgs.push(msg.clone());
                 }
             }
 
@@ -218,26 +226,41 @@ async fn poll(client: DNetView, model: Arc<Model>) -> Result<()> {
                     let new_slot = Slot::new(
                         is_empty,
                         addr.as_str().unwrap().to_string(),
-                        channel,
+                        channel.clone(),
                         state.as_str().unwrap().to_string(),
                     );
-                    slots.push(new_slot)
+                    slots.push(new_slot);
+                    addrs.push(addr.as_str().unwrap().to_string());
+                    msgs.push(channel.last_msg.clone());
                 }
             }
 
+            // create node_info
             let is_empty = is_empty_outbound(slots.clone());
             let oinfo = OutboundInfo::new(is_empty, slots.clone());
             oconnects.push(oinfo);
             let infos = NodeInfo { outbound: oconnects, manual: mconnects, inbound: iconnects };
             let mut node_info = FxHashMap::default();
-
             let node_name = &client.name.as_str();
             node_info.insert(&node_name, infos.clone());
 
+            // insert into model
             for (key, value) in node_info.clone() {
                 model.id_list.node_id.lock().await.insert(key.to_string().clone());
                 model.info_list.infos.lock().await.insert(key.to_string(), value);
             }
+
+            // TODO: this is just a placeholder. Later this will contain a message log.
+            // There's an obvious bug here (all addrs are matched with the same ainfo)
+            let mut addr_info = FxHashMap::default();
+            let ainfos = AddrInfo::new(msgs);
+            for addr in addrs {
+                addr_info.insert(addr, ainfos.clone());
+            }
+
+            for (key, value) in addr_info.clone() {
+                model.addr_list.infos.lock().await.insert(key.to_string(), value);
+            }
         } else {
             // TODO: error handling
             //debug!("Reply is empty");

+ 30 - 2
bin/dnetview/src/model.rs

@@ -7,11 +7,12 @@ use tui::widgets::ListState;
 pub struct Model {
     pub id_list: IdList,
     pub info_list: InfoList,
+    pub addr_list: AddrList,
 }
 
 impl Model {
-    pub fn new(id_list: IdList, info_list: InfoList) -> Model {
-        Model { id_list, info_list }
+    pub fn new(id_list: IdList, info_list: InfoList, addr_list: AddrList) -> Model {
+        Model { id_list, info_list, addr_list }
     }
 }
 
@@ -48,6 +49,21 @@ impl Default for InfoList {
     }
 }
 
+pub struct AddrList {
+    pub index: Mutex<usize>,
+    pub infos: Mutex<FxHashMap<String, AddrInfo>>,
+}
+
+impl AddrList {
+    pub fn new() -> AddrList {
+        let index = 0;
+        let index = Mutex::new(index);
+        let infos = Mutex::new(FxHashMap::default());
+
+        AddrList { index, infos }
+    }
+}
+
 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
 pub struct NodeInfo {
     pub outbound: Vec<OutboundInfo>,
@@ -128,3 +144,15 @@ impl InboundInfo {
         InboundInfo { is_empty, connected, channel }
     }
 }
+
+#[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
+pub struct AddrInfo {
+    // TODO: this will be a message log
+    pub msgs: Vec<String>,
+}
+
+impl AddrInfo {
+    pub fn new(msgs: Vec<String>) -> AddrInfo {
+        AddrInfo { msgs }
+    }
+}

+ 6 - 0
bin/dnetview/src/ui.rs

@@ -24,7 +24,10 @@ pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
         let id_span = Span::raw(id.to_string());
         let mut lines = vec![Spans::from(id_span)];
         data.push(id.to_string());
+        debug!("1 LINES: {:?}", lines);
 
+        // create a new vector of addresses
+        // render as a sub node
         match &view.info_list.infos.get(id) {
             Some(node) => {
                 if !node.outbound.iter().all(|node| node.is_empty == true) {
@@ -84,8 +87,11 @@ pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
             }
         }
 
+        debug!("2 LINES: {:?}", lines);
         let ids = ListItem::new(lines);
+        debug!("1 IDS: {:?}", ids);
         nodes.push(ids);
+        debug!("1 NODES: {:?}", nodes);
     }
 
     let nodes =

+ 29 - 0
bin/dnetview/src/view.rs

@@ -1,4 +1,5 @@
 use fxhash::{FxHashMap, FxHashSet};
+use log::debug;
 use tui::widgets::ListState;
 
 use crate::model::NodeInfo;
@@ -44,6 +45,7 @@ impl IdListView {
             None => 0,
         };
         self.state.select(Some(i));
+        debug!("NEXT STATE {:?}", i);
     }
 
     pub fn previous(&mut self) {
@@ -58,6 +60,7 @@ impl IdListView {
             None => 0,
         };
         self.state.select(Some(i));
+        debug!("PREV STATE {:?}", i);
     }
 
     pub fn unselect(&mut self) {
@@ -90,3 +93,29 @@ impl InfoListView {
         }
     }
 }
+
+#[derive(Clone)]
+pub struct AddrListView {
+    pub index: usize,
+    pub infos: FxHashMap<String, NodeInfo>,
+}
+
+impl AddrListView {
+    pub fn new(infos: FxHashMap<String, NodeInfo>) -> AddrListView {
+        let index = 0;
+
+        AddrListView { 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;
+        }
+    }
+}