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/jingle3/transport/jingle_ibb/JingleIBBTransport.java

94 lines
3.9 KiB
Java
Raw Normal View History

2017-07-19 23:15:17 +02:00
package org.jivesoftware.smackx.jingle3.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-19 23:15:17 +02:00
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportInfoElement;
2017-07-19 15:17:12 +02:00
import org.jivesoftware.smackx.jingle3.internal.Transport;
2017-07-21 17:58:57 +02:00
import org.jivesoftware.smackx.jingle3.internal.TransportCandidate;
2017-07-19 15:17:12 +02:00
import org.jivesoftware.smackx.jingle3.transport.BytestreamSessionEstablishedListener;
import org.jivesoftware.smackx.jingle3.transport.jingle_ibb.element.JingleIBBTransportElement;
2017-07-19 23:15:17 +02:00
import org.jxmpp.jid.FullJid;
2017-07-19 15:17:12 +02:00
/**
* Created by vanitas on 18.07.17.
*/
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);
}
@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-19 23:15:17 +02:00
public void establishIncomingBytestreamSession(final FullJid peer, final String transportSessionId, final BytestreamSessionEstablishedListener listener, XMPPConnection connection) {
InBandBytestreamManager.getByteStreamManager(connection)
2017-07-19 15:17:12 +02:00
.addIncomingBytestreamListener(new BytestreamListener() {
@Override
public void incomingBytestreamRequest(BytestreamRequest request) {
2017-07-19 23:15:17 +02:00
if (request.getFrom().asFullJidIfPossible().equals(peer)
&& request.getSessionID().equals(transportSessionId)) {
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-19 23:15:17 +02:00
public void establishOutgoingBytestreamSession(FullJid peer, String transportSessionId, BytestreamSessionEstablishedListener listener, XMPPConnection connection) {
InBandBytestreamManager inBandBytestreamManager = InBandBytestreamManager.getByteStreamManager(connection);
inBandBytestreamManager.setDefaultBlockSize(blockSize);
try {
BytestreamSession session = inBandBytestreamManager.establishSession(peer, transportSessionId);
listener.onBytestreamSessionEstablished(session);
} 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-21 17:58:57 +02:00
2017-07-19 15:17:12 +02:00
}
}