wip connection trait
This commit is contained in:
		
							parent
							
								
									5e141f1fbc
								
							
						
					
					
						commit
						b9fc06117e
					
				
					 2 changed files with 41 additions and 35 deletions
				
			
		|  | @ -9,17 +9,38 @@ use crate::Packet; | |||
| ///
 | ||||
| /// # Examples
 | ||||
| /// ```rust
 | ||||
| /// # use servicepoint::Command;
 | ||||
| /// let connection = servicepoint::Connection::open("172.23.42.29:2342")
 | ||||
| /// # use servicepoint::{Command, Connection};
 | ||||
| /// let connection = servicepoint::UdpConnection::open("172.23.42.29:2342")
 | ||||
| ///     .expect("connection failed");
 | ||||
| ///  connection.send(Command::Clear)
 | ||||
| ///     .expect("send failed");
 | ||||
| ///  connection.send(Command::Clear);
 | ||||
| /// ```
 | ||||
| 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, | ||||
| } | ||||
| 
 | ||||
| impl Connection { | ||||
| impl UdpConnection { | ||||
|     /// 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.
 | ||||
|  | @ -39,37 +60,22 @@ impl Connection { | |||
|         socket.connect(addr)?; | ||||
|         Ok(Self { socket }) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|     /// Send something packet-like to the display. Usually this is in the form of a Command.
 | ||||
|     ///
 | ||||
|     /// # 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> { | ||||
| impl Connection for UdpConnection { | ||||
|     fn send(&self, packet: impl Into<Packet>) -> bool { | ||||
|         let packet = packet.into(); | ||||
|         debug!("sending {packet:?}"); | ||||
|         let data: Vec<u8> = packet.into(); | ||||
|         self.socket.send(&data)?; | ||||
|         Ok(()) | ||||
|         self.socket.send(&data).is_err() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// 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::command::{Command, Cp437Grid, Offset}; | ||||
| 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::grid::Grid; | ||||
| pub use crate::origin::{Origin, Pixels, Tiles}; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter