From b7e2854f3bd4ec2d7c31278608351f1f5fd3dcaa Mon Sep 17 00:00:00 2001 From: Lishunyang Date: Mon, 11 Aug 2014 23:38:17 +0800 Subject: [PATCH] Add SctpMapExtension and provider. --- .../extensions/jingle/SctpMapExtension.java | 162 ++++++++++++++++++ .../jingle/SctpMapExtensionProvider.java | 46 +++++ 2 files changed, 208 insertions(+) create mode 100644 src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/SctpMapExtension.java create mode 100644 src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/SctpMapExtensionProvider.java diff --git a/src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/SctpMapExtension.java b/src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/SctpMapExtension.java new file mode 100644 index 000000000..3d62c2052 --- /dev/null +++ b/src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/SctpMapExtension.java @@ -0,0 +1,162 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.jitsi.jirecon.extension; + +import org.jivesoftware.smack.packet.PacketExtension; + +/** + * SctpMap extension in transport packet extension. + * Defined by XEP-0343: Signaling WebRTC datachannels in Jingle. + * + * @author lishunyang + * + */ +public class SctpMapExtension + implements PacketExtension +{ + /** + * The name of the "sctpmap" element. + */ + public static final String ELEMENT_NAME = "sctpmap"; + + /** + * The namespace for the "sctpmap" element. + */ + public static final String NAMESPACE = + "urn:xmpp:jingle:transports:dtls-sctp:1"; + + /** + * Port number of "sctpmap" element. + */ + public static final String PORT_ATTR_NAME = "number"; + + /** + * Protocol name of "sctpmap" element. + */ + public static final String PROTOCOL_ATTR_NAME = "protocol"; + + /** + * Number of streams of "sctpmap" element. + */ + public static final String STREAMS_ATTR_NAME = "streams"; + + /** + * Value of "port". + */ + private int port = -1; + + /** + * Value of "protocol". + * @See SctpMapExtension.Protocol + */ + private String protocol = ""; + + /** + * Number of "streams". + */ + private int streams = -1; + + /** + * {@inheritDoc} + */ + @Override + public String getElementName() + { + return ELEMENT_NAME; + } + + /** + * {@inheritDoc} + */ + @Override + public String getNamespace() + { + return NAMESPACE; + } + + /** + * {@inheritDoc} + */ + @Override + public String toXML() + { + StringBuilder builder = new StringBuilder(); + + builder.append("<").append(getElementName()); + builder.append(" ").append("xmlns").append("='").append(getNamespace()) + .append("'"); + builder.append(" ").append(PORT_ATTR_NAME).append("='").append(port) + .append("'"); + builder.append(" ").append(PROTOCOL_ATTR_NAME).append("='") + .append(protocol).append("'"); + builder.append(" ").append(STREAMS_ATTR_NAME).append("='") + .append(streams).append("'"); + builder.append("/>"); + + return builder.toString(); + } + + public void setPort(int port) + { + this.port = port; + } + + public int getPort() + { + return port; + } + + public void setProtocol(String protocol) + { + this.protocol = protocol; + } + + public void setProtocol(Protocol protocol) + { + this.protocol = protocol.toString(); + } + + public String getProtocol() + { + return protocol; + } + + public void setStreams(int streams) + { + this.streams = streams; + } + + public int getStreams() + { + return streams; + } + + /** + * Protocol enumeration of SctpMapExtension. Currently it only + * contains WEBRTC_CHANNEL. + * + * @author lishunyang + * + */ + public static enum Protocol + { + WEBRTC_CHANNEL("webrtc-datachannel"); + + private String name; + + private Protocol(String name) + { + this.name = name; + } + + @Override + public String toString() + { + return name; + } + } +} diff --git a/src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/SctpMapExtensionProvider.java b/src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/SctpMapExtensionProvider.java new file mode 100644 index 000000000..f205c8c01 --- /dev/null +++ b/src/net/java/sip/communicator/impl/protocol/jabber/extensions/jingle/SctpMapExtensionProvider.java @@ -0,0 +1,46 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.jitsi.jirecon.extension; + +import org.jivesoftware.smack.packet.*; +import org.jivesoftware.smack.provider.*; +import org.xmlpull.v1.XmlPullParser; + +/** + * The SctpMapExtensionProvider parses "sctpmap" elements into + * SctpMapExtension instances. + * + * @author lishunyang + * @see SctpMapExtension + */ +public class SctpMapExtensionProvider + implements PacketExtensionProvider +{ + + /** + * {@inheritDoc} + */ + @Override + public PacketExtension parseExtension(XmlPullParser parser) + throws Exception + { + SctpMapExtension result = new SctpMapExtension(); + + if (parser.getName().equals(SctpMapExtension.ELEMENT_NAME) + && parser.getNamespace().equals(SctpMapExtension.NAMESPACE)) + { + result.setPort(Integer.parseInt(parser.getAttributeValue(null, + SctpMapExtension.PORT_ATTR_NAME))); + result.setProtocol(parser.getAttributeValue(null, + SctpMapExtension.PROTOCOL_ATTR_NAME)); + result.setStreams(Integer.parseInt(parser.getAttributeValue(null, + SctpMapExtension.STREAMS_ATTR_NAME))); + } + + return result; + } +}