mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 18:10:14 +01:00
wip connection trait
This commit is contained in:
parent
5e141f1fbc
commit
b9fc06117e
|
@ -9,17 +9,38 @@ use crate::Packet;
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use servicepoint::Command;
|
/// # use servicepoint::{Command, Connection};
|
||||||
/// let connection = servicepoint::Connection::open("172.23.42.29:2342")
|
/// let connection = servicepoint::UdpConnection::open("172.23.42.29:2342")
|
||||||
/// .expect("connection failed");
|
/// .expect("connection failed");
|
||||||
/// connection.send(Command::Clear)
|
/// connection.send(Command::Clear);
|
||||||
/// .expect("send failed");
|
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Connection {
|
pub trait Connection {
|
||||||
|
/// Send something packet-like to the display. Usually this is in the form of a Command.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// - `packet`: the packet-like to send
|
||||||
|
///
|
||||||
|
/// returns: true if packet was sent, otherwise false
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use servicepoint::{Command, Connection };
|
||||||
|
/// # let connection = servicepoint::UdpConnection::open("172.23.42.29:2342")
|
||||||
|
/// # .expect("connection failed");
|
||||||
|
/// // turn off all pixels on display
|
||||||
|
/// connection.send(Command::Clear);
|
||||||
|
/// ```
|
||||||
|
fn send(&self, packet: impl Into<Packet>) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A real connection using the UDP protocol
|
||||||
|
pub struct UdpConnection {
|
||||||
socket: UdpSocket,
|
socket: UdpSocket,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Connection {
|
impl UdpConnection {
|
||||||
/// Open a new UDP socket and connect to the provided host.
|
/// Open a new UDP socket and connect to the provided host.
|
||||||
///
|
///
|
||||||
/// Note that this is UDP, which means that the open call can succeed even if the display is unreachable.
|
/// Note that this is UDP, which means that the open call can succeed even if the display is unreachable.
|
||||||
|
@ -39,37 +60,22 @@ impl Connection {
|
||||||
socket.connect(addr)?;
|
socket.connect(addr)?;
|
||||||
Ok(Self { socket })
|
Ok(Self { socket })
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Send something packet-like to the display. Usually this is in the form of a Command.
|
impl Connection for UdpConnection {
|
||||||
///
|
fn send(&self, packet: impl Into<Packet>) -> bool {
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// - `packet`: the packet-like to send
|
|
||||||
///
|
|
||||||
/// returns: Ok if packet was sent, otherwise socket error
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Any errors produced while sending using the underlying socket.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// # use servicepoint::{Command, CompressionCode, Grid, PixelGrid};
|
|
||||||
/// # let connection = servicepoint::Connection::open("172.23.42.29:2342")
|
|
||||||
/// # .expect("connection failed");
|
|
||||||
/// // turn off all pixels on display
|
|
||||||
/// connection.send(Command::Clear)
|
|
||||||
/// .expect("send failed");
|
|
||||||
/// ```
|
|
||||||
pub fn send(
|
|
||||||
&self,
|
|
||||||
packet: impl Into<Packet>,
|
|
||||||
) -> Result<(), std::io::Error> {
|
|
||||||
let packet = packet.into();
|
let packet = packet.into();
|
||||||
debug!("sending {packet:?}");
|
debug!("sending {packet:?}");
|
||||||
let data: Vec<u8> = packet.into();
|
let data: Vec<u8> = packet.into();
|
||||||
self.socket.send(&data)?;
|
self.socket.send(&data).is_err()
|
||||||
Ok(())
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A fake connection for testing that does not actually send anything
|
||||||
|
pub struct NoopConnection;
|
||||||
|
|
||||||
|
impl Connection for NoopConnection {
|
||||||
|
fn send(&self, packet: impl Into<Packet>) -> bool {
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ use bitvec::prelude::{BitVec, Msb0};
|
||||||
pub use crate::brightness::{Brightness, BrightnessGrid};
|
pub use crate::brightness::{Brightness, BrightnessGrid};
|
||||||
pub use crate::command::{Command, Cp437Grid, Offset};
|
pub use crate::command::{Command, Cp437Grid, Offset};
|
||||||
pub use crate::compression_code::CompressionCode;
|
pub use crate::compression_code::CompressionCode;
|
||||||
pub use crate::connection::Connection;
|
pub use crate::connection::{Connection, UdpConnection};
|
||||||
pub use crate::data_ref::DataRef;
|
pub use crate::data_ref::DataRef;
|
||||||
pub use crate::grid::Grid;
|
pub use crate::grid::Grid;
|
||||||
pub use crate::origin::{Origin, Pixels, Tiles};
|
pub use crate::origin::{Origin, Pixels, Tiles};
|
||||||
|
|
Loading…
Reference in a new issue