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

Merge branch 'jftPR' into jetPR

This commit is contained in:
vanitasvitae 2017-08-22 16:06:58 +02:00
commit 1ec9768f58
9 changed files with 85 additions and 48 deletions

View file

@ -40,6 +40,7 @@ public abstract class JingleFileTransfer extends JingleDescription<JingleFileTra
protected State state;
protected JingleFile metadata;
protected float percentage;
private final List<ProgressListener> progressListeners = Collections.synchronizedList(new ArrayList<ProgressListener>());
@ -83,29 +84,41 @@ public abstract class JingleFileTransfer extends JingleDescription<JingleFileTra
getParent().onContentCancel();
}
public void notifyProgressListeners(float progress) {
for (ProgressListener p : progressListeners) {
p.progress(progress);
}
}
public void notifyProgressListenersFinished() {
for (ProgressListener p : progressListeners) {
p.finished();
}
}
public void notifyProgressListenersStarted() {
for (ProgressListener p : progressListeners) {
p.started();
}
}
public void notifyProgressListenersTerminated(JingleReasonElement.Reason reason) {
for (ProgressListener p : progressListeners) {
p.terminated(reason);
}
}
/**
* Return progress as a float value between 0 and 1.
* If the transmission has not yet started, return -1.
* @return -1 or percentage in [0,1]
*/
public float getPercentage() {
if (state == State.pending || state == State.negotiating) {
return -1f;
}
return percentage;
}
@Override
public String getNamespace() {
return JingleFileTransfer.NAMESPACE;
}
@Override
public void handleContentTerminate(JingleReasonElement.Reason reason) {
notifyProgressListenersTerminated(reason);
}
@Override
public JingleFileTransferElement getElement() {
return new JingleFileTransferElement(metadata.getElement());

View file

@ -36,6 +36,7 @@ import org.jivesoftware.smackx.hashes.element.HashElement;
import org.jivesoftware.smackx.jingle.component.JingleSession;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionInfoElement;
import org.jivesoftware.smackx.jingle.element.JingleElement;
import org.jivesoftware.smackx.jingle.element.JingleReasonElement;
import org.jivesoftware.smackx.jingle_filetransfer.controller.IncomingFileOfferController;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChildElement;
@ -97,6 +98,9 @@ public class JingleIncomingFileOffer extends AbstractJingleFileOffer implements
target.write(bufbuf, 0, length);
read += length;
LOGGER.log(Level.INFO, "Read " + read + " (" + length + ") of " + metadata.getSize() + " bytes.");
percentage = ((float) read) / ((float) metadata.getSize());
if (read == (int) metadata.getSize()) {
break;
}
@ -133,7 +137,7 @@ public class JingleIncomingFileOffer extends AbstractJingleFileOffer implements
LOGGER.log(Level.INFO, "CHECKSUM MATCHED :)");
}
}
notifyProgressListenersFinished();
notifyProgressListenersTerminated(JingleReasonElement.Reason.success);
getParent().onContentFinished();
}

View file

@ -28,6 +28,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionInfoElement;
import org.jivesoftware.smackx.jingle.element.JingleElement;
import org.jivesoftware.smackx.jingle.element.JingleReasonElement;
import org.jivesoftware.smackx.jingle_filetransfer.controller.OutgoingFileOfferController;
/**
@ -66,6 +67,8 @@ public class JingleOutgoingFileOffer extends AbstractJingleFileOffer implements
byte[] buf = new byte[8192];
int written = 0;
while (true) {
if (getState() == State.cancelled) {
break;
@ -75,6 +78,8 @@ public class JingleOutgoingFileOffer extends AbstractJingleFileOffer implements
break;
}
outputStream.write(buf, 0, r);
written += r;
percentage = ((float) getMetadata().getSize()) / ((float) written);
}
outputStream.flush();
@ -91,7 +96,7 @@ public class JingleOutgoingFileOffer extends AbstractJingleFileOffer implements
}
}
notifyProgressListenersFinished();
notifyProgressListenersTerminated(JingleReasonElement.Reason.success);
}
@Override

View file

@ -16,6 +16,8 @@
*/
package org.jivesoftware.smackx.jingle_filetransfer.listener;
import org.jivesoftware.smackx.jingle.element.JingleReasonElement;
/**
* Created by vanitas on 27.07.17.
*/
@ -23,7 +25,5 @@ public interface ProgressListener {
void started();
void progress(float percent);
void finished();
void terminated(JingleReasonElement.Reason reason);
}

View file

@ -480,6 +480,10 @@ public class JingleContent implements JingleTransportCallback, JingleSecurityCal
session.onContentCancel(this);
}
public void handleContentTerminate(JingleReasonElement.Reason reason) {
description.handleContentTerminate(reason);
}
private void replaceTransport(Set<String> blacklist, XMPPConnection connection)
throws SmackException.NotConnectedException, InterruptedException,
XMPPException.XMPPErrorException, SmackException.NoResponseException {

View file

@ -20,6 +20,7 @@ import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionElement;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionInfoElement;
import org.jivesoftware.smackx.jingle.element.JingleElement;
import org.jivesoftware.smackx.jingle.element.JingleReasonElement;
/**
* Class that represents a contents description component.
@ -45,4 +46,6 @@ public abstract class JingleDescription<D extends JingleContentDescriptionElemen
public abstract void onBytestreamReady(BytestreamSession bytestreamSession);
public abstract String getNamespace();
public abstract void handleContentTerminate(JingleReasonElement.Reason reason);
}

View file

@ -145,6 +145,10 @@ public class JingleSession {
return JingleElement.createSessionAccept(getInitiator(), getResponder(), getSessionId(), contentElements);
}
/**
* Handle local content finished event. This includes terminating the session.
* @param jingleContent content which finished.
*/
void onContentFinished(JingleContent jingleContent) {
if (contents.get(jingleContent.getName()) == null) {
LOGGER.log(Level.WARNING, "Session does not contain content " + jingleContent.getName() + ". Ignore contentFinished.");
@ -169,6 +173,11 @@ public class JingleSession {
*/
}
/**
* Handle local content cancel event. This happens when the local user cancels a content.
* If there is only one content in the session, terminate the session, otherwise just cancel the one content.
* @param jingleContent content that gets cancelled.
*/
void onContentCancel(JingleContent jingleContent) {
if (contents.get(jingleContent.getName()) == null) {
LOGGER.log(Level.WARNING, "Session does not contain content " + jingleContent.getName() + ". Ignore onContentCancel.");
@ -188,6 +197,10 @@ public class JingleSession {
}
}
/**
* Send a session terminate and remove the session from the list of active sessions.
* @param reason reason of termination.
*/
public void terminateSession(JingleReasonElement.Reason reason) {
try {
jingleManager.getConnection().createStanzaCollectorAndSend(JingleElement.createSessionTerminate(getPeer(), getSessionId(), reason));
@ -285,7 +298,12 @@ public class JingleSession {
throw new AssertionError("Reason MUST not be null! (I guess)...");
}
//TODO: Inform client.
JingleReasonElement.Reason r = reason.asEnum();
for (JingleContent content : contents.values()) {
content.handleContentTerminate(r);
}
jingleManager.removeSession(this);
return IQ.createResultIQ(request);

View file

@ -30,6 +30,7 @@ import java.util.logging.Level;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy;
import org.jivesoftware.smackx.jingle.element.JingleReasonElement;
import org.jivesoftware.smackx.jingle.transport.jingle_ibb.JingleIBBTransportManager;
import org.jivesoftware.smackx.jingle_filetransfer.controller.IncomingFileOfferController;
import org.jivesoftware.smackx.jingle_filetransfer.controller.OutgoingFileOfferController;
@ -96,13 +97,10 @@ public class JingleFileTransferIntegrationTest extends AbstractSmackIntegrationT
}
@Override
public void progress(float percent) {
}
@Override
public void finished() {
resultSyncPoint2.signal();
public void terminated(JingleReasonElement.Reason reason) {
if (reason == JingleReasonElement.Reason.success) {
resultSyncPoint2.signal();
}
}
});
@ -114,7 +112,7 @@ public class JingleFileTransferIntegrationTest extends AbstractSmackIntegrationT
}
});
OutgoingFileOfferController sending = aftm.sendFile(source, bob);
final OutgoingFileOfferController sending = aftm.sendFile(source, bob);
sending.addProgressListener(new ProgressListener() {
@Override
@ -123,13 +121,10 @@ public class JingleFileTransferIntegrationTest extends AbstractSmackIntegrationT
}
@Override
public void progress(float percent) {
}
@Override
public void finished() {
resultSyncPoint1.signal();
public void terminated(JingleReasonElement.Reason reason) {
if (reason == JingleReasonElement.Reason.success) {
resultSyncPoint1.signal();
}
}
});

View file

@ -28,6 +28,7 @@ import java.util.logging.Level;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy;
import org.jivesoftware.smackx.jingle.element.JingleReasonElement;
import org.jivesoftware.smackx.jingle.transport.jingle_ibb.JingleIBBTransportManager;
import org.jivesoftware.smackx.jingle.transport.jingle_s5b.JingleS5BTransportManager;
import org.jivesoftware.smackx.jingle_filetransfer.controller.IncomingFileOfferController;
@ -105,13 +106,10 @@ public class JingleFileTransferTransportFallbackIntegrationTest extends Abstract
}
@Override
public void progress(float percent) {
}
@Override
public void finished() {
resultSyncPoint2.signal();
public void terminated(JingleReasonElement.Reason reason) {
if (reason == JingleReasonElement.Reason.success) {
resultSyncPoint2.signal();
}
}
});
@ -123,7 +121,7 @@ public class JingleFileTransferTransportFallbackIntegrationTest extends Abstract
}
});
OutgoingFileOfferController sending = aftm.sendFile(source, bob);
final OutgoingFileOfferController sending = aftm.sendFile(source, bob);
sending.addProgressListener(new ProgressListener() {
@Override
@ -132,13 +130,10 @@ public class JingleFileTransferTransportFallbackIntegrationTest extends Abstract
}
@Override
public void progress(float percent) {
}
@Override
public void finished() {
resultSyncPoint1.signal();
public void terminated(JingleReasonElement.Reason reason) {
if (reason == JingleReasonElement.Reason.success) {
resultSyncPoint1.signal();
}
}
});