use super::error::{ProtocolError, ProtocolResult};
use super::links_operations::is_notification;
use super::protocols::{LinoConnection, LinoProtocol};
use super::server::error_message;
use links_notation::binary::{format_document, parse_document, LinoDocument};
use links_notation::LiNo;
use std::collections::VecDeque;
use std::io;
use std::net::{TcpStream, ToSocketAddrs};
use std::time::Duration;
#[derive(Debug)]
pub struct LinksClient {
connection: LinoConnection<TcpStream, Box<dyn LinoProtocol>>,
notifications: VecDeque<LinoDocument>,
}
impl LinksClient {
pub fn connect(
address: impl ToSocketAddrs,
protocol: impl LinoProtocol + 'static,
) -> ProtocolResult<Self> {
let stream = TcpStream::connect(address)?;
let _ = stream.set_nodelay(true);
Ok(Self {
connection: LinoConnection::new(stream, Box::new(protocol)),
notifications: VecDeque::new(),
})
}
pub fn request(&mut self, document: &LinoDocument) -> ProtocolResult<LinoDocument> {
self.connection.send(document)?;
loop {
let reply = self
.connection
.receive()?
.ok_or_else(|| ProtocolError::malformed("connection closed before the reply"))?;
if is_notification(&reply) {
self.notifications.push_back(reply);
continue;
}
if let Some(message) = error_message(&reply) {
return Err(ProtocolError::Remote(message.to_string()));
}
return Ok(reply);
}
}
pub fn pending_notifications(&self) -> usize {
self.notifications.len()
}
pub fn receive_notification(
&mut self,
timeout: Duration,
) -> ProtocolResult<Option<LinoDocument>> {
if let Some(queued) = self.notifications.pop_front() {
return Ok(Some(queued));
}
if !self.connection.wait_for_message(timeout)? {
return Ok(None);
}
let document = self.connection.receive()?.ok_or_else(|| {
ProtocolError::Io(io::Error::new(
io::ErrorKind::UnexpectedEof,
"connection closed",
))
})?;
if is_notification(&document) {
Ok(Some(document))
} else {
Err(ProtocolError::malformed(format!(
"the server sent a reply nobody asked for: {}",
format_document(&document)
)))
}
}
pub fn query(&mut self, query: &str) -> ProtocolResult<LinoDocument> {
self.request(&parse_document(query)?)
}
pub fn query_text(&mut self, query: &str) -> ProtocolResult<String> {
Ok(format_document(&self.query(query)?))
}
pub fn authenticate(&mut self, token: &str) -> ProtocolResult<u32> {
let reply = self.request(&vec![LiNo::Link {
id: Some("auth".to_string()),
values: vec![LiNo::Ref(token.to_string())],
}])?;
if let [LiNo::Link {
id: Some(id),
values,
}] = reply.as_slice()
{
if let [LiNo::Ref(subject)] = values.as_slice() {
if id == "subject" && subject.bytes().all(|byte| byte.is_ascii_digit()) {
if let Ok(subject) = subject.parse() {
return Ok(subject);
}
}
}
}
Err(ProtocolError::Malformed(format!(
"expected a subject reply, found {}",
format_document(&reply)
)))
}
}