use std::{
fmt::Display,
net::{IpAddr, SocketAddr},
sync::Arc,
};
use btlib::BlockPath;
use btserde::field_helpers::smart_ptr;
use serde::{Deserialize, Serialize};
use crate::Result;
pub trait CallMsg<'de>: Serialize + Deserialize<'de> + Send + Sync {
type Reply<'r>: Serialize + Deserialize<'r> + Send;
}
pub trait SendMsg<'de>: CallMsg<'de> {}
#[derive(PartialEq, Eq, Hash, Clone, Debug, Serialize, Deserialize)]
pub struct BlockAddr {
#[serde(rename = "ipaddr")]
ip_addr: IpAddr,
#[serde(with = "smart_ptr")]
path: Arc<BlockPath>,
}
impl BlockAddr {
pub fn new(ip_addr: IpAddr, path: Arc<BlockPath>) -> Self {
Self { ip_addr, path }
}
pub fn ip_addr(&self) -> IpAddr {
self.ip_addr
}
pub fn path(&self) -> &Arc<BlockPath> {
&self.path
}
fn port(&self) -> Result<u16> {
self.path.port()
}
pub fn socket_addr(&self) -> Result<SocketAddr> {
Ok(SocketAddr::new(self.ip_addr, self.port()?))
}
}
impl Display for BlockAddr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}@{}", self.path, self.ip_addr)
}
}