Add debug mode to rupsc. Fixes to parsing.

This commit is contained in:
Aram 🍐 2021-07-31 03:28:32 -04:00
parent d1d6b4e1a8
commit 52afe6bbd1
7 changed files with 129 additions and 79 deletions

View file

@ -19,6 +19,7 @@ fn main() -> nut_client::Result<()> {
let config = ConfigBuilder::new() let config = ConfigBuilder::new()
.with_host(Host::Tcp(addr)) .with_host(Host::Tcp(addr))
.with_auth(auth) .with_auth(auth)
.with_debug(false) // Turn this on for debugging network chatter
.build(); .build();
let mut conn = Connection::new(config)?; let mut conn = Connection::new(config)?;

View file

@ -3,7 +3,7 @@ use std::io::{BufRead, BufReader, Write};
use std::net::{SocketAddr, TcpStream}; use std::net::{SocketAddr, TcpStream};
use crate::cmd::{Command, Response}; use crate::cmd::{Command, Response};
use crate::{ClientError, Config, Host, NutError, Variable}; use crate::{Config, Host, NutError, Variable};
/// A blocking NUT client connection. /// A blocking NUT client connection.
pub enum Connection { pub enum Connection {
@ -72,58 +72,76 @@ impl TcpConnection {
fn login(&mut self) -> crate::Result<()> { fn login(&mut self) -> crate::Result<()> {
if let Some(auth) = &self.config.auth { if let Some(auth) = &self.config.auth {
// Pass username and check for 'OK' // Pass username and check for 'OK'
Self::write_cmd(&mut self.tcp_stream, Command::SetUsername(&auth.username))?; Self::write_cmd(
Self::read_response(&mut self.tcp_stream)?.expect_ok()?; &mut self.tcp_stream,
Command::SetUsername(&auth.username),
self.config.debug,
)?;
Self::read_response(&mut self.tcp_stream, self.config.debug)?.expect_ok()?;
// Pass password and check for 'OK' // Pass password and check for 'OK'
if let Some(password) = &auth.password { if let Some(password) = &auth.password {
Self::write_cmd(&mut self.tcp_stream, Command::SetPassword(password))?; Self::write_cmd(
Self::read_response(&mut self.tcp_stream)?.expect_ok()?; &mut self.tcp_stream,
Command::SetPassword(password),
self.config.debug,
)?;
Self::read_response(&mut self.tcp_stream, self.config.debug)?.expect_ok()?;
} }
} }
Ok(()) Ok(())
} }
fn list_ups(&mut self) -> crate::Result<Vec<(String, String)>> { fn list_ups(&mut self) -> crate::Result<Vec<(String, String)>> {
Self::write_cmd(&mut self.tcp_stream, Command::List(&["UPS"]))?; Self::write_cmd(
let list = Self::read_list(&mut self.tcp_stream, &["UPS"])?; &mut self.tcp_stream,
Command::List(&["UPS"]),
self.config.debug,
)?;
let list = Self::read_list(&mut self.tcp_stream, &["UPS"], self.config.debug)?;
Ok(list list.into_iter().map(|row| row.expect_ups()).collect()
.into_iter()
.map(|mut row| (row.remove(0), row.remove(0)))
.collect())
} }
fn list_vars(&mut self, ups_name: &str) -> crate::Result<Vec<(String, String)>> { fn list_vars(&mut self, ups_name: &str) -> crate::Result<Vec<(String, String)>> {
let query = &["VAR", ups_name]; let query = &["VAR", ups_name];
Self::write_cmd(&mut self.tcp_stream, Command::List(query))?; Self::write_cmd(
let list = Self::read_list(&mut self.tcp_stream, query)?; &mut self.tcp_stream,
Command::List(query),
self.config.debug,
)?;
let list = Self::read_list(&mut self.tcp_stream, query, self.config.debug)?;
Ok(list list.into_iter().map(|row| row.expect_var()).collect()
.into_iter()
.map(|mut row| (row.remove(0), row.remove(0)))
.collect())
} }
fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result<(String, String)> { fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result<(String, String)> {
let query = &["VAR", ups_name, variable]; let query = &["VAR", ups_name, variable];
Self::write_cmd(&mut self.tcp_stream, Command::Get(query))?; Self::write_cmd(&mut self.tcp_stream, Command::Get(query), self.config.debug)?;
let resp = Self::read_response(&mut self.tcp_stream)?; let resp = Self::read_response(&mut self.tcp_stream, self.config.debug)?;
let (name, value) = resp.expect_var()?; resp.expect_var()
Ok((name.into(), value.into()))
} }
fn write_cmd(stream: &mut TcpStream, line: Command) -> crate::Result<()> { fn write_cmd(stream: &mut TcpStream, line: Command, debug: bool) -> crate::Result<()> {
let line = format!("{}\n", line); let line = format!("{}\n", line);
if debug {
eprint!("DEBUG -> {}", line);
}
stream.write_all(line.as_bytes())?; stream.write_all(line.as_bytes())?;
stream.flush()?; stream.flush()?;
Ok(()) Ok(())
} }
fn parse_line(reader: &mut BufReader<&mut TcpStream>) -> crate::Result<Vec<String>> { fn parse_line(
reader: &mut BufReader<&mut TcpStream>,
debug: bool,
) -> crate::Result<Vec<String>> {
let mut raw = String::new(); let mut raw = String::new();
reader.read_line(&mut raw)?; reader.read_line(&mut raw)?;
if debug {
eprint!("DEBUG <- {}", raw);
}
raw = raw[..raw.len() - 1].to_string(); // Strip off \n raw = raw[..raw.len() - 1].to_string(); // Strip off \n
// Parse args by splitting whitespace, minding quotes for args with multiple words // Parse args by splitting whitespace, minding quotes for args with multiple words
@ -133,46 +151,35 @@ impl TcpConnection {
Ok(args) Ok(args)
} }
fn read_response(stream: &mut TcpStream) -> crate::Result<Response> { fn read_response(stream: &mut TcpStream, debug: bool) -> crate::Result<Response> {
let mut reader = io::BufReader::new(stream); let mut reader = io::BufReader::new(stream);
let args = Self::parse_line(&mut reader)?; let args = Self::parse_line(&mut reader, debug)?;
Response::from_args(args) Response::from_args(args)
} }
fn read_list(stream: &mut TcpStream, query: &[&str]) -> crate::Result<Vec<Vec<String>>> { fn read_list(
stream: &mut TcpStream,
query: &[&str],
debug: bool,
) -> crate::Result<Vec<Response>> {
let mut reader = io::BufReader::new(stream); let mut reader = io::BufReader::new(stream);
let args = Self::parse_line(&mut reader)?; let args = Self::parse_line(&mut reader, debug)?;
Response::from_args(args)?.expect_begin_list(query)?; Response::from_args(args)?.expect_begin_list(query)?;
let mut lines: Vec<Vec<String>> = Vec::new(); let mut lines: Vec<Response> = Vec::new();
loop { loop {
let mut args = Self::parse_line(&mut reader)?; let args = Self::parse_line(&mut reader, debug)?;
let resp = Response::from_args(args.clone()); let resp = Response::from_args(args)?;
if let Ok(resp) = resp { match resp {
resp.expect_end_list(query)?; Response::EndList(_) => {
break; break;
} else {
let err = resp.unwrap_err();
if let ClientError::Nut(err) = err {
if let NutError::UnknownResponseType(_) = err {
// Likely an item entry, let's check...
if args.len() < query.len() || &args[0..query.len()] != query {
return Err(ClientError::Nut(err));
} else {
let args = args.drain(query.len()..).collect();
lines.push(args);
continue;
}
} else {
return Err(ClientError::Nut(err));
}
} else {
return Err(err);
} }
_ => lines.push(resp),
} }
} }
Ok(lines) Ok(lines)
} }
} }

View file

@ -51,8 +51,10 @@ pub enum Response {
BeginList(String), BeginList(String),
/// Marks the end of a list response. /// Marks the end of a list response.
EndList(String), EndList(String),
/// A variable response. /// A variable (VAR) response.
Var(String, String), Var(String, String),
/// A UPS (UPS) response.
Ups(String, String),
} }
impl Response { impl Response {
@ -115,6 +117,13 @@ impl Response {
} }
} }
"VAR" => { "VAR" => {
let _var_device = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified VAR device name in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let var_name = if args.is_empty() { let var_name = if args.is_empty() {
Err(ClientError::from(NutError::Generic( Err(ClientError::from(NutError::Generic(
"Unspecified VAR name in response".into(), "Unspecified VAR name in response".into(),
@ -131,6 +140,23 @@ impl Response {
}?; }?;
Ok(Response::Var(var_name, var_value)) Ok(Response::Var(var_name, var_value))
} }
"UPS" => {
let name = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified UPS name in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
let description = if args.is_empty() {
Err(ClientError::from(NutError::Generic(
"Unspecified UPS description in response".into(),
)))
} else {
Ok(args.remove(0))
}?;
Ok(Response::Ups(name, description))
}
_ => Err(NutError::UnknownResponseType(cmd_name).into()), _ => Err(NutError::UnknownResponseType(cmd_name).into()),
} }
} }
@ -155,22 +181,17 @@ impl Response {
} }
} }
pub fn expect_end_list(self, expected_args: &[&str]) -> crate::Result<Response> { pub fn expect_var(&self) -> crate::Result<(String, String)> {
let expected_args = shell_words::join(expected_args); if let Self::Var(name, value) = &self {
if let Self::EndList(args) = &self { Ok((name.to_owned(), value.to_owned()))
if &expected_args == args {
Ok(self)
} else {
Err(NutError::UnexpectedResponse.into())
}
} else { } else {
Err(NutError::UnexpectedResponse.into()) Err(NutError::UnexpectedResponse.into())
} }
} }
pub fn expect_var(&self) -> crate::Result<(&str, &str)> { pub fn expect_ups(&self) -> crate::Result<(String, String)> {
if let Self::Var(name, value) = &self { if let Self::Ups(name, description) = &self {
Ok((name, value)) Ok((name.to_owned(), description.to_owned()))
} else { } else {
Err(NutError::UnexpectedResponse.into()) Err(NutError::UnexpectedResponse.into())
} }

View file

@ -12,7 +12,7 @@ pub enum Host {
impl Default for Host { impl Default for Host {
fn default() -> Self { fn default() -> Self {
let addr = (String::from("localhost"), 3493) let addr = (String::from("127.0.0.1"), 3493)
.to_socket_addrs() .to_socket_addrs()
.expect("Failed to create local UPS socket address. This is a bug.") .expect("Failed to create local UPS socket address. This is a bug.")
.next() .next()
@ -58,15 +58,17 @@ pub struct Config {
pub(crate) host: Host, pub(crate) host: Host,
pub(crate) auth: Option<Auth>, pub(crate) auth: Option<Auth>,
pub(crate) timeout: Duration, pub(crate) timeout: Duration,
pub(crate) debug: bool,
} }
impl Config { impl Config {
/// Creates a connection configuration. /// Creates a connection configuration.
pub fn new(host: Host, auth: Option<Auth>, timeout: Duration) -> Self { pub fn new(host: Host, auth: Option<Auth>, timeout: Duration, debug: bool) -> Self {
Config { Config {
host, host,
auth, auth,
timeout, timeout,
debug,
} }
} }
} }
@ -77,6 +79,7 @@ pub struct ConfigBuilder {
host: Option<Host>, host: Option<Host>,
auth: Option<Auth>, auth: Option<Auth>,
timeout: Option<Duration>, timeout: Option<Duration>,
debug: Option<bool>,
} }
impl ConfigBuilder { impl ConfigBuilder {
@ -104,12 +107,19 @@ impl ConfigBuilder {
self self
} }
/// Enables debugging network calls by printing to stderr.
pub fn with_debug(mut self, debug: bool) -> Self {
self.debug = Some(debug);
self
}
/// Builds the configuration with this builder. /// Builds the configuration with this builder.
pub fn build(self) -> Config { pub fn build(self) -> Config {
Config::new( Config::new(
self.host.unwrap_or_default(), self.host.unwrap_or_default(),
self.auth, self.auth,
self.timeout.unwrap_or_else(|| Duration::from_secs(5)), self.timeout.unwrap_or_else(|| Duration::from_secs(5)),
self.debug.unwrap_or(false),
) )
} }
} }

View file

@ -4,11 +4,11 @@ use core::convert::TryInto;
use nut_client::blocking::Connection; use nut_client::blocking::Connection;
/// Lists each UPS on the upsd server, one per line. /// Lists each UPS on the upsd server, one per line.
pub fn list_devices(server: UpsdName, verbose: bool) -> anyhow::Result<()> { pub fn list_devices(server: UpsdName, with_description: bool, debug: bool) -> anyhow::Result<()> {
let mut conn = connect(server)?; let mut conn = connect(server, debug)?;
for (name, description) in conn.list_ups()? { for (name, description) in conn.list_ups()? {
if verbose { if with_description {
println!("{}: {}", name, description); println!("{}: {}", name, description);
} else { } else {
println!("{}", name); println!("{}", name);
@ -18,11 +18,11 @@ pub fn list_devices(server: UpsdName, verbose: bool) -> anyhow::Result<()> {
Ok(()) Ok(())
} }
pub fn print_variable(server: UpsdName, variable: &str) -> anyhow::Result<()> { pub fn print_variable(server: UpsdName, variable: &str, debug: bool) -> anyhow::Result<()> {
let ups_name = server let ups_name = server
.upsname .upsname
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?; .with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?;
let mut conn = connect(server)?; let mut conn = connect(server, debug)?;
let variable = conn.get_var(ups_name, variable)?; let variable = conn.get_var(ups_name, variable)?;
println!("{}", variable.value()); println!("{}", variable.value());
@ -30,11 +30,11 @@ pub fn print_variable(server: UpsdName, variable: &str) -> anyhow::Result<()> {
Ok(()) Ok(())
} }
pub fn list_variables(server: UpsdName) -> anyhow::Result<()> { pub fn list_variables(server: UpsdName, debug: bool) -> anyhow::Result<()> {
let ups_name = server let ups_name = server
.upsname .upsname
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?; .with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?;
let mut conn = connect(server)?; let mut conn = connect(server, debug)?;
for var in conn.list_vars(ups_name)? { for var in conn.list_vars(ups_name)? {
println!("{}", var); println!("{}", var);
@ -43,8 +43,11 @@ pub fn list_variables(server: UpsdName) -> anyhow::Result<()> {
Ok(()) Ok(())
} }
fn connect(server: UpsdName) -> anyhow::Result<Connection> { fn connect(server: UpsdName, debug: bool) -> anyhow::Result<Connection> {
let host = server.try_into()?; let host = server.try_into()?;
let config = nut_client::ConfigBuilder::new().with_host(host).build(); let config = nut_client::ConfigBuilder::new()
.with_host(host)
.with_debug(debug)
.build();
Connection::new(config).with_context(|| format!("Failed to connect to upsd: {}", server)) Connection::new(config).with_context(|| format!("Failed to connect to upsd: {}", server))
} }

View file

@ -37,6 +37,12 @@ fn main() -> anyhow::Result<()> {
.takes_value(false) .takes_value(false)
.help("Lists each client connected on <upsname>, one per line."), .help("Lists each client connected on <upsname>, one per line."),
) )
.arg(
Arg::with_name("debug")
.short("D")
.takes_value(false)
.help("Enables debug mode (logs network commands to stderr)."),
)
.arg( .arg(
Arg::with_name("upsd-server") Arg::with_name("upsd-server")
.required(false) .required(false)
@ -56,12 +62,14 @@ fn main() -> anyhow::Result<()> {
|s| s.try_into().with_context(|| "Invalid upsd server name"), |s| s.try_into().with_context(|| "Invalid upsd server name"),
)?; )?;
let debug = args.is_present("debug");
if args.is_present("list") { if args.is_present("list") {
return cmd::list_devices(server, false); return cmd::list_devices(server, false, debug);
} }
if args.is_present("list-full") { if args.is_present("list-full") {
return cmd::list_devices(server, true); return cmd::list_devices(server, true, debug);
} }
if args.is_present("clients") { if args.is_present("clients") {
@ -70,8 +78,8 @@ fn main() -> anyhow::Result<()> {
// Fallback: prints one variable (or all of them) // Fallback: prints one variable (or all of them)
if let Some(variable) = args.value_of("variable") { if let Some(variable) = args.value_of("variable") {
cmd::print_variable(server, variable) cmd::print_variable(server, variable, debug)
} else { } else {
cmd::list_variables(server) cmd::list_variables(server, debug)
} }
} }

View file

@ -3,7 +3,7 @@ use std::convert::{TryFrom, TryInto};
use std::fmt; use std::fmt;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
pub const DEFAULT_HOSTNAME: &str = "localhost"; pub const DEFAULT_HOSTNAME: &str = "127.0.0.1";
pub const DEFAULT_PORT: u16 = 3493; pub const DEFAULT_PORT: u16 = 3493;
/// Connection information for a upsd server. /// Connection information for a upsd server.
@ -131,7 +131,7 @@ mod tests {
port: DEFAULT_PORT port: DEFAULT_PORT
} }
); );
assert_eq!(format!("{}", name), "ups0@localhost:3493"); assert_eq!(format!("{}", name), "ups0@127.0.0.1:3493");
} }
#[test] #[test]