1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-23 11:07:57 +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

@ -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;
@ -115,10 +117,10 @@ public abstract class FileTransfer {
* 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;
} }
/** /**
@ -178,13 +180,25 @@ public abstract class FileTransfer {
} }
protected final void setStatus(Status status) { protected final void setStatus(Status status) {
synchronized (statusMonitor) {
this.status = status; 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,6 +231,7 @@ 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;
} }
} }
@ -228,6 +243,7 @@ public abstract class FileTransfer {
* *
*/ */
public static class Status { public static class Status {
/** /**
* An error occured during the transfer. * An error occured during the transfer.
* *
@ -235,6 +251,11 @@ public abstract class FileTransfer {
*/ */
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

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,16 +19,11 @@
*/ */
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
@ -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,11 +238,10 @@ 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);
@ -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)) {
return null;
}
// Negotiate the stream // Negotiate the stream
if (!updateStatus(Status.NEGOTIATING_TRANSFER, Status.NEGOTIATING_STREAM)) {
setStatus(Status.NEGOTIATING_STREAM); throw new XMPPException("Illegal state change");
}
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;
} }

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;