main.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. use async_std::sync::{Arc, Mutex};
  2. use std::{collections::hash_map::Entry, fs::File, io, io::Read, path::PathBuf};
  3. use easy_parallel::Parallel;
  4. use fxhash::{FxHashMap, FxHashSet};
  5. use log::{debug, info};
  6. use serde_json::{json, Value};
  7. use simplelog::*;
  8. use smol::Executor;
  9. use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
  10. use tui::{
  11. backend::{Backend, TermionBackend},
  12. Terminal,
  13. };
  14. use url::Url;
  15. use darkfi::{
  16. error::{Error, Result},
  17. rpc::{jsonrpc, jsonrpc::JsonResult},
  18. util::{
  19. async_util,
  20. cli::{log_config, spawn_config, Config},
  21. join_config_path,
  22. },
  23. };
  24. use dnetview::{
  25. config::{DnvConfig, CONFIG_FILE_CONTENTS},
  26. model::{ConnectInfo, Model, NodeInfo, SelectableObject, Session, SessionInfo},
  27. options::ProgramOptions,
  28. util::{is_empty_session, make_connect_id, make_empty_id, make_node_id, make_session_id},
  29. view::{IdListView, NodeInfoView, View},
  30. };
  31. struct DNetView {
  32. url: Url,
  33. name: String,
  34. }
  35. impl DNetView {
  36. pub fn new(url: Url, name: String) -> Self {
  37. Self { url, name }
  38. }
  39. async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
  40. let reply: JsonResult = match jsonrpc::send_request(&self.url, json!(r), None).await {
  41. Ok(v) => v,
  42. Err(e) => return Err(e),
  43. };
  44. match reply {
  45. JsonResult::Resp(r) => {
  46. debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
  47. Ok(r.result)
  48. }
  49. JsonResult::Err(e) => {
  50. debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
  51. Err(Error::JsonRpcError(e.error.message.to_string()))
  52. }
  53. JsonResult::Notif(n) => {
  54. debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
  55. Err(Error::JsonRpcError("Unexpected reply".to_string()))
  56. }
  57. }
  58. }
  59. // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
  60. // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
  61. async fn _ping(&self) -> Result<Value> {
  62. let req = jsonrpc::request(json!("ping"), json!([]));
  63. Ok(self.request(req).await?)
  64. }
  65. //--> {"jsonrpc": "2.0", "method": "poll", "params": [], "id": 42}
  66. // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42}
  67. async fn get_info(&self) -> Result<Value> {
  68. let req = jsonrpc::request(json!("get_info"), json!([]));
  69. Ok(self.request(req).await?)
  70. }
  71. }
  72. #[async_std::main]
  73. async fn main() -> Result<()> {
  74. let options = ProgramOptions::load()?;
  75. let verbosity_level = options.app.occurrences_of("verbose");
  76. let (lvl, cfg) = log_config(verbosity_level)?;
  77. let file = File::create(&*options.log_path).unwrap();
  78. WriteLogger::init(lvl, cfg, file)?;
  79. info!("Log level: {}", lvl);
  80. let config_path = join_config_path(&PathBuf::from("dnetview_config.toml"))?;
  81. spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  82. let config = Config::<DnvConfig>::load(config_path)?;
  83. let stdout = io::stdout().into_raw_mode()?;
  84. let backend = TermionBackend::new(stdout);
  85. let mut terminal = Terminal::new(backend)?;
  86. terminal.clear()?;
  87. let ids = Mutex::new(FxHashSet::default());
  88. let nodes = Mutex::new(FxHashMap::default());
  89. let selectables = Mutex::new(FxHashMap::default());
  90. let msg_log = Mutex::new(FxHashMap::default());
  91. let model = Arc::new(Model::new(ids, nodes, selectables, msg_log));
  92. let nthreads = num_cpus::get();
  93. let (signal, shutdown) = async_channel::unbounded::<()>();
  94. let ex = Arc::new(Executor::new());
  95. let ex2 = ex.clone();
  96. let (_, result) = Parallel::new()
  97. .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  98. .finish(|| {
  99. smol::future::block_on(async move {
  100. run_rpc(&config, ex2.clone(), model.clone()).await?;
  101. // msg_log
  102. render(&mut terminal, model.clone()).await?;
  103. drop(signal);
  104. Ok::<(), darkfi::Error>(())
  105. })
  106. });
  107. result
  108. }
  109. async fn run_rpc(config: &DnvConfig, ex: Arc<Executor<'_>>, model: Arc<Model>) -> Result<()> {
  110. for node in config.nodes.clone() {
  111. let client = DNetView::new(Url::parse(&node.rpc_url)?, node.name);
  112. ex.spawn(poll(client, model.clone())).detach();
  113. }
  114. Ok(())
  115. }
  116. async fn poll(client: DNetView, model: Arc<Model>) -> Result<()> {
  117. loop {
  118. let reply = client.get_info().await?;
  119. if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
  120. parse_data(reply.as_object().unwrap(), &client, model.clone()).await?;
  121. } else {
  122. // TODO: error handling
  123. //debug!("Reply is empty");
  124. }
  125. async_util::sleep(2).await;
  126. }
  127. }
  128. async fn parse_data(
  129. reply: &serde_json::Map<String, Value>,
  130. client: &DNetView,
  131. model: Arc<Model>,
  132. ) -> Result<()> {
  133. let _ext_addr = reply.get("external_addr");
  134. let inbound = &reply["session_inbound"];
  135. let manual = &reply["session_manual"];
  136. let outbound = &reply["session_outbound"];
  137. let mut sessions: Vec<SessionInfo> = Vec::new();
  138. let node_name = &client.name;
  139. let node_id = make_node_id(node_name)?;
  140. let in_session = parse_inbound(inbound, node_id.clone()).await?;
  141. let out_session = parse_outbound(outbound, node_id.clone()).await?;
  142. let man_session = parse_manual(manual, node_id.clone()).await?;
  143. sessions.push(in_session.clone());
  144. sessions.push(out_session.clone());
  145. sessions.push(man_session.clone());
  146. let nodes = NodeInfo::new(node_id.clone(), node_name.to_string(), sessions.clone());
  147. update_nodes(model.clone(), nodes.clone(), node_id.clone()).await;
  148. update_selectable_and_ids(model.clone(), sessions.clone(), nodes.clone()).await?;
  149. update_msgs(model.clone(), sessions.clone()).await?;
  150. //debug!("IDS: {:?}", model.ids.lock().await);
  151. //debug!("INFOS: {:?}", model.infos.lock().await);
  152. Ok(())
  153. }
  154. async fn update_msgs(model: Arc<Model>, sessions: Vec<SessionInfo>) -> Result<()> {
  155. for session in sessions {
  156. for connection in session.children {
  157. if !model.msg_log.lock().await.contains_key(&connection.connect_id) {
  158. model.msg_log.lock().await.insert(connection.connect_id, connection.msg_log);
  159. } else {
  160. match model.msg_log.lock().await.entry(connection.connect_id) {
  161. Entry::Vacant(e) => {
  162. e.insert(connection.msg_log);
  163. }
  164. Entry::Occupied(mut e) => {
  165. for msg in connection.msg_log {
  166. e.get_mut().push(msg);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. //debug!("MSGS: {:?}", model.msg_log.lock().await);
  174. Ok(())
  175. }
  176. async fn update_ids(model: Arc<Model>, id: String) {
  177. model.ids.lock().await.insert(id);
  178. }
  179. async fn update_nodes(model: Arc<Model>, node: NodeInfo, id: String) {
  180. model.nodes.lock().await.insert(id, node);
  181. }
  182. async fn update_selectable_and_ids(
  183. model: Arc<Model>,
  184. sessions: Vec<SessionInfo>,
  185. nodes: NodeInfo,
  186. ) -> Result<()> {
  187. let node_obj = SelectableObject::Node(nodes.clone());
  188. model.selectables.lock().await.insert(nodes.node_id.clone(), node_obj);
  189. update_ids(model.clone(), nodes.node_id.clone()).await;
  190. for session in sessions.clone() {
  191. let session_obj = SelectableObject::Session(session.clone());
  192. model.selectables.lock().await.insert(session.clone().session_id, session_obj);
  193. update_ids(model.clone(), session.clone().session_id).await;
  194. for connect in session.children {
  195. let connect_obj = SelectableObject::Connect(connect.clone());
  196. model.selectables.lock().await.insert(connect.clone().connect_id, connect_obj);
  197. update_ids(model.clone(), connect.clone().connect_id).await;
  198. }
  199. }
  200. Ok(())
  201. }
  202. async fn parse_inbound(inbound: &Value, node_id: String) -> Result<SessionInfo> {
  203. let session_name = "Inbound".to_string();
  204. let session_type = Session::Inbound;
  205. let session_id = make_session_id(node_id.clone(), &session_type)?;
  206. let mut connects: Vec<ConnectInfo> = Vec::new();
  207. let connections = &inbound["connected"];
  208. let mut connect_count = 0;
  209. match connections.as_object() {
  210. Some(connect) => {
  211. match connect.is_empty() {
  212. true => {
  213. connect_count += 1;
  214. // channel is empty. initialize with empty values
  215. // TODO: fix this
  216. let connect_id = make_empty_id(node_id.clone(), &session_type, connect_count)?;
  217. let addr = "Null".to_string();
  218. let msg = "Null".to_string();
  219. let status = "Null".to_string();
  220. let is_empty = true;
  221. let parent = session_id.clone();
  222. let state = "Null".to_string();
  223. let msg_log = Vec::new();
  224. let connect_info = ConnectInfo::new(
  225. connect_id, addr, is_empty, msg, status, state, msg_log, parent,
  226. );
  227. connects.push(connect_info.clone());
  228. }
  229. false => {
  230. // channel is not empty. initialize with whole values
  231. for k in connect.keys() {
  232. let node = connect.get(k);
  233. let addr = k.to_string();
  234. let msg =
  235. node.unwrap().get("last_msg").unwrap().as_str().unwrap().to_string();
  236. let status =
  237. node.unwrap().get("last_status").unwrap().as_str().unwrap().to_string();
  238. // TODO: state
  239. let id = node.unwrap().get("random_id").unwrap().as_u64().unwrap();
  240. let connect_id = make_connect_id(id)?;
  241. let state = "state".to_string();
  242. let is_empty = false;
  243. let parent = session_id.clone();
  244. let msg_values = node.unwrap().get("log").unwrap().as_array().unwrap();
  245. // append to existing values
  246. //let mut writer = msg_log.write().unwrap();
  247. //writer.insert(connect_id, connect.clone());
  248. let mut msgs: Vec<(String, String)> = Vec::new();
  249. for msg in msg_values {
  250. let msg: (String, String) = serde_json::from_value(msg.clone())?;
  251. msgs.push(msg);
  252. }
  253. let connect_info = ConnectInfo::new(
  254. connect_id, addr, is_empty, msg, status, state, msgs, parent,
  255. );
  256. connects.push(connect_info.clone());
  257. }
  258. }
  259. }
  260. let is_empty = is_empty_session(connects.clone());
  261. let session_info = SessionInfo::new(
  262. session_name,
  263. session_id.clone(),
  264. node_id.clone(),
  265. connects.clone(),
  266. is_empty,
  267. );
  268. Ok(session_info)
  269. }
  270. None => Err(Error::ValueIsNotObject),
  271. }
  272. }
  273. // TODO: placeholder for now
  274. async fn parse_manual(_manual: &Value, node_id: String) -> Result<SessionInfo> {
  275. let session_name = "Manual".to_string();
  276. let session_type = Session::Manual;
  277. let mut connects: Vec<ConnectInfo> = Vec::new();
  278. let session_id = make_session_id(node_id.clone(), &session_type)?;
  279. let id: u64 = 0;
  280. let connect_id = make_connect_id(id)?;
  281. let addr = "Null".to_string();
  282. let msg = "Null".to_string();
  283. let status = "Null".to_string();
  284. let is_empty = true;
  285. let parent = session_id.clone();
  286. let state = "Null".to_string();
  287. let msg_log = Vec::new();
  288. let connect_info =
  289. ConnectInfo::new(connect_id, addr, is_empty, msg, status, state, msg_log, parent);
  290. connects.push(connect_info.clone());
  291. let is_empty = is_empty_session(connects.clone());
  292. //let is_empty = false;
  293. let session_info =
  294. SessionInfo::new(session_name, session_id, node_id, connects.clone(), is_empty);
  295. Ok(session_info)
  296. }
  297. async fn parse_outbound(outbound: &Value, node_id: String) -> Result<SessionInfo> {
  298. let session_name = "Outbound".to_string();
  299. let session_type = Session::Outbound;
  300. let mut connects: Vec<ConnectInfo> = Vec::new();
  301. let slots = &outbound["slots"];
  302. let session_id = make_session_id(node_id.clone(), &session_type)?;
  303. let mut slot_count = 0;
  304. match slots.as_array() {
  305. Some(slots) => {
  306. for slot in slots {
  307. slot_count += 1;
  308. match slot["channel"].is_null() {
  309. true => {
  310. // channel is empty. initialize with empty values
  311. // TODO: fix this
  312. let connect_id = make_empty_id(node_id.clone(), &session_type, slot_count)?;
  313. let is_empty = true;
  314. let addr = "Null".to_string();
  315. let state = &slot["state"];
  316. let msg = "Null".to_string();
  317. let status = "Null".to_string();
  318. // TODO: msg log
  319. let msg_log = Vec::new();
  320. let parent = session_id.clone();
  321. let connect_info = ConnectInfo::new(
  322. connect_id,
  323. addr,
  324. is_empty,
  325. msg,
  326. status,
  327. state.as_str().unwrap().to_string(),
  328. msg_log,
  329. parent,
  330. );
  331. connects.push(connect_info.clone());
  332. }
  333. false => {
  334. // channel is not empty. initialize with whole values
  335. let channel = &slot["channel"];
  336. let last_msg = channel["last_msg"].as_str().unwrap().to_string();
  337. let last_status = channel["last_status"].as_str().unwrap().to_string();
  338. let id = channel["random_id"].as_u64().unwrap();
  339. let msg_values = channel["log"].as_array().unwrap();
  340. let connect_id = make_connect_id(id)?;
  341. let is_empty = false;
  342. let addr = &slot["addr"];
  343. let state = &slot["state"];
  344. let parent = session_id.clone();
  345. // append to existing values
  346. let mut msgs: Vec<(String, String)> = Vec::new();
  347. for msg in msg_values {
  348. let msg: (String, String) = serde_json::from_value(msg.clone())?;
  349. msgs.push(msg);
  350. }
  351. let connect_info = ConnectInfo::new(
  352. connect_id,
  353. addr.as_str().unwrap().to_string(),
  354. is_empty,
  355. last_msg,
  356. last_status,
  357. state.as_str().unwrap().to_string(),
  358. msgs,
  359. parent,
  360. );
  361. connects.push(connect_info.clone());
  362. }
  363. }
  364. }
  365. let is_empty = is_empty_session(connects.clone());
  366. let session_info =
  367. SessionInfo::new(session_name, session_id, node_id, connects.clone(), is_empty);
  368. Ok(session_info)
  369. }
  370. None => Err(Error::ValueIsNotObject),
  371. }
  372. }
  373. async fn render<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> Result<()> {
  374. let mut asi = async_stdin();
  375. terminal.clear()?;
  376. let active_ids = IdListView::new(FxHashSet::default());
  377. let info_list = NodeInfoView::new(FxHashMap::default());
  378. let selectable = FxHashMap::default();
  379. let msg_log = FxHashMap::default();
  380. let mut view = View::new(active_ids.clone(), info_list.clone(), selectable, msg_log);
  381. view.active_ids.state.select(Some(0));
  382. loop {
  383. view.update(
  384. model.nodes.lock().await.clone(),
  385. model.selectables.lock().await.clone(),
  386. model.msg_log.lock().await.clone(),
  387. );
  388. terminal.draw(|f| {
  389. view.render(f);
  390. })?;
  391. for k in asi.by_ref().keys() {
  392. match k.unwrap() {
  393. Key::Char('q') => {
  394. terminal.clear()?;
  395. return Ok(())
  396. }
  397. Key::Char('j') => {
  398. view.active_ids.next();
  399. }
  400. Key::Char('k') => {
  401. view.active_ids.previous();
  402. }
  403. _ => (),
  404. }
  405. }
  406. }
  407. }