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
//! STUN server related components.
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 {
    //! `Future` trait implementations.
    pub use super::base::BaseServerLoop;
    pub use super::udp::UdpServerLoop;
    pub use super::tcp::TcpServerLoop;
}

mod base;
mod udp;
mod tcp;

/// This trait allows to handle transactions issued by clients.
pub trait HandleMessage {
    /// STUN method type that this implementation can handle.
    type Method: Method;

    /// STUN attribute type that this implementation can handle.
    type Attribute: Attribute;

    /// `Future` type for handling request/response transactions.
    type HandleCall: Future<Item = Response<Self::Method, Self::Attribute>, Error = ()>;

    /// `Future` type for handling indication transactions.
    type HandleCast: Future<Item = (), Error = ()>;

    /// Handler specific information message type.
    type Info;

    /// Callback method which invoked after the initialization of a server.
    #[allow(unused_variables)]
    fn on_init(&mut self, info_tx: mpsc::Sender<Self::Info>, indication_tx: IndicationSender) {}

    /// Handles the request/response transaction issued by `client`.
    fn handle_call(&mut self,
                   client: SocketAddr,
                   message: Request<Self::Method, Self::Attribute>)
                   -> Self::HandleCall;

    /// Handles the indication transaction issued by `client`.
    fn handle_cast(&mut self,
                   client: SocketAddr,
                   message: Indication<Self::Method, Self::Attribute>)
                   -> Self::HandleCast;

    /// Handles the error occurred while processing a transaction issued by `client`.
    fn handle_error(&mut self, client: SocketAddr, error: Error);

    /// Handles the information message.
    #[allow(unused_variables)]
    fn handle_info(&mut self, info: Self::Info) {}
}

/// Indication message sender.
#[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 }
    }

    /// Sends the indication message to `peer`.
    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(())
    }
}