ソースを参照

poll: initalize node_info HashMap and pass parsed values.

node_info = HashMap<NodeId, NodeInfo>

If we receive an external addr from the rpc data, we set that as the NodeID.
otherwise we display client's rpc port as the NodeID.

Note: this is a temporary solution until we add a 'name' param to the
config. This commit indicates a number of TODOs.
lunar-mining 4 年 前
コミット
4dd9bc00d7
1 ファイル変更22 行追加12 行削除
  1. 22 12
      bin/dnetview/src/main.rs

+ 22 - 12
bin/dnetview/src/main.rs

@@ -151,8 +151,10 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
         debug!("{:?}", reply);
         debug!("{:?}", reply);
 
 
         if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
         if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
+            // TODO: clean up this section into seperate functions.
+            // TODO: replace if/else with match where possible
+            // TODO: test than all of these unwraps will never ever crash
             let ext_addr_option = reply.as_object().unwrap().get("external_addr");
             let ext_addr_option = reply.as_object().unwrap().get("external_addr");
-
             let inbound_obj = &reply.as_object().unwrap()["session_inbound"];
             let inbound_obj = &reply.as_object().unwrap()["session_inbound"];
             let manual_obj = &reply.as_object().unwrap()["session_manual"];
             let manual_obj = &reply.as_object().unwrap()["session_manual"];
             let outbound_obj = &reply.as_object().unwrap()["session_outbound"];
             let outbound_obj = &reply.as_object().unwrap()["session_outbound"];
@@ -164,7 +166,6 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
 
 
             // parse inbound connection data
             // parse inbound connection data
             let inbound_connected = &inbound_obj["connected"];
             let inbound_connected = &inbound_obj["connected"];
-
             if !inbound_connected.as_object().unwrap().is_empty() {
             if !inbound_connected.as_object().unwrap().is_empty() {
                 let inbound_connect: InboundInfo =
                 let inbound_connect: InboundInfo =
                     serde_json::from_value(inbound_connected.clone())?;
                     serde_json::from_value(inbound_connected.clone())?;
@@ -177,7 +178,6 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
 
 
             // parse outbound connection data
             // parse outbound connection data
             let outbound_slots = &outbound_obj["slots"];
             let outbound_slots = &outbound_obj["slots"];
-
             for slot in outbound_slots.as_array().unwrap() {
             for slot in outbound_slots.as_array().unwrap() {
                 if slot["channel"].is_null() {
                 if slot["channel"].is_null() {
                     // channel is empty. initialize with empty values
                     // channel is empty. initialize with empty values
@@ -199,21 +199,31 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
                     slots.push(new_slot)
                     slots.push(new_slot)
                 }
                 }
             }
             }
-
             let oconnect = OutboundInfo::new(slots);
             let oconnect = OutboundInfo::new(slots);
             outconnects.push(oconnect);
             outconnects.push(oconnect);
 
 
             let infos =
             let infos =
                 NodeInfo { outbound: outconnects, manual: manconnects, inbound: inconnects };
                 NodeInfo { outbound: outconnects, manual: manconnects, inbound: inconnects };
+            let mut node_info = HashMap::new();
+
+            // TODO: if the external_addr is empty, we set the display address as the rpc url.
+            // however we need to add a 'name' param to the config file to display instead
+            match ext_addr_option {
+                Some(addr) => {
+                    debug!("{:?}", addr);
+                    let external_addr = addr.as_str().unwrap();
+                    node_info.insert(external_addr, infos);
+                }
+                _ => {
+                    let external_addr = &client.url;
+                    node_info.insert(external_addr.as_str(), 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 (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);
+            }
         } else {
         } else {
             // TODO: error handling
             // TODO: error handling
             debug!("Reply is empty");
             debug!("Reply is empty");