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

map: init empty data structures on start

lunar-mining 4 лет назад
Родитель
Сommit
1cd23ffdc3
2 измененных файлов с 66 добавлено и 58 удалено
  1. 49 46
      bin/map/src/main.rs
  2. 17 12
      bin/map/src/model.rs

+ 49 - 46
bin/map/src/main.rs

@@ -79,48 +79,43 @@ async fn main() -> Result<()> {
 
 
     terminal.clear()?;
     terminal.clear()?;
 
 
-    let infos = vec![
-        NodeInfo {
-            id: "0385048034sodisofjhosd1111q3434".to_string(),
-            connections: 10,
-            is_active: true,
-            last_message: "hey how are you?".to_string(),
-        },
-        NodeInfo {
-            id: "09w30we9wsnfksdfkdjflsjkdfjdfsd".to_string(),
-            connections: 5,
-            is_active: false,
-            last_message: "lmao".to_string(),
-        },
-        NodeInfo {
-            id: "038043325alsdlasjfrsdfsdfsdjsdf".to_string(),
-            connections: 7,
-            is_active: true,
-            last_message: "gm".to_string(),
-        },
-        NodeInfo {
-            id: "04985034953ldflsdjflsdjflsdjfii".to_string(),
-            connections: 2,
-            is_active: true,
-            last_message: "hihi".to_string(),
-        },
-        NodeInfo {
-            id: "09850249352asdjapsdikalskasdkas".to_string(),
-            connections: 10,
-            is_active: true,
-            last_message: "wtf".to_string(),
-        },
-    ];
+    let infos = vec![NodeInfo::new()];
+    //let infos = vec![
+    //    NodeInfo {
+    //        id: "0385048034sodisofjhosd1111q3434".to_string(),
+    //        connections: 10,
+    //        is_active: true,
+    //        last_message: "hey how are you?".to_string(),
+    //    },
+    //    NodeInfo {
+    //        id: "09w30we9wsnfksdfkdjflsjkdfjdfsd".to_string(),
+    //        connections: 5,
+    //        is_active: false,
+    //        last_message: "lmao".to_string(),
+    //    },
+    //    NodeInfo {
+    //        id: "038043325alsdlasjfrsdfsdfsdjsdf".to_string(),
+    //        connections: 7,
+    //        is_active: true,
+    //        last_message: "gm".to_string(),
+    //    },
+    //    NodeInfo {
+    //        id: "04985034953ldflsdjflsdjflsdjfii".to_string(),
+    //        connections: 2,
+    //        is_active: true,
+    //        last_message: "hihi".to_string(),
+    //    },
+    //    NodeInfo {
+    //        id: "09850249352asdjapsdikalskasdkas".to_string(),
+    //        connections: 10,
+    //        is_active: true,
+    //        last_message: "wtf".to_string(),
+    //    },
+    //];
 
 
     let info_list = InfoList::new(infos.clone());
     let info_list = InfoList::new(infos.clone());
 
 
-    let ids = vec![
-        infos[0].id.clone(),
-        infos[1].id.clone(),
-        infos[2].id.clone(),
-        infos[3].id.clone(),
-        infos[4].id.clone(),
-    ];
+    let ids = vec![String::new()];
 
 
     let id_list = IdList::new(ids);
     let id_list = IdList::new(ids);
 
 
@@ -156,25 +151,33 @@ async fn run_rpc(ex: Arc<Executor<'_>>, model: Arc<Model>) -> Result<()> {
     Ok(())
     Ok(())
 }
 }
 
 
-async fn poll(client: Map, _model: Arc<Model>) -> Result<()> {
+async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
     loop {
     loop {
         let reply = client.get_info().await?;
         let reply = client.get_info().await?;
 
 
         if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
         if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
             let nodes = reply.as_object().unwrap().get("nodes").unwrap();
             let nodes = reply.as_object().unwrap().get("nodes").unwrap();
 
 
+            // todo: generalize this
             let node1 = &nodes[0];
             let node1 = &nodes[0];
-            let node2 = &nodes[1];
-            let node3 = &nodes[2];
+            //let node2 = &nodes[1];
+            //let node3 = &nodes[2];
 
 
-            let _infos = vec![NodeInfo {
+            // TODO: error handling
+            let infos = vec![NodeInfo {
                 id: node1["id"].to_string(),
                 id: node1["id"].to_string(),
                 connections: node1["connections"].as_u64().unwrap() as usize,
                 connections: node1["connections"].as_u64().unwrap() as usize,
-                is_active: node2["is_active"].as_bool().unwrap(),
-                last_message: node3["message"].to_string(),
+                is_active: node1["is_active"].as_bool().unwrap(),
+                last_message: node1["message"].to_string(),
             }];
             }];
 
 
-            //model.lock().await.update(infos).await;
+            //model.update(infos).await;
+
+            for node in infos {
+                model.id_list.node_id.lock().await.push(node.id);
+                //self.info_list.infos.lock().await.push(node.info);
+                //self.id_list.
+            }
         } else {
         } else {
             // TODO: error handling
             // TODO: error handling
             println!("Reply is an error");
             println!("Reply is an error");

+ 17 - 12
bin/map/src/model.rs

@@ -1,4 +1,5 @@
 use async_std::sync::Mutex;
 use async_std::sync::Mutex;
+use darkfi::error::{Error, Result};
 use tui::widgets::ListState;
 use tui::widgets::ListState;
 
 
 pub struct Model {
 pub struct Model {
@@ -11,21 +12,25 @@ impl Model {
         Model { id_list, info_list }
         Model { id_list, info_list }
     }
     }
 
 
-    pub async fn update(self, node_vec: Vec<NodeInfo>) -> Model {
-        let ids = vec![node_vec[0].id.clone()];
-
-        for id in ids {
-            self.id_list.node_id.lock().await.push(id);
+    pub async fn update(self, node_vec: Vec<NodeInfo>) -> Result<()> {
+        for node in node_vec {
+            self.id_list.node_id.lock().await.push(node.id);
+            //self.info_list.infos.lock().await.push(node.info);
+            //self.id_list.
         }
         }
+        //let ids = vec![node_vec[0].id.clone()];
 
 
-        let id_list = self.id_list;
+        //for id in ids {
+        //    self.id_list.node_id.lock().await.push(id);
+        //}
 
 
-        for info in node_vec {
-            self.info_list.infos.lock().await.push(info);
-        }
-        let info_list = self.info_list;
+        //let id_list = self.id_list;
 
 
-        Model { id_list, info_list }
+        //for info in node_vec {
+        //    self.info_list.infos.lock().await.push(info);
+        //}
+        //let info_list = self.info_list;
+        Ok(())
     }
     }
 }
 }
 
 
@@ -56,7 +61,7 @@ impl InfoList {
     }
     }
 }
 }
 
 
-pub type NodeId = u32;
+//pub type NodeId = u32;
 
 
 #[derive(Clone)]
 #[derive(Clone)]
 pub struct NodeInfo {
 pub struct NodeInfo {