ソースを参照

map: added dummy rpc client

lunar-mining 4 年 前
コミット
a69563a6a0
3 ファイル変更28 行追加4 行削除
  1. 4 0
      Cargo.lock
  2. 1 0
      bin/map/Cargo.toml
  3. 23 4
      bin/map/src/main.rs

+ 4 - 0
Cargo.lock

@@ -2820,7 +2820,11 @@ dependencies = [
 name = "map"
 version = "0.3.0"
 dependencies = [
+ "async-std",
+ "darkfi",
+ "log",
  "rand 0.6.5",
+ "serde_json",
  "smol",
  "termion",
  "tui",

+ 1 - 0
bin/map/Cargo.toml

@@ -14,6 +14,7 @@ tui = "0.16.0"
 
 # Async
 smol = "1.2.4"
+async-std = {version = "1.10.0", features = ["attributes"]}
 
 # Misc
 rand = "0.6.5"

+ 23 - 4
bin/map/src/main.rs

@@ -7,7 +7,7 @@ use darkfi::{
     rpc::{jsonrpc, jsonrpc::JsonResult},
 };
 
-use log::{debug, error};
+use log::debug;
 use serde_json::{json, Value};
 use std::{io, io::Read, time::Duration};
 use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
@@ -50,17 +50,36 @@ impl Map {
             }
         }
     }
+
+    // --> {"jsonrpc": "2.0", "method": "say_hello", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": "hello world", "id": 42}
+    async fn say_hello(&self) -> Result<Value> {
+        let req = jsonrpc::request(json!("say_hello"), json!([]));
+        Ok(self.request(req).await?)
+    }
 }
 
-fn main() -> Result<()> {
+async fn start() -> Result<()> {
+    let client = Map::new("127.0.0.1:8000".to_string());
+    let reply = client.say_hello().await?;
+    println!("Server replied: {}", &reply.to_string());
+    Ok(())
+}
+
+#[async_std::main]
+async fn main() -> Result<()> {
     // Set up terminal output
     let stdout = io::stdout().into_raw_mode()?;
     let backend = TermionBackend::new(stdout);
     let mut terminal = Terminal::new(backend)?;
 
-    // create app and run it
+    // we're not using this yet
     let tick_rate = Duration::from_millis(250);
-    // here
+
+    // start rpc
+    start().await?;
+
+    // create the app and run it
     let app = App::new();
     let res = run_app(&mut terminal, app, tick_rate);