mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 22:32: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:
parent
469b5c2a0a
commit
694c7863bd
2 changed files with 85 additions and 53 deletions
|
@ -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;
|
||||||
|
|
|
@ -35,18 +35,19 @@ 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
|
||||||
* negotiation process will timeout if the other user has not responded.
|
* negotiation process will timeout if the other user has not responded.
|
||||||
*
|
*
|
||||||
* @return Returns the time in milliseconds after which the file transfer
|
* @return Returns the time in milliseconds after which the file transfer
|
||||||
* negotiation process will timeout if the remote user has not
|
* negotiation process will timeout if the remote user has not
|
||||||
* responded.
|
* responded.
|
||||||
*/
|
*/
|
||||||
public static int getResponseTimeout() {
|
public static int getResponseTimeout() {
|
||||||
return RESPONSE_TIMEOUT;
|
return RESPONSE_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the time in milliseconds after which the file transfer negotiation
|
* Sets the time in milliseconds after which the file transfer negotiation
|
||||||
|
@ -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)
|
||||||
checkTransferThread();
|
{
|
||||||
|
if(progress == null) {
|
||||||
|
throw new IllegalArgumentException("Callback progress cannot be null.");
|
||||||
|
}
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -177,7 +182,7 @@ public class OutgoingFileTransfer extends FileTransfer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method handles the stream negotiation process and transmits the file
|
* This method handles the stream negotiation process and transmits the file
|
||||||
* to the remote user. It returns immediatly and the progress of the file
|
* to the remote user. It returns immediatly and the progress of the file
|
||||||
* transfer can be monitored through several methods:
|
* transfer can be monitored through several methods:
|
||||||
|
@ -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,19 +256,23 @@ 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 {
|
||||||
setException(e);
|
setStatus(FileTransfer.Status.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -313,43 +324,64 @@ 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.
|
||||||
*
|
*
|
||||||
* @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() {
|
*/
|
||||||
if (delegate == null) {
|
void statusUpdated(Status oldStatus, Status newStatus);
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue