Ver código fonte

model: created new data structures to handle p2p data

this change removes the struct called Connection and introduces ManualInfo, OutboundInfo and InboundInfo structs
lunar-mining 4 anos atrás
pai
commit
ab7650e3e0
3 arquivos alterados com 114 adições e 77 exclusões
  1. 31 32
      bin/dnetview/src/main.rs
  2. 47 9
      bin/dnetview/src/model.rs
  3. 36 36
      bin/dnetview/src/ui.rs

+ 31 - 32
bin/dnetview/src/main.rs

@@ -30,7 +30,7 @@ use url::Url;
 
 use dnetview::{
     config::{DnvConfig, CONFIG_FILE_CONTENTS},
-    model::{Connection, IdList, InfoList, NodeInfo},
+    model::{IdList, InfoList, NodeInfo},
     options::ProgramOptions,
     ui,
     view::{IdListView, InfoListView},
@@ -150,46 +150,45 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
         debug!("{:?}", reply);
 
         if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
-            let external_addr = reply.as_object().unwrap().get("external_addr");
-            let session_inbound = reply.as_object().unwrap().get("session_inbound");
-            let si_key =
-                session_inbound.unwrap().as_object().unwrap().get("key").unwrap().as_u64().unwrap();
+            //let external_addr = reply.as_object().unwrap().get("external_addr");
+            //let session_inbound = reply.as_object().unwrap().get("session_inbound");
+            //let si_key =
+            //    session_inbound.unwrap().as_object().unwrap().get("key").unwrap().as_u64().unwrap();
 
-            let session_manual = reply.as_object().unwrap().get("session_manual");
-            let sm_key =
-                session_manual.unwrap().as_object().unwrap().get("key").unwrap().as_u64().unwrap();
+            //let session_manual = reply.as_object().unwrap().get("session_manual");
+            //let sm_key =
+            //    session_manual.unwrap().as_object().unwrap().get("key").unwrap().as_u64().unwrap();
 
-            let session_outbound = reply.as_object().unwrap().get("session_outbound");
-            let so_key =
-                session_manual.unwrap().as_object().unwrap().get("key").unwrap().as_u64().unwrap();
+            //let session_outbound = reply.as_object().unwrap().get("session_outbound");
+            //let so_key =
+            //    session_manual.unwrap().as_object().unwrap().get("key").unwrap().as_u64().unwrap();
 
-            let channel_state = reply.as_object().unwrap().get("state").unwrap().as_str().unwrap();
-            let slots = reply.as_object().unwrap().get("slots");
+            //let channel_state = reply.as_object().unwrap().get("state").unwrap().as_str().unwrap();
+            //let slots = reply.as_object().unwrap().get("slots");
 
-            let session_in = Connection::new(si_key.to_string(), channel_state.to_string());
-            let session_man = Connection::new(sm_key.to_string(), channel_state.to_string());
-            let session_out = Connection::new(so_key.to_string(), channel_state.to_string());
+            //let session_in = Connection::new(si_key.to_string(), channel_state.to_string());
+            //let session_man = Connection::new(sm_key.to_string(), channel_state.to_string());
+            //let session_out = Connection::new(so_key.to_string(), channel_state.to_string());
 
-            let mut outconnects = Vec::new();
-            let mut inconnects = Vec::new();
-            let mut manconnects = Vec::new();
+            //let mut outconnects = Vec::new();
+            //let mut inconnects = Vec::new();
+            //let mut manconnects = Vec::new();
 
-            outconnects.push(session_out);
-            inconnects.push(session_in);
-            manconnects.push(session_man);
+            //outconnects.push(session_out);
+            //inconnects.push(session_in);
+            //manconnects.push(session_man);
 
-            let infos =
-                NodeInfo { outbound: outconnects, manual: manconnects, inbound: inconnects };
+            //let infos =
+            //    NodeInfo { outbound: outconnects, manual: manconnects, inbound: inconnects };
 
-            let mut node_info = HashMap::new();
-            // TODO: fix this. this key should be global identifier for each connection
-            // right now we are using si_key, which is the inbound session key.
-            node_info.insert(si_key, infos);
+            //let mut node_info = HashMap::new();
+            //// TODO: here we are setting the client url as the ID
+            //node_info.insert(client.url.clone(), infos);
 
-            for (si_key, value) in node_info.clone() {
-                model.id_list.node_id.lock().await.insert(si_key.to_string().clone());
-                model.info_list.infos.lock().await.insert(si_key.to_string(), value);
-            }
+            //for (si_key, value) in node_info.clone() {
+            //    model.id_list.node_id.lock().await.insert(si_key.to_string().clone());
+            //    model.info_list.infos.lock().await.insert(si_key.to_string(), value);
+            //}
         } else {
             // TODO: error handling
             debug!("Reply is empty");

+ 47 - 9
bin/dnetview/src/model.rs

@@ -1,5 +1,6 @@
 use async_std::sync::Mutex;
 use std::collections::{HashMap, HashSet};
+use std::net::SocketAddr;
 use tui::widgets::ListState;
 
 pub struct Model {
@@ -42,9 +43,9 @@ impl InfoList {
 
 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
 pub struct NodeInfo {
-    pub outbound: Vec<Connection>,
-    pub manual: Vec<Connection>,
-    pub inbound: Vec<Connection>,
+    pub outbound: Vec<OutboundInfo>,
+    pub manual: Vec<ManualInfo>,
+    pub inbound: Vec<InboundInfo>,
 }
 
 impl NodeInfo {
@@ -54,13 +55,50 @@ impl NodeInfo {
 }
 
 #[derive(Clone, Debug, PartialEq, Eq, Hash)]
-pub struct Connection {
-    pub id: String,
-    pub message: String,
+pub struct ManualInfo {
+    pub key: u64,
 }
 
-impl Connection {
-    pub fn new(id: String, message: String) -> Connection {
-        Connection { id, message }
+impl ManualInfo {
+    pub fn new(key: u64) -> ManualInfo {
+        ManualInfo { key }
+    }
+}
+
+#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+pub struct OutboundInfo {
+    // TODO:  make this a socket addr?
+    addr: String,
+    channel: Channel,
+    state: String,
+}
+
+impl OutboundInfo {
+    pub fn new(addr: String, channel: Channel, state: String) -> OutboundInfo {
+        OutboundInfo { addr, channel, state }
+    }
+}
+
+#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+pub struct Channel {
+    last_msg: String,
+    last_status: String,
+}
+
+impl Channel {
+    pub fn new(last_msg: String, last_status: String) -> Channel {
+        Channel { last_msg, last_status }
+    }
+}
+
+#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+pub struct InboundInfo {
+    connected: SocketAddr,
+    channel: Channel,
+}
+
+impl InboundInfo {
+    pub fn new(connected: SocketAddr, channel: Channel) -> InboundInfo {
+        InboundInfo { connected, channel }
     }
 }

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

@@ -88,23 +88,23 @@ fn render_info_left<B: Backend>(view: View, f: &mut Frame<'_, B>, _index: usize)
     let mut msgs = Vec::new();
 
     for id in &view.id_list.node_id {
-        match info.get(id) {
-            Some(connects) => {
-                msgs.push(Spans::from(""));
-                msgs.push(Spans::from(format!("[R: {}]", connects.outbound[0].message)));
-                //msgs.push(Spans::from(format!("[S: {}]", connects.outgoing[1].message)));
-                msgs.push(Spans::from(""));
-                msgs.push(Spans::from(format!("[R: {}]", connects.inbound[0].message)));
-                //msgs.push(Spans::from(format!("[S: {}]", connects.incoming[1].message)));
-                msgs.push(Spans::from(""));
-                msgs.push(Spans::from(""));
-                msgs.push(Spans::from(format!("[R: {}]", connects.manual[0].message)));
-                msgs.push(Spans::from(""));
-            }
-            None => {
-                // TODO
-            }
-        }
+        //match info.get(id) {
+        //    Some(connects) => {
+        //        msgs.push(Spans::from(""));
+        //        msgs.push(Spans::from(format!("[R: {}]", connects.outbound[0].message)));
+        //        //msgs.push(Spans::from(format!("[S: {}]", connects.outgoing[1].message)));
+        //        msgs.push(Spans::from(""));
+        //        msgs.push(Spans::from(format!("[R: {}]", connects.inbound[0].message)));
+        //        //msgs.push(Spans::from(format!("[S: {}]", connects.incoming[1].message)));
+        //        msgs.push(Spans::from(""));
+        //        msgs.push(Spans::from(""));
+        //        msgs.push(Spans::from(format!("[R: {}]", connects.manual[0].message)));
+        //        msgs.push(Spans::from(""));
+        //    }
+        //    None => {
+        //        // TODO
+        //    }
+        //}
     }
 
     let msg_graph = Paragraph::new(msgs).style(Style::default()).alignment(Alignment::Right);
@@ -121,25 +121,25 @@ fn render_info_left<B: Backend>(view: View, f: &mut Frame<'_, B>, _index: usize)
     let mut ids = Vec::new();
 
     for id in &view.id_list.node_id {
-        match info.get(id) {
-            Some(connects) => {
-                ids.push(Spans::from(""));
-                ids.push(Spans::from(format!("{}", connects.outbound[0].id)));
-                //ids.push(Spans::from(format!("{}", connects.outgoing[1].id)));
-                ids.push(Spans::from(""));
-                ids.push(Spans::from(""));
-                ids.push(Spans::from(format!("{}", connects.inbound[0].id)));
-                //ids.push(Spans::from(format!("{}", connects.incoming[1].id)));
-                ids.push(Spans::from(""));
-                ids.push(Spans::from(""));
-                ids.push(Spans::from(format!("{}", connects.manual[0].id)));
-                ids.push(Spans::from(""));
-                ids.push(Spans::from(""));
-            }
-            None => {
-                // TODO
-            }
-        }
+        //match info.get(id) {
+        //    Some(connects) => {
+        //        ids.push(Spans::from(""));
+        //        ids.push(Spans::from(format!("{}", connects.outbound[0].id)));
+        //        //ids.push(Spans::from(format!("{}", connects.outgoing[1].id)));
+        //        ids.push(Spans::from(""));
+        //        ids.push(Spans::from(""));
+        //        ids.push(Spans::from(format!("{}", connects.inbound[0].id)));
+        //        //ids.push(Spans::from(format!("{}", connects.incoming[1].id)));
+        //        ids.push(Spans::from(""));
+        //        ids.push(Spans::from(""));
+        //        ids.push(Spans::from(format!("{}", connects.manual[0].id)));
+        //        ids.push(Spans::from(""));
+        //        ids.push(Spans::from(""));
+        //    }
+        //    None => {
+        //        // TODO
+        //    }
+        //}
     }
 
     let id_graph = Paragraph::new(ids).style(Style::default()).alignment(Alignment::Left);