use addressing; use addressing::AddressTypeError; use sphinx::route::NodeAddressBytes; use std::error::Error; use std::net::SocketAddr; use tokio::prelude::*; #[derive(Debug)] pub struct MixPeer { connection: SocketAddr, } #[derive(Debug)] pub enum MixPeerError { InvalidAddressError, } impl From for MixPeerError { fn from(_: AddressTypeError) -> Self { use MixPeerError::*; InvalidAddressError } } impl MixPeer { // note that very soon `next_hop_address` will be changed to `next_hop_metadata` pub fn new(next_hop_address: NodeAddressBytes) -> Result { let next_hop_socket_address = addressing::socket_address_from_encoded_bytes(next_hop_address.to_bytes())?; Ok(MixPeer { connection: next_hop_socket_address, }) } pub async fn send(&self, bytes: Vec) -> Result<(), Box> { let next_hop_address = self.connection.clone(); let mut stream = tokio::net::TcpStream::connect(next_hop_address).await?; stream.write_all(&bytes).await?; Ok(()) } pub fn to_string(&self) -> String { self.connection.to_string() } }