Add doc tags and general cleanup

This commit is contained in:
Herbert Wolverson 2023-01-19 14:36:09 +00:00
parent a5fc96abc9
commit 4d569e698c
3 changed files with 14 additions and 1 deletions

View File

@ -2,12 +2,17 @@ use anyhow::Result;
use tokio::{net::UnixStream, io::{AsyncWriteExt, AsyncReadExt}};
use crate::{BUS_SOCKET_PATH, BusRequest, BusResponse, BusSession, encode_request, decode_response};
/// Provides a lqosd bus client that persists between connections. Useful for when you are
/// going to be repeatedly polling the bus for data (e.g. `lqtop`) and want to avoid the
/// overhead of an individual connection.
pub struct BusClient {
stream: UnixStream,
buffer: Vec<u8>,
}
impl BusClient {
/// Instantiates a bus client, connecting to the bus stream and initializing
/// a buffer.
pub async fn new() -> Result<Self> {
let stream = UnixStream::connect(BUS_SOCKET_PATH).await?;
Ok(Self {
@ -16,6 +21,8 @@ impl BusClient {
})
}
/// Analagous to the singe-task `bus_request`, sends a request to the existing
/// bus connection.
pub async fn request(&mut self, requests: Vec<BusRequest>) -> Result<Vec<BusResponse>> {
let test = BusSession {
persist: true,

View File

@ -5,9 +5,13 @@ use nix::libc::mode_t;
use tokio::{net::{UnixListener, UnixStream}, io::{AsyncReadExt, AsyncWriteExt}};
use log::warn;
/// Implements a Tokio-friendly server using Unix Sockets and the bus protocol.
/// Requests are handled and then forwarded to the handler.
pub struct UnixSocketServer {}
impl UnixSocketServer {
/// Creates a new `UnixSocketServer`. Will delete any pre-existing
/// socket file.
pub fn new() -> Result<Self> {
Self::delete_local_socket()?;
Ok(Self {})
@ -29,6 +33,8 @@ impl UnixSocketServer {
Ok(())
}
/// Start listening for bus traffic, forward requests to the `handle_bus_requests`
/// function for procesing.
pub async fn listen(&self, handle_bus_requests: fn(&[BusRequest], &mut Vec<BusResponse>)) -> Result<()>
{
// Setup the listener and grant permissions to it

View File

@ -1,7 +1,7 @@
use anyhow::Result;
use crossterm::{event::KeyCode, terminal::enable_raw_mode};
use lqos_bus::{
BusRequest, BusResponse, IpStats, bus_request, BusClient,
BusRequest, BusResponse, IpStats, BusClient,
};
use std::{io, time::Duration};
use tui::{