1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-23 02:58:00 +01:00

File transfer progress was not being rounded correctly. SMACK-127

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@3598 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-03-20 17:59:01 +00:00 committed by alex
parent 3709f51db0
commit 422317e238
4 changed files with 81 additions and 62 deletions

View file

@ -28,9 +28,9 @@ import org.jivesoftware.smack.XMPPException;
/** /**
* Contains the generic file information and progress related to a particular * Contains the generic file information and progress related to a particular
* file transfer. * file transfer.
* *
* @author Alexander Wenckus * @author Alexander Wenckus
* *
*/ */
public abstract class FileTransfer { public abstract class FileTransfer {
@ -44,6 +44,8 @@ public abstract class FileTransfer {
private org.jivesoftware.smackx.filetransfer.FileTransfer.Status status; private org.jivesoftware.smackx.filetransfer.FileTransfer.Status status;
private final Object statusMonitor = new Object();
protected FileTransferNegotiator negotiator; protected FileTransferNegotiator negotiator;
protected String streamID; protected String streamID;
@ -74,7 +76,7 @@ public abstract class FileTransfer {
/** /**
* Returns the size of the file being transfered. * Returns the size of the file being transfered.
* *
* @return Returns the size of the file being transfered. * @return Returns the size of the file being transfered.
*/ */
public long getFileSize() { public long getFileSize() {
@ -83,7 +85,7 @@ public abstract class FileTransfer {
/** /**
* Returns the name of the file being transfered. * Returns the name of the file being transfered.
* *
* @return Returns the name of the file being transfered. * @return Returns the name of the file being transfered.
*/ */
public String getFileName() { public String getFileName() {
@ -92,7 +94,7 @@ public abstract class FileTransfer {
/** /**
* Returns the local path of the file. * Returns the local path of the file.
* *
* @return Returns the local path of the file. * @return Returns the local path of the file.
*/ */
public String getFilePath() { public String getFilePath() {
@ -101,7 +103,7 @@ public abstract class FileTransfer {
/** /**
* Returns the JID of the peer for this file transfer. * Returns the JID of the peer for this file transfer.
* *
* @return Returns the JID of the peer for this file transfer. * @return Returns the JID of the peer for this file transfer.
*/ */
public String getPeer() { public String getPeer() {
@ -110,21 +112,21 @@ public abstract class FileTransfer {
/** /**
* Returns the progress of the file transfer as a number between 0 and 1. * Returns the progress of the file transfer as a number between 0 and 1.
* *
* @return Returns the progress of the file transfer as a number between 0 * @return Returns the progress of the file transfer as a number between 0
* and 1. * and 1.
*/ */
public double getProgress() { public double getProgress() {
if(amountWritten == 0) { if (amountWritten <= 0 || fileSize <= 0) {
return 0; return 0;
} }
return amountWritten / fileSize; return (double) amountWritten / (double) fileSize;
} }
/** /**
* Returns true if the transfer has been cancled, if it has stopped because * Returns true if the transfer has been cancled, 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 cancled, if it has stopped
* because of a an error, or the transfer completed succesfully. * because of a an error, or the transfer completed succesfully.
*/ */
@ -135,7 +137,7 @@ public abstract class FileTransfer {
/** /**
* Retuns the current status of the file transfer. * Retuns the current status of the file transfer.
* *
* @return Retuns the current status of the file transfer. * @return Retuns the current status of the file transfer.
*/ */
public Status getStatus() { public Status getStatus() {
@ -150,7 +152,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.
* *
* @return Returns the type of error that occured if one has occured. * @return Returns the type of error that occured if one has occured.
*/ */
public Error getError() { public Error getError() {
@ -160,7 +162,7 @@ public abstract class FileTransfer {
/** /**
* If an exception occurs asynchronously it will be stored for later * If an exception occurs asynchronously it will be stored for later
* retrival. If there is an error there maybe an exception set. * retrival. If there is an error there maybe an exception set.
* *
* @return The exception that occured or null if there was no exception. * @return The exception that occured or null if there was no exception.
* @see #getError() * @see #getError()
*/ */
@ -178,13 +180,25 @@ public abstract class FileTransfer {
} }
protected final void setStatus(Status status) { protected final void setStatus(Status status) {
this.status = status; synchronized (statusMonitor) {
} this.status = status;
}
}
protected final boolean updateStatus(Status oldStatus, Status newStatus) {
synchronized (statusMonitor) {
if (oldStatus != status) {
return false;
}
status = newStatus;
return true;
}
}
protected void writeToStream(final InputStream in, final OutputStream out) protected void writeToStream(final InputStream in, final OutputStream out)
throws XMPPException { throws XMPPException {
final byte[] b = new byte[1000]; final byte[] b = new byte[1000];
int count = 0; int count;
amountWritten = 0; amountWritten = 0;
try { try {
count = in.read(b); count = in.read(b);
@ -217,30 +231,37 @@ public abstract class FileTransfer {
// equal // equal
if (!getStatus().equals(Status.CANCLED) && getError() == Error.NONE if (!getStatus().equals(Status.CANCLED) && getError() == Error.NONE
&& amountWritten != fileSize) { && amountWritten != fileSize) {
setStatus(Status.ERROR);
this.error = Error.CONNECTION; this.error = Error.CONNECTION;
} }
} }
/** /**
* A class to represent the current status of the file transfer. * A class to represent the current status of the file transfer.
* *
* @author Alexander Wenckus * @author Alexander Wenckus
* *
*/ */
public static class Status { public static class 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(); public static final Status ERROR = new Status();
/** /**
* The initial status of the file transfer.
*/
public static final Status INITIAL = new Status();
/**
* The file transfer is being negotiated with the peer. The party * The file transfer is being negotiated with the peer. The party
* recieving the file has the option to accept or refuse a file transfer * recieving the file has the option to accept or refuse a file transfer
* 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(); public static final Status NEGOTIATING_TRANSFER = new Status();
@ -255,7 +276,7 @@ public abstract class FileTransfer {
* 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(); public static final Status NEGOTIATING_STREAM = new Status();
@ -269,7 +290,7 @@ public abstract class FileTransfer {
/** /**
* The transfer is in progress. * The transfer is in progress.
* *
* @see FileTransfer#getProgress() * @see FileTransfer#getProgress()
*/ */
public static final Status IN_PROGRESS = new Status(); public static final Status IN_PROGRESS = new Status();
@ -338,7 +359,7 @@ public abstract class FileTransfer {
/** /**
* Returns a String representation of this error. * Returns a String representation of this error.
* *
* @return Returns a String representation of this error. * @return Returns a String representation of this error.
*/ */
public String getMessage() { public String getMessage() {

View file

@ -149,7 +149,7 @@ public class FileTransferNegotiator {
* @param type The iq type of the packet. * @param type The iq type of the packet.
* @return The created IQ packet. * @return The created IQ packet.
*/ */
protected static IQ createIQ(final String ID, final String to, public static IQ createIQ(final String ID, final String to,
final String from, final IQ.Type type) { final String from, final IQ.Type type) {
IQ iqPacket = new IQ() { IQ iqPacket = new IQ() {
public String getChildElementXML() { public String getChildElementXML() {

View file

@ -19,23 +19,18 @@
*/ */
package org.jivesoftware.smackx.filetransfer; package org.jivesoftware.smackx.filetransfer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
import java.io.*;
/** /**
* Handles the sending of a file to another user. File transfer's in jabber have * Handles the sending of a file to another user. File transfer's in jabber have
* several steps and there are several methods in this class that handle these * several steps and there are several methods in this class that handle these
* steps differently. * steps differently.
* *
* @author Alexander Wenckus * @author Alexander Wenckus
* *
*/ */
public class OutgoingFileTransfer extends FileTransfer { public class OutgoingFileTransfer extends FileTransfer {
@ -44,7 +39,7 @@ public class OutgoingFileTransfer extends FileTransfer {
/** /**
* 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.
@ -56,7 +51,7 @@ public class OutgoingFileTransfer extends FileTransfer {
/** /**
* Sets the time in milliseconds after which the file transfer negotiation * Sets the time in milliseconds after which the file transfer negotiation
* process will timeout if the other user has not responded. * process will timeout if the other user has not responded.
* *
* @param responseTimeout * @param responseTimeout
* The timeout time in milliseconds. * The timeout time in milliseconds.
*/ */
@ -86,7 +81,7 @@ public class OutgoingFileTransfer extends FileTransfer {
* Returns the output stream connected to the peer to transfer the file. It * Returns the output stream connected to the peer to transfer the file. It
* is only available after it has been succesfully negotiated by the * is only available after it has been succesfully negotiated by the
* {@link StreamNegotiator}. * {@link StreamNegotiator}.
* *
* @return Returns the output stream connected to the peer to transfer the * @return Returns the output stream connected to the peer to transfer the
* file. * file.
*/ */
@ -101,7 +96,7 @@ public class OutgoingFileTransfer extends FileTransfer {
/** /**
* This method handles the negotiation of the file transfer and the stream, * This method handles the negotiation of the file transfer and the stream,
* it only returns the created stream after the negotiation has been completed. * it only returns the created stream after the negotiation has been completed.
* *
* @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
* preferable for this name to have an extension as it will be * preferable for this name to have an extension as it will be
@ -138,7 +133,7 @@ public class OutgoingFileTransfer extends FileTransfer {
* {@link NegotiationProgress} callback. When the negotiation process is * {@link NegotiationProgress} callback. When the negotiation process is
* complete the OutputStream can be retrieved from the callback via the * complete the OutputStream can be retrieved from the callback via the
* {@link NegotiationProgress#getOutputStream()} method. * {@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
* preferable for this name to have an extension as it will be * preferable for this name to have an extension as it will be
@ -186,13 +181,13 @@ 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:
* *
* <UL> * <UL>
* <LI>{@link FileTransfer#getStatus()} * <LI>{@link FileTransfer#getStatus()}
* <LI>{@link FileTransfer#getProgress()} * <LI>{@link FileTransfer#getProgress()}
* <LI>{@link FileTransfer#isDone()} * <LI>{@link FileTransfer#isDone()}
* </UL> * </UL>
* *
* @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.
@ -219,10 +214,9 @@ public class OutgoingFileTransfer extends FileTransfer {
return; return;
} }
if (!getStatus().equals(Status.NEGOTIATED)) { if (!updateStatus(Status.NEGOTIATED, Status.IN_PROGRESS)) {
return; return;
} }
setStatus(Status.IN_PROGRESS);
InputStream inputStream = null; InputStream inputStream = null;
try { try {
@ -244,12 +238,11 @@ public class OutgoingFileTransfer extends FileTransfer {
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
/* Do Nothing */
} }
} }
if (getStatus().equals(Status.IN_PROGRESS)) { updateStatus(Status.IN_PROGRESS, FileTransfer.Status.COMPLETE);
setStatus(FileTransfer.Status.COMPLETE);
} }
}
}, "File Transfer " + streamID); }, "File Transfer " + streamID);
transferThread.start(); transferThread.start();
@ -279,7 +272,7 @@ public class OutgoingFileTransfer extends FileTransfer {
* Note: This method is only useful when the {@link #sendFile(File, String)} * Note: This method is only useful when the {@link #sendFile(File, String)}
* method is called, as it is the only method that actualy transmits the * method is called, as it is the only method that actualy transmits the
* file. * file.
* *
* @return Returns the amount of bytes that have been sent for the file * @return Returns the amount of bytes that have been sent for the file
* transfer. Or -1 if the file transfer has not started. * transfer. Or -1 if the file transfer has not started.
*/ */
@ -291,7 +284,9 @@ public class OutgoingFileTransfer extends FileTransfer {
String description) throws XMPPException { String description) throws XMPPException {
// Negotiate the file transfer profile // Negotiate the file transfer profile
setStatus(Status.NEGOTIATING_TRANSFER); if (!updateStatus(Status.INITIAL, Status.NEGOTIATING_TRANSFER)) {
throw new XMPPException("Illegal state change");
}
StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer( StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer(
getPeer(), streamID, fileName, fileSize, description, getPeer(), streamID, fileName, fileSize, description,
RESPONSE_TIMEOUT); RESPONSE_TIMEOUT);
@ -302,19 +297,16 @@ public class OutgoingFileTransfer extends FileTransfer {
return null; return null;
} }
if (!getStatus().equals(Status.NEGOTIATING_TRANSFER)) { // Negotiate the stream
return null; if (!updateStatus(Status.NEGOTIATING_TRANSFER, Status.NEGOTIATING_STREAM)) {
} throw new XMPPException("Illegal state change");
}
// Negotiate the stream
setStatus(Status.NEGOTIATING_STREAM);
outputStream = streamNegotiator.createOutgoingStream(streamID, outputStream = streamNegotiator.createOutgoingStream(streamID,
initiator, getPeer()); initiator, getPeer());
if (!getStatus().equals(Status.NEGOTIATING_STREAM)) {
return null; if (!updateStatus(Status.NEGOTIATING_STREAM, Status.NEGOTIATED)) {
throw new XMPPException("Illegal state change");
} }
setStatus(Status.NEGOTIATED);
return outputStream; return outputStream;
} }
@ -325,9 +317,9 @@ public class OutgoingFileTransfer extends FileTransfer {
/** /**
* 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 static class NegotiationProgress {
@ -335,7 +327,7 @@ public class OutgoingFileTransfer extends FileTransfer {
/** /**
* Returns the current status of the negotiation process. * Returns the current status of the negotiation process.
* *
* @return Returns the current status of the negotiation process. * @return Returns the current status of the negotiation process.
*/ */
public Status getStatus() { public Status getStatus() {
@ -348,10 +340,10 @@ public class OutgoingFileTransfer extends FileTransfer {
/** /**
* 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 * @return Once the negotiation process is completed the output stream
* can be retrieved. * can be retrieved.
* *
*/ */
public OutputStream getOutputStream() { public OutputStream getOutputStream() {
if (delegate == null) { if (delegate == null) {

View file

@ -244,6 +244,12 @@ public class Bytestream extends IQ {
buf.append(">"); buf.append(">");
if (getUsedHost() != null) if (getUsedHost() != null)
buf.append(getUsedHost().toXML()); buf.append(getUsedHost().toXML());
// A result from the server can also contain stream hosts
else if (countStreamHosts() > 0) {
for (Iterator it = getStreamHosts().iterator(); it.hasNext();) {
buf.append(((StreamHost) it.next()).toXML());
}
}
} }
else { else {
return null; return null;