lunar-mining 4 лет назад
Родитель
Сommit
de29238a41
2 измененных файлов с 76 добавлено и 10 удалено
  1. 16 4
      bin/map/src/app.rs
  2. 60 6
      bin/map/src/main.rs

+ 16 - 4
bin/map/src/app.rs

@@ -5,6 +5,9 @@ use crate::{
 //use smol::Timer;
 //use std::{collections::HashMap, time::Duration};
 
+// make a structure to be able to modify and read them
+// protect using a mutex
+// arc reference
 #[derive(Clone)]
 pub struct App {
     pub node_list: NodeIdList,
@@ -13,6 +16,7 @@ pub struct App {
 
 impl App {
     pub fn new() -> App {
+        // append to vector of node info
         let infos = vec![
             NodeInfo {
                 id: "0385048034sodisofjhosd1111q3434".to_string(),
@@ -65,8 +69,16 @@ impl App {
     //    Timer::after(dur).await;
     //}
 
-    //pub async fn update(mut self) {
-    //    self.node_list.nodes.insert("New node joined".to_string(), "".to_string());
-    //    //self.sleep(Duration::from_secs(2)).await;
-    //}
+    pub async fn update(mut self, node_vec: Vec<NodeInfo>) -> App {
+        let node_info = NodeInfoView::new(node_vec.clone());
+        
+        let ids = vec![
+            node_vec[0].id.clone(),
+            node_vec[1].id.clone(),
+            node_vec[2].id.clone(),
+        ];
+
+        let node_list = NodeIdList::new(ids);
+        App { node_list, node_info }
+    }
 }

+ 60 - 6
bin/map/src/main.rs

@@ -20,7 +20,11 @@ use tui::{
     Terminal,
 };
 
-use map::{ui, App};
+use map::{
+    list::NodeIdList,
+    node_info::{NodeInfo, NodeInfoView},
+    ui, App,
+};
 
 struct Map {
     url: String,
@@ -91,7 +95,7 @@ async fn main() -> Result<()> {
         // Run the main future on the current thread.
         .finish(|| {
             smol::future::block_on(async move {
-                start(ex2.clone()).await?;
+                start(ex2.clone(), app.clone()).await?;
                 run_app(&mut terminal, app).await?;
                 drop(signal);
                 Ok::<(), darkfi::Error>(())
@@ -101,24 +105,74 @@ async fn main() -> Result<()> {
     result
 }
 
-async fn start(ex: Arc<Executor<'_>>) -> Result<()> {
+async fn start(ex: Arc<Executor<'_>>, mut app: App) -> Result<()> {
     let client = Map::new("tcp://127.0.0.1:8000".to_string());
 
     ex.spawn(async {
-        poll(client).await;
+        poll(client, app).await;
     })
     .detach();
 
     Ok(())
 }
 
-async fn poll(client: Map) -> Result<()> {
+async fn poll(client: Map, mut app: App) -> Result<()> {
     loop {
-        client.get_info().await?;
+        let reply = client.get_info().await?;
+        update(app.clone(), reply).await?;
         async_util::sleep(1).await;
     }
 }
 
+async fn update(mut app: App, reply: Value) -> Result<()> {
+    if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
+        //let args = params.as_array();
+        let nodes = reply.as_object().unwrap().get("nodes").unwrap();
+
+        let node1 = &nodes[0];
+        let node2 = &nodes[1];
+        let node3 = &nodes[2];
+
+        let infos = vec![
+            NodeInfo {
+                id: node1["id"].to_string(),
+                connections: node1["connections"].as_u64().unwrap() as usize,
+                is_active: node2["is_active"].as_bool().unwrap(),
+                last_message: "message".to_string(),
+            },
+            //NodeInfo {
+            //    id: node2["id"].to_string(),
+            //    connections: node2["connections"].as_u64().unwrap() as usize,
+            //    is_active: node2["is_active"].as_bool().unwrap(),
+            //    last_message: node2["message"].to_string(),
+            //},
+            //NodeInfo {
+            //    id: node3["id"].to_string(),
+            //    connections: node3["connections"].as_u64().unwrap() as usize,
+            //    is_active: node3["is_active"].as_bool().unwrap(),
+            //    last_message: node3["message"].to_string(),
+            //},
+        ];
+
+        //app.node_info(
+        //let node_info = NodeInfoView::new(infos.clone());
+
+        //let ids = vec![node1["id"].to_string(), node2["id"].to_string(), node3["id"].to_string()];
+
+        // mutex
+        app.update(infos).await;
+        //let node_list = NodeIdList::new(ids);
+        //println!("{}", test);
+        // do something
+    }
+    else {
+        // TODO: error handling
+        println!("Reply is an error");
+    }
+
+    Ok(())
+}
+
 async fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
     let mut asi = async_stdin();