| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- use async_std::sync::{Arc, Mutex};
- use std::{collections::hash_map::Entry, fs::File, io, io::Read, path::PathBuf};
- use easy_parallel::Parallel;
- use fxhash::{FxHashMap, FxHashSet};
- use log::info;
- use serde_json::{json, Value};
- use simplelog::*;
- use smol::Executor;
- use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
- use tui::{
- backend::{Backend, TermionBackend},
- Terminal,
- };
- use url::Url;
- use darkfi::{
- error::Result,
- rpc::{client::RpcClient, jsonrpc::JsonRequest},
- util::{
- async_util,
- cli::{log_config, spawn_config, Config},
- join_config_path, NanoTimestamp,
- },
- };
- use dnetview::{
- config::{DnvConfig, CONFIG_FILE_CONTENTS},
- error::{DnetViewError, DnetViewResult},
- model::{ConnectInfo, Model, NodeInfo, SelectableObject, Session, SessionInfo},
- options::ProgramOptions,
- util::{is_empty_session, make_connect_id, make_empty_id, make_node_id, make_session_id},
- view::{IdListView, NodeInfoView, View},
- };
- use log::error;
- struct DnetView {
- name: String,
- rpc_client: RpcClient,
- }
- impl DnetView {
- async fn new(url: Url, name: String) -> Result<Self> {
- let rpc_client = RpcClient::new(url).await?;
- Ok(Self { name, rpc_client })
- }
- // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
- // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
- async fn _ping(&self) -> Result<Value> {
- let req = JsonRequest::new("ping", json!([]));
- self.rpc_client.request(req).await
- }
- //--> {"jsonrpc": "2.0", "method": "poll", "params": [], "id": 42}
- // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42}
- async fn get_info(&self) -> DnetViewResult<Value> {
- let req = JsonRequest::new("get_info", json!([]));
- match self.rpc_client.request(req).await {
- Ok(req) => Ok(req),
- Err(e) => Err(DnetViewError::Darkfi(e)),
- }
- }
- }
- #[async_std::main]
- async fn main() -> DnetViewResult<()> {
- let options = ProgramOptions::load()?;
- let verbosity_level = options.app.occurrences_of("verbose");
- let (lvl, cfg) = log_config(verbosity_level)?;
- let file = File::create(&*options.log_path).unwrap();
- WriteLogger::init(lvl, cfg, file)?;
- info!("Log level: {}", lvl);
- let config_path = join_config_path(&PathBuf::from("dnetview_config.toml"))?;
- spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
- let config = Config::<DnvConfig>::load(config_path)?;
- let stdout = io::stdout().into_raw_mode()?;
- let backend = TermionBackend::new(stdout);
- let mut terminal = Terminal::new(backend)?;
- terminal.clear()?;
- let ids = Mutex::new(FxHashSet::default());
- let nodes = Mutex::new(FxHashMap::default());
- let selectables = Mutex::new(FxHashMap::default());
- let msg_log = Mutex::new(FxHashMap::default());
- let model = Arc::new(Model::new(ids, nodes, selectables, msg_log));
- let nthreads = num_cpus::get();
- let (signal, shutdown) = async_channel::unbounded::<()>();
- let ex = Arc::new(Executor::new());
- let ex2 = ex.clone();
- let (_, result) = Parallel::new()
- .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
- .finish(|| {
- smol::future::block_on(async move {
- poll_and_update_model(&config, ex2.clone(), model.clone()).await?;
- render_view(&mut terminal, model.clone()).await?;
- drop(signal);
- Ok(())
- })
- });
- result
- }
- // create a new RPC instance for every node in the config file
- // spawn poll() and detach in the background
- async fn poll_and_update_model(
- config: &DnvConfig,
- ex: Arc<Executor<'_>>,
- model: Arc<Model>,
- ) -> DnetViewResult<()> {
- for node in &config.nodes {
- info!("Attempting to poll {}, RPC URL: {}", node.name, node.rpc_url);
- match DnetView::new(Url::parse(&node.rpc_url)?, node.name.clone()).await {
- Ok(client) => ex.spawn(poll(client, model.clone())).detach(),
- Err(e) => error!("{}", e),
- }
- }
- Ok(())
- }
- async fn poll(client: DnetView, model: Arc<Model>) -> DnetViewResult<()> {
- loop {
- match client.get_info().await {
- Ok(reply) => {
- if reply.as_object().is_some() && !reply.as_object().unwrap().is_empty() {
- parse_data(reply.as_object().unwrap(), &client, model.clone()).await?;
- } else {
- return Err(DnetViewError::EmptyRpcReply)
- }
- }
- Err(e) => {
- error!("{:?}", e);
- parse_offline(&client, model.clone()).await?;
- }
- }
- async_util::sleep(2).await;
- }
- }
- async fn parse_offline(client: &DnetView, model: Arc<Model>) -> DnetViewResult<()> {
- let name = "Offline".to_string();
- let session_type = Session::Offline;
- let node_name = &client.name;
- let node_id = make_node_id(node_name)?;
- let session_id = make_session_id(&node_id, &session_type)?;
- let mut connects: Vec<ConnectInfo> = Vec::new();
- let mut sessions: Vec<SessionInfo> = Vec::new();
- // initialize with empty values
- let id = make_empty_id(&node_id, &session_type, 0)?;
- let addr = "Null".to_string();
- let state = "Null".to_string();
- let parent = node_id.clone();
- let msg_log = Vec::new();
- let is_empty = true;
- let last_msg = "Null".to_string();
- let last_status = "Null".to_string();
- let connect_info =
- ConnectInfo::new(id, addr, state, parent.clone(), msg_log, is_empty, last_msg, last_status);
- connects.push(connect_info.clone());
- let accept_addr = None;
- let session_info =
- SessionInfo::new(session_id, name, is_empty, parent.clone(), connects, accept_addr);
- sessions.push(session_info);
- let node = NodeInfo::new(node_id.clone(), node_name.to_string(), sessions.clone(), None, true);
- update_node(model.clone(), node.clone(), node_id.clone()).await;
- update_selectable_and_ids(model.clone(), sessions, node.clone()).await?;
- Ok(())
- }
- async fn parse_data(
- reply: &serde_json::Map<String, Value>,
- client: &DnetView,
- model: Arc<Model>,
- ) -> DnetViewResult<()> {
- let addr = &reply.get("external_addr");
- let inbound = &reply["session_inbound"];
- let _manual = &reply["session_manual"];
- let outbound = &reply["session_outbound"];
- let mut sessions: Vec<SessionInfo> = Vec::new();
- let node_name = &client.name;
- let node_id = make_node_id(node_name)?;
- //let external_addr = ext_addr.unwrap().as_str().unwrap();
- let ext_addr = parse_external_addr(addr).await?;
- let in_session = parse_inbound(inbound, &node_id).await?;
- let out_session = parse_outbound(outbound, &node_id).await?;
- //let man_session = parse_manual(manual, &node_id).await?;
- sessions.push(in_session.clone());
- sessions.push(out_session.clone());
- //sessions.push(man_session.clone());
- let node =
- NodeInfo::new(node_id.clone(), node_name.to_string(), sessions.clone(), ext_addr, false);
- update_node(model.clone(), node.clone(), node_id.clone()).await;
- update_selectable_and_ids(model.clone(), sessions.clone(), node.clone()).await?;
- update_msgs(model.clone(), sessions.clone()).await?;
- //debug!("IDS: {:?}", model.ids.lock().await);
- //debug!("INFOS: {:?}", model.nodes.lock().await);
- Ok(())
- }
- async fn update_msgs(model: Arc<Model>, sessions: Vec<SessionInfo>) -> DnetViewResult<()> {
- for session in sessions {
- for connection in session.children {
- if !model.msg_log.lock().await.contains_key(&connection.id) {
- model.msg_log.lock().await.insert(connection.id, connection.msg_log);
- } else {
- match model.msg_log.lock().await.entry(connection.id) {
- Entry::Vacant(e) => {
- e.insert(connection.msg_log);
- }
- Entry::Occupied(mut e) => {
- for msg in connection.msg_log {
- e.get_mut().push(msg);
- }
- }
- }
- }
- }
- }
- Ok(())
- }
- async fn update_ids(model: Arc<Model>, id: String) {
- model.ids.lock().await.insert(id);
- }
- async fn update_node(model: Arc<Model>, node: NodeInfo, id: String) {
- model.nodes.lock().await.insert(id, node);
- }
- async fn update_selectable_and_ids(
- model: Arc<Model>,
- sessions: Vec<SessionInfo>,
- node: NodeInfo,
- ) -> DnetViewResult<()> {
- let node_obj = SelectableObject::Node(node.clone());
- model.selectables.lock().await.insert(node.id.clone(), node_obj);
- update_ids(model.clone(), node.id.clone()).await;
- for session in sessions {
- let session_obj = SelectableObject::Session(session.clone());
- model.selectables.lock().await.insert(session.clone().id, session_obj);
- update_ids(model.clone(), session.clone().id).await;
- for connect in session.children {
- let connect_obj = SelectableObject::Connect(connect.clone());
- model.selectables.lock().await.insert(connect.clone().id, connect_obj);
- update_ids(model.clone(), connect.clone().id).await;
- }
- }
- Ok(())
- }
- async fn parse_external_addr(addr: &Option<&Value>) -> DnetViewResult<Option<String>> {
- match addr {
- Some(addr) => match addr.as_str() {
- Some(addr) => Ok(Some(addr.to_string())),
- None => Ok(None),
- },
- None => Err(DnetViewError::NoExternalAddr),
- }
- }
- async fn parse_inbound(inbound: &Value, node_id: &String) -> DnetViewResult<SessionInfo> {
- let name = "Inbound".to_string();
- let session_type = Session::Inbound;
- let parent = node_id.to_string();
- let id = make_session_id(&parent, &session_type)?;
- let mut connects: Vec<ConnectInfo> = Vec::new();
- let connections = &inbound["connected"];
- let mut connect_count = 0;
- let mut accept_vec = Vec::new();
- match connections.as_object() {
- Some(connect) => {
- match connect.is_empty() {
- true => {
- connect_count += 1;
- // channel is empty. initialize with empty values
- let id = make_empty_id(node_id, &session_type, connect_count)?;
- let addr = "Null".to_string();
- let state = "Null".to_string();
- let parent = parent.clone();
- let msg_log = Vec::new();
- let is_empty = true;
- let last_msg = "Null".to_string();
- let last_status = "Null".to_string();
- let connect_info = ConnectInfo::new(
- id,
- addr,
- state,
- parent,
- msg_log,
- is_empty,
- last_msg,
- last_status,
- );
- connects.push(connect_info);
- }
- false => {
- // channel is not empty. initialize with whole values
- for k in connect.keys() {
- let node = connect.get(k);
- let addr = k.to_string();
- let info = node.unwrap().as_array();
- // get the accept address
- let accept_addr = info.unwrap().get(0);
- let acc_addr = accept_addr
- .unwrap()
- .get("accept_addr")
- .unwrap()
- .as_str()
- .unwrap()
- .to_string();
- accept_vec.push(acc_addr);
- let info2 = info.unwrap().get(1);
- let id = info2.unwrap().get("random_id").unwrap().as_u64().unwrap();
- let id = make_connect_id(&id)?;
- let state = "state".to_string();
- let parent = parent.clone();
- let msg_values = info2.unwrap().get("log").unwrap().as_array().unwrap();
- let mut msg_log: Vec<(NanoTimestamp, String, String)> = Vec::new();
- for msg in msg_values {
- let msg: (NanoTimestamp, String, String) =
- serde_json::from_value(msg.clone())?;
- msg_log.push(msg);
- }
- let is_empty = false;
- let last_msg =
- info2.unwrap().get("last_msg").unwrap().as_str().unwrap().to_string();
- let last_status = info2
- .unwrap()
- .get("last_status")
- .unwrap()
- .as_str()
- .unwrap()
- .to_string();
- let connect_info = ConnectInfo::new(
- id,
- addr,
- state,
- parent,
- msg_log,
- is_empty,
- last_msg,
- last_status,
- );
- connects.push(connect_info.clone());
- }
- }
- }
- let is_empty = is_empty_session(&connects);
- // TODO: clean this up
- if accept_vec.is_empty() {
- let accept_addr = None;
- let session_info =
- SessionInfo::new(id, name, is_empty, parent, connects, accept_addr);
- Ok(session_info)
- } else {
- let accept_addr = Some(accept_vec[0].clone());
- let session_info =
- SessionInfo::new(id, name, is_empty, parent, connects, accept_addr);
- Ok(session_info)
- }
- }
- None => Err(DnetViewError::ValueIsNotObject),
- }
- }
- // TODO: placeholder for now
- async fn parse_manual(_manual: &Value, node_id: &String) -> DnetViewResult<SessionInfo> {
- let name = "Manual".to_string();
- let session_type = Session::Manual;
- let mut connects: Vec<ConnectInfo> = Vec::new();
- let parent = node_id.to_string();
- let session_id = make_session_id(&parent, &session_type)?;
- //let id: u64 = 0;
- let connect_id = make_empty_id(node_id, &session_type, 0)?;
- //let connect_id = make_connect_id(&id)?;
- let addr = "Null".to_string();
- let state = "Null".to_string();
- let msg_log = Vec::new();
- let is_empty = true;
- let msg = "Null".to_string();
- let status = "Null".to_string();
- let connect_info =
- ConnectInfo::new(connect_id.clone(), addr, state, parent, msg_log, is_empty, msg, status);
- connects.push(connect_info);
- let parent = connect_id;
- let is_empty = is_empty_session(&connects);
- let accept_addr = None;
- let session_info =
- SessionInfo::new(session_id, name, is_empty, parent, connects.clone(), accept_addr);
- Ok(session_info)
- }
- async fn parse_outbound(outbound: &Value, node_id: &String) -> DnetViewResult<SessionInfo> {
- let name = "Outbound".to_string();
- let session_type = Session::Outbound;
- let parent = node_id.to_string();
- let id = make_session_id(&parent, &session_type)?;
- let mut connects: Vec<ConnectInfo> = Vec::new();
- let slots = &outbound["slots"];
- let mut slot_count = 0;
- match slots.as_array() {
- Some(slots) => {
- for slot in slots {
- slot_count += 1;
- match slot["channel"].is_null() {
- true => {
- // channel is empty. initialize with empty values
- let id = make_empty_id(node_id, &session_type, slot_count)?;
- let addr = "Null".to_string();
- let state = &slot["state"];
- let state = state.as_str().unwrap().to_string();
- let parent = parent.clone();
- let msg_log = Vec::new();
- let is_empty = true;
- let last_msg = "Null".to_string();
- let last_status = "Null".to_string();
- let connect_info = ConnectInfo::new(
- id,
- addr,
- state,
- parent,
- msg_log,
- is_empty,
- last_msg,
- last_status,
- );
- connects.push(connect_info.clone());
- }
- false => {
- // channel is not empty. initialize with whole values
- let channel = &slot["channel"];
- let id = channel["random_id"].as_u64().unwrap();
- let id = make_connect_id(&id)?;
- let addr = &slot["addr"];
- let addr = addr.as_str().unwrap().to_string();
- let state = &slot["state"];
- let state = state.as_str().unwrap().to_string();
- let parent = parent.clone();
- let msg_values = channel["log"].as_array().unwrap();
- let mut msg_log: Vec<(NanoTimestamp, String, String)> = Vec::new();
- for msg in msg_values {
- let msg: (NanoTimestamp, String, String) =
- serde_json::from_value(msg.clone())?;
- msg_log.push(msg);
- }
- let is_empty = false;
- let last_msg = channel["last_msg"].as_str().unwrap().to_string();
- let last_status = channel["last_status"].as_str().unwrap().to_string();
- let connect_info = ConnectInfo::new(
- id,
- addr,
- state,
- parent,
- msg_log,
- is_empty,
- last_msg,
- last_status,
- );
- connects.push(connect_info.clone());
- }
- }
- }
- let is_empty = is_empty_session(&connects);
- let accept_addr = None;
- let session_info = SessionInfo::new(id, name, is_empty, parent, connects, accept_addr);
- Ok(session_info)
- }
- None => Err(DnetViewError::ValueIsNotObject),
- }
- }
- async fn render_view<B: Backend>(
- terminal: &mut Terminal<B>,
- model: Arc<Model>,
- ) -> DnetViewResult<()> {
- let mut asi = async_stdin();
- terminal.clear()?;
- let nodes = NodeInfoView::new(FxHashMap::default());
- let msg_log = FxHashMap::default();
- let active_ids = IdListView::new(FxHashSet::default());
- let selectables = FxHashMap::default();
- let mut view = View::new(nodes, msg_log, active_ids, selectables);
- view.active_ids.state.select(Some(0));
- loop {
- view.update(
- model.nodes.lock().await.clone(),
- model.msg_log.lock().await.clone(),
- model.selectables.lock().await.clone(),
- );
- let mut err: Option<DnetViewError> = None;
- terminal.draw(|f| match view.render(f) {
- Ok(()) => {}
- Err(e) => {
- err = Some(e);
- }
- })?;
- match err {
- Some(e) => return Err(e),
- None => {}
- }
- for k in asi.by_ref().keys() {
- match k.unwrap() {
- Key::Char('q') => {
- terminal.clear()?;
- return Ok(())
- }
- Key::Char('j') => {
- view.active_ids.next();
- }
- Key::Char('k') => {
- view.active_ids.previous();
- }
- _ => (),
- }
- }
- }
- }
|