GiFTDaemon.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.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.net.NetHelper;
import org.xnap.peer.Peer;
import org.xnap.plugin.gift.GiFTPlugin;
import org.xnap.plugin.gift.net.event.ControlEvent;
import org.xnap.plugin.gift.net.event.SearchControlEvent;
import org.xnap.plugin.gift.net.event.SearchItemEvent;
import org.xnap.plugin.gift.net.event.ShareItemEvent;
import org.xnap.plugin.gift.net.event.UploadUpdatedEvent;
import org.xnap.plugin.gift.net.lexer.Command;
import org.xnap.plugin.gift.net.lexer.StreamLexer;
import org.xnap.transfer.DownloadManager;
import org.xnap.transfer.UploadManager;
import org.xnap.util.FiniteStateMachine;
import org.xnap.util.IllegalOperationException;
import org.xnap.util.Scheduler;
import org.xnap.util.State;
import org.xnap.util.XNapTask;
/**
* Handles a connection to a single giFT daemon.
*
* @author Tammo van Lessen
* @author Steffen Pingel
*/
public class GiFTDaemon {
/**
* The poll intervall for network stats.
*/
public static final int NETWORK_STATS_INTERVAL = 3 * 60 * 1000; // 3 min
private static final Hashtable TRANSITION_TABLE;
static {
State[][] table = new State[][] {
{ org.xnap.util.State.DISCONNECTED,
org.xnap.util.State.CONNECTING, },
{ org.xnap.util.State.CONNECTING,
org.xnap.util.State.CONNECTED, org.xnap.util.State.DISCONNECTED },
{ org.xnap.util.State.CONNECTED,
org.xnap.util.State.DISCONNECTING, },
{ org.xnap.util.State.DISCONNECTING,
org.xnap.util.State.DISCONNECTED },
};
TRANSITION_TABLE = FiniteStateMachine.createStateTable(table);
}
long NetworkStatsTimer = 0;
private Logger logger = Logger.getLogger(GiFTDaemon.class);
private Hashtable downloads;
private Hashtable uploads;
private Hashtable searches;
private String host;
private String serverName;
private String serverVersion;
private String user;
private Vector listeners = new Vector();
private int port;
private StateMachine sm = new StateMachine();
private String message;
private String username;
private String stats;
private String verboseMessage;
//--- Constructors ---
/**
*
*/
public GiFTDaemon(String host, int port, String user)
{
this.host = host;
this.port = port;
this.user = user;
}
//--- Methods ---
/**
* Adds an event listener.
*
* @param listener the listener
*/
public void addDaemonListener(GiFTDaemonListener listener)
{
listeners.add(listener);
}
public void addDownload(GiFTSearchResult sr)
{
Command cmd = new Command("addsource");
cmd.addKey("hash", (String)sr.getHash());
cmd.addKey("size", Long.toString(sr.getFilesize()));
cmd.addKey("url", sr.getUrl());
cmd.addKey("user", ((GiFTUser) sr.getPeer()).getUsername());
// TODO
cmd.addKey("save", sr.getShortFilename());
queueCommand(cmd);
}
public void changeDownload(GiFTDownloadContainer dc, String action)
{
Command cmd = new Command("transfer");
cmd.setCommandArgument(dc.getGID());
cmd.addKey("action", action);
queueCommand(cmd);
}
public void changeSearch(GiFTSearch s, String action)
{
Command cmd = new Command("search");
cmd.setCommandArgument(s.getGID());
cmd.addKey("action", action);
queueCommand(cmd);
}
public void changeUpload(GiFTUpload u, String action)
{
Command cmd = new Command("transfer");
cmd.setCommandArgument(u.getGID());
cmd.addKey("action", action);
queueCommand(cmd);
}
public void deleteSource(GiFTDownloadContainer dc, String url)
{
Command cmd = new Command("delsource");
cmd.setCommandArgument(dc.getGID());
cmd.addKey("url", url);
queueCommand(cmd);
}
// private void fireMessageReceived(String message)
// {
// GiFTDaemonListener[] l
// = (GiFTDaemonListener[])listeners.toArray(new GiFTDaemonListener[0]);
// for (int i = 0; i < l.length; i++) {
// l[i].messageReceived(this, message);
// }
// }
//
// private void fireStatsUpdated(String stats)
// {
// GiFTDaemonListener[] l
// = (GiFTDaemonListener[])listeners.toArray(new GiFTDaemonListener[0]);
// for (int i = 0; i < l.length; i++) {
// l[i].statsUpdated(this, stats);
// }
// }
private void fireStatusChanged()
{
GiFTDaemonListener[] l
= (GiFTDaemonListener[])listeners.toArray(new GiFTDaemonListener[0]);
for (int i = 0; i < l.length; i++) {
l[i].statusChanged(this);
}
}
protected GiFTDownloadContainer getDownloadContainer
(String filename, String hash, long size)
{
GiFTDownloadContainer dc = (GiFTDownloadContainer)downloads.get(hash);
if (dc == null || dc.isDone()) {
dc = new GiFTDownloadContainer(this, filename, hash, size);
downloads.put(hash, dc);
DownloadManager.getInstance().add(dc);
}
return dc;
}
public String getHost()
{
return host;
}
public String getMessage()
{
return message;
}
public int getPort()
{
return port;
}
// protected GiFTUpload getUpload(String filename, String hash, long size)
// {
// GiFTUpload u = (GiFTUpload)uploads.get(hash);
// if (u == null) {
// u = new GiFTUpload(filename, hash, size);
// uploads.put(hash, u);
// UploadManager.getInstance().add(u);
// }
// return u;
// }
public String getUsername()
{
return username;
}
public String getStats()
{
return stats;
}
public String getStatus()
{
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append(getServerInfo());
if (getStats() != null) {
sb.append("<br>");
sb.append(getStats());
}
if (getMessage() != null) {
sb.append("<hr");
sb.append(getMessage());
}
return sb.toString();
}
/**
* Returns giFT server's name and version as String
*
* @return server info
*/
public String getServerInfo()
{
if (sm.getState() == org.xnap.util.State.CONNECTED && serverName != null) {
// only send version if set, daemons compiled from cvs for
// instance do not send a version
return "<b>" + serverName + "</b>"
+ ((serverVersion != null) ? " v" + serverVersion : "");
}
else {
return sm.getDescription();
}
}
public String getVerboseMessage()
{
return verboseMessage;
}
public boolean isConnected()
{
return sm.getState() == org.xnap.util.State.CONNECTED;
}
public boolean isDisconnected()
{
return sm.getState() == org.xnap.util.State.DISCONNECTED;
}
/**
* Searches for more sources for dc.
*/
public void locate(GiFTDownloadContainer dc)
{
String gid = GIDManager.createGID() + "";
searches.put(gid, dc);
Command cmd = new Command("locate");
cmd.setCommandArgument(Integer.toString(dc.hashCode()));
cmd.addKey("query", dc.getHash());
queueCommand(cmd);
}
protected void queueCommand(Command cmd)
{
synchronized (sm) {
if (sm.getState() == org.xnap.util.State.CONNECTED) {
sm.getWriter().enqueue(cmd);
}
}
}
/**
* Removes an event listener.
*
* @param listener
*/
public void removeDaemonListener(GiFTDaemonListener listener)
{
listeners.remove(listener);
}
/**
* Forces giFT to send network stats
*/
public synchronized void updateNetworkStats()
{
Command cmd = new Command("stats");
queueCommand(cmd);
}
/**
* Forces giFT to list its shares
*/
public void updateShareListing()
{
Command cmd = new Command("shares");
queueCommand(cmd);
}
/**
* Adds a new search
*
* @param sf SearchFilter
*/
public void search(GiFTSearch s)
{
String gid = GIDManager.createGID() + "";
searches.put(gid, s);
s.setGID(gid);
Command cmd = new Command("search");
cmd.setCommandArgument(gid);
cmd.addKey("query", s.getFilter().getText());
queueCommand(cmd);
s.searchStarted(new SearchControlEvent(ControlEvent.STARTED));
}
/**
* Sets giFT host
*
* @param host
*/
public void setHost(String host)
{
this.host = host;
}
/**
* Sets giFT port
*
* @param port
*/
public void setPort(int port)
{
this.port = port;
}
void setState(State newState, String description)
{
sm.setState(newState, description);
fireStatusChanged();
}
void setState(State newState)
{
sm.setState(newState);
fireStatusChanged();
}
void setStateDescription(String description)
{
sm.setDescription(description);
fireStatusChanged();
}
/**
* Sets giFT user name
*
* @param user
*/
public void setUsername(String username) {
this.username = username;
}
public void setVerboseMessage(Exception e)
{
if (e == null) {
verboseMessage = null;
}
else if (e instanceof ConnectException) {
// connection refused
StringBuffer sb = new StringBuffer();
sb.append("<br><p>");
sb.append(XNap.tr("Could not connect to giFT daemon. This can have several reasons:"));
sb.append("<ol><li>");
sb.append(XNap.tr("<b>giFT is not installed</b>. This plugin requires an external programm called giFT that handles the network connection. Please see http://gift.sf.net/ for more information and download links."));
sb.append("<li>");
sb.append(XNap.tr("The plugin <b>settings are incorrect</b>, e.g. the giFT daemon port is wrong. Please check the settings under Settings -> Configure Plugins."));
sb.append("<li>");
sb.append(XNap.tr("The <b>giFT daemon is not running</b>. Please click the Start button on the giFT daemon panel."));
sb.append("</ol>");
verboseMessage = sb.toString();
}
}
/**
* Connects to the daemon.
*/
public void start()
{
setState(org.xnap.util.State.CONNECTING);
}
/**
* Disconnects the daemon.
*/
public void stop(boolean killGiFT)
{
Command cmd
= (killGiFT) ? new Command("quit") : new Command("detach");
queueCommand(cmd);
}
/**
* Forces giFT to sync its shares index
*/
public void syncShares()
{
Command cmd = new Command("share");
cmd.addKey("action", "sync");
queueCommand(cmd);
}
/**
* Dispatches incomming commands.
*/
private void dispatchCommand(Command cmd)
{
if (cmd.getCommand().equalsIgnoreCase("ATTACH")) {
serverName = cmd.getKey("server");
serverVersion = cmd.getKey("version");
fireStatusChanged();
}
else if (cmd.getCommand().equalsIgnoreCase("STATS")) {
StringBuffer sb = new StringBuffer();
Vector subCommands = cmd.getSubCommands();
for (int i = 0; i < subCommands.size(); i++) {
Command subCmd = (Command)subCommands.get(i);
if (subCmd.getKey("files") != null
&& subCmd.getKey("size") != null) {
try {
String protocol
= (subCmd.getCommand().equalsIgnoreCase("gift"))
? XNap.tr("Local")
: subCmd.getCommand();
String s;
if (subCmd.getKey("users") != null) {
s = XNap.tr
("{0}: {1} Files, {2} Users, {3} GB",
protocol,
new Long((String)subCmd.getKey("files")),
new Long((String)subCmd.getKey("users")),
new Float((String)subCmd.getKey("size")));
}
else {
s = XNap.tr
("{0}: {1} Files, {2} GB",
protocol,
new Long((String)subCmd.getKey("files")),
new Float((String)subCmd.getKey("size")));
}
if (sb.length() > 0) {
sb.append("<br>");
}
sb.append(s);
}
catch (NumberFormatException e) {
}
}
}
this.stats = sb.toString();
fireStatusChanged();
}
else if (cmd.getCommand().equalsIgnoreCase("ITEM")) {
if (cmd.getCommandArgument() == null) {
if (cmd.hasKeys()) {
// received share item
ShareItemEvent se = new ShareItemEvent();
se.setPath(cmd.getKey("path"));
try {
se.setSize(Long.parseLong(cmd.getKey("size")));
} catch (Exception e) {
}
se.setMime(cmd.getKey("mime"));
se.setHash(cmd.getKey("hash"));
Command meta = cmd.getSubCommandByName("META");
if (meta != null) {
Enumeration keys = meta.getKeys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
se.addMetaItem(key, meta.getKey(key));
}
}
}
else {
// finished receiving shares
}
}
else {
if (cmd.hasKeys()) {
// received search item
if ((cmd.getKey("file") == null) ||
(cmd.getKey("hash") == null) ||
(cmd.getKey("node") == null) ||
(cmd.getKey("url") == null)) {
logger.debug("Received invalied search result");
return;
}
long size = -1;
try {
size = Long.parseLong(cmd.getKey("size"));
}
catch (NumberFormatException e) {
}
int score = 1;
try {
score = Integer.parseInt(cmd.getKey("availability"));
}
catch (NumberFormatException e) {
}
Hashtable meta = new Hashtable();
Command metaCmd = cmd.getSubCommandByName("META");
if (metaCmd != null) {
Enumeration keys = metaCmd.getKeys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
meta.put(key, metaCmd.getKey(key));
}
}
Peer user = new GiFTUser(cmd.getKey("user"));
GiFTSearchResult sr =
new GiFTSearchResult
(this,
size,
user,
cmd.getKey("file"),
cmd.getKey("hash"),
cmd.getKey("node"),
cmd.getKey("mime"),
cmd.getKey("url"),
score,
meta);
Object searcher = searches.get(cmd.getCommandArgument());
logger.debug(searcher);
if (searcher instanceof GiFTSearch) {
GiFTSearch search =
(GiFTSearch) searches.get(cmd.getCommandArgument());
search.searchItemReceived(
new SearchItemEvent(search.getFilter(), sr));
}
else if (searcher instanceof GiFTDownloadContainer){
addDownload(sr);
}
}
else {
// search finished
GiFTSearch search
= (GiFTSearch)searches.get(cmd.getCommandArgument());
if (search != null) {
searches.remove(cmd.getCommandArgument());
search.searchFinished
(new SearchControlEvent(ControlEvent.FINISHED));
}
}
}
}
else if (cmd.getCommand().equalsIgnoreCase("ADDDOWNLOAD")) {
String filename = cmd.getKey("file");
String hash = cmd.getKey("hash");
long size = -1;
try {
size = Long.parseLong(cmd.getKey("size"));
} catch (Exception e) {
}
long transmit = -1;
try {
transmit = Long.parseLong(cmd.getKey("transmit"));
} catch (Exception e) {
}
GiFTDownloadContainer dc =
getDownloadContainer(filename, hash, size);
dc.setState(cmd.getKey("state"));
dc.setGID(cmd.getCommandArgument());
dc.setTotalBytesTransferred(transmit);
dc.setOffset(transmit);
addSources(cmd, dc);
}
else if (cmd.getCommand().equalsIgnoreCase("CHGDOWNLOAD")) {
GiFTDownloadContainer dc =
(GiFTDownloadContainer)downloads.get(cmd.getKey("hash"));
if (dc != null) {
if (cmd.getKey("throughput") != null) {
try {
dc.setCurrentRate
(Long.parseLong(cmd.getKey("throughput")));
}
catch (NumberFormatException e) {
}
}
if (cmd.getKey("transmit") != null) {
try {
dc.setTotalBytesTransferred
(Long.parseLong(cmd.getKey("transmit")));
}
catch (NumberFormatException e) {
}
}
dc.setState(cmd.getKey("state"));
addSources(cmd, dc);
}
}
else if (cmd.getCommand().equalsIgnoreCase("ADDUPLOAD")) {
String filename = cmd.getKey("file");
String hash = cmd.getKey("hash");
long size = -1;
try {
size = Long.parseLong(cmd.getKey("size"));
} catch (Exception e) {
}
long transmit = -1;
try {
transmit = Long.parseLong(cmd.getKey("transmit"));
} catch (Exception e) {
}
GiFTUpload u = (GiFTUpload)uploads.get(hash);
if (u == null) {
u = new GiFTUpload(new GiFTUser(cmd.getKey("user")),
cmd.getKey("url"), size, transmit);
u.setGID(cmd.getCommandArgument());
uploads.put(hash, u);
UploadManager.getInstance().add(u);
}
u.setStatus(cmd.getKey("state"));
//fireEvent(new DownloadAddedEvent(dc));
} else if (cmd.getCommand().equalsIgnoreCase("CHGUPLOAD")) {
String filename = cmd.getKey("file");
String hash = cmd.getKey("hash");
long size = -1;
try {
size = Long.parseLong(cmd.getKey("size"));
} catch (Exception e) {
}
long transmit = -1;
try {
transmit = Long.parseLong(cmd.getKey("transmit"));
} catch (Exception e) {
}
long elapsed = -1;
try {
elapsed = Long.parseLong(cmd.getKey("elapsed"));
} catch (Exception e) {
}
long throughput = -1;
try {
throughput = Long.parseLong(cmd.getKey("throughput"));
} catch (Exception e) {
}
String state = cmd.getKey("state");
GiFTUpload u =
(GiFTUpload) uploads.get(cmd.getKey("hash"));
if (u != null) {
UploadUpdatedEvent due = new UploadUpdatedEvent();
due.setElapsed(elapsed);
due.setFilename(filename);
due.setHash(hash);
due.setSize(size);
due.setState(state);
due.setThroughput(throughput);
due.setTransmit(transmit);
// fire event directly
u.uploadUpdated(due);
}
} else if (cmd.getCommand().equalsIgnoreCase("DELUPLOAD")) {
Enumeration it = uploads.elements();
while (it.hasMoreElements()) {
GiFTUpload u = (GiFTUpload) it.nextElement();
if (u.getGID().equals(cmd.getCommandArgument())) {
u.uploadCanceled();
}
}
} else if (cmd.getCommand().equalsIgnoreCase("DELDOWNLOAD")) {
Enumeration it = downloads.elements();
while (it.hasMoreElements()) {
GiFTDownloadContainer dc = (GiFTDownloadContainer) it.nextElement();
if (dc.getGID().equals(cmd.getCommandArgument())) {
dc.deleted();
}
}
} else if (cmd.getCommand().equalsIgnoreCase("MESSAGE")) {
message = cmd.getCommandArgument();
fireStatusChanged();
}
}
private void addSources(Command cmd, GiFTDownloadContainer dc)
{
dc.markChildren();
Vector v = cmd.getAllSubCommandsByName("source");
Iterator i = v.iterator();
while (i.hasNext()) {
Command c = (Command)i.next();
GiFTDownload d = dc.getSourceByURL(c.getKey("url"));
if (d == null) {
d = dc.addSource(new GiFTUser(c.getKey("user")),
c.getKey("url"));
}
if (c.getKey("start") != null
&& c.getKey("transmit") != null
&& c.getKey("total") != null) {
try {
d.updated(Long.parseLong(c.getKey("start")),
Long.parseLong(c.getKey("transmit")),
Long.parseLong(c.getKey("total")));
}
catch (NumberFormatException e) {
}
}
d.setMarked(false);
d.setState(c.getKey("statusgrl"), c.getKey("status"));
}
dc.orphanMarkedChildren();
}
private class ReaderThread extends Thread
{
private InputStream in;
private OutputStream out;
private Socket socket;
private boolean stopped = false;
public ReaderThread()
{
super("GiFTDeamonReader " + getHost() + ":" + getPort());
}
public void die()
{
stopped = true;
interrupt();
}
public OutputStream getOutputStream()
{
return out;
}
public void run()
{
setVerboseMessage(null);
try {
logger.debug("Connecting to giFT damon @"
+ getHost() + ":" + getPort());
Socket socket = new Socket(getHost(), getPort());
socket.setSoTimeout(NETWORK_STATS_INTERVAL * 2);
in = new BufferedInputStream(socket.getInputStream());
out = socket.getOutputStream();
}
catch (IOException e) {
setState(org.xnap.util.State.DISCONNECTED, NetHelper.getErrorMessage(e));
setVerboseMessage(e);
return;
}
setState(org.xnap.util.State.CONNECTED);
try {
StreamLexer lexer = new StreamLexer(in);
while (!stopped) {
Command cmd = readNextCommand();
if (cmd != null) {
if (XNap.isRunFromCvs()) {
logger.debug("< " + cmd.print());
}
dispatchCommand(cmd);
}
}
}
catch (IOException e) {
try {
setState(org.xnap.util.State.DISCONNECTING,
NetHelper.getErrorMessage(e));
}
catch (IllegalOperationException e2) {
// disconnect is already in progress
}
}
finally {
try {
if (socket != null) {
socket.close();
}
}
catch (IOException e) {
}
}
sm.readerStopped();
}
private Command readNextCommand() throws IOException
{
StringBuffer sb = new StringBuffer();
while (!stopped) {
int cint = in.read();
if (cint == -1) {
throw new IOException(XNap.tr("Socket closed"));
}
else if (cint == ';') {
break;
}
sb.append((char)cint);
}
Command cmd = new Command();
return (stopped || !cmd.parse(sb.toString().trim() + ";"))
? null
: cmd;
}
}
private class WriterThread extends Thread
{
private OutputStream out;
private LinkedList outQueue = new LinkedList();
private boolean stopped = false;
private WriterThread(OutputStream out)
{
super("GiFTDeamonWriter " + getHost() + ":" + getPort());
this.out = out;
}
public synchronized void enqueue(Command cmd)
{
outQueue.addLast(cmd);
notify();
}
public void die()
{
stopped = true;
interrupt();
}
public void run()
{
try {
while (!stopped) {
synchronized (this) {
while (!stopped && outQueue.isEmpty()) {
try {
this.wait();
}
catch (InterruptedException e) {
}
}
}
if (stopped) {
break;
}
Command cmd = (Command) outQueue.removeFirst();
if (XNap.isRunFromCvs()) {
logger.debug("> " + cmd.print());
}
out.write(cmd.print().getBytes());
}
}
catch (IOException e) {
try {
setState(org.xnap.util.State.DISCONNECTING,
NetHelper.getErrorMessage(e));
}
catch (IllegalOperationException e2) {
// disconnect is already in progress
}
}
sm.writerStopped();
}
}
private class StateMachine extends FiniteStateMachine
{
private StatsRequestorTask statsRequestor;
private ReaderThread reader;
private WriterThread writer;
public StateMachine()
{
super(org.xnap.util.State.DISCONNECTED, TRANSITION_TABLE);
}
public WriterThread getWriter()
{
return writer;
}
public void readerStopped()
{
synchronized (this) {
reader = null;
if (writer == null) {
GiFTDaemon.this.setState(org.xnap.util.State.DISCONNECTED);
}
}
}
public void writerStopped()
{
boolean disconnected = false;
synchronized (this) {
writer = null;
if (reader == null) {
disconnected = true;
}
}
if (disconnected) {
GiFTDaemon.this.setState(org.xnap.util.State.DISCONNECTED);
}
}
protected synchronized void stateChanged(State oldState,
State newState)
{
if (newState == org.xnap.util.State.CONNECTING) {
downloads = new Hashtable();
uploads = new Hashtable();
searches = new Hashtable();
message = null;
stats = null;
reader = new ReaderThread();
reader.start();
}
else if (newState == org.xnap.util.State.CONNECTED) {
writer = new WriterThread(reader.getOutputStream());
writer.start();
Command cmd = new Command("attach");
cmd.addKey("client", "XNap");
cmd.addKey("version",
GiFTPlugin.getInstance().getInfo().getVersion());
if (username != null) {
cmd.addKey("profile", username);
}
queueCommand(cmd);
statsRequestor = new StatsRequestorTask();
Scheduler.run(0, NETWORK_STATS_INTERVAL, statsRequestor);
}
else if (newState == org.xnap.util.State.DISCONNECTING) {
statsRequestor.cancel();
statsRequestor = null;
reader.die();
if (writer != null) {
writer.die();
}
}
else if (newState == org.xnap.util.State.DISCONNECTED) {
for (Iterator i = downloads.values().iterator(); i.hasNext();) {
DownloadManager.getInstance().remove
((GiFTDownloadContainer)i.next());
}
for (Iterator i = uploads.values().iterator(); i.hasNext();) {
UploadManager.getInstance().remove
((GiFTUpload)i.next());
}
// fix: remove uploads
}
}
}
private static class GIDManager {
public static int MIN_GID = 1;
/**
* The maximum allowed gid to be used by the client as a session-id
* as confirmed by jasta in irc chat.
*/
public static int MAX_GID = 32767; // 2^15 - 1
public static int last = MIN_GID - 1;
/**
* Returns a unique id.
*/
public static int createGID()
{
return (++last > MAX_GID) ? last = MIN_GID : last;
}
}
/**
* Resends the download request in regular intervals.
*/
private class StatsRequestorTask extends XNapTask
{
public StatsRequestorTask()
{
}
public void run()
{
updateNetworkStats();
}
}
}
The table below shows all metrics for GiFTDaemon.java.




