Smack/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/OutgoingJingleFileOffer.java

173 lines
7.2 KiB
Java
Raw Normal View History

/**
*
* 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_filetransfer;
2017-06-21 00:16:47 +02:00
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
2017-06-19 19:22:59 +02:00
import org.jivesoftware.smack.packet.IQ;
2017-06-21 00:16:47 +02:00
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
import org.jivesoftware.smackx.jingle.Role;
2017-06-19 19:22:59 +02:00
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent;
2017-06-21 00:16:47 +02:00
import org.jivesoftware.smackx.jingle.transports.JingleTransportInitiationCallback;
import org.jivesoftware.smackx.jingle.transports.JingleTransportManager;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
import org.jxmpp.jid.FullJid;
/**
* We are the initiator and we are the sender.
*/
public class OutgoingJingleFileOffer extends JingleFileTransferSession {
private static final Logger LOGGER = Logger.getLogger(OutgoingJingleFileOffer.class.getName());
2017-06-21 14:11:42 +02:00
public enum State {
fresh,
pending,
sent_transport_replace,
active,
2017-06-23 22:48:28 +02:00
terminated
}
2017-06-21 14:11:42 +02:00
2017-06-21 00:16:47 +02:00
private Thread sendingThread;
private File source;
2017-06-21 14:11:42 +02:00
private State state;
2017-06-21 00:16:47 +02:00
public OutgoingJingleFileOffer(XMPPConnection connection, FullJid responder, String sid) {
super(connection, connection.getUser().asFullJidOrThrow(), responder, Role.initiator, sid, Type.offer);
2017-06-21 14:11:42 +02:00
state = State.fresh;
}
public OutgoingJingleFileOffer(XMPPConnection connection, FullJid recipient) {
2017-06-23 22:48:28 +02:00
this(connection, recipient, JingleManager.randomId());
}
2017-06-22 14:47:39 +02:00
public void send(File file) throws InterruptedException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException {
source = file;
2017-06-23 22:48:28 +02:00
String contentName = JingleManager.randomId();
2017-06-22 14:47:39 +02:00
JingleFileTransfer transfer = JingleFileTransferManager.fileTransferFromFile(file);
initiateFileOffer(transfer, JingleContent.Creator.initiator, contentName);
}
2017-06-21 15:54:20 +02:00
public void initiateFileOffer(JingleFileTransfer file, JingleContent.Creator creator, String name) throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {
if (state != State.fresh) {
throw new IllegalStateException("This session is not fresh.");
}
2017-06-23 22:48:28 +02:00
JingleTransportManager<?> transportManager = JingleTransportMethodManager.getInstanceFor(connection)
2017-06-21 15:54:20 +02:00
.getBestAvailableTransportManager();
2017-06-21 15:54:20 +02:00
if (transportManager == null) {
throw new IllegalStateException("There must be at least one workable transport method.");
}
2017-06-21 15:54:20 +02:00
2017-06-23 22:48:28 +02:00
transportSession = transportManager.transportSession(this);
2017-06-21 15:54:20 +02:00
state = State.pending;
2017-06-22 14:47:39 +02:00
2017-06-23 22:48:28 +02:00
jutil.sendSessionInitiateFileOffer(getResponder(), getSessionId(), creator, name, file, transportSession.createTransport());
}
2017-06-19 19:22:59 +02:00
@Override
2017-06-21 14:11:42 +02:00
public IQ handleSessionAccept(Jingle sessionAccept) throws SmackException.NotConnectedException, InterruptedException {
// Out of order?
if (state != State.pending) {
2017-06-22 14:47:39 +02:00
LOGGER.log(Level.WARNING, "Session state is " + state + ", so session-accept is out of order.");
2017-06-21 15:54:20 +02:00
return jutil.createErrorOutOfOrder(sessionAccept);
2017-06-21 14:11:42 +02:00
}
2017-06-21 00:16:47 +02:00
2017-06-22 14:47:39 +02:00
LOGGER.log(Level.INFO, "Session was accepted. Initiate Bytestream.");
2017-06-21 15:54:20 +02:00
state = State.active;
2017-06-23 22:48:28 +02:00
queued.add(threadPool.submit(new Runnable() {
2017-06-21 15:54:20 +02:00
@Override
2017-06-22 14:47:39 +02:00
public void run() {
2017-06-23 22:48:28 +02:00
transportSession.initiateOutgoingSession(new JingleTransportInitiationCallback() {
2017-06-22 14:47:39 +02:00
@Override
public void onSessionInitiated(final BytestreamSession session) {
LOGGER.log(Level.INFO, "BytestreamSession initiated. Start transfer.");
sendingThread = new SendingThread(session, source);
sendingThread.start();
}
@Override
public void onException(Exception e) {
LOGGER.log(Level.SEVERE, "Cannot create outgoing Bytestream session: ", e);
}
});
2017-06-21 15:54:20 +02:00
}
2017-06-23 22:48:28 +02:00
}));
2017-06-21 15:54:20 +02:00
2017-06-19 19:22:59 +02:00
return jutil.createAck(sessionAccept);
}
@Override
public IQ handleSessionTerminate(Jingle sessionTerminate) {
2017-06-22 14:47:39 +02:00
LOGGER.log(Level.INFO, "Received session-terminate: " + sessionTerminate.getReason().asEnum());
2017-06-21 00:16:47 +02:00
if (sendingThread != null && !sendingThread.isInterrupted()) {
2017-06-22 14:47:39 +02:00
LOGGER.log(Level.INFO, "Interrupt sending thread.");
2017-06-21 00:16:47 +02:00
sendingThread.interrupt();
}
2017-06-21 14:11:42 +02:00
state = State.terminated;
2017-06-19 19:22:59 +02:00
return jutil.createAck(sessionTerminate);
}
@Override
2017-06-22 14:47:39 +02:00
public IQ handleTransportReplace(final Jingle transportReplace)
2017-06-19 19:22:59 +02:00
throws InterruptedException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException {
2017-06-22 14:47:39 +02:00
LOGGER.log(Level.INFO, "Received transport-replace.");
final JingleTransportManager<?> replacementManager = JingleTransportMethodManager.getInstanceFor(connection)
2017-06-19 19:22:59 +02:00
.getTransportManager(transportReplace);
2017-06-23 22:48:28 +02:00
queued.add(threadPool.submit(new Runnable() {
2017-06-22 14:47:39 +02:00
@Override
public void run() {
try {
if (replacementManager != null) {
LOGGER.log(Level.INFO, "Accept transport-replace.");
jutil.sendTransportAccept(transportReplace.getFrom().asFullJidOrThrow(),
transportReplace.getInitiator(), transportReplace.getSid(), creator, name,
2017-06-23 22:48:28 +02:00
transportSession.createTransport());
2017-06-22 14:47:39 +02:00
} else {
LOGGER.log(Level.INFO, "Unsupported transport. Reject transport-replace.");
jutil.sendTransportReject(transportReplace.getFrom().asFullJidOrThrow(), transportReplace.getInitiator(),
transportReplace.getSid(), creator, name, transportReplace.getContents().get(0).getJingleTransport());
}
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.SEVERE, "Help me please!", e);
}
}
2017-06-23 22:48:28 +02:00
}));
2017-06-21 00:16:47 +02:00
return jutil.createAck(transportReplace);
}
}