AzureusDownloadContainer.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.azureus |
![]() |
![]() |
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.
| Metric | Description | |
|---|---|---|
/*
* 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.azureus;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.Action;
import org.apache.log4j.Logger;
import org.gudy.azureus2.core3.disk.DiskManager;
import org.gudy.azureus2.core3.disk.DiskManagerFileInfo;
import org.gudy.azureus2.core3.download.DownloadManager;
import org.gudy.azureus2.core3.download.DownloadManagerListener;
import org.gudy.azureus2.core3.download.DownloadManagerPeerListener;
import org.gudy.azureus2.core3.download.DownloadManagerTrackerListener;
import org.gudy.azureus2.core3.peer.PEPeer;
import org.gudy.azureus2.core3.peer.PEPeerManager;
import org.gudy.azureus2.core3.peer.PEPiece;
import org.gudy.azureus2.core3.torrent.TOTorrent;
import org.gudy.azureus2.core3.tracker.client.TRTrackerResponse;
import org.gudy.azureus2.core3.tracker.client.TRTrackerScraperResponse;
import org.xnap.XNap;
import org.xnap.peer.Peer;
import org.xnap.plugin.Plugin;
import org.xnap.transfer.AbstractDownload;
import org.xnap.transfer.DefaultSegment;
import org.xnap.transfer.Segment;
import org.xnap.transfer.Transfer;
import org.xnap.transfer.action.AbstractStartAction;
import org.xnap.transfer.action.AbstractStopAction;
/**
* Manages {@link AzureusDownload} objects.
*/
public class AzureusDownloadContainer extends AbstractDownload
implements DownloadManagerListener, DownloadManagerPeerListener,
DownloadManagerTrackerListener {
//--- Constant(s) ---
//--- Data field(s) ---
private static Logger logger
= Logger.getLogger(AzureusDownloadContainer.class);
private DownloadManager manager;
private DefaultSegment[] segments;
private long offset = 0;
private boolean childrenAdded;
//--- Constructor(s) ---
/**
*
*/
public AzureusDownloadContainer(DownloadManager manager)
{
this.manager = manager;
manager.addListener(this);
manager.addPeerListener(this);
manager.addTrackerListener(this);
}
//--- Method(s) ---
public Action[] getActions()
{
return new Action[] { new StartAction(), new StopAction() };
}
public long getBytesTransferred()
{
return getTotalBytesTransferred() - offset;
}
public String getDescription()
{
StringBuffer sb = new StringBuffer();
TOTorrent torrent = manager.getTorrent();
if (torrent != null) {
sb.append("URL: " + torrent.getAnnounceURL() + "<br>");
}
return sb.toString();
}
public File getFile()
{
return new File(manager.getFullName());
}
public String getFilename()
{
return getFile().getName();
}
public long getFilesize()
{
return manager.getSize();
}
public Peer getPeer()
{
return null;
}
public Plugin getPlugin()
{
return AzureusPlugin.getInstance();
}
public Segment[] getSegments()
{
if (segments != null) {
boolean[] status = manager.getPiecesStatus();
if (status != null) {
for (int i = 0; i < segments.length && i < status.length; i++) {
segments[i].setTransferred((status[i])
? segments[i].getEnd() - segments[i].getStart()
: 0);
}
}
}
return segments;
}
public String getStatus()
{
switch (manager.getState()) {
case DownloadManager.STATE_WAITING :
return XNap.tr("Waiting");
case DownloadManager.STATE_INITIALIZING:
return XNap.tr("Initializing");
case DownloadManager.STATE_INITIALIZED:
return XNap.tr("Initialized");
case DownloadManager.STATE_ALLOCATING:
return XNap.tr("Allocating");
case DownloadManager.STATE_CHECKING:
return XNap.tr("Checking");
case DownloadManager.STATE_READY:
return XNap.tr("Ready");
case DownloadManager.STATE_DOWNLOADING:
return XNap.tr("Downloading");
case DownloadManager.STATE_SEEDING:
return XNap.tr("Seeding");
case DownloadManager.STATE_STOPPING:
return XNap.tr("Stopping");
case DownloadManager.STATE_STOPPED:
return XNap.tr("Stopped");
case DownloadManager.STATE_QUEUED:
return XNap.tr("Queued");
case DownloadManager.STATE_ERROR:
return XNap.tr("Error {0}", manager.getErrorDetails());
default:
return XNap.tr("Unknown State {0}", manager.getState());
}
}
public long getTotalBytesTransferred()
{
DiskManager dm = manager.getDiskManager();
return (dm != null) ? dm.getTotalLength() - dm.getRemaining() : 0;
}
public boolean isDone()
{
int s = manager.getState();
return s == DownloadManager.STATE_SEEDING
|| s == DownloadManager.STATE_ERROR;
}
public boolean isRunning()
{
int s = manager.getState();
return s != DownloadManager.STATE_STOPPED;
}
/**
* Returns -1;
*/
public int getQueuePosition()
{
return -1;
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerListener#stateChanged(org.gudy.azureus2.core3.download.DownloadManager, int)
*/
public void stateChanged(DownloadManager manager, int state)
{
if (state == DownloadManager.STATE_CHECKING) {
initializeChildren();
}
else if (state == DownloadManager.STATE_READY) {
initializeChildren();
this.offset = getTotalBytesTransferred();
manager.startDownload();
}
else if (state == DownloadManager.STATE_DOWNLOADING) {
transferStarted();
Transfer[] children = getChildren();
for (int i = 0 ; i < children.length; i++) {
((AzureusDownload)children[i]).transferStarted();
}
}
else {
transferStopped();
Transfer[] children = getChildren();
for (int i = 0 ; i < children.length; i++) {
((AzureusDownload)children[i]).transferStopped();
}
}
stateChanged();
}
/**
*
*/
private void initializeChildren()
{
if (!childrenAdded) {
childrenAdded = true;
DefaultSegment[] segments = new DefaultSegment[manager.getNbPieces()];
for (int i = 0; i < segments.length; i++) {
long start = i * manager.getDiskManager().getPieceLength();
segments[i] = new DefaultSegment(getFilesize(), start, start + manager.getDiskManager().getPieceLength(), 0, 0);
}
// atomic copy?
this.segments = segments;
childrenAdded = true;
DiskManagerFileInfo[] files = manager.getDiskManager().getFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
add(new AzureusDownload(this, files[i]));
}
}
}
}
public void updateAvailability()
{
if (segments == null) {
return;
}
PEPeerManager pm = manager.getPeerManager();
if (pm != null) {
int[] availability = pm.getAvailability();
if (availability != null) {
for (int i = 0; i < segments.length && i < availability.length; i++) {
if (availability[i] > 0) {
segments[i].setAvailability(64 + 5 * availability[i]);
}
else {
segments[i].setAvailability(0);
}
}
}
}
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerListener#downloadComplete(org.gudy.azureus2.core3.download.DownloadManager)
*/
public void downloadComplete(DownloadManager manager)
{
stateChanged();
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerListener#completionChanged(org.gudy.azureus2.core3.download.DownloadManager, boolean)
*/
public void completionChanged(DownloadManager manager, boolean bCompleted)
{
stateChanged();
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerPeerListener#peerAdded(org.gudy.azureus2.core3.peer.PEPeer)
*/
public void peerAdded(PEPeer peer)
{
updateAvailability();
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerPeerListener#peerRemoved(org.gudy.azureus2.core3.peer.PEPeer)
*/
public void peerRemoved(PEPeer peer)
{
updateAvailability();
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerPeerListener#pieceAdded(org.gudy.azureus2.core3.peer.PEPiece)
*/
public void pieceAdded(PEPiece piece)
{
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerPeerListener#pieceRemoved(org.gudy.azureus2.core3.peer.PEPiece)
*/
public void pieceRemoved(PEPiece piece)
{
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerTrackerListener#scrapeResult(org.gudy.azureus2.core3.tracker.client.TRTrackerScraperResponse)
*/
public void scrapeResult(TRTrackerScraperResponse response)
{
}
/**
* @see org.gudy.azureus2.core3.download.DownloadManagerTrackerListener#announceResult(org.gudy.azureus2.core3.tracker.client.TRTrackerResponse)
*/
public void announceResult(TRTrackerResponse response)
{
}
//--- Inner Class(es) ---
private class StartAction extends AbstractStartAction {
public void actionPerformed(ActionEvent e)
{
manager.initialize();
}
}
private class StopAction extends AbstractStopAction {
public void actionPerformed(ActionEvent event)
{
manager.stopIt();
}
}
/**
* @return
*/
public DownloadManager getManager()
{
return manager;
}
}
The table below shows all metrics for AzureusDownloadContainer.java.
| Metric | Value | Description | |
|---|---|---|---|



