mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
Add support for XEP-0198: Stream Management
- De-duplicate code by moving it into AbstractXMPPConnection - Introduce TopLevelStreamElement as superclass for all XMPP stream elements. - Add SynchronizationPoint, ParserUtils - Add ParserUtils Fixes SMACK-333 and SMACK-521
This commit is contained in:
parent
07c10a7444
commit
fc51f3df48
69 changed files with 3277 additions and 1083 deletions
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,50 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class SMUtils {
|
||||
|
||||
private static long MASK_32_BIT = BigInteger.ONE.shiftLeft(32).subtract(BigInteger.ONE).longValue();
|
||||
|
||||
/**
|
||||
* Quoting XEP-198 4.:
|
||||
* "In the unlikely case that the number of stanzas handled during a stream management session exceeds the number
|
||||
* of digits that can be represented by the unsignedInt datatype as specified in XML Schema Part 2 [10]
|
||||
* (i.e., 2^32), the value of 'h' SHALL be reset from 2^32-1 back to zero (rather than being incremented to 2^32)."
|
||||
*
|
||||
* @param height
|
||||
* @return the incremented height
|
||||
*/
|
||||
public static long incrementHeight(long height) {
|
||||
return ++height & MASK_32_BIT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the delta of the last known stanza handled count and the new
|
||||
* reported stanza handled count while considering that the new value may be
|
||||
* wrapped after 2^32-1.
|
||||
*
|
||||
* @param reportedHandledCount
|
||||
* @param lastKnownHandledCount
|
||||
* @return the delta
|
||||
*/
|
||||
public static long calculateDelta(long reportedHandledCount, long lastKnownHandledCount) {
|
||||
return (reportedHandledCount - lastKnownHandledCount) & MASK_32_BIT;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm;
|
||||
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
|
||||
public abstract class StreamManagementException extends SmackException {
|
||||
|
||||
public StreamManagementException() {
|
||||
}
|
||||
|
||||
public StreamManagementException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 3767590115788821101L;
|
||||
|
||||
public static class StreamManagementNotEnabledException extends StreamManagementException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 2624821584352571307L;
|
||||
|
||||
}
|
||||
|
||||
public static class StreamIdDoesNotMatchException extends StreamManagementException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1191073341336559621L;
|
||||
|
||||
public StreamIdDoesNotMatchException(String expected, String got) {
|
||||
super("Stream IDs do not match. Expected '" + expected + "', but got '" + got + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,344 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.packet;
|
||||
|
||||
import org.jivesoftware.smack.packet.FullStreamElement;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
public class StreamManagement {
|
||||
public static final String NAMESPACE = "urn:xmpp:sm:3";
|
||||
|
||||
public static class StreamManagementFeature implements PacketExtension {
|
||||
|
||||
public static final String ELEMENT = "sm";
|
||||
public static final StreamManagementFeature INSTANCE = new StreamManagementFeature();
|
||||
|
||||
private StreamManagementFeature() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
|
||||
private static abstract class AbstractEnable extends FullStreamElement {
|
||||
|
||||
/**
|
||||
* Preferred maximum resumption time in seconds (optional).
|
||||
*/
|
||||
protected int max = -1;
|
||||
|
||||
protected boolean resume = false;
|
||||
|
||||
protected void maybeAddResumeAttributeTo(XmlStringBuilder xml) {
|
||||
if (resume) {
|
||||
// XEP 198 never mentions the case where resume='false', it's either set to true or
|
||||
// not set at all. We reflect this in this code part
|
||||
xml.attribute("resume", "true");
|
||||
}
|
||||
}
|
||||
|
||||
protected void maybeAddMaxAttributeTo(XmlStringBuilder xml) {
|
||||
if (max > 0) {
|
||||
xml.attribute("max", Integer.toString(max));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isResumeSet() {
|
||||
return resume;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the max resumption time in seconds.
|
||||
* @return the max resumption time in seconds
|
||||
*/
|
||||
public int getMaxResumptionTime() {
|
||||
return max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Enable extends AbstractEnable {
|
||||
public static final String ELEMENT = "enable";
|
||||
|
||||
public static final Enable INSTANCE = new Enable();
|
||||
|
||||
private Enable() {
|
||||
}
|
||||
|
||||
public Enable(boolean resume) {
|
||||
this.resume = resume;
|
||||
}
|
||||
|
||||
public Enable(boolean resume, int max) {
|
||||
this(resume);
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
maybeAddResumeAttributeTo(xml);
|
||||
maybeAddMaxAttributeTo(xml);
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A Stream Management 'enabled' element.
|
||||
* <p>
|
||||
* Here is a full example, all attributes besides 'xmlns' are optional.
|
||||
* </p>
|
||||
* <pre>
|
||||
* {@code
|
||||
* <enabled xmlns='urn:xmpp:sm:3'
|
||||
* id='some-long-sm-id'
|
||||
* location='[2001:41D0:1:A49b::1]:9222'
|
||||
* resume='true'/>
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public static class Enabled extends AbstractEnable {
|
||||
public static final String ELEMENT = "enabled";
|
||||
|
||||
/**
|
||||
* The stream id ("SM-ID")
|
||||
*/
|
||||
private final String id;
|
||||
|
||||
/**
|
||||
* The location where the server prefers reconnection.
|
||||
*/
|
||||
private final String location;
|
||||
|
||||
public Enabled(String id, boolean resume) {
|
||||
this(id, resume, null, -1);
|
||||
}
|
||||
|
||||
public Enabled(String id, boolean resume, String location, int max) {
|
||||
this.id = id;
|
||||
this.resume = resume;
|
||||
this.location = location;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.optAttribute("id", id);
|
||||
maybeAddResumeAttributeTo(xml);
|
||||
xml.optAttribute("location", location);
|
||||
maybeAddMaxAttributeTo(xml);
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Failed extends FullStreamElement {
|
||||
public static final String ELEMENT = "failed";
|
||||
|
||||
private XMPPError error;
|
||||
|
||||
public Failed() {
|
||||
}
|
||||
|
||||
public Failed(XMPPError error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public XMPPError getXMPPError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
if (error != null) {
|
||||
xml.rightAngleBracket();
|
||||
xml.append(error.toXML());
|
||||
xml.closeElement(ELEMENT);
|
||||
}
|
||||
else {
|
||||
xml.closeEmptyElement();
|
||||
}
|
||||
return xml;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static abstract class AbstractResume extends FullStreamElement {
|
||||
|
||||
private final long handledCount;
|
||||
private final String previd;
|
||||
|
||||
public AbstractResume(long handledCount, String previd) {
|
||||
this.handledCount = handledCount;
|
||||
this.previd = previd;
|
||||
}
|
||||
|
||||
public long getHandledCount() {
|
||||
return handledCount;
|
||||
}
|
||||
|
||||
public String getPrevId() {
|
||||
return previd;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.attribute("h", Long.toString(handledCount));
|
||||
xml.attribute("previd", previd);
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Resume extends AbstractResume {
|
||||
public static final String ELEMENT = "resume";
|
||||
|
||||
public Resume(long handledCount, String previd) {
|
||||
super(handledCount, previd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Resumed extends AbstractResume {
|
||||
public static final String ELEMENT = "resumed";
|
||||
|
||||
public Resumed(long handledCount, String previd) {
|
||||
super(handledCount, previd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
}
|
||||
|
||||
public static class AckAnswer extends FullStreamElement {
|
||||
public static final String ELEMENT = "a";
|
||||
|
||||
private final long handledCount;
|
||||
|
||||
public AckAnswer(long handledCount) {
|
||||
this.handledCount = handledCount;
|
||||
}
|
||||
|
||||
public long getHandledCount() {
|
||||
return handledCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.attribute("h", Long.toString(handledCount));
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
}
|
||||
|
||||
public static class AckRequest extends FullStreamElement {
|
||||
public static final String ELEMENT = "r";
|
||||
public static final AckRequest INSTANCE = new AckRequest();
|
||||
|
||||
private AckRequest() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence toXML() {
|
||||
return '<' + ELEMENT + " xmlns='" + NAMESPACE + "'/>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.predicates;
|
||||
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
public class AfterXStanzas implements PacketFilter {
|
||||
|
||||
final int count;
|
||||
int currentCount;
|
||||
|
||||
public AfterXStanzas(int count) {
|
||||
this.count = count;
|
||||
currentCount = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean accept(Packet packet) {
|
||||
currentCount++;
|
||||
if (currentCount == count) {
|
||||
resetCounter();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized void resetCounter() {
|
||||
currentCount = 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.predicates;
|
||||
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
public class ForEveryMessage implements PacketFilter {
|
||||
|
||||
public static final ForEveryMessage INSTANCE = new ForEveryMessage();
|
||||
|
||||
private ForEveryMessage() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Packet packet) {
|
||||
if (packet instanceof Message) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.predicates;
|
||||
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
public class ForEveryStanza implements PacketFilter {
|
||||
|
||||
public static final ForEveryStanza INSTANCE = new ForEveryStanza();
|
||||
|
||||
private ForEveryStanza() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Packet packet) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.predicates;
|
||||
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
public class ForMatchingPredicateOrAfterXStanzas implements PacketFilter {
|
||||
|
||||
private final PacketFilter predicate;
|
||||
private final AfterXStanzas afterXStanzas;
|
||||
|
||||
public ForMatchingPredicateOrAfterXStanzas(PacketFilter predicate, int count) {
|
||||
this.predicate = predicate;
|
||||
this.afterXStanzas = new AfterXStanzas(count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Packet packet) {
|
||||
if (predicate.accept(packet)) {
|
||||
afterXStanzas.resetCounter();
|
||||
return true;
|
||||
}
|
||||
return afterXStanzas.accept(packet);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.predicates;
|
||||
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
public class OnceForThisStanza implements PacketFilter {
|
||||
|
||||
private final String id;
|
||||
private final XMPPTCPConnection connection;
|
||||
|
||||
public static void setup(XMPPTCPConnection connection, Packet packet) {
|
||||
PacketFilter packetFilter = new OnceForThisStanza(connection, packet);
|
||||
connection.addRequestAckPredicate(packetFilter);
|
||||
}
|
||||
|
||||
private OnceForThisStanza(XMPPTCPConnection connection, Packet packet) {
|
||||
this.connection = connection;
|
||||
this.id = packet.getPacketID();
|
||||
if (StringUtils.isNullOrEmpty(id)) {
|
||||
throw new IllegalArgumentException("Stanza ID must be set");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Packet packet) {
|
||||
String otherId = packet.getPacketID();
|
||||
if (StringUtils.isNullOrEmpty(otherId)) {
|
||||
return false;
|
||||
}
|
||||
if (id.equals(otherId)) {
|
||||
connection.removeRequestAckPredicate(this);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.predicates;
|
||||
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
|
||||
public class Predicate {
|
||||
|
||||
public static PacketFilter forMessagesOrAfter5Stanzas() {
|
||||
return new ForMatchingPredicateOrAfterXStanzas(ForEveryMessage.INSTANCE, 5);
|
||||
}
|
||||
|
||||
public static AfterXStanzas after5Stanzas() {
|
||||
return new AfterXStanzas(5);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.predicates;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
public class ShortcutPredicates implements PacketFilter {
|
||||
|
||||
private final Set<PacketFilter> predicates = new LinkedHashSet<PacketFilter>();
|
||||
|
||||
public ShortcutPredicates() {
|
||||
}
|
||||
|
||||
public ShortcutPredicates(Collection<? extends PacketFilter> predicates) {
|
||||
this.predicates.addAll(predicates);
|
||||
}
|
||||
|
||||
public boolean addPredicate(PacketFilter predicate) {
|
||||
return predicates.add(predicate);
|
||||
}
|
||||
|
||||
public boolean removePredicate(PacketFilter prediacte) {
|
||||
return predicates.remove(prediacte);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Packet packet) {
|
||||
for (PacketFilter predicate : predicates) {
|
||||
if (predicate.accept(packet)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.tcp.sm.provider;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.AckAnswer;
|
||||
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.Enabled;
|
||||
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.Failed;
|
||||
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.Resumed;
|
||||
import org.jivesoftware.smack.util.ParserUtils;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
public class ParseStreamManagement {
|
||||
|
||||
public static Enabled enabled(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
ParserUtils.assertAtStartTag(parser);
|
||||
boolean resume = ParserUtils.getBooleanAttribute(parser, "resume", false);
|
||||
String id = parser.getAttributeValue("", "id");
|
||||
String location = parser.getAttributeValue("", "location");
|
||||
int max = ParserUtils.getIntegerAttribute(parser, "max", -1);
|
||||
parser.next();
|
||||
ParserUtils.assertAtEndTag(parser);
|
||||
return new Enabled(id, resume, location, max);
|
||||
}
|
||||
|
||||
public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
ParserUtils.assertAtStartTag(parser);
|
||||
String name;
|
||||
String condition = "unknown";
|
||||
outerloop:
|
||||
while(true) {
|
||||
int event = parser.next();
|
||||
switch (event) {
|
||||
case XmlPullParser.START_TAG:
|
||||
name = parser.getName();
|
||||
String namespace = parser.getNamespace();
|
||||
if (XMPPError.NAMESPACE.equals(namespace)) {
|
||||
condition = name;
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.END_TAG:
|
||||
name = parser.getName();
|
||||
if (Failed.ELEMENT.equals(name)) {
|
||||
break outerloop;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
ParserUtils.assertAtEndTag(parser);
|
||||
XMPPError error = new XMPPError(condition);
|
||||
return new Failed(error);
|
||||
}
|
||||
|
||||
public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
ParserUtils.assertAtStartTag(parser);
|
||||
long h = ParserUtils.getLongAttribute(parser, "h");
|
||||
String previd = parser.getAttributeValue("", "previd");
|
||||
parser.next();
|
||||
ParserUtils.assertAtEndTag(parser);
|
||||
return new Resumed(h, previd);
|
||||
}
|
||||
|
||||
public static AckAnswer ackAnswer(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
ParserUtils.assertAtStartTag(parser);
|
||||
long h = ParserUtils.getLongAttribute(parser, "h");
|
||||
parser.next();
|
||||
ParserUtils.assertAtEndTag(parser);
|
||||
return new AckAnswer(h);
|
||||
}
|
||||
|
||||
}
|
|
@ -40,19 +40,22 @@ public class PacketWriterTest {
|
|||
*
|
||||
* @throws InterruptedException
|
||||
* @throws BrokenBarrierException
|
||||
* @throws NotConnectedException
|
||||
*/
|
||||
@SuppressWarnings("javadoc")
|
||||
@Test
|
||||
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException {
|
||||
XMPPTCPConnection connection = new XMPPTCPConnection("foobar.com");
|
||||
final PacketWriter pw = connection.new PacketWriter();
|
||||
pw.setWriter(new BlockingStringWriter());
|
||||
pw.startup();
|
||||
connection.packetWriter = pw;
|
||||
connection.packetReader = connection.new PacketReader();
|
||||
connection.setWriter(new BlockingStringWriter());
|
||||
pw.init();
|
||||
|
||||
for (int i = 0; i < XMPPTCPConnection.PacketWriter.QUEUE_SIZE; i++) {
|
||||
pw.sendPacket(new Message());
|
||||
pw.sendStreamElement(new Message());
|
||||
}
|
||||
|
||||
|
||||
final CyclicBarrier barrier = new CyclicBarrier(2);
|
||||
shutdown = false;
|
||||
prematureUnblocked = false;
|
||||
|
@ -61,7 +64,7 @@ public class PacketWriterTest {
|
|||
public void run() {
|
||||
try {
|
||||
barrier.await();
|
||||
pw.sendPacket(new Message());
|
||||
pw.sendStreamElement(new Message());
|
||||
// should only return after the pw was interrupted
|
||||
if (!shutdown) {
|
||||
prematureUnblocked = true;
|
||||
|
@ -85,9 +88,9 @@ public class PacketWriterTest {
|
|||
Thread.sleep(250);
|
||||
|
||||
// Set to true for testing purposes, so that shutdown() won't wait packet writer
|
||||
pw.shutdownDone.set(true);
|
||||
pw.shutdownDone.reportSuccess();
|
||||
// Shutdown the packetwriter
|
||||
pw.shutdown();
|
||||
pw.shutdown(false);
|
||||
shutdown = true;
|
||||
barrier.await();
|
||||
if (prematureUnblocked) {
|
||||
|
|
|
@ -0,0 +1,154 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2014 Vyacheslav Blinov
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.jivesoftware.smack.tcp.sm.provider;
|
||||
|
||||
import com.jamesmurty.utils.XMLBuilder;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.junit.Test;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class ParseStreamManagementTest {
|
||||
private static final Properties outputProperties = initOutputProperties();
|
||||
|
||||
@Test
|
||||
public void testParseEnabled() throws Exception {
|
||||
String stanzaID = "zid615d9";
|
||||
boolean resume = true;
|
||||
String location = "test";
|
||||
int max = 42;
|
||||
|
||||
String enabledStanza = XMLBuilder.create("enabled")
|
||||
.a("xmlns", "urn:xmpp:sm:3")
|
||||
.a("id", "zid615d9")
|
||||
.a("resume", String.valueOf(resume))
|
||||
.a("location", location)
|
||||
.a("max", String.valueOf(max))
|
||||
.asString(outputProperties);
|
||||
|
||||
StreamManagement.Enabled enabledPacket = ParseStreamManagement.enabled(
|
||||
PacketParserUtils.getParserFor(enabledStanza));
|
||||
|
||||
assertThat(enabledPacket, is(notNullValue()));
|
||||
assertThat(enabledPacket.getId(), equalTo(stanzaID));
|
||||
assertThat(enabledPacket.getLocation(), equalTo(location));
|
||||
assertThat(enabledPacket.isResumeSet(), equalTo(resume));
|
||||
assertThat(enabledPacket.getMaxResumptionTime(), equalTo(max));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testParseEnabledInvariant() throws XmlPullParserException, IOException {
|
||||
String enabledString = (new StreamManagement.Enabled("stream-id", false)).toXML().toString();
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(enabledString);
|
||||
StreamManagement.Enabled enabled = ParseStreamManagement.enabled(parser);
|
||||
|
||||
assertEquals(enabledString, enabled.toXML().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseFailed() throws Exception {
|
||||
String failedStanza = XMLBuilder.create("failed")
|
||||
.a("xmlns", "urn:xmpp:sm:3")
|
||||
.asString(outputProperties);
|
||||
|
||||
StreamManagement.Failed failedPacket = ParseStreamManagement.failed(
|
||||
PacketParserUtils.getParserFor(failedStanza));
|
||||
|
||||
assertThat(failedPacket, is(notNullValue()));
|
||||
XMPPError error = failedPacket.getXMPPError();
|
||||
|
||||
assertThat(error, is(notNullValue()));
|
||||
assertThat(error.getCondition(), equalTo("unknown"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseFailedError() throws Exception {
|
||||
String errorCondition = "failure";
|
||||
|
||||
String failedStanza = XMLBuilder.create("failed")
|
||||
.a("xmlns", "urn:xmpp:sm:3")
|
||||
.element(errorCondition, XMPPError.NAMESPACE)
|
||||
.asString(outputProperties);
|
||||
|
||||
System.err.println(failedStanza);
|
||||
|
||||
StreamManagement.Failed failedPacket = ParseStreamManagement.failed(
|
||||
PacketParserUtils.getParserFor(failedStanza));
|
||||
|
||||
assertThat(failedPacket, is(notNullValue()));
|
||||
XMPPError error = failedPacket.getXMPPError();
|
||||
|
||||
assertThat(error, is(notNullValue()));
|
||||
assertThat(error.getCondition(), equalTo(errorCondition));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseResumed() throws Exception {
|
||||
long handledPackets = 42;
|
||||
String previousID = "zid615d9";
|
||||
|
||||
String resumedStanza = XMLBuilder.create("resumed")
|
||||
.a("xmlns", "urn:xmpp:sm:3")
|
||||
.a("h", String.valueOf(handledPackets))
|
||||
.a("previd", previousID)
|
||||
.asString(outputProperties);
|
||||
|
||||
StreamManagement.Resumed resumedPacket = ParseStreamManagement.resumed(
|
||||
PacketParserUtils.getParserFor(resumedStanza));
|
||||
|
||||
assertThat(resumedPacket, is(notNullValue()));
|
||||
assertThat(resumedPacket.getHandledCount(), equalTo(handledPackets));
|
||||
assertThat(resumedPacket.getPrevId(), equalTo(previousID));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseAckAnswer() throws Exception {
|
||||
long handledPackets = 42 + 42;
|
||||
|
||||
String ackStanza = XMLBuilder.create("a")
|
||||
.a("xmlns", "urn:xmpp:sm:3")
|
||||
.a("h", String.valueOf(handledPackets))
|
||||
.asString(outputProperties);
|
||||
|
||||
StreamManagement.AckAnswer acknowledgementPacket = ParseStreamManagement.ackAnswer(
|
||||
PacketParserUtils.getParserFor(ackStanza));
|
||||
|
||||
assertThat(acknowledgementPacket, is(notNullValue()));
|
||||
assertThat(acknowledgementPacket.getHandledCount(), equalTo(handledPackets));
|
||||
}
|
||||
|
||||
|
||||
private static Properties initOutputProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
return properties;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue