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;
|
2017-07-27 00:12:42 +02:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2017-07-19 15:17:12 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
2017-07-25 20:25:36 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener;
|
2017-07-19 15:17:12 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
2017-07-25 20:25:36 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest;
|
2017-07-27 18:09:49 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.callbacks.JingleTransportCallback;
|
2017-07-27 15:18:18 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.components.JingleSession;
|
|
|
|
import org.jivesoftware.smackx.jingle.components.JingleTransport;
|
|
|
|
import org.jivesoftware.smackx.jingle.components.JingleTransportCandidate;
|
2017-07-27 18:09:49 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfoElement;
|
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleElement;
|
2017-07-21 23:05:46 +02:00
|
|
|
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
|
|
|
*/
|
2017-07-21 23:51:06 +02:00
|
|
|
public class JingleIBBTransport extends JingleTransport<JingleIBBTransportElement> {
|
2017-07-19 15:17:12 +02:00
|
|
|
|
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-27 18:09:49 +02:00
|
|
|
public void establishIncomingBytestreamSession(final XMPPConnection connection, final JingleTransportCallback callback) {
|
2017-07-21 23:51:06 +02:00
|
|
|
final JingleSession session = getParent().getParent();
|
2017-07-25 20:25:36 +02:00
|
|
|
|
|
|
|
final InBandBytestreamManager inBandBytestreamManager = InBandBytestreamManager.getByteStreamManager(connection);
|
|
|
|
|
|
|
|
InBandBytestreamListener bytestreamListener = new InBandBytestreamListener() {
|
|
|
|
@Override
|
|
|
|
public void incomingBytestreamRequest(InBandBytestreamRequest request) {
|
|
|
|
if (request.getFrom().asFullJidIfPossible().equals(session.getPeer())
|
|
|
|
&& request.getSessionID().equals(getSid())) {
|
|
|
|
|
|
|
|
inBandBytestreamManager.removeIncomingBytestreamListener(this);
|
|
|
|
|
2017-07-27 18:09:49 +02:00
|
|
|
BytestreamSession bytestreamSession;
|
2017-07-25 20:25:36 +02:00
|
|
|
try {
|
|
|
|
bytestreamSession = request.accept();
|
|
|
|
} catch (InterruptedException | SmackException e) {
|
2017-07-27 18:09:49 +02:00
|
|
|
callback.onTransportFailed(e);
|
2017-07-25 20:25:36 +02:00
|
|
|
return;
|
2017-07-19 15:17:12 +02:00
|
|
|
}
|
2017-07-25 20:25:36 +02:00
|
|
|
|
|
|
|
JingleIBBTransport.this.bytestreamSession = bytestreamSession;
|
2017-07-27 18:09:49 +02:00
|
|
|
callback.onTransportReady(JingleIBBTransport.this.bytestreamSession);
|
2017-07-25 20:25:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
InBandBytestreamManager.getByteStreamManager(connection)
|
|
|
|
.addIncomingBytestreamListener(bytestreamListener);
|
2017-07-19 15:17:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-27 18:09:49 +02:00
|
|
|
public void establishOutgoingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback) {
|
2017-07-21 23:51:06 +02:00
|
|
|
JingleSession session = getParent().getParent();
|
2017-07-19 23:15:17 +02:00
|
|
|
InBandBytestreamManager inBandBytestreamManager = InBandBytestreamManager.getByteStreamManager(connection);
|
|
|
|
inBandBytestreamManager.setDefaultBlockSize(blockSize);
|
|
|
|
try {
|
2017-07-25 20:25:36 +02:00
|
|
|
JingleIBBTransport.this.bytestreamSession = inBandBytestreamManager.establishSession(session.getPeer(), getSid());
|
2017-07-27 18:09:49 +02:00
|
|
|
callback.onTransportReady(this.bytestreamSession);
|
2017-07-19 23:15:17 +02:00
|
|
|
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
|
2017-07-27 18:09:49 +02:00
|
|
|
callback.onTransportFailed(e);
|
2017-07-19 23:15:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-21 23:51:06 +02:00
|
|
|
public void addCandidate(JingleTransportCandidate<?> candidate) {
|
2017-07-21 17:58:57 +02:00
|
|
|
// Sorry, we don't want any candidates.
|
2017-07-19 23:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-22 00:26:50 +02:00
|
|
|
public void handleTransportInfo(JingleContentTransportInfoElement info, JingleElement wrapping) {
|
2017-07-19 23:15:17 +02:00
|
|
|
// Nothing to do.
|
2017-07-19 15:17:12 +02:00
|
|
|
}
|
2017-07-27 00:12:42 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isNonFatalException(Exception exception) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void handleStanza(Stanza stanza) throws SmackException.NotConnectedException, InterruptedException {
|
|
|
|
|
|
|
|
}
|
2017-07-19 15:17:12 +02:00
|
|
|
}
|