JTellaDownload.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.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Hashtable;
import javax.swing.Action;
import org.apache.log4j.Logger;
import org.xnap.io.ThrottledInputStream;
import org.xnap.peer.Peer;
import org.xnap.plugin.Plugin;
import org.xnap.transfer.AbstractTransfer;
import org.xnap.transfer.Download;
import org.xnap.util.FileHelper;
import org.xnap.util.FiniteStateMachine;
import org.xnap.util.IllegalOperationException;
import org.xnap.util.State;
import com.kenmccrary.jtella.SearchReplyMessage;
/**
* Downloads a file.
*/
public class JTellaDownload extends AbstractTransfer implements Download {
//--- Constant(s) ---
private static final Hashtable TRANSITION_TABLE;
static {
State[][] table = new State[][] {
{ State.NOT_STARTED,
State.CONNECTING, },
{ State.CONNECTING,
State.DOWNLOADING, State.ABORTING, State.FAILED, },
{ State.DOWNLOADING,
State.SUCCEEDED, State.ABORTING, State.FAILED, },
{ State.ABORTING,
State.ABORTED }
};
TRANSITION_TABLE = FiniteStateMachine.createStateTable(table);
}
//--- Data field(s) ---
private static Logger logger = Logger.getLogger(JTellaDownload.class);
private File file;
private JTellaSearchResult result;
private long bytesTransferred = 0;
private StateMachine sm = new StateMachine();
private DownloadRunner runner;
//--- Constructor(s) ---
public JTellaDownload(JTellaSearchResult result)
{
this.result = result;
}
//--- Method(s) ---
public long getBytesTransferred()
{
return bytesTransferred;
}
public Action[] getActions()
{
return null;
}
public File getFile()
{
return file;
}
public String getFilename()
{
return result.getFilename();
}
public long getFilesize()
{
return result.getFilesize();
}
public Peer getPeer()
{
return result.getPeer();
}
public Plugin getPlugin()
{
return JTellaPlugin.getInstance();
}
public String getStatus()
{
return sm.getDescription();
}
public long getTotalBytesTransferred()
{
return bytesTransferred;
}
public boolean isDone()
{
State s = sm.getState();
return s == State.SUCCEEDED || s == State.ABORTED || s == State.FAILED;
}
public boolean isFailed()
{
State s = sm.getState();
return s == State.FAILED;
}
public boolean isRunning()
{
State s = sm.getState();
return s == State.CONNECTING || s == State.DOWNLOADING
|| s == State.ABORTING;
}
public void start()
{
setState(State.CONNECTING);
}
private void setState(State newState, String description)
{
sm.setState(newState, description);
stateChanged();
}
private void setState(State newState)
{
sm.setState(newState);
stateChanged();
}
//--- 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 (newState == State.CONNECTING) {
runner = new DownloadRunner();
Thread t
= new Thread(runner, "JTellaDownload:"
+ getFilename());
t.start();
return;
}
else if (oldState == State.CONNECTING) {
if (newState == State.ABORTING) {
runner.die = true;
return;
}
}
}
}
private class DownloadRunner implements Runnable
{
//--- Data field(s) ---
private boolean die = false;
//--- Method(s) ---
public String getFilenameRequest()
{
StringBuffer sb = new StringBuffer();
sb.append("/get/");
sb.append(result.getFileRecord().getIndex());
sb.append("/");
sb.append(result.getFilename());
sb.append("/");
return sb.toString();
}
public void run()
{
HttpURLConnection connection = null;
InputStream in = null;
try {
SearchReplyMessage reply = result.getReply();
URL url = new URL("http", reply.getIPAddress(),
reply.getPort(), getFilenameRequest());
connection = (HttpURLConnection)url.openConnection();
if (die) {
return;
}
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
in = new ThrottledInputStream(connection.getInputStream());
download(in);
}
else {
throw new IOException(connection.getResponseMessage());
}
setState(State.SUCCEEDED);
}
catch (IOException e) {
try {
logger.warn("connection failed", e);
setState(State.FAILED, e.getLocalizedMessage());
}
catch (IllegalOperationException e2) {
}
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e2) {
}
}
if (connection != null) {
connection.disconnect();
}
}
}
public void download(InputStream in) throws IOException
{
OutputStream out = null;
try {
file = FileHelper.createIncompleteFile(result.getFilename());
out = new BufferedOutputStream(new FileOutputStream(file));
setState(State.DOWNLOADING);
byte[] data = new byte[512];
while (!die && bytesTransferred < getFilesize()) {
// compute the number of bytes to read
long toRead = getFilesize() - bytesTransferred;
int len = (int)Math.min(toRead, data.length);
len = in.read(data, 0, len);
if (len == -1) {
break;
}
out.write(data, 0, len);
bytesTransferred += len;
}
out.flush();
}
finally {
transferStopped();
if (out != null) {
try {
out.close();
}
catch (IOException e) {
}
}
}
}
}
}
The table below shows all metrics for JTellaDownload.java.




