GiFTDownloadContainer.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.gift.net |
![]() |
![]() |
XNap 3 |
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.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* 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.
*
* 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 org.xnap.plugin.gift.net;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import javax.swing.Action;
import javax.swing.Icon;
import org.xnap.XNap;
import org.xnap.gui.util.IconHelper;
import org.xnap.peer.Peer;
import org.xnap.plugin.Plugin;
import org.xnap.plugin.gift.GiFTPlugin;
import org.xnap.plugin.gift.action.AbstractClearSourcesAction;
import org.xnap.transfer.AbstractDownload;
import org.xnap.transfer.Segment;
import org.xnap.transfer.action.AbstractDeleteAction;
import org.xnap.transfer.action.AbstractFindMoreSourcesAction;
import org.xnap.transfer.action.AbstractTransferAction;
/**
* GiFTDownloadContainer
*
* @author <a href="mailto:taval@users.sf.net">Tammo van Lessen</a>
* @version CVS $Id: GiFTDownloadContainer.java,v 1.6 2004/03/23 19:36:21 vanto Exp $
*/
public class GiFTDownloadContainer extends AbstractDownload {
// FIX: should be initialized in constructor
private String gid = "";
private boolean done = false;
private boolean paused = true;
/**
* The number of bytes that were already transfered when the
* container was constructed.
*/
private GiFTDaemon daemon;
private long offset;
private long totalBytesTransferred;
private String filename;
private String hash;
private long currentRate;
private long filesize;
private String status;
private Hashtable sources;
private LinkedList segments = new LinkedList();
private AbstractTransferAction cancelAction = new CancelAction();
//~ Constructors -----------------------------------------------------------
/**
* Constructor for GiFTDownloadContainer.
*
* @param filename DOCUMENT ME!
* @param hash DOCUMENT ME!
* @param filesize DOCUMENT ME!
* @param transmit DOCUMENT ME!
* @param state DOCUMENT ME!
*/
public GiFTDownloadContainer(GiFTDaemon daemon, String filename,
String hash, long filesize)
{
this.daemon = daemon;
this.filename = filename;
this.filesize = filesize;
this.hash = hash;
this.sources = new Hashtable();
this.status = XNap.tr("Waiting for daemon");
}
/**
*
*/
public void cancel()
{
daemon.changeDownload(this, "cancel");
setStatus(XNap.tr("Cancelling"));
}
/**
* @see xnap.net.ITransferContainer#getFilename()
*/
public String getFilename() {
return filename.substring(filename.lastIndexOf("/") + 1);
}
/**
* @see xnap.net.ITransferContainer#getFilesize()
*/
public long getFilesize() {
return filesize;
}
public Plugin getPlugin()
{
return GiFTPlugin.getInstance();
}
/**
* Invoked by {@link GiFTDaemon} when the download has been deleted.
*/
void deleted()
{
// in case of a successful completion this method will also be called
// so only change the status if we haven't noticed already
if (!done) {
done();
setStatus("Deleted");
}
}
private void done()
{
done = true;
transferStopped();
}
/**
* @see xnap.transfer.Transfer#getActions()
*/
public Action[] getActions(String url)
{
return new Action[] {
new PauseAction(),
cancelAction,
new CancelSourceAction(url),
new FindMoreSourcesAction(),
new ClearSourcesAction(),
};
}
public Action[] getActions()
{
return getActions(null);
}
/**
* @see xnap.transfer.Transfer#getFile()
*/
public File getFile()
{
return null;
}
/**
* @see xnap.transfer.Transfer#getPeer()
*/
public Peer getPeer()
{
return null;
}
public Segment[] getSegments()
{
return (Segment[])segments.toArray(new Segment[0]);
}
/**
* @see xnap.transfer.Transfer#getStatus()
*/
public String getStatus()
{
return status;
}
/**
* @see xnap.transfer.Transfer#getTotalBytesTransferred()
*/
public long getTotalBytesTransferred()
{
return totalBytesTransferred;
}
/**
* @see xnap.transfer.Transfer#isDone()
*/
public boolean isDone()
{
return done;
}
/**
* @see xnap.transfer.Transfer#isRunning()
*/
public boolean isRunning()
{
return !done && !paused;
}
/**
* @see xnap.transfer.AbstractTransfer#getBytesTransferred()
*/
public long getBytesTransferred()
{
return totalBytesTransferred - offset;
}
/**
* @see xnap.transfer.Transfer#getCurrentRate()
*/
public long getCurrentRate()
{
return currentRate;
}
public void markChildren()
{
Enumeration enumeration = sources.elements();
while (enumeration.hasMoreElements()) {
GiFTDownload element = (GiFTDownload) enumeration.nextElement();
element.setMarked(true);
}
}
public void orphanMarkedChildren()
{
Enumeration enumeration = sources.elements();
while (enumeration.hasMoreElements()) {
GiFTDownload element
= (GiFTDownload) enumeration.nextElement();
if (element.isMarked()) {
element.setState("Inactive", null);
}
}
}
/**
*
*/
public void clearOrphaned()
{
Enumeration enumeration = sources.elements();
while (enumeration.hasMoreElements()) {
GiFTDownload element = (GiFTDownload) enumeration.nextElement();
if (element.getStatus().equals("Inactive")) {
sources.remove(element);
remove(element);
}
}
}
public GiFTDownload getSourceByURL(String url)
{
return (GiFTDownload)sources.get(url);
}
public GiFTDownload addSource(GiFTUser user, String url)
{
GiFTDownload d = new GiFTDownload(this, user, url);
// add the segment from the newly created download
Segment[] s = d.getSegments();
segments.add(s[0]);
sources.put(url, d);
add(d);
return d;
}
/**
* @return unique id
*/
public String getGID()
{
return gid;
}
/**
* @return unique hash
*/
public String getHash()
{
return hash;
}
public Icon getIcon()
{
return GiFTPlugin.ICON_16;
}
/**
*
*/
public void pause()
{
daemon.changeDownload(this, (paused) ? "unpause" : "pause");
}
public void setGID(String gid)
{
this.gid = gid;
}
public void setCurrentRate(long currentRate)
{
this.currentRate = currentRate;
}
public void setOffset(long offset)
{
this.offset = offset;
}
/**
* Invoked by {@link Enginge} when the download state changed.
*/
public void setState(String state)
{
if (state.equals("Active")) {
if (paused) {
paused = false;
transferStarted();
}
}
else if (state.equals("Paused")) {
if (!paused) {
paused = true;
transferStopped();
}
}
else if (state.equals("Completed")) {
if (!done) {
done();
Enumeration enumeration = sources.elements();
while (enumeration.hasMoreElements()) {
GiFTDownload download
= (GiFTDownload)enumeration.nextElement();
download.setState("Complete", null);
}
}
}
setStatus(state);
}
private void setStatus(String status)
{
this.status = status;
stateChanged();
}
public void setTotalBytesTransferred(long totalBytesTransferred)
{
this.totalBytesTransferred = totalBytesTransferred;
}
private class CancelAction extends AbstractDeleteAction {
public void actionPerformed(ActionEvent e)
{
GiFTDownloadContainer.this.cancel();
}
}
private class CancelSourceAction extends AbstractDeleteAction {
private String url;
public CancelSourceAction(String url)
{
this.url = url;
putValue(Action.NAME, XNap.tr("Delete Source"));
setEnabled(url != null);
}
public void actionPerformed(ActionEvent e)
{
daemon.deleteSource(GiFTDownloadContainer.this, url);
}
}
private class ClearSourcesAction extends AbstractClearSourcesAction {
public void actionPerformed(ActionEvent e)
{
GiFTDownloadContainer.this.clearOrphaned();
}
}
private class FindMoreSourcesAction extends AbstractFindMoreSourcesAction {
public void actionPerformed(ActionEvent e)
{
setStatus(XNap.tr("Retrieving more sources"));
daemon.locate(GiFTDownloadContainer.this);
}
}
private class PauseAction extends AbstractTransferAction {
public PauseAction()
{
putValue(Action.NAME, XNap.tr("(Un)Pause Download"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Pauses or unpauses the download."));
putValue(IconHelper.XNAP_ICON, "player_pause.png");
}
public void actionPerformed(ActionEvent e)
{
GiFTDownloadContainer.this.pause();
}
}
}
The table below shows all metrics for GiFTDownloadContainer.java.




