001/**
002 *
003 * Copyright 2017 Paul Schaub
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.jingle.transports.jingle_ibb.element;
018
019import javax.xml.namespace.QName;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.util.StringUtils;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
026
027/**
028 * Transport Element for JingleInBandBytestream transports.
029 */
030public class JingleIBBTransport extends JingleContentTransport implements ExtensionElement {
031    public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:ibb:1";
032    public static final QName QNAME = new QName(NAMESPACE_V1, ELEMENT);
033
034    public static final String ATTR_BLOCK_SIZE = "block-size";
035    public static final String ATTR_SID = "sid";
036
037    public static final short DEFAULT_BLOCK_SIZE = 4096;
038
039    private final short blockSize;
040    private final String sid;
041
042    public JingleIBBTransport() {
043        this(DEFAULT_BLOCK_SIZE);
044    }
045
046    public JingleIBBTransport(String sid) {
047        this(DEFAULT_BLOCK_SIZE, sid);
048    }
049
050    public JingleIBBTransport(short blockSize) {
051        this(blockSize, StringUtils.randomString(24));
052    }
053
054    public JingleIBBTransport(short blockSize, String sid) {
055        super(null);
056        if (blockSize > 0) {
057            this.blockSize = blockSize;
058        } else {
059            this.blockSize = DEFAULT_BLOCK_SIZE;
060        }
061        this.sid = sid;
062    }
063
064    public String getSessionId() {
065        return sid;
066    }
067
068    public short getBlockSize() {
069        return blockSize;
070    }
071
072    @Override
073    protected void addExtraAttributes(XmlStringBuilder xml) {
074        xml.attribute(ATTR_BLOCK_SIZE, blockSize);
075        xml.attribute(ATTR_SID, sid);
076    }
077
078    @Override
079    public String getNamespace() {
080        return QNAME.getNamespaceURI();
081    }
082
083    @Override
084    public boolean equals(Object other) {
085        if (other == null || !(other instanceof JingleIBBTransport)) {
086            return false;
087        }
088
089        if (this == other) {
090            return true;
091        }
092
093        JingleIBBTransport otherTransport = (JingleIBBTransport) other;
094        return this.getSessionId().equals(otherTransport.getSessionId()) &&
095            this.getBlockSize() == otherTransport.getBlockSize();
096    }
097
098    @Override
099    public int hashCode() {
100        return this.toXML().toString().hashCode();
101    }
102}