1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 16:22:06 +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;
}
protected final void setStatus(Status status) {
protected void setStatus(Status status) {
synchronized (statusMonitor) {
this.status = status;
}
}
protected final boolean updateStatus(Status oldStatus, Status newStatus) {
protected boolean updateStatus(Status oldStatus, Status newStatus) {
synchronized (statusMonitor) {
if (oldStatus != status) {
return false;

View file

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