use crate::ns;
use jid::Jid;
use minidom::Element;
use std::net::IpAddr;
use xso::error::{Error, FromElementError};
generate_attribute!(
Type, "type", {
Assisted => "assisted",
Direct => "direct",
Proxy => "proxy",
Tunnel => "tunnel",
}, Default = Direct
);
generate_attribute!(
Mode, "mode", {
Tcp => "tcp",
Udp => "udp",
}, Default = Tcp
);
generate_id!(
CandidateId
);
generate_id!(
StreamId
);
generate_element!(
Candidate, "candidate", JINGLE_S5B,
attributes: [
cid: Required<CandidateId> = "cid",
host: Required<IpAddr> = "host",
jid: Required<Jid> = "jid",
port: Option<u16> = "port",
priority: Required<u32> = "priority",
type_: Default<Type> = "type",
]
);
impl Candidate {
pub fn new(cid: CandidateId, host: IpAddr, jid: Jid, priority: u32) -> Candidate {
Candidate {
cid,
host,
jid,
priority,
port: Default::default(),
type_: Default::default(),
}
}
pub fn with_port(mut self, port: u16) -> Candidate {
self.port = Some(port);
self
}
pub fn with_type(mut self, type_: Type) -> Candidate {
self.type_ = type_;
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum TransportPayload {
Activated(CandidateId),
Candidates(Vec<Candidate>),
CandidateError,
CandidateUsed(CandidateId),
ProxyError,
None,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Transport {
pub sid: StreamId,
pub dstaddr: Option<String>,
pub mode: Mode,
pub payload: TransportPayload,
}
impl Transport {
pub fn new(sid: StreamId) -> Transport {
Transport {
sid,
dstaddr: None,
mode: Default::default(),
payload: TransportPayload::None,
}
}
pub fn with_dstaddr(mut self, dstaddr: String) -> Transport {
self.dstaddr = Some(dstaddr);
self
}
pub fn with_mode(mut self, mode: Mode) -> Transport {
self.mode = mode;
self
}
pub fn with_payload(mut self, payload: TransportPayload) -> Transport {
self.payload = payload;
self
}
}
impl TryFrom<Element> for Transport {
type Error = FromElementError;
fn try_from(elem: Element) -> Result<Transport, FromElementError> {
check_self!(elem, "transport", JINGLE_S5B);
check_no_unknown_attributes!(elem, "transport", ["sid", "dstaddr", "mode"]);
let sid = get_attr!(elem, "sid", Required);
let dstaddr = get_attr!(elem, "dstaddr", Option);
let mode = get_attr!(elem, "mode", Default);
let mut payload = None;
for child in elem.children() {
payload = Some(if child.is("candidate", ns::JINGLE_S5B) {
let mut candidates =
match payload {
Some(TransportPayload::Candidates(candidates)) => candidates,
Some(_) => return Err(Error::Other(
"Non-candidate child already present in JingleS5B transport element.",
)
.into()),
None => vec![],
};
candidates.push(Candidate::try_from(child.clone())?);
TransportPayload::Candidates(candidates)
} else if child.is("activated", ns::JINGLE_S5B) {
if payload.is_some() {
return Err(Error::Other(
"Non-activated child already present in JingleS5B transport element.",
)
.into());
}
let cid = get_attr!(child, "cid", Required);
TransportPayload::Activated(cid)
} else if child.is("candidate-error", ns::JINGLE_S5B) {
if payload.is_some() {
return Err(Error::Other(
"Non-candidate-error child already present in JingleS5B transport element.",
)
.into());
}
TransportPayload::CandidateError
} else if child.is("candidate-used", ns::JINGLE_S5B) {
if payload.is_some() {
return Err(Error::Other(
"Non-candidate-used child already present in JingleS5B transport element.",
)
.into());
}
let cid = get_attr!(child, "cid", Required);
TransportPayload::CandidateUsed(cid)
} else if child.is("proxy-error", ns::JINGLE_S5B) {
if payload.is_some() {
return Err(Error::Other(
"Non-proxy-error child already present in JingleS5B transport element.",
)
.into());
}
TransportPayload::ProxyError
} else {
return Err(Error::Other("Unknown child in JingleS5B transport element.").into());
});
}
let payload = payload.unwrap_or(TransportPayload::None);
Ok(Transport {
sid,
dstaddr,
mode,
payload,
})
}
}
impl From<Transport> for Element {
fn from(transport: Transport) -> Element {
Element::builder("transport", ns::JINGLE_S5B)
.attr("sid", transport.sid)
.attr("dstaddr", transport.dstaddr)
.attr("mode", transport.mode)
.append_all(match transport.payload {
TransportPayload::Candidates(candidates) => candidates
.into_iter()
.map(Element::from)
.collect::<Vec<_>>(),
TransportPayload::Activated(cid) => {
vec![Element::builder("activated", ns::JINGLE_S5B)
.attr("cid", cid)
.build()]
}
TransportPayload::CandidateError => {
vec![Element::builder("candidate-error", ns::JINGLE_S5B).build()]
}
TransportPayload::CandidateUsed(cid) => {
vec![Element::builder("candidate-used", ns::JINGLE_S5B)
.attr("cid", cid)
.build()]
}
TransportPayload::ProxyError => {
vec![Element::builder("proxy-error", ns::JINGLE_S5B).build()]
}
TransportPayload::None => vec![],
})
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Type, 1);
assert_size!(Mode, 1);
assert_size!(CandidateId, 12);
assert_size!(StreamId, 12);
assert_size!(Candidate, 56);
assert_size!(TransportPayload, 16);
assert_size!(Transport, 44);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Type, 1);
assert_size!(Mode, 1);
assert_size!(CandidateId, 24);
assert_size!(StreamId, 24);
assert_size!(Candidate, 88);
assert_size!(TransportPayload, 32);
assert_size!(Transport, 88);
}
#[test]
fn test_simple() {
let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:s5b:1' sid='coucou'/>"
.parse()
.unwrap();
let transport = Transport::try_from(elem).unwrap();
assert_eq!(transport.sid, StreamId(String::from("coucou")));
assert_eq!(transport.dstaddr, None);
assert_eq!(transport.mode, Mode::Tcp);
match transport.payload {
TransportPayload::None => (),
_ => panic!("Wrong element inside transport!"),
}
}
#[test]
fn test_serialise_activated() {
let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:s5b:1' sid='coucou'><activated cid='coucou'/></transport>".parse().unwrap();
let transport = Transport {
sid: StreamId(String::from("coucou")),
dstaddr: None,
mode: Mode::Tcp,
payload: TransportPayload::Activated(CandidateId(String::from("coucou"))),
};
let elem2: Element = transport.into();
assert_eq!(elem, elem2);
}
#[test]
fn test_serialise_candidate() {
let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:s5b:1' sid='coucou'><candidate cid='coucou' host='127.0.0.1' jid='coucou@coucou' priority='0'/></transport>".parse().unwrap();
let transport = Transport {
sid: StreamId(String::from("coucou")),
dstaddr: None,
mode: Mode::Tcp,
payload: TransportPayload::Candidates(vec![Candidate {
cid: CandidateId(String::from("coucou")),
host: IpAddr::from_str("127.0.0.1").unwrap(),
jid: Jid::new("coucou@coucou").unwrap(),
port: None,
priority: 0u32,
type_: Type::Direct,
}]),
};
let elem2: Element = transport.into();
assert_eq!(elem, elem2);
}
}