2018-05-18 17:04:02 +00:00
|
|
|
// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// 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/.
|
|
|
|
|
2018-12-18 14:27:30 +00:00
|
|
|
use crate::stanza_error::DefinedCondition;
|
2018-05-18 17:04:02 +00:00
|
|
|
|
2018-05-28 14:42:35 +00:00
|
|
|
generate_element!(
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Acknowledgement of the currently received stanzas.
|
2018-05-28 14:42:35 +00:00
|
|
|
A, "a", SM,
|
|
|
|
attributes: [
|
2018-08-08 18:43:49 +00:00
|
|
|
/// The last handled stanza.
|
2019-02-24 19:26:40 +00:00
|
|
|
h: Required<u32> = "h",
|
2018-05-18 17:04:02 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
impl A {
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Generates a new `<a/>` element.
|
2018-05-18 17:04:02 +00:00
|
|
|
pub fn new(h: u32) -> A {
|
|
|
|
A { h }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:43:49 +00:00
|
|
|
generate_attribute!(
|
|
|
|
/// Whether to allow resumption of a previous stream.
|
2018-12-18 14:32:05 +00:00
|
|
|
ResumeAttr,
|
|
|
|
"resume",
|
|
|
|
bool
|
2018-08-08 18:43:49 +00:00
|
|
|
);
|
2018-05-18 17:04:02 +00:00
|
|
|
|
2018-05-28 14:42:35 +00:00
|
|
|
generate_element!(
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Client request for enabling stream management.
|
2019-02-21 20:00:58 +00:00
|
|
|
#[derive(Default)]
|
2018-05-28 14:42:35 +00:00
|
|
|
Enable, "enable", SM,
|
|
|
|
attributes: [
|
2018-08-08 18:43:49 +00:00
|
|
|
/// The preferred resumption time in seconds by the client.
|
2018-05-18 17:04:02 +00:00
|
|
|
// TODO: should be the infinite integer set ≥ 1.
|
2019-02-24 19:26:40 +00:00
|
|
|
max: Option<u32> = "max",
|
2018-08-08 18:43:49 +00:00
|
|
|
|
|
|
|
/// Whether the client wants to be allowed to resume the stream.
|
2019-02-24 19:26:40 +00:00
|
|
|
resume: Default<ResumeAttr> = "resume",
|
2018-05-18 17:04:02 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
impl Enable {
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Generates a new `<enable/>` element.
|
2018-05-18 17:04:02 +00:00
|
|
|
pub fn new() -> Self {
|
2019-02-21 20:00:58 +00:00
|
|
|
Enable::default()
|
2018-05-18 17:04:02 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Sets the preferred resumption time in seconds.
|
2018-05-18 17:04:02 +00:00
|
|
|
pub fn with_max(mut self, max: u32) -> Self {
|
|
|
|
self.max = Some(max);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Asks for resumption to be possible.
|
2018-05-18 17:04:02 +00:00
|
|
|
pub fn with_resume(mut self) -> Self {
|
|
|
|
self.resume = ResumeAttr::True;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:43:49 +00:00
|
|
|
generate_id!(
|
|
|
|
/// A random identifier used for stream resumption.
|
|
|
|
StreamId
|
|
|
|
);
|
|
|
|
|
2018-05-28 14:42:35 +00:00
|
|
|
generate_element!(
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Server response once stream management is enabled.
|
2018-05-28 14:42:35 +00:00
|
|
|
Enabled, "enabled", SM,
|
|
|
|
attributes: [
|
2018-08-08 18:43:49 +00:00
|
|
|
/// A random identifier used for stream resumption.
|
2019-02-24 19:26:40 +00:00
|
|
|
id: Option<StreamId> = "id",
|
2018-08-08 18:43:49 +00:00
|
|
|
|
|
|
|
/// The preferred IP, domain, IP:port or domain:port location for
|
|
|
|
/// resumption.
|
2019-02-24 19:26:40 +00:00
|
|
|
location: Option<String> = "location",
|
2018-08-08 18:43:49 +00:00
|
|
|
|
|
|
|
/// The preferred resumption time in seconds by the server.
|
2018-05-18 17:04:02 +00:00
|
|
|
// TODO: should be the infinite integer set ≥ 1.
|
2019-02-24 19:26:40 +00:00
|
|
|
max: Option<u32> = "max",
|
2018-08-08 18:43:49 +00:00
|
|
|
|
|
|
|
/// Whether stream resumption is allowed.
|
2019-02-24 19:26:40 +00:00
|
|
|
resume: Default<ResumeAttr> = "resume",
|
2018-05-18 17:04:02 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-05-28 14:45:13 +00:00
|
|
|
generate_element!(
|
2018-08-08 18:43:49 +00:00
|
|
|
/// A stream management error happened.
|
2018-05-18 17:04:02 +00:00
|
|
|
Failed, "failed", SM,
|
|
|
|
attributes: [
|
2018-08-08 18:43:49 +00:00
|
|
|
/// The last handled stanza.
|
2019-02-24 19:26:40 +00:00
|
|
|
h: Option<u32> = "h",
|
2018-05-18 17:04:02 +00:00
|
|
|
],
|
2018-05-28 14:29:51 +00:00
|
|
|
children: [
|
2018-08-08 18:43:49 +00:00
|
|
|
/// The error returned.
|
2018-05-18 17:04:02 +00:00
|
|
|
// XXX: implement the * handling.
|
|
|
|
error: Option<DefinedCondition> = ("*", XMPP_STANZAS) => DefinedCondition
|
2018-05-28 14:29:51 +00:00
|
|
|
]
|
2018-05-18 17:04:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
generate_empty_element!(
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Requests the currently received stanzas by the other party.
|
2018-12-18 14:32:05 +00:00
|
|
|
R,
|
|
|
|
"r",
|
|
|
|
SM
|
2018-05-18 17:04:02 +00:00
|
|
|
);
|
|
|
|
|
2018-05-28 14:42:35 +00:00
|
|
|
generate_element!(
|
2018-08-08 18:43:49 +00:00
|
|
|
/// Requests a stream resumption.
|
2018-05-28 14:42:35 +00:00
|
|
|
Resume, "resume", SM,
|
|
|
|
attributes: [
|
2018-08-08 18:43:49 +00:00
|
|
|
/// The last handled stanza.
|
2019-02-24 19:26:40 +00:00
|
|
|
h: Required<u32> = "h",
|
2018-08-08 18:43:49 +00:00
|
|
|
|
|
|
|
/// The previous id given by the server on
|
|
|
|
/// [enabled](struct.Enabled.html).
|
2019-02-24 19:26:40 +00:00
|
|
|
previd: Required<StreamId> = "previd",
|
2018-05-18 17:04:02 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-05-28 14:42:35 +00:00
|
|
|
generate_element!(
|
2018-08-08 18:43:49 +00:00
|
|
|
/// The response by the server for a successfully resumed stream.
|
2018-05-28 14:42:35 +00:00
|
|
|
Resumed, "resumed", SM,
|
|
|
|
attributes: [
|
2018-08-08 18:43:49 +00:00
|
|
|
/// The last handled stanza.
|
2019-02-24 19:26:40 +00:00
|
|
|
h: Required<u32> = "h",
|
2018-08-08 18:43:49 +00:00
|
|
|
|
|
|
|
/// The previous id given by the server on
|
|
|
|
/// [enabled](struct.Enabled.html).
|
2019-02-24 19:26:40 +00:00
|
|
|
previd: Required<StreamId> = "previd",
|
2018-05-18 17:04:02 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: add support for optional and required.
|
|
|
|
generate_empty_element!(
|
|
|
|
/// Represents availability of Stream Management in `<stream:features/>`.
|
2018-12-18 14:32:05 +00:00
|
|
|
StreamManagement,
|
|
|
|
"sm",
|
|
|
|
SM
|
2018-05-18 17:04:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use minidom::Element;
|
2018-12-18 14:32:05 +00:00
|
|
|
use try_from::TryFrom;
|
2018-05-18 17:04:02 +00:00
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(A, 4);
|
|
|
|
assert_size!(ResumeAttr, 1);
|
|
|
|
assert_size!(Enable, 12);
|
|
|
|
assert_size!(StreamId, 12);
|
|
|
|
assert_size!(Enabled, 36);
|
|
|
|
assert_size!(Failed, 12);
|
|
|
|
assert_size!(R, 0);
|
|
|
|
assert_size!(Resume, 16);
|
|
|
|
assert_size!(Resumed, 16);
|
|
|
|
assert_size!(StreamManagement, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(A, 4);
|
|
|
|
assert_size!(ResumeAttr, 1);
|
|
|
|
assert_size!(Enable, 12);
|
|
|
|
assert_size!(StreamId, 24);
|
|
|
|
assert_size!(Enabled, 64);
|
|
|
|
assert_size!(Failed, 12);
|
|
|
|
assert_size!(R, 0);
|
|
|
|
assert_size!(Resume, 32);
|
|
|
|
assert_size!(Resumed, 32);
|
|
|
|
assert_size!(StreamManagement, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:04:02 +00:00
|
|
|
#[test]
|
|
|
|
fn a() {
|
|
|
|
let elem: Element = "<a xmlns='urn:xmpp:sm:3' h='5'".parse().unwrap();
|
|
|
|
let a = A::try_from(elem).unwrap();
|
|
|
|
assert_eq!(a.h, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn stream_feature() {
|
|
|
|
let elem: Element = "<sm xmlns='urn:xmpp:sm:3'/>".parse().unwrap();
|
|
|
|
StreamManagement::try_from(elem).unwrap();
|
|
|
|
}
|
2018-08-08 18:43:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resume() {
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<enable xmlns='urn:xmpp:sm:3' resume='true'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2018-08-08 18:43:49 +00:00
|
|
|
let enable = Enable::try_from(elem).unwrap();
|
|
|
|
assert_eq!(enable.max, None);
|
|
|
|
assert_eq!(enable.resume, ResumeAttr::True);
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<enabled xmlns='urn:xmpp:sm:3' resume='true' id='coucou' max='600'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2018-08-08 18:43:49 +00:00
|
|
|
let enabled = Enabled::try_from(elem).unwrap();
|
|
|
|
let previd = enabled.id.unwrap();
|
|
|
|
assert_eq!(enabled.resume, ResumeAttr::True);
|
|
|
|
assert_eq!(previd, StreamId(String::from("coucou")));
|
|
|
|
assert_eq!(enabled.max, Some(600));
|
|
|
|
assert_eq!(enabled.location, None);
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<resume xmlns='urn:xmpp:sm:3' h='5' previd='coucou'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2018-08-08 18:43:49 +00:00
|
|
|
let resume = Resume::try_from(elem).unwrap();
|
|
|
|
assert_eq!(resume.h, 5);
|
|
|
|
assert_eq!(resume.previd, previd);
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<resumed xmlns='urn:xmpp:sm:3' h='5' previd='coucou'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2018-08-08 18:43:49 +00:00
|
|
|
let resumed = Resumed::try_from(elem).unwrap();
|
|
|
|
assert_eq!(resumed.h, 5);
|
|
|
|
assert_eq!(resumed.previd, previd);
|
|
|
|
}
|
2018-05-18 17:04:02 +00:00
|
|
|
}
|