main.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // select each connection and show log of traffic
  2. // use rpc to get some info from the ircd network
  3. // ircd::logger keeps track of network info
  4. // map rpc polls logger for info about nodes, etc
  5. use darkfi::{
  6. error::{Error, Result},
  7. rpc::{jsonrpc, jsonrpc::JsonResult},
  8. };
  9. use log::debug;
  10. use serde_json::{json, Value};
  11. use std::{io, io::Read, time::Duration};
  12. use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
  13. use tui::{
  14. backend::{Backend, TermionBackend},
  15. Terminal,
  16. };
  17. use map::{ui, App};
  18. struct Map {
  19. url: String,
  20. }
  21. impl Map {
  22. pub fn new(url: String) -> Self {
  23. Self { url }
  24. }
  25. async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
  26. let reply: JsonResult = match jsonrpc::send_request(&self.url, json!(r)).await {
  27. Ok(v) => v,
  28. Err(e) => return Err(e),
  29. };
  30. match reply {
  31. JsonResult::Resp(r) => {
  32. debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
  33. Ok(r.result)
  34. }
  35. JsonResult::Err(e) => {
  36. debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
  37. Err(Error::JsonRpcError(e.error.message.to_string()))
  38. }
  39. JsonResult::Notif(n) => {
  40. debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
  41. Err(Error::JsonRpcError("Unexpected reply".to_string()))
  42. }
  43. }
  44. }
  45. // --> {"jsonrpc": "2.0", "method": "say_hello", "params": [], "id": 42}
  46. // <-- {"jsonrpc": "2.0", "result": "hello world", "id": 42}
  47. async fn say_hello(&self) -> Result<Value> {
  48. let req = jsonrpc::request(json!("say_hello"), json!([]));
  49. Ok(self.request(req).await?)
  50. }
  51. //--> {"jsonrpc": "2.0", "method": "poll", "params": [], "id": 42}
  52. // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42}
  53. async fn get_info(&self) -> Result<Value> {
  54. let req = jsonrpc::request(json!("get_info"), json!([]));
  55. Ok(self.request(req).await?)
  56. }
  57. }
  58. async fn start() -> Result<()> {
  59. let client = Map::new("tcp://127.0.0.1:8000".to_string());
  60. // call this every 1 second (poll)
  61. let reply = client.get_info().await?;
  62. println!("Server replied: {}", &reply.to_string());
  63. Ok(())
  64. }
  65. #[async_std::main]
  66. async fn main() -> Result<()> {
  67. // Set up terminal output
  68. let stdout = io::stdout().into_raw_mode()?;
  69. let backend = TermionBackend::new(stdout);
  70. let mut terminal = Terminal::new(backend)?;
  71. // we're not using this yet
  72. let tick_rate = Duration::from_millis(250);
  73. // start rpc
  74. start().await?;
  75. // create the app and run it
  76. let app = App::new();
  77. let res = run_app(&mut terminal, app, tick_rate);
  78. terminal.clear()?;
  79. if let Err(err) = res {
  80. println!("{:?}", err)
  81. }
  82. Ok(())
  83. }
  84. fn run_app<B: Backend>(
  85. terminal: &mut Terminal<B>,
  86. mut app: App,
  87. _tick_rate: Duration,
  88. ) -> io::Result<()> {
  89. let mut asi = async_stdin();
  90. terminal.clear()?;
  91. app.node_list.state.select(Some(0));
  92. app.node_info.index = 0;
  93. //let mut last_tick = Instant::now();
  94. loop {
  95. terminal.draw(|f| ui::ui(f, &mut app))?;
  96. for k in asi.by_ref().keys() {
  97. match k.unwrap() {
  98. Key::Char('q') => {
  99. terminal.clear()?;
  100. return Ok(())
  101. }
  102. Key::Char('j') => {
  103. app.node_list.next();
  104. app.node_info.next();
  105. }
  106. Key::Char('k') => {
  107. app.node_list.previous();
  108. app.node_info.previous();
  109. }
  110. _ => (),
  111. }
  112. }
  113. //if last_tick.elapsed() >= tick_rate {
  114. // app.clone().update();
  115. // last_tick = Instant::now();
  116. //}
  117. }
  118. }