1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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(())
}
}