1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 04:22:05 +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 String peer;
private org.jivesoftware.smackx.filetransfer.FileTransfer.Status status = Status.INITIAL; private Status status = Status.initial;
private final Object statusMonitor = new Object(); 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. * 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. * because of a an error, or the transfer completed succesfully.
*/ */
public boolean isDone() { public boolean isDone() {
return status == Status.CANCLED || status == Status.ERROR return status == Status.cancelled || status == Status.error
|| status == Status.COMPLETE; || 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 * during the transfer, the type of error can be retrieved through this
* method. * method.
* *
@ -223,13 +223,13 @@ public abstract class FileTransfer {
} catch (IOException e) { } catch (IOException e) {
throw new XMPPException("error reading from input stream", 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 // 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) { && amountWritten != fileSize) {
setStatus(Status.ERROR); setStatus(Status.error);
this.error = Error.CONNECTION; this.error = Error.connection;
} }
} }
@ -239,19 +239,19 @@ public abstract class FileTransfer {
* @author Alexander Wenckus * @author Alexander Wenckus
* *
*/ */
public static class Status { public enum Status {
/** /**
* An error occured during the transfer. * An error occured during the transfer.
* *
* @see FileTransfer#getError() * @see FileTransfer#getError()
*/ */
public static final Status ERROR = new Status("Error"); error("Error"),
/** /**
* The initial status of the file transfer. * 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 * 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 * request. If they accept, then the process of stream negotiation will
* begin. If they refuse the file will not be transfered. * 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 * The peer has refused the file transfer request halting the file
* transfer negotiation process. * 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 * The stream to transfer the file is being negotiated over the chosen
* stream type. After the stream negotiating process is complete the * stream type. After the stream negotiating process is complete the
* status becomes negotiated. * 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 * After the stream negotitation has completed the intermediate state
* between the time when the negotiation is finished and the actual * between the time when the negotiation is finished and the actual
* transfer begins. * transfer begins.
*/ */
public static final Status NEGOTIATED = new Status("Negotiated"); negotiated("Negotiated"),
/** /**
* The transfer is in progress. * The transfer is in progress.
* *
* @see FileTransfer#getProgress() * @see FileTransfer#getProgress()
*/ */
public static final Status IN_PROGRESS = new Status("In Progress"); in_progress("In Progress"),
/** /**
* The transfer has completed successfully. * The transfer has completed successfully.
*/ */
public static final Status COMPLETE = new Status("Complete"); complete("Complete"),
/** /**
* The file transfer was canceled * The file transfer was canceled
*/ */
public static final Status CANCLED = new Status("Cancled"); cancelled("Cancelled");
private String status; private String status;
@ -321,42 +321,37 @@ public abstract class FileTransfer {
return amountWritten; return amountWritten;
} }
public static class Error { public enum Error {
/** /**
* No 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 * The peer did not find any of the provided stream mechanisms
* acceptable. * acceptable.
*/ */
public static final Error NOT_ACCEPTABLE = new Error( not_acceptable("The peer did not find any of the provided stream mechanisms 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. * The provided file to transfer does not exist or could not be read.
*/ */
public static final Error BAD_FILE = new Error( bad_file("The provided file to transfer does not exist or could not be read."),
"The provided file to transfer does not exist or could not be read.");
/** /**
* The remote user did not respond or the connection timed out. * The remote user did not respond or the connection timed out.
*/ */
public static final Error NO_RESPONSE = new Error( no_response("The remote user did not respond or the connection timed out."),
"The remote user did not respond or the connection timed out.");
/** /**
* An error occured over the socket connected to send the file. * An error occured over the socket connected to send the file.
*/ */
public static final Error CONNECTION = new Error( connection("An error occured over the socket connected to send the file."),
"An error occured over the socket connected to send the file.");
/** /**
* An error occured while sending or recieving the file * An error occured while sending or recieving the file
*/ */
protected static final Error STREAM = new Error( stream("An error occured while sending or recieving the file.");
"An error occured while sending or recieving the file");
private final String msg; private final String msg;

View file

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

View file

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