diff --git a/bin/dnetview/src/model.rs b/bin/dnetview/src/model.rs index 9415ec3d6..911ec5c21 100644 --- a/bin/dnetview/src/model.rs +++ b/bin/dnetview/src/model.rs @@ -86,8 +86,8 @@ pub struct SessionInfo { pub dnet_id: String, pub node_id: String, pub addr: String, - pub state: String, - pub info: Vec, + pub state: Option, + pub info: SlotInfo, pub is_empty: bool, } @@ -96,8 +96,8 @@ impl SessionInfo { dnet_id: String, node_id: String, addr: String, - state: String, - info: Vec, + state: Option, + info: SlotInfo, is_empty: bool, ) -> Self { Self { dnet_id, node_id, addr, state, info, is_empty } diff --git a/bin/dnetview/src/parser.rs b/bin/dnetview/src/parser.rs index 8aabbf288..d58ab769c 100644 --- a/bin/dnetview/src/parser.rs +++ b/bin/dnetview/src/parser.rs @@ -33,7 +33,7 @@ use crate::{ LilithInfo, Model, NetworkInfo, NodeInfo, SelectableObject, Session, SessionInfo, SlotInfo, }, rpc::RpcConnect, - util::{is_empty_session, make_empty_id, make_info_id, make_node_id, make_session_id}, + util::{make_empty_id, make_info_id, make_node_id, make_session_id}, }; pub struct DataParser { @@ -123,14 +123,13 @@ impl DataParser { let name = "Offline".to_string(); let session_type = Session::Offline; - let mut slots: Vec = Vec::new(); let mut sessions: Vec = Vec::new(); let hosts = Vec::new(); let node_id = make_node_id(&node_name)?; let dnet_id = make_empty_id(&node_id, &session_type, 0)?; let addr = "Null".to_string(); - let state = "Null".to_string(); + let state = None; let random_id = 0; let remote_id = "Null".to_string(); let log = Vec::new(); @@ -145,7 +144,6 @@ impl DataParser { log, is_empty, ); - slots.push(slot.clone()); let session_info = SessionInfo::new( dnet_id, @@ -153,7 +151,7 @@ impl DataParser { //name.clone(), addr.clone(), state, - slots, + slot, is_empty, ); sessions.push(session_info); @@ -233,40 +231,44 @@ impl DataParser { outbounds: Vec, ) -> DnetViewResult<()> { for inbound in inbounds { - for info in inbound.info { - if !self.model.msg_map.lock().await.contains_key(&info.dnet_id) { - // we don't have this ID: it is a new node - self.model.msg_map.lock().await.insert(info.dnet_id, info.log.clone()); - } else { - // we have this id: append the msg values - match self.model.msg_map.lock().await.entry(info.dnet_id) { - Entry::Vacant(e) => { - e.insert(info.log); - } - Entry::Occupied(mut e) => { - for msg in info.log { - e.get_mut().push(msg); - } + if !self.model.msg_map.lock().await.contains_key(&inbound.info.dnet_id) { + // we don't have this ID: it is a new node + self.model + .msg_map + .lock() + .await + .insert(inbound.info.dnet_id, inbound.info.log.clone()); + } else { + // we have this id: append the msg values + match self.model.msg_map.lock().await.entry(inbound.info.dnet_id) { + Entry::Vacant(e) => { + e.insert(inbound.info.log); + } + Entry::Occupied(mut e) => { + for msg in inbound.info.log { + e.get_mut().push(msg); } } } } } for outbound in outbounds { - for info in outbound.info { - if !self.model.msg_map.lock().await.contains_key(&info.dnet_id) { - // we don't have this ID: it is a new node - self.model.msg_map.lock().await.insert(info.dnet_id, info.log.clone()); - } else { - // we have this id: append the msg values - match self.model.msg_map.lock().await.entry(info.dnet_id) { - Entry::Vacant(e) => { - e.insert(info.log); - } - Entry::Occupied(mut e) => { - for msg in info.log { - e.get_mut().push(msg); - } + if !self.model.msg_map.lock().await.contains_key(&outbound.info.dnet_id) { + // we don't have this ID: it is a new node + self.model + .msg_map + .lock() + .await + .insert(outbound.info.dnet_id, outbound.info.log.clone()); + } else { + // we have this id: append the msg values + match self.model.msg_map.lock().await.entry(outbound.info.dnet_id) { + Entry::Vacant(e) => { + e.insert(outbound.info.log); + } + Entry::Occupied(mut e) => { + for msg in outbound.info.log { + e.get_mut().push(msg); } } } @@ -291,14 +293,12 @@ impl DataParser { .lock() .await .insert(inbound.clone().dnet_id, inbound_obj.clone()); - for info in inbound.info { - let info_obj = SelectableObject::Connect(info.clone()); - self.model - .selectables - .lock() - .await - .insert(info.clone().dnet_id, info_obj.clone()); - } + let info_obj = SelectableObject::Connect(inbound.info.clone()); + self.model + .selectables + .lock() + .await + .insert(inbound.info.clone().dnet_id, info_obj.clone()); } } for outbound in node.outbound { @@ -309,14 +309,12 @@ impl DataParser { .lock() .await .insert(outbound.clone().dnet_id, outbound_obj.clone()); - for info in outbound.info { - let info_obj = SelectableObject::Connect(info.clone()); - self.model - .selectables - .lock() - .await - .insert(info.clone().dnet_id, info_obj.clone()); - } + let info_obj = SelectableObject::Connect(outbound.info.clone()); + self.model + .selectables + .lock() + .await + .insert(outbound.info.clone().dnet_id, info_obj.clone()); } } } @@ -330,7 +328,6 @@ impl DataParser { prefix: Session, ) -> DnetViewResult> { let session_id = make_session_id(&node_id, &prefix)?; - let mut slots: Vec = Vec::new(); let mut session_info: Vec = Vec::new(); // TODO: improve this ugly hack. @@ -357,33 +354,31 @@ impl DataParser { log, is_empty, ); - slots.push(slot); - // Check whether the session is empty. - let is_empty = is_empty_session(&slots); + let is_empty = true; let addr = "Null".to_string(); - let state = "Null".to_string(); - let session = SessionInfo::new( - session_id.clone(), - node_id.clone(), - addr, - state, - slots.clone(), - is_empty, - ); + let state = None; + let session = + SessionInfo::new(session_id.clone(), node_id.clone(), addr, state, slot, is_empty); session_info.push(session); return Ok(session_info) } let sessions = reply.as_array().unwrap(); + debug!(target: "dnetview", "parse_outbound() len session{:?}", sessions.len()); for session in sessions { match session.as_object() { Some(obj) => { debug!(target: "dnetview", "parse_outbound() obj {:?}", session); let addr = obj.get("addr").unwrap().as_str().unwrap().to_string(); - let state = obj.get("state").unwrap().as_str().unwrap().to_string(); + + let state: Option = match obj.get("state") { + Some(state) => Some(state.as_str().unwrap().to_string()), + None => None, + }; + let info: serde_json::Map = serde_json::from_value(obj.get("info").unwrap().clone()).unwrap(); @@ -408,14 +403,13 @@ impl DataParser { log, is_empty, ); - slots.push(slot); let session = SessionInfo::new( session_id.clone(), node_id.clone(), addr.clone(), state, - slots.clone(), + slot, is_empty, ); session_info.push(session); @@ -440,17 +434,15 @@ impl DataParser { log, is_empty, ); - slots.push(slot); - // Check whether the session is empty. - let is_empty = is_empty_session(&slots); + let is_empty = true; - let state = "Null".to_string(); + let state = None; let session = SessionInfo::new( session_id.clone(), node_id.clone(), addr.clone(), state, - slots.clone(), + slot, is_empty, ); session_info.push(session); diff --git a/bin/dnetview/src/view.rs b/bin/dnetview/src/view.rs index b5974d47d..e2c0f842d 100644 --- a/bin/dnetview/src/view.rs +++ b/bin/dnetview/src/view.rs @@ -98,11 +98,11 @@ impl<'a> View { if !self.ordered_list.iter().any(|i| i == &inbound.dnet_id) { self.ordered_list.push(inbound.dnet_id.clone()); } - for info in &inbound.info { - if !self.ordered_list.iter().any(|i| i == &info.dnet_id) { - self.ordered_list.push(info.dnet_id.clone()); - } + //for info in &inbound.info { + if !self.ordered_list.iter().any(|i| i == &inbound.info.dnet_id) { + self.ordered_list.push(inbound.info.dnet_id.clone()); } + //} } } for outbound in &node.outbound { @@ -110,11 +110,11 @@ impl<'a> View { if !self.ordered_list.iter().any(|i| i == &outbound.dnet_id) { self.ordered_list.push(outbound.dnet_id.clone()); } - for info in &outbound.info { - if !self.ordered_list.iter().any(|i| i == &info.dnet_id) { - self.ordered_list.push(info.dnet_id.clone()); - } + //for info in &outbound.info { + if !self.ordered_list.iter().any(|i| i == &outbound.info.dnet_id) { + self.ordered_list.push(outbound.info.dnet_id.clone()); } + //} } } } @@ -221,37 +221,36 @@ impl<'a> View { let lines = vec![Spans::from(name)]; let names = ListItem::new(lines); nodes.push(names); - for info in &inbound.info { - let mut infos = Vec::new(); - match info.addr.as_str() { - "Null" => { - let style = Style::default() - .fg(Color::Blue) - .add_modifier(Modifier::ITALIC); - let name = Span::styled( - format!(" {} ", info.addr), + //for info in &inbound.info { + let mut infos = Vec::new(); + match inbound.info.addr.as_str() { + "Null" => { + let style = Style::default() + .fg(Color::Blue) + .add_modifier(Modifier::ITALIC); + let name = Span::styled( + format!(" {} ", inbound.info.addr), + style, + ); + infos.push(name); + } + addr => { + let name = Span::styled(format!(" {}", addr), style); + infos.push(name); + if !inbound.info.remote_id.is_empty() { + let remote_id = Span::styled( + format!("({})", inbound.info.remote_id), style, ); - infos.push(name); - } - addr => { - let name = - Span::styled(format!(" {}", addr), style); - infos.push(name); - if !info.remote_id.is_empty() { - let remote_id = Span::styled( - format!("({})", info.remote_id), - style, - ); - infos.push(remote_id) - } + infos.push(remote_id) } } - - let lines = vec![Spans::from(infos)]; - let names = ListItem::new(lines); - nodes.push(names); } + + let lines = vec![Spans::from(infos)]; + let names = ListItem::new(lines); + nodes.push(names); + //} } } @@ -261,37 +260,36 @@ impl<'a> View { let lines = vec![Spans::from(name)]; let names = ListItem::new(lines); nodes.push(names); - for info in &outbound.info { - let mut infos = Vec::new(); - match info.addr.as_str() { - "Null" => { - let style = Style::default() - .fg(Color::Blue) - .add_modifier(Modifier::ITALIC); - let name = Span::styled( - format!(" {} ", info.addr), + //for info in &outbound.info { + let mut infos = Vec::new(); + match outbound.info.addr.as_str() { + "Null" => { + let style = Style::default() + .fg(Color::Blue) + .add_modifier(Modifier::ITALIC); + let name = Span::styled( + format!(" {} ", outbound.info.addr), + style, + ); + infos.push(name); + } + addr => { + let name = Span::styled(format!(" {}", addr), style); + infos.push(name); + if !outbound.info.remote_id.is_empty() { + let remote_id = Span::styled( + format!("({})", outbound.info.remote_id), style, ); - infos.push(name); - } - addr => { - let name = - Span::styled(format!(" {}", addr), style); - infos.push(name); - if !info.remote_id.is_empty() { - let remote_id = Span::styled( - format!("({})", info.remote_id), - style, - ); - infos.push(remote_id) - } + infos.push(remote_id) } } - - let lines = vec![Spans::from(infos)]; - let names = ListItem::new(lines); - nodes.push(names); } + + let lines = vec![Spans::from(infos)]; + let names = ListItem::new(lines); + nodes.push(names); + //} } } } @@ -381,11 +379,22 @@ impl<'a> View { let addr = Span::styled(format!("Addr: {}", session.addr), style); lines.push(Spans::from(addr)); - // TODO: this will be an option - let addr = Span::styled(format!("State: {}", session.state), style); - lines.push(Spans::from(addr)); + if session.state.is_some() { + let addr = Span::styled( + format!("State: {}", session.state.as_ref().unwrap()), + style, + ); + lines.push(Spans::from(addr)); + } } Some(SelectableObject::Connect(connect)) => { + // TODO: this renders in the top right and overwrites the msg log + //let addr = Span::styled(format!("Addr: {}", connect.addr), style); + //let random_id = Span::styled(format!("Random id: {}", connect.random_id), style); + //let remote_id = Span::styled(format!("Remote id: {}", connect.remote_id), style); + //lines.push(Spans::from(addr)); + //lines.push(Spans::from(random_id)); + //lines.push(Spans::from(remote_id)); //debug!(target: "dnetview", "render_info()::SelectableObject::Connect"); let text = self.parse_msg_list(connect.dnet_id.clone())?; f.render_stateful_widget(text, slice[1], &mut self.msg_list.state);