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

@ -44,6 +44,8 @@ public abstract class FileTransfer {
private org.jivesoftware.smackx.filetransfer.FileTransfer.Status status;
private final Object statusMonitor = new Object();
protected FileTransferNegotiator negotiator;
protected String streamID;
@ -115,10 +117,10 @@ public abstract class FileTransfer {
* and 1.
*/
public double getProgress() {
if(amountWritten == 0) {
if (amountWritten <= 0 || fileSize <= 0) {
return 0;
}
return amountWritten / fileSize;
return (double) amountWritten / (double) fileSize;
}
/**
@ -178,13 +180,25 @@ public abstract class FileTransfer {
}
protected final void setStatus(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)
throws XMPPException {
final byte[] b = new byte[1000];
int count = 0;
int count;
amountWritten = 0;
try {
count = in.read(b);
@ -217,6 +231,7 @@ public abstract class FileTransfer {
// equal
if (!getStatus().equals(Status.CANCLED) && getError() == Error.NONE
&& amountWritten != fileSize) {
setStatus(Status.ERROR);
this.error = Error.CONNECTION;
}
}
@ -228,6 +243,7 @@ public abstract class FileTransfer {
*
*/
public static class Status {
/**
* An error occured during the transfer.
*
@ -235,6 +251,11 @@ public abstract class FileTransfer {
*/
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
* recieving the file has the option to accept or refuse a file transfer

View File

@ -149,7 +149,7 @@ public class FileTransferNegotiator {
* @param type The iq type of the 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) {
IQ iqPacket = new IQ() {
public String getChildElementXML() {

View File

@ -19,16 +19,11 @@
*/
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.packet.XMPPError;
import java.io.*;
/**
* 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
@ -219,10 +214,9 @@ public class OutgoingFileTransfer extends FileTransfer {
return;
}
if (!getStatus().equals(Status.NEGOTIATED)) {
if (!updateStatus(Status.NEGOTIATED, Status.IN_PROGRESS)) {
return;
}
setStatus(Status.IN_PROGRESS);
InputStream inputStream = null;
try {
@ -244,11 +238,10 @@ public class OutgoingFileTransfer extends FileTransfer {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
/* Do Nothing */
}
}
if (getStatus().equals(Status.IN_PROGRESS)) {
setStatus(FileTransfer.Status.COMPLETE);
}
updateStatus(Status.IN_PROGRESS, FileTransfer.Status.COMPLETE);
}
}, "File Transfer " + streamID);
@ -291,7 +284,9 @@ public class OutgoingFileTransfer extends FileTransfer {
String description) throws XMPPException {
// 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(
getPeer(), streamID, fileName, fileSize, description,
RESPONSE_TIMEOUT);
@ -302,19 +297,16 @@ public class OutgoingFileTransfer extends FileTransfer {
return null;
}
if (!getStatus().equals(Status.NEGOTIATING_TRANSFER)) {
return null;
}
// Negotiate the stream
setStatus(Status.NEGOTIATING_STREAM);
if (!updateStatus(Status.NEGOTIATING_TRANSFER, Status.NEGOTIATING_STREAM)) {
throw new XMPPException("Illegal state change");
}
outputStream = streamNegotiator.createOutgoingStream(streamID,
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;
}

View File

@ -244,6 +244,12 @@ public class Bytestream extends IQ {
buf.append(">");
if (getUsedHost() != null)
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 {
return null;