use std::net::SocketAddr;
use fibers::sync::mpsc;
use futures::Future;
use {Result, Method, Attribute, Error, ErrorKind};
use message::{Indication, Request, Response, RawMessage};
pub use self::base::BaseServer;
pub use self::udp::UdpServer;
pub use self::tcp::TcpServer;
pub mod futures {
pub use super::base::BaseServerLoop;
pub use super::udp::UdpServerLoop;
pub use super::tcp::TcpServerLoop;
}
mod base;
mod udp;
mod tcp;
pub trait HandleMessage {
type Method: Method;
type Attribute: Attribute;
type HandleCall: Future<Item = Response<Self::Method, Self::Attribute>, Error = ()>;
type HandleCast: Future<Item = (), Error = ()>;
type Info;
#[allow(unused_variables)]
fn on_init(&mut self, info_tx: mpsc::Sender<Self::Info>, indication_tx: IndicationSender) {}
fn handle_call(&mut self,
client: SocketAddr,
message: Request<Self::Method, Self::Attribute>)
-> Self::HandleCall;
fn handle_cast(&mut self,
client: SocketAddr,
message: Indication<Self::Method, Self::Attribute>)
-> Self::HandleCast;
fn handle_error(&mut self, client: SocketAddr, error: Error);
#[allow(unused_variables)]
fn handle_info(&mut self, info: Self::Info) {}
}
#[derive(Debug, Clone)]
pub struct IndicationSender {
inner_tx: mpsc::Sender<(SocketAddr, Result<RawMessage>)>,
}
impl IndicationSender {
fn new(inner_tx: mpsc::Sender<(SocketAddr, Result<RawMessage>)>) -> Self {
IndicationSender { inner_tx: inner_tx }
}
pub fn send<M, A>(&self, peer: SocketAddr, indication: Indication<M, A>) -> Result<()>
where M: Method,
A: Attribute
{
let message = track_try!(RawMessage::try_from_indication(indication));
track_try!(self.inner_tx.send((peer, Ok(message))).map_err(|_| ErrorKind::Other));
Ok(())
}
}