From 668fae06177fc7edfcbfdfba28d3eae7a8526d3b Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Sat, 16 Apr 2022 10:23:23 +0200 Subject: [PATCH] dnetview: create enum SelectableObject and pass to view, model this commit breaks ui::ui() --- bin/dnetview/src/main.rs | 24 +++++++++-- bin/dnetview/src/model.rs | 36 ++++++++++++++++- bin/dnetview/src/ui.rs | 84 +++++++++++++++++++-------------------- bin/dnetview/src/view.rs | 8 ++-- 4 files changed, 102 insertions(+), 50 deletions(-) diff --git a/bin/dnetview/src/main.rs b/bin/dnetview/src/main.rs index b7ec37c68..9a0402f46 100644 --- a/bin/dnetview/src/main.rs +++ b/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) -> Result<()> { let manual_obj = &reply.as_object().unwrap()["session_manual"]; let outbound_obj = &reply.as_object().unwrap()["session_outbound"]; + let mut model_vec: Vec = 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) -> 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 diff --git a/bin/dnetview/src/model.rs b/bin/dnetview/src/model.rs index dbf72aae1..e7513ef84 100644 --- a/bin/dnetview/src/model.rs +++ b/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, - pub infos: Mutex>, + pub infos: Mutex>>, } 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 } + } +} diff --git a/bin/dnetview/src/ui.rs b/bin/dnetview/src/ui.rs index eed270ccf..6942e4f86 100644 --- a/bin/dnetview/src/ui.rs +++ b/bin/dnetview/src/ui.rs @@ -27,48 +27,48 @@ pub fn ui(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 diff --git a/bin/dnetview/src/view.rs b/bin/dnetview/src/view.rs index cfd84b7f6..a8df70b61 100644 --- a/bin/dnetview/src/view.rs +++ b/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) { + pub fn update(&mut self, infos: FxHashMap>) { 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, + pub infos: FxHashMap>, } impl InfoListView { - pub fn new(infos: FxHashMap) -> InfoListView { + pub fn new(infos: FxHashMap>) -> InfoListView { let index = 0; InfoListView { index, infos }