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

dnetview: make_empty_id to avoid endlessly creating ids

lunar-mining 4 лет назад
Родитель
Сommit
e11709f460
3 измененных файлов с 50 добавлено и 5 удалено
  1. 7 3
      bin/dnetview/src/main.rs
  2. 29 0
      bin/dnetview/src/util.rs
  3. 14 2
      bin/dnetview/src/view.rs

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

@@ -30,7 +30,7 @@ use dnetview::{
     model::{ConnectInfo, Model, NodeInfo, SelectableObject, Session, SessionInfo},
     options::ProgramOptions,
     ui,
-    util::{generate_id, make_connect_id, make_node_id, make_session_id},
+    util::{generate_id, make_connect_id, make_empty_id, make_node_id, make_session_id},
     view::{ConnectInfoView, IdListView, InfoListView, NodeInfoView, SessionInfoView, View},
 };
 
@@ -220,14 +220,16 @@ async fn parse_inbound(inbound: &Value, node_id: String) -> Result<SessionInfo>
     let session_id = make_session_id(node_id.clone(), &session_type)?;
     let mut connects: Vec<ConnectInfo> = Vec::new();
     let connections = &inbound["connected"];
+    let mut connect_count = 0;
 
     match connections.as_object() {
         Some(connect) => {
             match connect.is_empty() {
                 true => {
+                    connect_count += 1;
                     // channel is empty. initialize with empty values
                     // TODO: fix this
-                    let connect_id = generate_id()?;
+                    let connect_id = make_empty_id(node_id.clone(), &session_type, connect_count)?;
                     let addr = "Null".to_string();
                     let msg = "Null".to_string();
                     let status = "Null".to_string();
@@ -305,15 +307,17 @@ async fn parse_outbound(outbound: &Value, node_id: String) -> Result<SessionInfo
     let mut connects: Vec<ConnectInfo> = Vec::new();
     let slots = &outbound["slots"];
     let session_id = make_session_id(node_id.clone(), &session_type)?;
+    let mut slot_count = 0;
 
     match slots.as_array() {
         Some(slots) => {
             for slot in slots {
+                slot_count += 1;
                 match slot["channel"].is_null() {
                     true => {
                         // channel is empty. initialize with empty values
                         // TODO: fix this
-                        let connect_id = generate_id()?;
+                        let connect_id = make_empty_id(node_id.clone(), &session_type, slot_count)?;
                         let is_empty = true;
                         let addr = "Null".to_string();
                         let state = &slot["state"];

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

@@ -45,6 +45,35 @@ pub fn generate_id() -> Result<String> {
     Ok(serial::serialize_hex(&id))
 }
 
+pub fn make_empty_id(node_id: String, session: &Session, count: u64) -> Result<String> {
+    let mut num = 0_u64;
+
+    match session {
+        Session::Inbound => {
+            for i in ['i', 'n'] {
+                num += i as u64;
+            }
+        }
+        Session::Outbound => {
+            for i in ['o', 'u', 't'] {
+                num += i as u64;
+            }
+        }
+        Session::Manual => {
+            for i in ['m', 'a', 'n'] {
+                num += i as u64;
+            }
+        }
+    }
+
+    for i in node_id.chars() {
+        num += i as u64
+    }
+
+    num += count;
+
+    Ok(serial::serialize_hex(&num))
+}
 //pub fn is_empty_outbound(slots: Vec<Slot>) -> bool {
 //    return slots.iter().all(|slot| slot.is_empty);
 //}

+ 14 - 2
bin/dnetview/src/view.rs

@@ -92,8 +92,20 @@ impl View {
                             nodes.push(names);
                             //nodes.push(node);
                         }
-                        SelectableObject::Session(info) => self.session_info.clone().render(info),
-                        SelectableObject::Connect(info) => self.connect_info.clone().render(info),
+                        SelectableObject::Session(info) => {
+                            let name_span = Span::raw(&info.session_name);
+                            let lines = vec![Spans::from(name_span)];
+                            let names = ListItem::new(lines);
+                            nodes.push(names);
+                            //self.session_info.clone().render(info),
+                        }
+                        SelectableObject::Connect(info) => {
+                            let name_span = Span::raw(&info.connect_id);
+                            let lines = vec![Spans::from(name_span)];
+                            let names = ListItem::new(lines);
+                            nodes.push(names);
+                            //self.connect_info.clone().render(info),
+                        }
                     }
                     //
                 }