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 org.jivesoftware.smack.util.StringUtils;
020import org.jivesoftware.smack.util.XmlStringBuilder;
021
022import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
023
024/**
025 * Transport Element for JingleInBandBytestream transports.
026 */
027public class JingleIBBTransport extends JingleContentTransport {
028    public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:ibb:1";
029    public static final String ATTR_BLOCK_SIZE = "block-size";
030    public static final String ATTR_SID = "sid";
031
032    public static final short DEFAULT_BLOCK_SIZE = 4096;
033
034    private final short blockSize;
035    private final String sid;
036
037    public JingleIBBTransport() {
038        this(DEFAULT_BLOCK_SIZE);
039    }
040
041    public JingleIBBTransport(String sid) {
042        this(DEFAULT_BLOCK_SIZE, sid);
043    }
044
045    public JingleIBBTransport(short blockSize) {
046        this(blockSize, StringUtils.randomString(24));
047    }
048
049    public JingleIBBTransport(short blockSize, String sid) {
050        super(null);
051        if (blockSize > 0) {
052            this.blockSize = blockSize;
053        } else {
054            this.blockSize = DEFAULT_BLOCK_SIZE;
055        }
056        this.sid = sid;
057    }
058
059    public String getSessionId() {
060        return sid;
061    }
062
063    public short getBlockSize() {
064        return blockSize;
065    }
066
067    @Override
068    protected void addExtraAttributes(XmlStringBuilder xml) {
069        xml.attribute(ATTR_BLOCK_SIZE, blockSize);
070        xml.attribute(ATTR_SID, sid);
071    }
072
073    @Override
074    public String getNamespace() {
075        return NAMESPACE_V1;
076    }
077
078    @Override
079    public boolean equals(Object other) {
080        if (other == null || !(other instanceof JingleIBBTransport)) {
081            return false;
082        }
083
084        if (this == other) {
085            return true;
086        }
087
088        JingleIBBTransport otherTransport = (JingleIBBTransport) other;
089        return this.getSessionId().equals(otherTransport.getSessionId()) &&
090            this.getBlockSize() == otherTransport.getBlockSize();
091    }
092
093    @Override
094    public int hashCode() {
095        return this.toXML().toString().hashCode();
096    }
097}