AbstractTransfer.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
xnap.net |
![]() |
![]() |
XNap |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Java Napster version x.yz (for current version number as well as for
* additional information see version.txt)
*
* Previous versions of this program were written by Florian Student
* and Michael Ransburg available at www.weblicity.de/jnapster and
* http://www.tux.org/~daneel/content/projects/10.shtml respectively.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package xnap.net;
import xnap.util.*;
import java.beans.*;
import java.io.*;
import java.net.*;
import java.util.*;
public abstract class AbstractTransfer implements Runnable {
//--- Constant(s) ---
public final static int BROADCAST_INTERVAL = 500;
public final static int STATUS_NOT_STARTED = 0;
public final static int STATUS_CONNECTING = 1;
public final static int STATUS_LOCALLY_QUEUED = 2;
public final static int STATUS_REMOTELY_QUEUED = 3;
public final static int STATUS_DOWNLOADING = 4;
public final static int STATUS_FINISHED = 5;
public final static int STATUS_FAILED = 6;
public final static int STATUS_INCOMPLETE = 7;
public final static int STATUS_ABORTED = 8;
public final static int STATUS_UPLOADING = 9;
public final static int STATUS_SEARCHING = 10;
private final static String[] STATUS_MSGS = {
"", "connecting...", "locally queued", "remotelly queued",
"downloading...", "finished", "failed", "incomplete", "aborted",
"uploading...", "searching"
};
//--- Data field(s) ---
protected transient PropertyChangeSupport propertyChange
= new PropertyChangeSupport(this);
protected boolean die = false;
// the index into transfertablemodel
protected long bytesTransferred = -1;
protected long currentRate = -1;
protected File file = null;
protected String filename = null;
protected long filesize = -1;
protected long offset = 0;
protected String response;
protected long transferStartTime = -1;
protected int status = STATUS_NOT_STARTED;
protected String user = null;
private long lastElapsedTime = 0;
private long lastBytesTransferred = 0;
//--- Constructor(s) ---
//--- Method(s) ---
public boolean isFinished()
{
status = getStatus();
return ((status == STATUS_FINISHED) || (status == STATUS_FAILED)
|| (status == STATUS_ABORTED) || (status == STATUS_INCOMPLETE));
}
public boolean isRunning()
{
status = getStatus();
return ((status == STATUS_CONNECTING)
|| (status == STATUS_LOCALLY_QUEUED)
|| (status == STATUS_REMOTELY_QUEUED)
|| (status == STATUS_DOWNLOADING)
|| (status == STATUS_UPLOADING));
}
public long getBytesTransferred() {
return bytesTransferred + offset;
}
public long getCurrentRate() {
return currentRate;
}
public void setCurrentRate(long newValue)
{
long oldValue = currentRate;
currentRate = newValue;
firePropertyChange("currentRate", new Long(oldValue),
new Long(newValue));
}
public long getElapsedTime()
{
return System.currentTimeMillis() - transferStartTime;
}
public String getFilename()
{
return filename;
}
public File getFile()
{
return file;
}
public void setFilename(String newValue)
{
String oldValue = filename;
filename = newValue;
firePropertyChange("filename", oldValue, newValue);
}
public long getFilesize()
{
return filesize;
}
public void setFilesize(long newValue)
{
long oldValue = currentRate;
filesize = newValue;
firePropertyChange("filesize", new Long(oldValue),
new Long(newValue));
}
public int getStatus()
{
return status;
}
public void setStatus(int newValue)
{
setStatus(newValue, null);
}
public void setStatus(int newValue, String newResponse)
{
/* be sure to have the most current status set */
status = getStatus();
// work around for ugly race conditions
if (status == STATUS_ABORTED
&& (newValue != STATUS_NOT_STARTED
&& newValue != STATUS_CONNECTING
&& newValue != STATUS_SEARCHING)) {
Debug.log("abstract transfer: uhh, fix those race conditions");
return;
}
response = newResponse;
int oldValue = status;
status = newValue;
firePropertyChange("status", new Integer(oldValue),
new Integer(newValue));
}
public String getStatusText() {
if (response == null)
return STATUS_MSGS[getStatus()];
else
return response;
}
public String getShortStatusText() {
return STATUS_MSGS[getStatus()];
}
public String getUser() {
return user;
}
public void setUser(String newValue)
{
String oldValue = user;
user = newValue;
firePropertyChange("user", oldValue, newValue);
}
public synchronized void addPropertyChangeListener(PropertyChangeListener l)
{
propertyChange.addPropertyChangeListener(l);
}
public void firePropertyChange(String propertyName, Object oldValue,
Object newValue)
{
propertyChange.firePropertyChange(propertyName, oldValue, newValue);
}
public synchronized void removePropertyChangeListener(PropertyChangeListener l)
{
propertyChange.removePropertyChangeListener(l);
}
public void abort() {
die = true;
if (!isFinished())
setStatus(STATUS_ABORTED);
}
abstract public void run();
public void start() {
die = false;
(new Thread(this)).start();
}
protected boolean recalculateRate() {
long deltaTime = System.currentTimeMillis()*2 - lastElapsedTime;
long deltaBytes = bytesTransferred - lastBytesTransferred;
if (deltaTime > BROADCAST_INTERVAL) {
lastElapsedTime += deltaTime;
lastBytesTransferred = bytesTransferred;
setCurrentRate((deltaBytes * 2000) / deltaTime);
return true;
}
return false;
}
protected void startTransfer()
{
transferStartTime = System.currentTimeMillis();
}
}
The table below shows all metrics for AbstractTransfer.java.




