// Copyright (c) 2020 Emmanuel Gil Peyrot // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. use crate::jingle_ice_udp::Type; use std::net::IpAddr; generate_element!( /// Wrapper element for an raw UDP transport. #[derive(Default)] Transport, "transport", JINGLE_RAW_UDP, children: [ /// List of candidates for this raw UDP session. candidates: Vec = ("candidate", JINGLE_RAW_UDP) => Candidate ] ); impl Transport { /// Create a new ICE-UDP transport. pub fn new() -> Transport { Transport::default() } /// Add a candidate to this transport. pub fn add_candidate(mut self, candidate: Candidate) -> Self { self.candidates.push(candidate); self } } generate_element!( /// A candidate for an ICE-UDP session. Candidate, "candidate", JINGLE_RAW_UDP, attributes: [ /// A Component ID as defined in ICE-CORE. component: Required = "component", /// An index, starting at 0, that enables the parties to keep track of updates to the /// candidate throughout the life of the session. generation: Required = "generation", /// A unique identifier for the candidate. id: Required = "id", /// The Internet Protocol (IP) address for the candidate transport mechanism; this can be /// either an IPv4 address or an IPv6 address. ip: Required = "ip", /// The port at the candidate IP address. port: Required = "port", /// A Candidate Type as defined in ICE-CORE. type_: Option = "type", ] ); #[cfg(test)] mod tests { use super::*; use crate::Element; #[cfg(target_pointer_width = "32")] #[test] fn test_size() { assert_size!(Transport, 12); assert_size!(Candidate, 36); } #[cfg(target_pointer_width = "64")] #[test] fn test_size() { assert_size!(Transport, 24); assert_size!(Candidate, 48); } #[test] fn example_1() { let elem: Element = " " .parse() .unwrap(); let mut transport = Transport::try_from(elem).unwrap(); assert_eq!(transport.candidates.len(), 1); let candidate = transport.candidates.pop().unwrap(); assert_eq!(candidate.component, 1); assert_eq!(candidate.generation, 0); assert_eq!(candidate.id, "a9j3mnbtu1"); assert_eq!(candidate.ip, "10.1.1.104".parse::().unwrap()); assert_eq!(candidate.port, 13540u16); assert!(candidate.type_.is_none()); } }