refactor for tracing and api logging
This commit is contained in:
@@ -8,6 +8,7 @@ use std::cell::RefCell;
|
||||
use std::net::SocketAddr;
|
||||
use std::rc::Rc;
|
||||
use veilid_core::xx::*;
|
||||
use veilid_core::*;
|
||||
|
||||
macro_rules! capnp_failed {
|
||||
($ex:expr) => {{
|
||||
@@ -17,6 +18,17 @@ macro_rules! capnp_failed {
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! pry_result {
|
||||
($ex:expr) => {
|
||||
match $ex {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
return capnp_failed!(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
struct VeilidClientImpl {
|
||||
comproc: CommandProcessor,
|
||||
}
|
||||
@@ -34,57 +46,23 @@ impl veilid_client::Server for VeilidClientImpl {
|
||||
_results: veilid_client::UpdateResults,
|
||||
) -> Promise<(), ::capnp::Error> {
|
||||
let veilid_update = pry!(pry!(params.get()).get_veilid_update());
|
||||
let veilid_update: VeilidUpdate = pry_result!(deserialize_json(veilid_update));
|
||||
|
||||
let which = match veilid_update.which() {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
return capnp_failed!(format!("(missing update kind in schema: {:?})", e));
|
||||
match veilid_update {
|
||||
VeilidUpdate::Log(log) => {
|
||||
self.comproc.update_log(log);
|
||||
}
|
||||
};
|
||||
match which {
|
||||
veilid_update::Attachment(Ok(attachment)) => {
|
||||
let state = pry!(attachment.get_state());
|
||||
|
||||
trace!("Attachment: {}", state as u16);
|
||||
self.comproc.update_attachment(state);
|
||||
VeilidUpdate::Attachment(attachment) => {
|
||||
self.comproc.update_attachment(attachment);
|
||||
}
|
||||
veilid_update::Attachment(Err(e)) => {
|
||||
return capnp_failed!(format!("Update Attachment Error: {}", e));
|
||||
}
|
||||
veilid_update::Network(Ok(network)) => {
|
||||
let started = network.get_started();
|
||||
let bps_down = network.get_bps_down();
|
||||
let bps_up = network.get_bps_up();
|
||||
|
||||
trace!(
|
||||
"Network: started: {} bps_down: {} bps_up: {}",
|
||||
started,
|
||||
bps_down,
|
||||
bps_up
|
||||
);
|
||||
self.comproc
|
||||
.update_network_status(started, bps_down, bps_up);
|
||||
}
|
||||
veilid_update::Network(Err(e)) => {
|
||||
return capnp_failed!(format!("Update Network Error: {}", e));
|
||||
}
|
||||
veilid_update::Shutdown(()) => {
|
||||
return capnp_failed!("Should not get Shutdown here".to_owned());
|
||||
VeilidUpdate::Network(network) => {
|
||||
self.comproc.update_network_status(network);
|
||||
}
|
||||
VeilidUpdate::Shutdown => self.comproc.update_shutdown(),
|
||||
}
|
||||
|
||||
Promise::ok(())
|
||||
}
|
||||
|
||||
fn log_message(
|
||||
&mut self,
|
||||
params: veilid_client::LogMessageParams,
|
||||
_results: veilid_client::LogMessageResults,
|
||||
) -> Promise<(), ::capnp::Error> {
|
||||
let message = pry!(pry!(params.get()).get_message());
|
||||
self.comproc.add_log_message(message);
|
||||
Promise::ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct ClientApiConnectionInner {
|
||||
@@ -116,53 +94,20 @@ impl ClientApiConnection {
|
||||
}
|
||||
async fn process_veilid_state<'a>(
|
||||
&'a mut self,
|
||||
veilid_state: veilid_state::Reader<'a>,
|
||||
veilid_state: VeilidState,
|
||||
) -> Result<(), String> {
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
|
||||
// Process attachment state
|
||||
let attachment = veilid_state
|
||||
.reborrow()
|
||||
.get_attachment()
|
||||
.map_err(map_to_string)?;
|
||||
let attachment_state = attachment.get_state().map_err(map_to_string)?;
|
||||
|
||||
let network = veilid_state
|
||||
.reborrow()
|
||||
.get_network()
|
||||
.map_err(map_to_string)?;
|
||||
let started = network.get_started();
|
||||
let bps_down = network.get_bps_down();
|
||||
let bps_up = network.get_bps_up();
|
||||
|
||||
inner.comproc.update_attachment(attachment_state);
|
||||
inner
|
||||
.comproc
|
||||
.update_network_status(started, bps_down, bps_up);
|
||||
inner.comproc.update_attachment(veilid_state.attachment);
|
||||
inner.comproc.update_network_status(veilid_state.network);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_connection(&mut self) -> Result<(), String> {
|
||||
trace!("ClientApiConnection::handle_connection");
|
||||
let connect_addr = self.inner.borrow().connect_addr.unwrap();
|
||||
// Connect the TCP socket
|
||||
let stream = async_std::net::TcpStream::connect(connect_addr)
|
||||
.await
|
||||
.map_err(map_to_string)?;
|
||||
// If it succeed, disable nagle algorithm
|
||||
stream.set_nodelay(true).map_err(map_to_string)?;
|
||||
|
||||
// Create the VAT network
|
||||
let (reader, writer) = stream.split();
|
||||
let rpc_network = Box::new(twoparty::VatNetwork::new(
|
||||
reader,
|
||||
writer,
|
||||
rpc_twoparty_capnp::Side::Client,
|
||||
Default::default(),
|
||||
));
|
||||
// Create the rpc system
|
||||
let mut rpc_system = RpcSystem::new(rpc_network, None);
|
||||
async fn spawn_rpc_system(
|
||||
&mut self,
|
||||
connect_addr: SocketAddr,
|
||||
mut rpc_system: RpcSystem<rpc_twoparty_capnp::Side>,
|
||||
) -> Result<(), String> {
|
||||
let mut request;
|
||||
{
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
@@ -195,29 +140,72 @@ impl ClientApiConnection {
|
||||
));
|
||||
}
|
||||
|
||||
// Process the rpc system until we decide we're done
|
||||
if let Ok(rpc_jh) = AsyncStd.spawn_handle_local(rpc_system) {
|
||||
// Send the request and get the state object and the registration object
|
||||
if let Ok(response) = request.send().promise.await {
|
||||
if let Ok(response) = response.get() {
|
||||
if let Ok(_registration) = response.get_registration() {
|
||||
if let Ok(state) = response.get_state() {
|
||||
// Set up our state for the first time
|
||||
if self.process_veilid_state(state).await.is_ok() {
|
||||
// Don't drop the registration, doing so will remove the client
|
||||
// object mapping from the server which we need for the update backchannel
|
||||
let rpc_jh = AsyncStd
|
||||
.spawn_handle_local(rpc_system)
|
||||
.map_err(|e| format!("failed to spawn rpc system: {}", e))?;
|
||||
|
||||
// Wait until rpc system completion or disconnect was requested
|
||||
if let Err(e) = rpc_jh.await {
|
||||
error!("Client RPC system error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Send the request and get the state object and the registration object
|
||||
let response = request
|
||||
.send()
|
||||
.promise
|
||||
.await
|
||||
.map_err(|e| format!("failed to send register request: {}", e))?;
|
||||
let response = response
|
||||
.get()
|
||||
.map_err(|e| format!("failed to get register response: {}", e))?;
|
||||
|
||||
// Get the registration object, which drops our connection when it is dropped
|
||||
let _registration = response
|
||||
.get_registration()
|
||||
.map_err(|e| format!("failed to get registration object: {}", e))?;
|
||||
|
||||
// Get the initial veilid state
|
||||
let veilid_state = response
|
||||
.get_state()
|
||||
.map_err(|e| format!("failed to get initial veilid state: {}", e))?;
|
||||
|
||||
// Set up our state for the first time
|
||||
let veilid_state: VeilidState = deserialize_json(veilid_state)
|
||||
.map_err(|e| format!("failed to get deserialize veilid state: {}", e))?;
|
||||
self.process_veilid_state(veilid_state).await?;
|
||||
|
||||
// Don't drop the registration, doing so will remove the client
|
||||
// object mapping from the server which we need for the update backchannel
|
||||
|
||||
// Wait until rpc system completion or disconnect was requested
|
||||
rpc_jh
|
||||
.await
|
||||
.map_err(|e| format!("client RPC system error: {}", e))
|
||||
}
|
||||
|
||||
async fn handle_connection(&mut self) -> Result<(), String> {
|
||||
trace!("ClientApiConnection::handle_connection");
|
||||
let connect_addr = self.inner.borrow().connect_addr.unwrap();
|
||||
// Connect the TCP socket
|
||||
let stream = async_std::net::TcpStream::connect(connect_addr)
|
||||
.await
|
||||
.map_err(map_to_string)?;
|
||||
// If it succeed, disable nagle algorithm
|
||||
stream.set_nodelay(true).map_err(map_to_string)?;
|
||||
|
||||
// Create the VAT network
|
||||
let (reader, writer) = stream.split();
|
||||
let rpc_network = Box::new(twoparty::VatNetwork::new(
|
||||
reader,
|
||||
writer,
|
||||
rpc_twoparty_capnp::Side::Client,
|
||||
Default::default(),
|
||||
));
|
||||
|
||||
// Create the rpc system
|
||||
let rpc_system = RpcSystem::new(rpc_network, None);
|
||||
|
||||
// Process the rpc system until we decide we're done
|
||||
match self.spawn_rpc_system(connect_addr, rpc_system).await {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
error!("Failed to spawn client RPC system: {}", e);
|
||||
}
|
||||
} else {
|
||||
error!("Failed to spawn client RPC system");
|
||||
}
|
||||
|
||||
// Drop the server and disconnector too (if we still have it)
|
||||
|
@@ -1,7 +1,6 @@
|
||||
use crate::client_api_connection::*;
|
||||
use crate::settings::Settings;
|
||||
use crate::ui::*;
|
||||
use crate::veilid_client_capnp::*;
|
||||
use async_std::prelude::FutureExt;
|
||||
use log::*;
|
||||
use std::cell::*;
|
||||
@@ -273,18 +272,23 @@ debug - send a debugging command to the Veilid server
|
||||
// called by client_api_connection
|
||||
// calls into ui
|
||||
////////////////////////////////////////////
|
||||
pub fn update_attachment(&mut self, state: AttachmentState) {
|
||||
self.inner_mut().ui.set_attachment_state(state);
|
||||
pub fn update_attachment(&mut self, attachment: veilid_core::VeilidStateAttachment) {
|
||||
self.inner_mut().ui.set_attachment_state(attachment.state);
|
||||
}
|
||||
|
||||
pub fn update_network_status(&mut self, started: bool, bps_down: u64, bps_up: u64) {
|
||||
pub fn update_network_status(&mut self, network: veilid_core::VeilidStateNetwork) {
|
||||
self.inner_mut()
|
||||
.ui
|
||||
.set_network_status(started, bps_down, bps_up);
|
||||
.set_network_status(network.started, network.bps_down, network.bps_up);
|
||||
}
|
||||
|
||||
pub fn add_log_message(&mut self, message: &str) {
|
||||
self.inner().ui.add_node_event(message);
|
||||
pub fn update_log(&mut self, log: veilid_core::VeilidStateLog) {
|
||||
let message = format!("{}: {}", log.log_level, log.message);
|
||||
self.inner().ui.add_node_event(&message);
|
||||
}
|
||||
|
||||
pub fn update_shutdown(&mut self) {
|
||||
// Do nothing with this, we'll process shutdown when rpc connection closes
|
||||
}
|
||||
|
||||
// called by client_api_connection
|
||||
|
@@ -1,6 +1,5 @@
|
||||
use crate::command_processor::*;
|
||||
use crate::settings::Settings;
|
||||
use crate::veilid_client_capnp::*;
|
||||
use crossbeam_channel::Sender;
|
||||
use cursive::align::*;
|
||||
use cursive::event::*;
|
||||
@@ -15,7 +14,7 @@ use log::*;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::rc::Rc;
|
||||
// use thiserror::Error;
|
||||
use veilid_core::*;
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
///
|
||||
@@ -280,9 +279,8 @@ impl UI {
|
||||
.button("Close", move |s| {
|
||||
s.pop_layer();
|
||||
close_cb(s);
|
||||
})
|
||||
//.wrap_with(CircularFocus::new)
|
||||
//.wrap_tab(),
|
||||
}), //.wrap_with(CircularFocus::new)
|
||||
//.wrap_tab(),
|
||||
);
|
||||
s.set_global_callback(cursive::event::Event::Key(Key::Esc), move |s| {
|
||||
s.set_global_callback(cursive::event::Event::Key(Key::Esc), UI::quit_handler);
|
||||
|
Reference in New Issue
Block a user