Przeglądaj źródła

dnetview: create enum SelectableObject and pass to view, model

this commit breaks ui::ui()
lunar-mining 4 lat temu
rodzic
commit
668fae0617

+ 21 - 3
bin/dnetview/src/main.rs

@@ -27,7 +27,10 @@ use darkfi::{
 
 use dnetview::{
     config::{DnvConfig, CONFIG_FILE_CONTENTS},
-    model::{Channel, IdList, InboundInfo, InfoList, ManualInfo, NodeInfo, OutboundInfo, Slot},
+    model::{
+        Channel, ConnectInfo, IdList, InboundInfo, InfoList, ManualInfo, NodeInfo, OutboundInfo,
+        SelectableObject, SessionInfo, Slot,
+    },
     options::ProgramOptions,
     ui,
     view::{IdListView, InfoListView},
@@ -156,6 +159,8 @@ async fn poll(client: DNetView, model: Arc<Model>) -> Result<()> {
             let manual_obj = &reply.as_object().unwrap()["session_manual"];
             let outbound_obj = &reply.as_object().unwrap()["session_outbound"];
 
+            let mut model_vec: Vec<SelectableObject> = Vec::new();
+
             let mut iconnects = Vec::new();
             let mut mconnects = Vec::new();
             let mut oconnects = Vec::new();
@@ -235,15 +240,28 @@ async fn poll(client: DNetView, model: Arc<Model>) -> Result<()> {
             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 node = SelectableObject::Node(infos.clone());
+            model_vec.push(node);
+
+            let session_infos = SessionInfo::new();
+            let session = SelectableObject::Session(session_infos);
+            model_vec.push(session);
+
+            let connect_infos = ConnectInfo::new();
+            let connect = SelectableObject::Connect(connect_infos);
+            model_vec.push(connect);
+
             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() {
+            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);
+                // value
+                model.info_list.infos.lock().await.insert(key.to_string(), model_vec.clone());
             }
         } else {
             // TODO: error handling

+ 35 - 1
bin/dnetview/src/model.rs

@@ -4,6 +4,16 @@ use fxhash::{FxHashMap, FxHashSet};
 use serde::Deserialize;
 use tui::widgets::ListState;
 
+#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+pub enum SelectableObject {
+    // top level selectable object
+    Node(NodeInfo),
+    // outgoing or incoming sessions
+    Session(SessionInfo),
+    // connections: outgoing, incoming or manual
+    Connect(ConnectInfo),
+}
+
 pub struct Model {
     pub id_list: IdList,
     pub info_list: InfoList,
@@ -29,7 +39,7 @@ impl IdList {
 
 pub struct InfoList {
     pub index: Mutex<usize>,
-    pub infos: Mutex<FxHashMap<String, NodeInfo>>,
+    pub infos: Mutex<FxHashMap<String, Vec<SelectableObject>>>,
 }
 
 impl InfoList {
@@ -128,3 +138,27 @@ impl InboundInfo {
         InboundInfo { is_empty, connected, channel }
     }
 }
+
+#[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
+pub struct ConnectInfo {
+    pub message: String,
+}
+
+impl ConnectInfo {
+    pub fn new() -> ConnectInfo {
+        let message = "This is the connect screen".to_string();
+        ConnectInfo { message }
+    }
+}
+
+#[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
+pub struct SessionInfo {
+    pub message: String,
+}
+
+impl SessionInfo {
+    pub fn new() -> SessionInfo {
+        let message = "This is the connect screen".to_string();
+        SessionInfo { message }
+    }
+}

+ 42 - 42
bin/dnetview/src/ui.rs

@@ -27,48 +27,48 @@ pub fn ui<B: Backend>(f: &mut Frame<'_, B>, mut view: View) {
         // render as a sub node
         match &view.info_list.infos.get(id) {
             Some(node) => {
-                if !node.outbound.iter().all(|node| node.is_empty) {
-                    lines.push(Spans::from(Span::styled("   Outgoing", Style::default())));
-                }
-                for outbound in &node.outbound.clone() {
-                    for slot in outbound.slots.clone() {
-                        let addr = Span::styled(format!("       {}", slot.addr), style);
-                        let msg: Span = match slot.channel.last_status.as_str() {
-                            "recv" => Span::styled(
-                                format!("               [R: {}]", slot.channel.last_msg),
-                                style,
-                            ),
-                            "sent" => Span::styled(
-                                format!("               [S: {}]", slot.channel.last_msg),
-                                style,
-                            ),
-                            a => Span::styled(a.to_string(), style),
-                        };
-                        lines.push(Spans::from(vec![addr, msg]));
-                    }
-                }
-                if !node.inbound.iter().all(|node| node.is_empty) {
-                    lines.push(Spans::from(Span::styled("   Incoming", Style::default())));
-                }
-                for inbound in &node.inbound {
-                    let addr = Span::styled(format!("       {}", inbound.connected), style);
-                    let msg: Span = match inbound.channel.last_status.as_str() {
-                        "recv" => Span::styled(
-                            format!("               [R: {}]", inbound.channel.last_msg),
-                            style,
-                        ),
-                        "sent" => Span::styled(
-                            format!("               [R: {}]", inbound.channel.last_msg),
-                            style,
-                        ),
-                        a => Span::styled(a.to_string(), style),
-                    };
-                    lines.push(Spans::from(vec![addr, msg]));
-                }
-                lines.push(Spans::from(Span::styled("   Manual", Style::default())));
-                for connect in &node.manual {
-                    lines.push(Spans::from(Span::styled(format!("       {}", connect.key), style)));
-                }
+                //if !node.[0].outbound.iter().all(|node| node.is_empty) {
+                //    lines.push(Spans::from(Span::styled("   Outgoing", Style::default())));
+                //}
+                //for outbound in &node.outbound.clone() {
+                //    for slot in outbound.slots.clone() {
+                //        let addr = Span::styled(format!("       {}", slot.addr), style);
+                //        let msg: Span = match slot.channel.last_status.as_str() {
+                //            "recv" => Span::styled(
+                //                format!("               [R: {}]", slot.channel.last_msg),
+                //                style,
+                //            ),
+                //            "sent" => Span::styled(
+                //                format!("               [S: {}]", slot.channel.last_msg),
+                //                style,
+                //            ),
+                //            a => Span::styled(a.to_string(), style),
+                //        };
+                //        lines.push(Spans::from(vec![addr, msg]));
+                //    }
+                //}
+                //if !node.inbound.iter().all(|node| node.is_empty) {
+                //    lines.push(Spans::from(Span::styled("   Incoming", Style::default())));
+                //}
+                //for inbound in &node.inbound {
+                //    let addr = Span::styled(format!("       {}", inbound.connected), style);
+                //    let msg: Span = match inbound.channel.last_status.as_str() {
+                //        "recv" => Span::styled(
+                //            format!("               [R: {}]", inbound.channel.last_msg),
+                //            style,
+                //        ),
+                //        "sent" => Span::styled(
+                //            format!("               [R: {}]", inbound.channel.last_msg),
+                //            style,
+                //        ),
+                //        a => Span::styled(a.to_string(), style),
+                //    };
+                //    lines.push(Spans::from(vec![addr, msg]));
+                //}
+                //lines.push(Spans::from(Span::styled("   Manual", Style::default())));
+                //for connect in &node.manual {
+                //    lines.push(Spans::from(Span::styled(format!("       {}", connect.key), style)));
+                //}
             }
             None => {
                 // TODO

+ 4 - 4
bin/dnetview/src/view.rs

@@ -2,7 +2,7 @@ use fxhash::{FxHashMap, FxHashSet};
 //use log::debug;
 use tui::widgets::ListState;
 
-use crate::model::NodeInfo;
+use crate::model::{NodeInfo, SelectableObject};
 
 #[derive(Clone)]
 pub struct View {
@@ -15,7 +15,7 @@ impl View {
         View { id_list, info_list }
     }
 
-    pub fn update(&mut self, infos: FxHashMap<String, NodeInfo>) {
+    pub fn update(&mut self, infos: FxHashMap<String, Vec<SelectableObject>>) {
         for (id, info) in infos {
             self.id_list.node_id.insert(id.clone());
             self.info_list.infos.insert(id, info);
@@ -69,11 +69,11 @@ impl IdListView {
 #[derive(Clone)]
 pub struct InfoListView {
     pub index: usize,
-    pub infos: FxHashMap<String, NodeInfo>,
+    pub infos: FxHashMap<String, Vec<SelectableObject>>,
 }
 
 impl InfoListView {
-    pub fn new(infos: FxHashMap<String, NodeInfo>) -> InfoListView {
+    pub fn new(infos: FxHashMap<String, Vec<SelectableObject>>) -> InfoListView {
         let index = 0;
 
         InfoListView { index, infos }