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

map: added dependencies and changed error handling to darkfi::Error

lunar-mining 4 лет назад
Родитель
Сommit
251e1bad9d
2 измененных файлов с 53 добавлено и 4 удалено
  1. 7 3
      bin/map/Cargo.toml
  2. 46 1
      bin/map/src/main.rs

+ 7 - 3
bin/map/Cargo.toml

@@ -8,12 +8,16 @@ path = "../../"
 features = ["rpc"]
 
 [dependencies]
-# tui
+# Tui
 termion = "1.5.6"
 tui = "0.16.0"
 
-# async
+# Async
 smol = "1.2.4"
 
-#misc
+# Misc
 rand = "0.6.5"
+log = "0.4.14"
+
+# Encoding and parsing
+serde_json = "1.0.74"

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

@@ -1,3 +1,14 @@
+// select each connection and show log of traffic
+// use rpc to get some info from the ircd network
+// ircd::logger keeps track of network info
+// map rpc polls logger for info about nodes, etc
+use darkfi::{
+    error::{Error, Result},
+    rpc::{jsonrpc, jsonrpc::JsonResult},
+};
+
+use log::{debug, error};
+use serde_json::{json, Value};
 use std::{io, io::Read, time::Duration};
 use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
 use tui::{
@@ -7,7 +18,41 @@ use tui::{
 
 use map::{ui, App};
 
-fn main() -> Result<(), io::Error> {
+struct Map {
+    url: String,
+}
+
+impl Map {
+    pub fn new(url: String) -> Self {
+        Self { url }
+    }
+
+    async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
+        let reply: JsonResult = match jsonrpc::send_request(&self.url, json!(r)).await {
+            Ok(v) => v,
+            Err(e) => return Err(e),
+        };
+
+        match reply {
+            JsonResult::Resp(r) => {
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
+                Ok(r.result)
+            }
+
+            JsonResult::Err(e) => {
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
+                Err(Error::JsonRpcError(e.error.message.to_string()))
+            }
+
+            JsonResult::Notif(n) => {
+                debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
+                Err(Error::JsonRpcError("Unexpected reply".to_string()))
+            }
+        }
+    }
+}
+
+fn main() -> Result<()> {
     // Set up terminal output
     let stdout = io::stdout().into_raw_mode()?;
     let backend = TermionBackend::new(stdout);