1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-27 14:47:59 +01:00

1) FileTransfer#isDone() will now return true on refused. SMACK-158

2) Learned to spell.
3) Changed to enums.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@5163 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-09-01 17:38:53 +00:00 committed by alex
parent 8576a13de7
commit 39662d3b0f
3 changed files with 59 additions and 66 deletions

View file

@ -42,7 +42,7 @@ public abstract class FileTransfer {
private String peer;
private org.jivesoftware.smackx.filetransfer.FileTransfer.Status status = Status.INITIAL;
private Status status = Status.initial;
private final Object statusMonitor = new Object();
@ -129,15 +129,15 @@ public abstract class FileTransfer {
}
/**
* Returns true if the transfer has been cancled, if it has stopped because
* Returns true if the transfer has been cancelled, if it has stopped because
* of a an error, or the transfer completed succesfully.
*
* @return Returns true if the transfer has been cancled, if it has stopped
* @return Returns true if the transfer has been cancelled, if it has stopped
* because of a an error, or the transfer completed succesfully.
*/
public boolean isDone() {
return status == Status.CANCLED || status == Status.ERROR
|| status == Status.COMPLETE;
return status == Status.cancelled || status == Status.error
|| status == Status.complete || status == Status.refused;
}
/**
@ -154,7 +154,7 @@ public abstract class FileTransfer {
}
/**
* When {@link #getStatus()} returns that there was an {@link Status#ERROR}
* When {@link #getStatus()} returns that there was an {@link Status#error}
* during the transfer, the type of error can be retrieved through this
* method.
*
@ -223,13 +223,13 @@ public abstract class FileTransfer {
} catch (IOException e) {
throw new XMPPException("error reading from input stream", e);
}
} while (count != -1 && !getStatus().equals(Status.CANCLED));
} while (count != -1 && !getStatus().equals(Status.cancelled));
// the connection was likely terminated abrubtly if these are not equal
if (!getStatus().equals(Status.CANCLED) && getError() == Error.NONE
if (!getStatus().equals(Status.cancelled) && getError() == Error.none
&& amountWritten != fileSize) {
setStatus(Status.ERROR);
this.error = Error.CONNECTION;
setStatus(Status.error);
this.error = Error.connection;
}
}
@ -239,19 +239,19 @@ public abstract class FileTransfer {
* @author Alexander Wenckus
*
*/
public static class Status {
public enum Status {
/**
* An error occured during the transfer.
*
* @see FileTransfer#getError()
*/
public static final Status ERROR = new Status("Error");
error("Error"),
/**
* The initial status of the file transfer.
*/
public static final Status INITIAL = new Status("Initial");
initial("Initial"),
/**
* The file transfer is being negotiated with the peer. The party
@ -259,48 +259,48 @@ public abstract class FileTransfer {
* request. If they accept, then the process of stream negotiation will
* begin. If they refuse the file will not be transfered.
*
* @see #NEGOTIATING_STREAM
* @see #negotiating_stream
*/
public static final Status NEGOTIATING_TRANSFER = new Status("Negotiating Transfer");
negotiating_transfer("Negotiating Transfer"),
/**
* The peer has refused the file transfer request halting the file
* transfer negotiation process.
*/
public static final Status REFUSED = new Status("Refused");
refused("Refused"),
/**
* The stream to transfer the file is being negotiated over the chosen
* stream type. After the stream negotiating process is complete the
* status becomes negotiated.
*
* @see #NEGOTIATED
* @see #negotiated
*/
public static final Status NEGOTIATING_STREAM = new Status("Negotiating Stream");
negotiating_stream("Negotiating Stream"),
/**
* After the stream negotitation has completed the intermediate state
* between the time when the negotiation is finished and the actual
* transfer begins.
*/
public static final Status NEGOTIATED = new Status("Negotiated");
negotiated("Negotiated"),
/**
* The transfer is in progress.
*
* @see FileTransfer#getProgress()
*/
public static final Status IN_PROGRESS = new Status("In Progress");
in_progress("In Progress"),
/**
* The transfer has completed successfully.
*/
public static final Status COMPLETE = new Status("Complete");
complete("Complete"),
/**
* The file transfer was canceled
*/
public static final Status CANCLED = new Status("Cancled");
cancelled("Cancelled");
private String status;
@ -321,42 +321,37 @@ public abstract class FileTransfer {
return amountWritten;
}
public static class Error {
public enum Error {
/**
* No error
*/
public static final Error NONE = new Error("No error");
none("No error"),
/**
* The peer did not find any of the provided stream mechanisms
* acceptable.
*/
public static final Error NOT_ACCEPTABLE = new Error(
"The peer did not find any of the provided stream mechanisms acceptable.");
not_acceptable("The peer did not find any of the provided stream mechanisms acceptable."),
/**
* The provided file to transfer does not exist or could not be read.
*/
public static final Error BAD_FILE = new Error(
"The provided file to transfer does not exist or could not be read.");
bad_file("The provided file to transfer does not exist or could not be read."),
/**
* The remote user did not respond or the connection timed out.
*/
public static final Error NO_RESPONSE = new Error(
"The remote user did not respond or the connection timed out.");
no_response("The remote user did not respond or the connection timed out."),
/**
* An error occured over the socket connected to send the file.
*/
public static final Error CONNECTION = new Error(
"An error occured over the socket connected to send the file.");
connection("An error occured over the socket connected to send the file."),
/**
* An error occured while sending or recieving the file
*/
protected static final Error STREAM = new Error(
"An error occured while sending or recieving the file");
stream("An error occured while sending or recieving the file.");
private final String msg;

View file

@ -47,8 +47,6 @@ public class IncomingFileTransfer extends FileTransfer {
private FileTransferRequest recieveRequest;
private Thread transferThread;
private InputStream inputStream;
protected IncomingFileTransfer(FileTransferRequest request,
@ -119,7 +117,7 @@ public class IncomingFileTransfer extends FileTransfer {
throw new IllegalArgumentException("File cannot be null");
}
transferThread = new Thread(new Runnable() {
Thread transferThread = new Thread(new Runnable() {
public void run() {
try {
inputStream = negotiateStream();
@ -132,22 +130,22 @@ public class IncomingFileTransfer extends FileTransfer {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
setStatus(Status.IN_PROGRESS);
setStatus(Status.in_progress);
writeToStream(inputStream, outputStream);
}
catch (XMPPException e) {
setStatus(FileTransfer.Status.ERROR);
setError(Error.STREAM);
setStatus(Status.error);
setError(Error.stream);
setException(e);
}
catch (FileNotFoundException e) {
setStatus(FileTransfer.Status.ERROR);
setError(Error.BAD_FILE);
setStatus(Status.error);
setError(Error.bad_file);
setException(e);
}
if (getStatus().equals(Status.IN_PROGRESS))
setStatus(Status.COMPLETE);
if (getStatus().equals(Status.in_progress))
setStatus(Status.complete);
try {
if (inputStream != null) {
inputStream.close();
@ -157,31 +155,31 @@ public class IncomingFileTransfer extends FileTransfer {
}
}
catch (IOException e) {
/** We need to do something here **/
}
}
}, "File Transfer " + streamID);
transferThread.start();
}
private void handleXMPPException(XMPPException e) {
setStatus(FileTransfer.Status.ERROR);
setStatus(FileTransfer.Status.error);
setException(e);
}
private InputStream negotiateStream() throws XMPPException {
setStatus(Status.NEGOTIATING_TRANSFER);
setStatus(Status.negotiating_transfer);
StreamNegotiator streamNegotiator = negotiator
.selectStreamNegotiator(recieveRequest);
setStatus(Status.NEGOTIATING_STREAM);
setStatus(Status.negotiating_stream);
InputStream inputStream = streamNegotiator
.createIncomingStream(recieveRequest.getStreamInitiation());
setStatus(Status.NEGOTIATED);
setStatus(Status.negotiated);
return inputStream;
}
public void cancel() {
setStatus(Status.CANCLED);
setStatus(Status.cancelled);
}
}

View file

@ -86,7 +86,7 @@ public class OutgoingFileTransfer extends FileTransfer {
* file.
*/
protected OutputStream getOutputStream() {
if (getStatus().equals(FileTransfer.Status.NEGOTIATED)) {
if (getStatus().equals(FileTransfer.Status.negotiated)) {
return outputStream;
} else {
return null;
@ -214,7 +214,7 @@ public class OutgoingFileTransfer extends FileTransfer {
return;
}
if (!updateStatus(Status.NEGOTIATED, Status.IN_PROGRESS)) {
if (!updateStatus(Status.negotiated, Status.in_progress)) {
return;
}
@ -223,11 +223,11 @@ public class OutgoingFileTransfer extends FileTransfer {
inputStream = new FileInputStream(file);
writeToStream(inputStream, outputStream);
} catch (FileNotFoundException e) {
setStatus(FileTransfer.Status.ERROR);
setError(Error.BAD_FILE);
setStatus(FileTransfer.Status.error);
setError(Error.bad_file);
setException(e);
} catch (XMPPException e) {
setStatus(FileTransfer.Status.ERROR);
setStatus(FileTransfer.Status.error);
setException(e);
} finally {
try {
@ -241,7 +241,7 @@ public class OutgoingFileTransfer extends FileTransfer {
/* Do Nothing */
}
}
updateStatus(Status.IN_PROGRESS, FileTransfer.Status.COMPLETE);
updateStatus(Status.in_progress, FileTransfer.Status.complete);
}
}, "File Transfer " + streamID);
@ -249,16 +249,16 @@ public class OutgoingFileTransfer extends FileTransfer {
}
private void handleXMPPException(XMPPException e) {
setStatus(FileTransfer.Status.ERROR);
setStatus(FileTransfer.Status.error);
XMPPError error = e.getXMPPError();
if (error != null) {
int code = error.getCode();
if (code == 403) {
setStatus(Status.REFUSED);
setStatus(Status.refused);
return;
} else if (code == 400) {
setStatus(Status.ERROR);
setError(Error.NOT_ACCEPTABLE);
setStatus(Status.error);
setError(Error.not_acceptable);
}
}
setException(e);
@ -283,7 +283,7 @@ public class OutgoingFileTransfer extends FileTransfer {
String description) throws XMPPException {
// Negotiate the file transfer profile
if (!updateStatus(Status.INITIAL, Status.NEGOTIATING_TRANSFER)) {
if (!updateStatus(Status.initial, Status.negotiating_transfer)) {
throw new XMPPException("Illegal state change");
}
StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer(
@ -291,26 +291,26 @@ public class OutgoingFileTransfer extends FileTransfer {
RESPONSE_TIMEOUT);
if (streamNegotiator == null) {
setStatus(Status.ERROR);
setError(Error.NO_RESPONSE);
setStatus(Status.error);
setError(Error.no_response);
return null;
}
// Negotiate the stream
if (!updateStatus(Status.NEGOTIATING_TRANSFER, Status.NEGOTIATING_STREAM)) {
if (!updateStatus(Status.negotiating_transfer, Status.negotiating_stream)) {
throw new XMPPException("Illegal state change");
}
outputStream = streamNegotiator.createOutgoingStream(streamID,
initiator, getPeer());
if (!updateStatus(Status.NEGOTIATING_STREAM, Status.NEGOTIATED)) {
if (!updateStatus(Status.negotiating_stream, Status.negotiated)) {
throw new XMPPException("Illegal state change");
}
return outputStream;
}
public void cancel() {
setStatus(Status.CANCLED);
setStatus(Status.cancelled);
}
/**