Просмотр исходного кода

dnetview: add dnet_enabled() flag to NodeInfo and update View

View now displays a warning when dnetview is not enabled.
lunar-mining 3 лет назад
Родитель
Сommit
51bfdfdd58
4 измененных файлов с 131 добавлено и 106 удалено
  1. 4 1
      bin/dnetview/src/model.rs
  2. 30 44
      bin/dnetview/src/parser.rs
  3. 20 0
      bin/dnetview/src/util.rs
  4. 77 61
      bin/dnetview/src/view.rs

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

@@ -31,6 +31,7 @@ pub enum Session {
     Outbound,
     //Manual,
     Offline,
+    Null,
 }
 
 #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
@@ -66,6 +67,7 @@ pub struct NodeInfo {
     pub inbound: Vec<SessionInfo>,
     pub outbound: Vec<SessionInfo>,
     pub is_offline: bool,
+    pub dnet_enabled: bool,
 }
 
 impl NodeInfo {
@@ -76,8 +78,9 @@ impl NodeInfo {
         inbound: Vec<SessionInfo>,
         outbound: Vec<SessionInfo>,
         is_offline: bool,
+        dnet_enabled: bool,
     ) -> Self {
-        Self { dnet_id, name, hosts, inbound, outbound, is_offline }
+        Self { dnet_id, name, hosts, inbound, outbound, is_offline, dnet_enabled }
     }
 }
 

+ 30 - 44
bin/dnetview/src/parser.rs

@@ -33,7 +33,9 @@ use crate::{
         LilithInfo, Model, NetworkInfo, NodeInfo, SelectableObject, Session, SessionInfo, SlotInfo,
     },
     rpc::RpcConnect,
-    util::{make_empty_id, make_info_id, make_network_id, make_node_id, make_session_id},
+    util::{
+        make_empty_id, make_info_id, make_network_id, make_node_id, make_null_id, make_session_id,
+    },
 };
 
 pub struct DataParser {
@@ -119,7 +121,7 @@ impl DataParser {
     // If poll times out, inititalize data structures with empty values.
     async fn parse_offline(&self, node_name: String) -> DnetViewResult<()> {
         debug!(target: "dnetview", "parse_offline() START");
-        let name = "Offline".to_string();
+        //let name = "Offline".to_string();
         let sort = Session::Offline;
 
         let mut sessions: Vec<SessionInfo> = Vec::new();
@@ -158,11 +160,12 @@ impl DataParser {
         // TODO: clean this up
         let node = NodeInfo::new(
             node_id.clone(),
-            name.clone(),
+            node_name.clone(),
             hosts,
             sessions.clone(),
             sessions.clone(),
             is_empty,
+            true,
         );
 
         self.update_selectables(node).await?;
@@ -180,11 +183,28 @@ impl DataParser {
 
         let node_id = make_node_id(&name)?;
 
+        let dnet_enabled: bool = {
+            if hosts.is_null() && inbound.is_null() && outbound.is_null() {
+                false
+            } else {
+                true
+            }
+        };
+        debug!("dnet_enabled? {}", dnet_enabled);
+
         let hosts = self.parse_hosts(hosts).await?;
         let inbound = self.parse_session(inbound, &node_id, Session::Inbound).await?;
         let outbound = self.parse_session(outbound, &node_id, Session::Outbound).await?;
 
-        let node = NodeInfo::new(node_id, name, hosts, inbound.clone(), outbound.clone(), false);
+        let node = NodeInfo::new(
+            node_id,
+            name,
+            hosts,
+            inbound.clone(),
+            outbound.clone(),
+            false,
+            dnet_enabled,
+        );
 
         self.update_selectables(node).await?;
         self.update_msgs(inbound.clone(), outbound.clone()).await?;
@@ -329,13 +349,10 @@ impl DataParser {
         let session_id = make_session_id(&node_id, &sort)?;
         let mut session_info: Vec<SessionInfo> = Vec::new();
 
-        // TODO: improve this ugly hack.
-        let mut slot_count = 0;
-
         // Dnetview is not enabled.
         if reply.is_null() {
-            slot_count += 1;
-            let info_id = make_empty_id(&node_id, &sort, slot_count)?;
+            let sort2 = Session::Null;
+            let info_id = make_null_id(&node_id)?;
             let node_id = node_id.to_string();
             let addr = "Null".to_string();
             let random_id = 0;
@@ -357,12 +374,13 @@ impl DataParser {
             let addr = "Null".to_string();
             let state = None;
             let session = SessionInfo::new(
-                session_id.clone(),
+                // ..
+                info_id.clone(),
                 node_id.clone(),
                 addr,
                 state,
                 slot,
-                sort.clone(),
+                sort2.clone(),
                 is_empty,
             );
             session_info.push(session);
@@ -377,7 +395,6 @@ impl DataParser {
             if !session.is_null() {
                 match session.as_object() {
                     Some(obj) => {
-                        debug!(target: "dnetview", "parse_outbound() OBJ {:?}", obj);
                         let addr = obj.get("addr").unwrap().as_str().unwrap().to_string();
 
                         let state: Option<String> = match obj.get("state") {
@@ -423,38 +440,7 @@ impl DataParser {
                         session_info.push(session);
                     }
                     None => {
-                        // TODO: clean up empty info boilerplate.
-                        slot_count += 1;
-                        let info_id = make_empty_id(node_id, &sort, slot_count)?;
-                        let node_id = node_id.to_string();
-                        let addr = "Null".to_string();
-                        let random_id = 0;
-                        let remote_id = "Null".to_string();
-                        let log = Vec::new();
-                        let is_empty = true;
-
-                        let slot = SlotInfo::new(
-                            info_id.clone(),
-                            node_id.clone(),
-                            addr.clone(),
-                            random_id,
-                            remote_id,
-                            log,
-                            is_empty,
-                        );
-                        let is_empty = true;
-
-                        let state = None;
-                        let session = SessionInfo::new(
-                            session_id.clone(),
-                            node_id.clone(),
-                            addr.clone(),
-                            state,
-                            slot.clone(),
-                            sort.clone(),
-                            is_empty,
-                        );
-                        session_info.push(session);
+                        return Err(DnetViewError::ValueIsNotObject)
                     }
                 }
             }

+ 20 - 0
bin/dnetview/src/util.rs

@@ -32,6 +32,12 @@ pub fn make_network_id(node_name: &String) -> Result<String> {
     Ok(id)
 }
 
+pub fn make_null_id(node_name: &String) -> Result<String> {
+    let mut id = hex::encode(node_name);
+    id.insert_str(0, "NULL");
+    Ok(id)
+}
+
 pub fn make_session_id(node_id: &str, session: &Session) -> Result<String> {
     let mut num = 0_u64;
 
@@ -40,6 +46,7 @@ pub fn make_session_id(node_id: &str, session: &Session) -> Result<String> {
         Session::Outbound => vec!['o', 'u', 't'],
         //Session::Manual => vec!['m', 'a', 'n'],
         Session::Offline => vec!['o', 'f', 'f'],
+        Session::Null => vec!['n', 'u', 'l', 'l'],
     };
 
     for i in session_chars {
@@ -119,6 +126,19 @@ pub fn make_empty_id(node_id: &str, session: &Session, count: u64) -> Result<Str
             id.insert_str(0, "EMPTYOFF");
             id
         }
+        Session::Null => {
+            let session_chars = vec!['n', 'u', 'l', 'l'];
+            for i in session_chars {
+                num += i as u64
+            }
+            for i in node_id.chars() {
+                num += i as u64
+            }
+            num += count;
+            let mut id = hex::encode(num.to_ne_bytes());
+            id.insert_str(0, "NULL");
+            id
+        }
     };
 
     Ok(id)

+ 77 - 61
bin/dnetview/src/view.rs

@@ -195,7 +195,8 @@ impl<'a> View {
             match obj {
                 SelectableObject::Node(node) => {
                     if node.is_offline {
-                        let style = Style::default().fg(Color::Blue).add_modifier(Modifier::ITALIC);
+                        let style =
+                            Style::default().fg(Color::LightBlue).add_modifier(Modifier::ITALIC);
                         let mut name = String::new();
                         name.push_str(&node.name);
                         name.push_str("(Offline)");
@@ -204,82 +205,97 @@ impl<'a> View {
                         let names = ListItem::new(lines);
                         nodes.push(names);
                     } else {
-                        let name_span = Span::raw(&node.name);
-                        let lines = vec![Spans::from(name_span)];
-                        let names = ListItem::new(lines);
-                        nodes.push(names);
-
-                        if !node.inbound.is_empty() {
-                            let name = Span::styled(format!("    Inbound"), style);
-                            let lines = vec![Spans::from(name)];
+                        if !node.dnet_enabled {
+                            let style = Style::default()
+                                .fg(Color::LightBlue)
+                                .add_modifier(Modifier::BOLD);
+                            let mut name = String::new();
+                            name.push_str(&node.name);
+                            name.push_str("(dnetview is not enabled)");
+                            let name_span = Span::styled(name, style);
+                            let lines = vec![Spans::from(name_span)];
+                            let names = ListItem::new(lines);
+                            nodes.push(names);
+                        } else {
+                            let name_span = Span::raw(&node.name);
+                            let lines = vec![Spans::from(name_span)];
                             let names = ListItem::new(lines);
                             nodes.push(names);
 
-                            for inbound in &node.inbound {
-                                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),
+                            if !node.inbound.is_empty() {
+                                let name = Span::styled(format!("    Inbound"), style);
+                                let lines = vec![Spans::from(name)];
+                                let names = ListItem::new(lines);
+                                nodes.push(names);
+
+                                for inbound in &node.inbound {
+                                    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(remote_id)
+                                            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(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);
                             }
-                        }
 
-                        if !&node.outbound.is_empty() {
-                            let name = Span::styled(format!("    Outbound"), style);
-                            let lines = vec![Spans::from(name)];
-                            let names = ListItem::new(lines);
-                            nodes.push(names);
+                            if !&node.outbound.is_empty() {
+                                let name = Span::styled(format!("    Outbound"), style);
+                                let lines = vec![Spans::from(name)];
+                                let names = ListItem::new(lines);
+                                nodes.push(names);
 
-                            for outbound in &node.outbound {
-                                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),
+                                for outbound in &node.outbound {
+                                    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(remote_id)
+                                            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(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);
                             }
                         }
                     }