1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-27 22:42:06 +01:00

Fix cancelling file transfers and create int tests

This commit is contained in:
vanitasvitae 2017-08-23 14:09:41 +02:00
parent 8bd4954314
commit 97baf75a86
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 180 additions and 0 deletions

View file

@ -19,6 +19,8 @@ package org.jivesoftware.smackx.jingle_filetransfer.component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
@ -35,6 +37,8 @@ import org.jivesoftware.smackx.jingle_filetransfer.listener.ProgressListener;
*/ */
public abstract class JingleFileTransfer extends JingleDescription<JingleFileTransferElement> implements JingleFileTransferController { public abstract class JingleFileTransfer extends JingleDescription<JingleFileTransferElement> implements JingleFileTransferController {
private static final Logger LOGGER = Logger.getLogger(JingleFileTransfer.class.getName());
public static final String NAMESPACE_V5 = "urn:xmpp:jingle:apps:file-transfer:5"; public static final String NAMESPACE_V5 = "urn:xmpp:jingle:apps:file-transfer:5";
public static final String NAMESPACE = NAMESPACE_V5; public static final String NAMESPACE = NAMESPACE_V5;
@ -69,19 +73,23 @@ public abstract class JingleFileTransfer extends JingleDescription<JingleFileTra
switch (state) { switch (state) {
case pending: case pending:
if (session.isResponder()) { if (session.isResponder()) {
LOGGER.log(Level.INFO, "Cancel pending file transfer as responder.");
connection.createStanzaCollectorAndSend(JingleElement.createSessionTerminate(session.getPeer(), session.getSessionId(), JingleReasonElement.Reason.decline)); connection.createStanzaCollectorAndSend(JingleElement.createSessionTerminate(session.getPeer(), session.getSessionId(), JingleReasonElement.Reason.decline));
} else { } else {
LOGGER.log(Level.INFO, "Cancel pending file transfer as initiator.");
connection.createStanzaCollectorAndSend(JingleElement.createSessionTerminate(session.getPeer(), session.getSessionId(), JingleReasonElement.Reason.cancel)); connection.createStanzaCollectorAndSend(JingleElement.createSessionTerminate(session.getPeer(), session.getSessionId(), JingleReasonElement.Reason.cancel));
} }
break; break;
case active: case active:
LOGGER.log(Level.INFO, "Cancel active file transfer.");
connection.createStanzaCollectorAndSend(JingleElement.createSessionTerminate(session.getPeer(), session.getSessionId(), JingleReasonElement.Reason.cancel)); connection.createStanzaCollectorAndSend(JingleElement.createSessionTerminate(session.getPeer(), session.getSessionId(), JingleReasonElement.Reason.cancel));
break; break;
default: break; default: break;
} }
getParent().onContentCancel(); getParent().onContentCancel();
state = State.cancelled;
} }
public void notifyProgressListenersStarted() { public void notifyProgressListenersStarted() {

View file

@ -67,6 +67,7 @@ public class JingleIncomingFileOffer extends AbstractJingleFileOffer implements
if (state == State.negotiating) { if (state == State.negotiating) {
state = State.active; state = State.active;
notifyProgressListenersStarted();
} else { } else {
return; return;
} }

View file

@ -42,11 +42,13 @@ public class JingleOutgoingFileOffer extends AbstractJingleFileOffer implements
public JingleOutgoingFileOffer(File file, JingleFile metadata) throws FileNotFoundException { public JingleOutgoingFileOffer(File file, JingleFile metadata) throws FileNotFoundException {
super(metadata); super(metadata);
this.source = new FileInputStream(file); this.source = new FileInputStream(file);
this.state = State.pending;
} }
public JingleOutgoingFileOffer(InputStream inputStream, JingleFile metadata) { public JingleOutgoingFileOffer(InputStream inputStream, JingleFile metadata) {
super(metadata); super(metadata);
this.source = inputStream; this.source = inputStream;
this.state = State.pending;
} }
@Override @Override
@ -60,6 +62,10 @@ public class JingleOutgoingFileOffer extends AbstractJingleFileOffer implements
throw new IllegalStateException("Source InputStream is null!"); throw new IllegalStateException("Source InputStream is null!");
} }
state = State.active;
notifyProgressListenersStarted();
OutputStream outputStream = null; OutputStream outputStream = null;
try { try {

View file

@ -0,0 +1,165 @@
/**
*
* 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;
import static junit.framework.TestCase.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.jingle.element.JingleReasonElement;
import org.jivesoftware.smackx.jingle.transport.jingle_ibb.JingleIBBTransport;
import org.jivesoftware.smackx.jingle.transport.jingle_ibb.JingleIBBTransportManager;
import org.jivesoftware.smackx.jingle_filetransfer.component.JingleFile;
import org.jivesoftware.smackx.jingle_filetransfer.controller.IncomingFileOfferController;
import org.jivesoftware.smackx.jingle_filetransfer.controller.OutgoingFileOfferController;
import org.jivesoftware.smackx.jingle_filetransfer.listener.IncomingFileOfferListener;
import org.jivesoftware.smackx.jingle_filetransfer.listener.ProgressListener;
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
import org.junit.BeforeClass;
public class JingleFileTransferCancelIntegrationTest extends AbstractSmackIntegrationTest {
public JingleFileTransferCancelIntegrationTest(SmackIntegrationTestEnvironment environment) {
super(environment);
}
@BeforeClass
public void setup() {
JingleFileTransferManager.getInstanceFor(conOne);
JingleIBBTransportManager.getInstanceFor(conOne);
ServiceDiscoveryManager.getInstanceFor(conOne).addFeature(JingleIBBTransport.NAMESPACE);
JingleFileTransferManager.getInstanceFor(conThree);
JingleIBBTransportManager.getInstanceFor(conThree);
ServiceDiscoveryManager.getInstanceFor(conThree).addFeature(JingleIBBTransport.NAMESPACE);
}
@SmackIntegrationTest
public void senderCancelTest() throws Exception {
final SimpleResultSyncPoint s1 = new SimpleResultSyncPoint();
byte[] payload = new byte[320000];
new Random().nextBytes(payload);
JingleFileTransferManager.getInstanceFor(conThree).addIncomingFileOfferListener(new IncomingFileOfferListener() {
@Override
public void onIncomingFileOffer(IncomingFileOfferController offer) {
offer.addProgressListener(new ProgressListener() {
@Override
public void started() {
}
@Override
public void terminated(JingleReasonElement.Reason reason) {
if (reason == JingleReasonElement.Reason.cancel) {
s1.signal();
} else {
s1.signalFailure();
}
}
});
try {
offer.accept(conThree, new ByteArrayOutputStream());
} catch (InterruptedException | IOException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
fail(e.toString());
}
}
});
final OutgoingFileOfferController out = JingleFileTransferManager.getInstanceFor(conOne).sendStream(new ByteArrayInputStream(payload), new JingleFile("name", null, 320000, null, null, null), conThree.getUser().asFullJidOrThrow());
out.addProgressListener(new ProgressListener() {
@Override
public void started() {
try {
out.cancel(conOne);
} catch (SmackException.NotConnectedException | InterruptedException e) {
fail(e.toString());
}
}
@Override
public void terminated(JingleReasonElement.Reason reason) {
}
});
s1.waitForResult(60 * 1000);
}
@SmackIntegrationTest
public void receiverCancelTest() throws Exception {
final SimpleResultSyncPoint s1 = new SimpleResultSyncPoint();
byte[] payload = new byte[320000];
new Random().nextBytes(payload);
JingleFileTransferManager.getInstanceFor(conThree).addIncomingFileOfferListener(new IncomingFileOfferListener() {
@Override
public void onIncomingFileOffer(final IncomingFileOfferController offer) {
offer.addProgressListener(new ProgressListener() {
@Override
public void started() {
try {
offer.cancel(conThree);
} catch (SmackException.NotConnectedException | InterruptedException e) {
fail(e.toString());
}
}
@Override
public void terminated(JingleReasonElement.Reason reason) {
}
});
try {
offer.accept(conThree, new ByteArrayOutputStream());
} catch (InterruptedException | IOException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
fail(e.toString());
}
}
});
final OutgoingFileOfferController out = JingleFileTransferManager.getInstanceFor(conOne).sendStream(new ByteArrayInputStream(payload), new JingleFile("name", null, 320000, null, null, null), conThree.getUser().asFullJidOrThrow());
out.addProgressListener(new ProgressListener() {
@Override
public void started() {
}
@Override
public void terminated(JingleReasonElement.Reason reason) {
if (reason == JingleReasonElement.Reason.cancel) {
s1.signal();
} else {
s1.signalFailure();
}
}
});
s1.waitForResult(60 * 1000);
}
}