// 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; use serde_json::{json, Value}; use std::{io, io::Read, time::Duration}; use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode}; use tui::{ backend::{Backend, TermionBackend}, Terminal, }; use map::{ui, App}; struct Map { url: String, } impl Map { pub fn new(url: String) -> Self { Self { url } } async fn request(&self, r: jsonrpc::JsonRequest) -> Result { 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())) } } } // --> {"jsonrpc": "2.0", "method": "say_hello", "params": [], "id": 42} // <-- {"jsonrpc": "2.0", "result": "hello world", "id": 42} async fn say_hello(&self) -> Result { let req = jsonrpc::request(json!("say_hello"), json!([])); Ok(self.request(req).await?) } //--> {"jsonrpc": "2.0", "method": "poll", "params": [], "id": 42} // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42} async fn get_info(&self) -> Result { let req = jsonrpc::request(json!("get_info"), json!([])); Ok(self.request(req).await?) } } async fn start() -> Result<()> { let client = Map::new("tcp://127.0.0.1:8000".to_string()); // call this every 1 second (poll) let reply = client.get_info().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)?; // we're not using this yet let tick_rate = Duration::from_millis(250); // start rpc start().await?; // create the app and run it let app = App::new(); let res = run_app(&mut terminal, app, tick_rate); terminal.clear()?; if let Err(err) = res { println!("{:?}", err) } Ok(()) } fn run_app( terminal: &mut Terminal, mut app: App, _tick_rate: Duration, ) -> io::Result<()> { let mut asi = async_stdin(); terminal.clear()?; app.node_list.state.select(Some(0)); app.node_info.index = 0; //let mut last_tick = Instant::now(); loop { terminal.draw(|f| ui::ui(f, &mut app))?; for k in asi.by_ref().keys() { match k.unwrap() { Key::Char('q') => { terminal.clear()?; return Ok(()) } Key::Char('j') => { app.node_list.next(); app.node_info.next(); } Key::Char('k') => { app.node_list.previous(); app.node_info.previous(); } _ => (), } } //if last_tick.elapsed() >= tick_rate { // app.clone().update(); // last_tick = Instant::now(); //} } }