mirror of
https://github.com/aramperes/nut-rs.git
synced 2025-09-09 13:38:30 -04:00
Start re-implementing client API
This commit is contained in:
parent
92268b742f
commit
162c015680
3 changed files with 85 additions and 11 deletions
|
@ -8,7 +8,7 @@ pub struct Client {
|
|||
/// The client configuration.
|
||||
config: Config,
|
||||
/// The client connection.
|
||||
stream: ConnectionStream,
|
||||
pub(crate) stream: ConnectionStream,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
|
@ -30,22 +30,31 @@ impl Client {
|
|||
|
||||
client = client.enable_ssl()?;
|
||||
|
||||
// TODO: Enable SSL
|
||||
// TODO: Login
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
/// Authenticates to the given UPS device with the username and password set in the config.
|
||||
pub fn login(&mut self, ups_name: String) -> crate::Result<()> {
|
||||
if let Some(auth) = self.config.auth.clone() {
|
||||
// Pass username and check for 'OK'
|
||||
self.set_username(auth.username)?;
|
||||
|
||||
// Pass password and check for 'OK'
|
||||
if let Some(password) = auth.password {
|
||||
self.set_password(password)?;
|
||||
}
|
||||
|
||||
// Submit login
|
||||
self.exec_login(ups_name)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssl")]
|
||||
fn enable_ssl(mut self) -> crate::Result<Self> {
|
||||
if self.config.ssl {
|
||||
// Send STARTTLS
|
||||
self.stream
|
||||
.write_sentence(&ServerSentences::ExecStartTLS {})?;
|
||||
|
||||
// Expect the OK
|
||||
self.stream
|
||||
.read_sentence::<ClientSentences>()?
|
||||
.exactly(ClientSentences::StartTLSOk {})?;
|
||||
self.exec_start_tls()?;
|
||||
|
||||
// Initialize SSL configurations
|
||||
let mut ssl_config = rustls::ClientConfig::new();
|
||||
|
|
|
@ -8,6 +8,8 @@ use crate::{Config, Error, Host, NutError};
|
|||
mod client;
|
||||
mod stream;
|
||||
|
||||
pub use client::Client;
|
||||
|
||||
/// A blocking NUT client connection.
|
||||
pub enum Connection {
|
||||
/// A TCP connection.
|
||||
|
|
|
@ -693,6 +693,37 @@ macro_rules! implement_action_commands {
|
|||
};
|
||||
}
|
||||
|
||||
/// A macro for implementing client action commands that return `OK`.
|
||||
///
|
||||
/// Each function should return the sentence to pass.
|
||||
macro_rules! implement_client_action_commands {
|
||||
(
|
||||
$(
|
||||
$(#[$attr:meta])+
|
||||
$vis:vis fn $name:ident($($argname:ident: $argty:ty),*) {
|
||||
$cmd:block,
|
||||
$ret:block
|
||||
}
|
||||
)*
|
||||
) => {
|
||||
impl crate::blocking::Client {
|
||||
$(
|
||||
$(#[$attr])*
|
||||
#[allow(dead_code)]
|
||||
$vis fn $name(&mut self$(, $argname: $argty)*) -> crate::Result<()> {
|
||||
use crate::proto::{Sentence, ClientSentences, ClientSentences::*, ServerSentences::*};
|
||||
self.stream
|
||||
.write_sentence(&$cmd)?;
|
||||
self.stream
|
||||
.read_sentence::<ClientSentences>()?
|
||||
.exactly($ret)
|
||||
.map(|_| ())
|
||||
}
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
implement_list_commands! {
|
||||
/// Queries a list of UPS devices.
|
||||
pub fn list_ups() -> Vec<(String, String)> {
|
||||
|
@ -835,3 +866,35 @@ implement_action_commands! {
|
|||
Command::Logout
|
||||
}
|
||||
}
|
||||
|
||||
implement_client_action_commands! {
|
||||
/// Sends the login username.
|
||||
pub(crate) fn set_username(username: String) {
|
||||
{ SetUsername { username } },
|
||||
{ GenericOk {} }
|
||||
}
|
||||
|
||||
/// Sends the login password.
|
||||
pub(crate) fn set_password(password: String) {
|
||||
{ SetPassword { password } },
|
||||
{ GenericOk {} }
|
||||
}
|
||||
|
||||
/// Executes the login.
|
||||
pub(crate) fn exec_login(ups_name: String) {
|
||||
{ ExecLogin { ups_name } },
|
||||
{ GenericOk {} }
|
||||
}
|
||||
|
||||
/// Executes the logout.
|
||||
pub(crate) fn exec_logout() {
|
||||
{ ExecLogout {} },
|
||||
{ LogoutOk {} }
|
||||
}
|
||||
|
||||
/// Asks the server to upgrade to TLS.
|
||||
pub(crate) fn exec_start_tls() {
|
||||
{ ExecStartTLS {} },
|
||||
{ StartTLSOk {} }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue