JTellaDownloadContainer.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.jtella |
![]() |
![]() |
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.jtella;
import java.io.File;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.Action;
import org.apache.log4j.Logger;
import org.xnap.event.StateEvent;
import org.xnap.event.StateListener;
import org.xnap.peer.Peer;
import org.xnap.plugin.Plugin;
import org.xnap.transfer.AbstractDownload;
import org.xnap.transfer.DownloadManager;
import org.xnap.transfer.Queueable;
import org.xnap.util.FiniteStateMachine;
import org.xnap.util.IllegalOperationException;
import org.xnap.util.State;
/**
* Manages {@link JTellaDownload} objects.
*/
public class JTellaDownloadContainer extends AbstractDownload
implements Queueable {
//--- Constant(s) ---
private static final Hashtable TRANSITION_TABLE;
static {
State[][] table = new State[][] {
{ State.NOT_STARTED,
State.RUNNING, },
{ State.RUNNING,
State.SUCCEEDED, State.FAILED, },
};
TRANSITION_TABLE = FiniteStateMachine.createStateTable(table);
}
//--- Data field(s) ---
private static Logger logger
= Logger.getLogger(JTellaDownloadContainer.class);
private JTellaDownload activeDownload;
private long enqueueTime = System.currentTimeMillis();
private String filename;
private long filesize;
private int queuePosition;
private StateMachine sm = new StateMachine();
/**
* A list of {@link JTellaDownload} objects.
*/
private LinkedList queue = new LinkedList();
//--- Constructor(s) ---
/**
* Constructs a JTellaDownload. Creates {@link JTellaDownload}
* objects from the results contained in <code>container</code>.
* Sequentially tries each download until one succeeds.
*/
public JTellaDownloadContainer(JTellaSearchResultContainer container)
{
this.filename = container.getFilename();
this.filesize = container.getFilesize();
for (Iterator i = container.iterator(); i.hasNext();) {
add(new JTellaDownload((JTellaSearchResult)i.next()));
}
}
/**
* Constructs a JTellaDownload. Creates {@link JTellaDownload}
* objects from the <code>result</code>.
*/
public JTellaDownloadContainer(JTellaSearchResult result)
{
this.filename = result.getFilename();
this.filesize = result.getFilesize();
add(new JTellaDownload(result));
}
//--- Method(s) ---
public void add(JTellaDownload d)
{
queue.add(d);
super.add(d);
}
public Action[] getActions()
{
return null;
}
public long getBytesTransferred()
{
JTellaDownload download = getDownload();
return (download != null) ? download.getBytesTransferred() : 0;
}
public JTellaDownload getDownload()
{
return activeDownload;
}
public long getEnqueueTime()
{
return enqueueTime;
}
public File getFile()
{
JTellaDownload download = getDownload();
return (download != null) ? download.getFile() : null;
}
public String getFilename()
{
return filename;
}
public long getFilesize()
{
return filesize;
}
public Peer getPeer()
{
JTellaDownload download = getDownload();
return (download != null) ? download.getPeer() : null;
}
public Plugin getPlugin()
{
return JTellaPlugin.getInstance();
}
public String getStatus()
{
JTellaDownload download = getDownload();
return (download != null) ? download.getStatus() : null;
}
public long getTotalBytesTransferred()
{
JTellaDownload download = getDownload();
return (download != null) ? download.getTotalBytesTransferred() : 0;
}
public boolean isDone()
{
State s = sm.getState();
return s == State.SUCCEEDED || s == State.FAILED;
}
public boolean isFailed()
{
return sm.getState() == State.FAILED;
}
public boolean isRunning()
{
return sm.getState() == State.RUNNING;
}
/**
* Returns 1.
*/
public int getPriority()
{
return 1;
}
/**
* Returns the position in the {@link DownloadManager} queue.
*/
public int getQueuePosition()
{
return queuePosition;
}
/**
*
*/
public void setQueuePosition(int position)
{
queuePosition = position;
}
private void setState(State newState)
{
sm.setState(newState);
stateChanged();
}
public void start()
{
DownloadManager.getInstance().getQueue().add(this);
}
public void startNextDownload()
{
if (queue.isEmpty()) {
activeDownload = null;
try {
setState(State.FAILED);
}
catch (IllegalOperationException e) {
logger.error("unexpected exception", e);
}
return;
}
activeDownload = (JTellaDownload)queue.removeFirst();
activeDownload.addStateListener(new DownloadListener());
try {
activeDownload.start();
}
catch (IllegalOperationException e) {
logger.error("unexpected exception", e);
}
}
public boolean startTransfer()
{
try {
setState(State.RUNNING);
return true;
}
catch (IllegalOperationException e) {
logger.error("unexpected exception", e);
return false;
}
}
//--- Inner Class(es) ---
private class StateMachine extends FiniteStateMachine
{
//--- Constructor(s) ---
public StateMachine()
{
super(State.NOT_STARTED, TRANSITION_TABLE);
}
//--- Method(s) ---
protected synchronized void stateChanged(State oldState,
State newState)
{
if (oldState == State.NOT_STARTED) {
if (newState == State.RUNNING) {
startNextDownload();
}
}
else if (newState == State.SUCCEEDED || newState == State.FAILED) {
DownloadManager.getInstance().getQueue().remove
(JTellaDownloadContainer.this);
}
}
}
private class DownloadListener implements StateListener
{
public void stateChanged(StateEvent event)
{
if (activeDownload.isDone()) {
activeDownload.removeStateListener(this);
if (activeDownload.isFailed()) {
startNextDownload();
}
}
}
}
}
The table below shows all metrics for JTellaDownloadContainer.java.




