1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-25 04:44:49 +02:00
Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transport/jingle_ibb/JingleIBBTransport.java

118 lines
4.7 KiB
Java
Raw Normal View History

2017-07-21 23:05:46 +02:00
/**
*
* Copyright 2017 Paul Schaub
*
* 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.smackx.jingle.transport.jingle_ibb;
2017-07-19 15:17:12 +02:00
import org.jivesoftware.smack.SmackException;
2017-07-19 23:15:17 +02:00
import org.jivesoftware.smack.XMPPConnection;
2017-07-19 15:17:12 +02:00
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.BytestreamListener;
import org.jivesoftware.smackx.bytestreams.BytestreamRequest;
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
2017-07-21 23:05:46 +02:00
import org.jivesoftware.smackx.jingle.internal.Transport;
import org.jivesoftware.smackx.jingle.internal.TransportCandidate;
import org.jivesoftware.smackx.jingle.transport.BytestreamSessionEstablishedListener;
import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfoElement;
import org.jivesoftware.smackx.jingle.internal.Session;
import org.jivesoftware.smackx.jingle.transport.jingle_ibb.element.JingleIBBTransportElement;
2017-07-19 23:15:17 +02:00
2017-07-19 15:17:12 +02:00
/**
2017-07-21 23:05:46 +02:00
* Jingle InBandBytestream Transport component.
2017-07-19 15:17:12 +02:00
*/
public class JingleIBBTransport extends Transport<JingleIBBTransportElement> {
2017-07-19 23:15:17 +02:00
public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:ibb:1";
public static final String NAMESPACE = NAMESPACE_V1;
2017-07-19 15:17:12 +02:00
private final String streamId;
private final Short blockSize;
public JingleIBBTransport(String streamId, Short blockSize) {
this.streamId = streamId;
this.blockSize = blockSize;
}
public JingleIBBTransport() {
this(StringUtils.randomString(10), JingleIBBTransportElement.DEFAULT_BLOCK_SIZE);
}
2017-07-21 23:05:46 +02:00
public Short getBlockSize() {
return blockSize;
}
public String getSid() {
return streamId;
}
2017-07-19 15:17:12 +02:00
@Override
public JingleIBBTransportElement getElement() {
2017-07-19 23:15:17 +02:00
return new JingleIBBTransportElement(streamId, blockSize);
2017-07-19 15:17:12 +02:00
}
@Override
public String getNamespace() {
2017-07-19 23:15:17 +02:00
return NAMESPACE;
2017-07-19 15:17:12 +02:00
}
@Override
2017-07-21 23:05:46 +02:00
public void establishIncomingBytestreamSession(final BytestreamSessionEstablishedListener listener, final XMPPConnection connection) {
final Session session = getParent().getParent();
2017-07-19 23:15:17 +02:00
InBandBytestreamManager.getByteStreamManager(connection)
2017-07-19 15:17:12 +02:00
.addIncomingBytestreamListener(new BytestreamListener() {
@Override
public void incomingBytestreamRequest(BytestreamRequest request) {
2017-07-21 23:05:46 +02:00
if (request.getFrom().asFullJidIfPossible().equals(session.getPeer())
&& request.getSessionID().equals(getSid())) {
2017-07-19 15:17:12 +02:00
BytestreamSession bytestreamSession;
try {
bytestreamSession = request.accept();
} catch (InterruptedException | SmackException | XMPPException.XMPPErrorException e) {
2017-07-19 23:15:17 +02:00
listener.onBytestreamSessionFailed(e);
2017-07-19 15:17:12 +02:00
return;
}
2017-07-19 23:15:17 +02:00
listener.onBytestreamSessionEstablished(bytestreamSession);
2017-07-19 15:17:12 +02:00
}
}
});
}
@Override
2017-07-21 23:05:46 +02:00
public void establishOutgoingBytestreamSession(BytestreamSessionEstablishedListener listener, XMPPConnection connection) {
Session session = getParent().getParent();
2017-07-19 23:15:17 +02:00
InBandBytestreamManager inBandBytestreamManager = InBandBytestreamManager.getByteStreamManager(connection);
inBandBytestreamManager.setDefaultBlockSize(blockSize);
try {
2017-07-21 23:05:46 +02:00
BytestreamSession bytestreamSession = inBandBytestreamManager.establishSession(session.getPeer(), getSid());
listener.onBytestreamSessionEstablished(bytestreamSession);
2017-07-19 23:15:17 +02:00
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
listener.onBytestreamSessionFailed(e);
}
}
@Override
2017-07-21 17:58:57 +02:00
public void addCandidate(TransportCandidate<?> candidate) {
// Sorry, we don't want any candidates.
2017-07-19 23:15:17 +02:00
}
@Override
public void handleTransportInfo(JingleContentTransportInfoElement info) {
// Nothing to do.
2017-07-19 15:17:12 +02:00
}
}