| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // 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<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()))
- }
- }
- }
- // --> {"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?)
- }
- //--> {"jsonrpc": "2.0", "method": "poll", "params": [], "id": 42}
- // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42}
- async fn get_info(&self) -> Result<Value> {
- 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<B: Backend>(
- terminal: &mut Terminal<B>,
- 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();
- //}
- }
- }
|