1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 04:22:05 +01:00

file transfer callback is now actually a callback.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6378 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-12-12 19:47:59 +00:00 committed by alex
parent 469b5c2a0a
commit 694c7863bd
2 changed files with 85 additions and 53 deletions

View file

@ -184,13 +184,13 @@ public abstract class FileTransfer {
this.exception = exception; this.exception = exception;
} }
protected final void setStatus(Status status) { protected void setStatus(Status status) {
synchronized (statusMonitor) { synchronized (statusMonitor) {
this.status = status; this.status = status;
} }
} }
protected final boolean updateStatus(Status oldStatus, Status newStatus) { protected boolean updateStatus(Status oldStatus, Status newStatus) {
synchronized (statusMonitor) { synchronized (statusMonitor) {
if (oldStatus != status) { if (oldStatus != status) {
return false; return false;

View file

@ -35,6 +35,7 @@ import java.io.*;
public class OutgoingFileTransfer extends FileTransfer { public class OutgoingFileTransfer extends FileTransfer {
private static int RESPONSE_TIMEOUT = 60 * 1000; private static int RESPONSE_TIMEOUT = 60 * 1000;
private NegotiationProgress callback;
/** /**
* Returns the time in milliseconds after which the file transfer * Returns the time in milliseconds after which the file transfer
@ -129,10 +130,8 @@ public class OutgoingFileTransfer extends FileTransfer {
/** /**
* This methods handles the transfer and stream negotiation process. It * This methods handles the transfer and stream negotiation process. It
* returns immediately and its progress can be monitored through the * returns immediately and its progress will be updated through the
* {@link NegotiationProgress} callback. When the negotiation process is * {@link NegotiationProgress} callback.
* complete the OutputStream can be retrieved from the callback via the
* {@link NegotiationProgress#getOutputStream()} method.
* *
* @param fileName * @param fileName
* The name of the file that will be transmitted. It is * The name of the file that will be transmitted. It is
@ -149,20 +148,26 @@ public class OutgoingFileTransfer extends FileTransfer {
*/ */
public synchronized void sendFile(final String fileName, public synchronized void sendFile(final String fileName,
final long fileSize, final String description, final long fileSize, final String description,
NegotiationProgress progress) { final NegotiationProgress progress)
{
if(progress == null) {
throw new IllegalArgumentException("Callback progress cannot be null.");
}
checkTransferThread(); checkTransferThread();
if (isDone() || outputStream != null) { if (isDone() || outputStream != null) {
throw new IllegalStateException( throw new IllegalStateException(
"The negotation process has already" "The negotation process has already"
+ " been attempted for this file transfer"); + " been attempted for this file transfer");
} }
progress.delegate = this; this.callback = progress;
transferThread = new Thread(new Runnable() { transferThread = new Thread(new Runnable() {
public void run() { public void run() {
try { try {
OutgoingFileTransfer.this.outputStream = negotiateStream( OutgoingFileTransfer.this.outputStream = negotiateStream(
fileName, fileSize, description); fileName, fileSize, description);
} catch (XMPPException e) { progress.outputStreamEstablished(OutgoingFileTransfer.this.outputStream);
}
catch (XMPPException e) {
handleXMPPException(e); handleXMPPException(e);
} }
} }
@ -188,6 +193,8 @@ public class OutgoingFileTransfer extends FileTransfer {
* <LI>{@link FileTransfer#isDone()} * <LI>{@link FileTransfer#isDone()}
* </UL> * </UL>
* *
* @param file the file to transfer to the remote entity.
* @param description a description for the file to transfer.
* @throws XMPPException * @throws XMPPException
* If there is an error during the negotiation process or the * If there is an error during the negotiation process or the
* sending of the file. * sending of the file.
@ -249,18 +256,22 @@ public class OutgoingFileTransfer extends FileTransfer {
} }
private void handleXMPPException(XMPPException e) { private void handleXMPPException(XMPPException e) {
setStatus(FileTransfer.Status.error);
XMPPError error = e.getXMPPError(); XMPPError error = e.getXMPPError();
if (error != null) { if (error != null) {
int code = error.getCode(); int code = error.getCode();
if (code == 403) { if (code == 403) {
setStatus(Status.refused); setStatus(Status.refused);
return; return;
} else if (code == 400) { }
else if (code == 400) {
setStatus(Status.error); setStatus(Status.error);
setError(Error.not_acceptable); setError(Error.not_acceptable);
} }
else {
setStatus(FileTransfer.Status.error);
} }
}
setException(e); setException(e);
} }
@ -313,6 +324,32 @@ public class OutgoingFileTransfer extends FileTransfer {
setStatus(Status.cancelled); setStatus(Status.cancelled);
} }
@Override
protected boolean updateStatus(Status oldStatus, Status newStatus) {
boolean isUpdated = super.updateStatus(oldStatus, newStatus);
if(callback != null && isUpdated) {
callback.statusUpdated(oldStatus, newStatus);
}
return isUpdated;
}
@Override
protected void setStatus(Status status) {
Status oldStatus = getStatus();
super.setStatus(status);
if(callback != null) {
callback.statusUpdated(oldStatus, status);
}
}
@Override
protected void setException(Exception exception) {
super.setException(exception);
if(callback != null) {
callback.errorEstablishingStream(exception);
}
}
/** /**
* A callback class to retrive the status of an outgoing transfer * A callback class to retrive the status of an outgoing transfer
* negotiation process. * negotiation process.
@ -320,36 +357,31 @@ public class OutgoingFileTransfer extends FileTransfer {
* @author Alexander Wenckus * @author Alexander Wenckus
* *
*/ */
public static class NegotiationProgress { public interface NegotiationProgress {
private OutgoingFileTransfer delegate;
/** /**
* Returns the current status of the negotiation process. * Called when the status changes
* *
* @return Returns the current status of the negotiation process. * @param oldStatus the previous status of the file transfer.
* @param newStatus the new status of the file transfer.
*/ */
public Status getStatus() { void statusUpdated(Status oldStatus, Status newStatus);
if (delegate == null) {
throw new IllegalStateException("delegate not yet set");
}
return delegate.getStatus();
}
/** /**
* Once the negotiation process is completed the output stream can be * Once the negotiation process is completed the output stream can be
* retrieved. * retrieved.
* *
* @return Once the negotiation process is completed the output stream * @param stream the established stream which can be used to transfer the file to the remote
* can be retrieved. * entity
*
*/ */
public OutputStream getOutputStream() { void outputStreamEstablished(OutputStream stream);
if (delegate == null) {
throw new IllegalStateException("delegate not yet set"); /**
} * Called when an exception occurs during the negotiation progress.
return delegate.getOutputStream(); *
} * @param e the exception that occured.
*/
void errorEstablishingStream(Exception e);
} }
} }