use crate::{addr::Endpoint, auth::*, core::*, error::*, *};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Server {
inner: Arc<RawSocket>,
}
impl Server {
pub fn new() -> Result<Self, Error> {
let inner = Arc::new(RawSocket::new(RawSocketType::Server)?);
Ok(Self { inner })
}
pub fn with_ctx(handle: CtxHandle) -> Result<Server, Error> {
let inner =
Arc::new(RawSocket::with_ctx(RawSocketType::Server, handle)?);
Ok(Self { inner })
}
pub fn ctx(&self) -> CtxHandle {
self.inner.ctx()
}
pub fn route<M>(&self, msg: M, id: RoutingId) -> Result<(), Error<Msg>>
where
M: Into<Msg>,
{
let mut msg = msg.into();
msg.set_routing_id(id);
self.send(msg)
}
pub fn try_route<M>(&self, msg: M, id: RoutingId) -> Result<(), Error<Msg>>
where
M: Into<Msg>,
{
let mut msg = msg.into();
msg.set_routing_id(id);
self.try_send(msg)
}
}
impl GetRawSocket for Server {
fn raw_socket(&self) -> &RawSocket {
&self.inner
}
}
impl Heartbeating for Server {}
impl Socket for Server {}
impl SendMsg for Server {}
impl RecvMsg for Server {}
unsafe impl Send for Server {}
unsafe impl Sync for Server {}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(from = "FlatServerConfig")]
#[serde(into = "FlatServerConfig")]
pub struct ServerConfig {
socket_config: SocketConfig,
send_config: SendConfig,
recv_config: RecvConfig,
heartbeat_config: HeartbeatingConfig,
}
impl ServerConfig {
pub fn new() -> Self {
Self::default()
}
pub fn build(&self) -> Result<Server, Error> {
self.with_ctx(Ctx::global())
}
pub fn with_ctx(&self, handle: CtxHandle) -> Result<Server, Error> {
let server = Server::with_ctx(handle)?;
self.apply(&server)?;
Ok(server)
}
pub fn apply(&self, server: &Server) -> Result<(), Error> {
self.send_config.apply(server)?;
self.recv_config.apply(server)?;
self.heartbeat_config.apply(server)?;
self.socket_config.apply(server)?;
Ok(())
}
}
#[derive(Clone, Serialize, Deserialize)]
struct FlatServerConfig {
connect: Option<Vec<Endpoint>>,
bind: Option<Vec<Endpoint>>,
heartbeat: Option<Heartbeat>,
send_hwm: HighWaterMark,
send_timeout: Period,
recv_hwm: HighWaterMark,
recv_timeout: Period,
mechanism: Option<Mechanism>,
}
impl From<ServerConfig> for FlatServerConfig {
fn from(config: ServerConfig) -> Self {
let socket_config = config.socket_config;
let send_config = config.send_config;
let recv_config = config.recv_config;
let heartbeat_config = config.heartbeat_config;
Self {
connect: socket_config.connect,
bind: socket_config.bind,
heartbeat: heartbeat_config.heartbeat,
mechanism: socket_config.mechanism,
send_hwm: send_config.send_hwm,
send_timeout: send_config.send_timeout,
recv_hwm: recv_config.recv_hwm,
recv_timeout: recv_config.recv_timeout,
}
}
}
impl From<FlatServerConfig> for ServerConfig {
fn from(flat: FlatServerConfig) -> Self {
let socket_config = SocketConfig {
connect: flat.connect,
bind: flat.bind,
mechanism: flat.mechanism,
};
let send_config = SendConfig {
send_hwm: flat.send_hwm,
send_timeout: flat.send_timeout,
};
let recv_config = RecvConfig {
recv_hwm: flat.recv_hwm,
recv_timeout: flat.recv_timeout,
};
let heartbeat_config = HeartbeatingConfig {
heartbeat: flat.heartbeat,
};
Self {
socket_config,
send_config,
recv_config,
heartbeat_config,
}
}
}
impl GetSocketConfig for ServerConfig {
fn socket_config(&self) -> &SocketConfig {
&self.socket_config
}
fn socket_config_mut(&mut self) -> &mut SocketConfig {
&mut self.socket_config
}
}
impl ConfigureSocket for ServerConfig {}
impl GetRecvConfig for ServerConfig {
fn recv_config(&self) -> &RecvConfig {
&self.recv_config
}
fn recv_config_mut(&mut self) -> &mut RecvConfig {
&mut self.recv_config
}
}
impl ConfigureRecv for ServerConfig {}
impl GetSendConfig for ServerConfig {
fn send_config(&self) -> &SendConfig {
&self.send_config
}
fn send_config_mut(&mut self) -> &mut SendConfig {
&mut self.send_config
}
}
impl ConfigureSend for ServerConfig {}
impl GetHeartbeatingConfig for ServerConfig {
fn heartbeat_config(&self) -> &HeartbeatingConfig {
&self.heartbeat_config
}
fn heartbeat_config_mut(&mut self) -> &mut HeartbeatingConfig {
&mut self.heartbeat_config
}
}
impl ConfigureHeartbeating for ServerConfig {}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct ServerBuilder {
inner: ServerConfig,
}
impl ServerBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn build(&self) -> Result<Server, Error> {
self.inner.build()
}
pub fn with_ctx(&self, handle: CtxHandle) -> Result<Server, Error> {
self.inner.with_ctx(handle)
}
}
impl GetSocketConfig for ServerBuilder {
fn socket_config(&self) -> &SocketConfig {
self.inner.socket_config()
}
fn socket_config_mut(&mut self) -> &mut SocketConfig {
self.inner.socket_config_mut()
}
}
impl BuildSocket for ServerBuilder {}
impl GetSendConfig for ServerBuilder {
fn send_config(&self) -> &SendConfig {
self.inner.send_config()
}
fn send_config_mut(&mut self) -> &mut SendConfig {
self.inner.send_config_mut()
}
}
impl BuildSend for ServerBuilder {}
impl GetRecvConfig for ServerBuilder {
fn recv_config(&self) -> &RecvConfig {
self.inner.recv_config()
}
fn recv_config_mut(&mut self) -> &mut RecvConfig {
self.inner.recv_config_mut()
}
}
impl BuildRecv for ServerBuilder {}
impl GetHeartbeatingConfig for ServerBuilder {
fn heartbeat_config(&self) -> &HeartbeatingConfig {
self.inner.heartbeat_config()
}
fn heartbeat_config_mut(&mut self) -> &mut HeartbeatingConfig {
self.inner.heartbeat_config_mut()
}
}
impl BuildHeartbeating for ServerBuilder {}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ser_de() {
let config = ServerConfig::new();
let ron = serde_yaml::to_string(&config).unwrap();
let de: ServerConfig = serde_yaml::from_str(&ron).unwrap();
assert_eq!(config, de);
}
}