Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle3/transport/jingle_ibb/JingleIBBTransportSession.java

116 lines
4.6 KiB
Java
Raw Normal View History

2017-06-23 22:48:28 +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.
*/
2017-07-19 15:17:12 +02:00
package org.jivesoftware.smackx.jingle3.transport.jingle_ibb;
2017-06-23 22:48:28 +02:00
2017-06-23 23:41:40 +02:00
import java.util.logging.Level;
import java.util.logging.Logger;
2017-06-23 22:48:28 +02:00
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
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;
import org.jivesoftware.smackx.jingle.JingleSession;
2017-07-19 15:17:12 +02:00
import org.jivesoftware.smackx.jingle3.element.JingleElement;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportElement;
2017-06-23 22:48:28 +02:00
import org.jivesoftware.smackx.jingle.transports.JingleTransportInitiationCallback;
import org.jivesoftware.smackx.jingle.transports.JingleTransportManager;
import org.jivesoftware.smackx.jingle.transports.JingleTransportSession;
2017-07-19 15:17:12 +02:00
import org.jivesoftware.smackx.jingle3.transport.jingle_ibb.element.JingleIBBTransportElement;
2017-06-23 22:48:28 +02:00
2017-07-19 15:17:12 +02:00
public class JingleIBBTransportSession extends JingleTransportSession<JingleIBBTransportElement> {
2017-06-23 23:41:40 +02:00
private static final Logger LOGGER = Logger.getLogger(JingleIBBTransportSession.class.getName());
2017-06-23 22:48:28 +02:00
private final JingleIBBTransportManager transportManager;
public JingleIBBTransportSession(JingleSession session) {
super(session);
transportManager = JingleIBBTransportManager.getInstanceFor(session.getConnection());
}
@Override
2017-07-19 15:17:12 +02:00
public JingleIBBTransportElement createTransport() {
2017-06-23 22:48:28 +02:00
2017-06-26 18:05:17 +02:00
if (theirProposal == null) {
2017-07-19 15:17:12 +02:00
return new JingleIBBTransportElement();
2017-06-23 22:48:28 +02:00
} else {
2017-07-19 15:17:12 +02:00
return new JingleIBBTransportElement(theirProposal.getBlockSize(), theirProposal.getSessionId());
2017-06-23 22:48:28 +02:00
}
}
2017-06-26 18:05:17 +02:00
@Override
2017-07-19 15:17:12 +02:00
public void setTheirProposal(JingleContentTransportElement transport) {
theirProposal = (JingleIBBTransportElement) transport;
2017-06-26 18:05:17 +02:00
}
2017-06-23 22:48:28 +02:00
@Override
public void initiateOutgoingSession(JingleTransportInitiationCallback callback) {
2017-06-23 23:41:40 +02:00
LOGGER.log(Level.INFO, "Initiate Jingle InBandBytestream session.");
2017-06-23 22:48:28 +02:00
BytestreamSession session;
try {
2017-06-26 18:05:17 +02:00
session = InBandBytestreamManager.getByteStreamManager(jingleSession.getConnection())
.establishSession(jingleSession.getRemote(), theirProposal.getSessionId());
2017-06-23 22:48:28 +02:00
callback.onSessionInitiated(session);
} catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
callback.onException(e);
}
}
@Override
public void initiateIncomingSession(final JingleTransportInitiationCallback callback) {
2017-06-23 23:41:40 +02:00
LOGGER.log(Level.INFO, "Await Jingle InBandBytestream session.");
2017-06-23 22:48:28 +02:00
2017-06-26 18:05:17 +02:00
InBandBytestreamManager.getByteStreamManager(jingleSession.getConnection()).addIncomingBytestreamListener(new BytestreamListener() {
2017-06-23 22:48:28 +02:00
@Override
public void incomingBytestreamRequest(BytestreamRequest request) {
2017-06-26 18:05:17 +02:00
if (request.getFrom().asFullJidIfPossible().equals(jingleSession.getRemote())
&& request.getSessionID().equals(theirProposal.getSessionId())) {
2017-06-23 22:48:28 +02:00
BytestreamSession session;
try {
session = request.accept();
} catch (InterruptedException | SmackException | XMPPException.XMPPErrorException e) {
callback.onException(e);
return;
}
callback.onSessionInitiated(session);
}
}
});
}
@Override
public String getNamespace() {
return transportManager.getNamespace();
}
@Override
2017-07-19 15:17:12 +02:00
public IQ handleTransportInfo(JingleElement transportInfo) {
2017-06-23 22:48:28 +02:00
return IQ.createResultIQ(transportInfo);
//TODO
}
@Override
2017-07-19 15:17:12 +02:00
public JingleTransportManager<JingleIBBTransportElement> transportManager() {
2017-06-26 18:05:17 +02:00
return JingleIBBTransportManager.getInstanceFor(jingleSession.getConnection());
2017-06-23 22:48:28 +02:00
}
}