QueryResultItem.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.furthurnet.xmlparser |
![]() |
![]() |
Furthurnet |
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.
/*
* FURTHUR - A distributed peer-to-peer file sharing system.
* Copyright (C) 2001-2002 Jamie M. Addessi, Furthur Network
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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.furthurnet.xmlparser;
import java.util.Vector;
public class QueryResultItem
{
private String name;
private String saveName;
public String id;
public String signature;
public String user;
public String size;
public String speed;
public boolean firewall;
public String remoteId;
public String openPort;
public String remoteVer;
public String remoteUserNick = null;
public boolean canAcceptAntelope = false;
public String pcpIp;
public String pcpPort;
// public String pcpPassword;
public String xml;
public String networkType;
public long bytesReceived = 0; // for partials only
public String status = "Stopped"; // for partials only
public long lastAutoRetry = 0; // for partials only, assume never
public long lastConnectAttempt = 0; // for partials only
public String saveLoc = null;
public AttributeSet attributes = null;
public Vector hostList = null; // the list of QueryResultItems that reference this same file set on multiple hosts, should contain the original (this)
public Vector additionalVersionHosts = null; // the list of QueryResultItems that reference this same file set but with different md5Ids on multiple hosts
public static final int NO_MATCH = 0;
public static final int ALREADY_HAVE_SHARED = 1;
public static final int ALREADY_DOWNLOADING = 2;
public int localMatches = 0;
private long lastHostListUpdate = System.currentTimeMillis();
private static long hostListUpdatePeriod = 2 * 60 * 60 * 1000; // only update the host list once every 2 hours.
private int numErrors = 0;
private int maxErrors = 3;
private long lastActivity = 0; /* When the last activity occurred */
/**
* Set LastActivity to timestamp
* @param timeStamp
*/
public void setLastActivity(long timeStamp)
{
lastActivity = timeStamp;
}
/**
* Set lastAutoRetry to timestamp
* @param timestamp
*/
public void setLastAutoRetry(long timestamp)
{
lastAutoRetry = timestamp;
}
public long getLastActivity()
{
return lastActivity;
}
public void incNumErrors()
{
++numErrors;
}
public boolean needsHostUpdate()
{
long ctime = System.currentTimeMillis();
if ((ctime - lastHostListUpdate > hostListUpdatePeriod) ||
(numErrors >= maxErrors)) {
numErrors = 0;
return true;
}
return false;
}
public void hostListUpdated()
{
lastHostListUpdate = System.currentTimeMillis();
}
public QueryResultItem(XmlObject obj, String _networkType) throws XmlException
{
hostList = new Vector();
hostList.add(this);
additionalVersionHosts = new Vector();
if (! obj.getName().equals(XmlTags.QUERY_RESULT_ITEM))
throw new XmlException("Could not read element " + XmlTags.QUERY_RESULT_ITEM);
else
{
xml = obj.getXml();
networkType = _networkType;
for (int i = 0; i < obj.numAttributes(); i++)
{
XmlObject attrObj = obj.getAttribute(i);
if (attrObj.getName().equals(XmlTags.NAME))
name = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.ID))
id = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.SIGNATURE))
signature = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.USER))
user = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.SIZE))
size = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.SPEED))
speed = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.FIREWALL))
firewall = (new String("F").equals(attrObj.getValue()) || new String("Y").equals(attrObj.getValue()));
else if (attrObj.getName().equals(XmlTags.REMOTE_ID))
remoteId = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.FURTHUR_VERSION))
remoteVer = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.PCP_IP))
pcpIp = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.PCP_PORT))
pcpPort = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.OPEN_PORT))
openPort = attrObj.getValue();
// else if (attrObj.getName().equals(XmlTags.PCP_PASSWORD))
// pcpPassword = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.NICKNAME))
remoteUserNick = attrObj.getValue();
else if (attrObj.getName().equals(XmlTags.ANTELOPE))
canAcceptAntelope = new String("Y").equals(attrObj.getValue());
else if (attrObj.getName().equals(XmlTags.INPUT_SPEC))
attributes = new AttributeSet(attrObj);
}
//System.out.println("Adding to hostList: " + this.pcpIp + ":" + this.pcpPort + " (can accept Antelope = " + this.canAcceptAntelope + ")");
}
}
public void additionalSourceFound(QueryResultItem match)
{
for (int i=0; i<hostList.size(); i++)
if (((QueryResultItem)hostList.elementAt(i)).remoteId.equals(match.remoteId))
return; // make sure it's not the same host
//System.out.println("Adding addtl to hostList: " + match.pcpIp + ":" + match.pcpPort + " (can accept Antelope = " + match.canAcceptAntelope + ")");
hostList.add(match);
}
public QueryResultItem additionalVersionFoundIsBetter(QueryResultItem match)
{
for (int i=0; i<additionalVersionHosts.size(); i++)
{
QueryResultItem next = ((QueryResultItem)additionalVersionHosts.elementAt(i));
if (next.id.equals(match.id))
{
next.additionalSourceFound(match);
return isBetterVersion(next);
}
}
additionalVersionHosts.add(match);
return null;
}
private QueryResultItem isBetterVersion(QueryResultItem test)
{
// a query result item will keep track of other items with the same name but
// different ids. if one of them ends up with more hosts than this, then it
// should replace this, so we'd return a link to it.
int testAvail = test.getAvailableHostCount();
int myAvail = getAvailableHostCount();
if (testAvail > myAvail)
return test;
else if (testAvail < myAvail)
return null;
else
{
int testTotal = test.getTotalHosts();
int myTotal = getTotalHosts();
if (testTotal > myTotal)
return test;
else if (testTotal < myTotal)
return null;
else
{
double testSpeed = new Double(test.speed).doubleValue();
double mySpeed = new Double(speed).doubleValue();
if (testSpeed < mySpeed)
return test;
else
return null;
}
}
}
public int getTotalHosts()
{
return hostList.size();
}
public int getAvailableHostCount()
{
// this should only count connections that are available for antelope
// but make sure it is at least one, since a pcp connection will override
// an antelope connection.
int total = 0;
if (hostList == null)
return 1;
for (int i=0; i<hostList.size(); i++)
{
if (((QueryResultItem)hostList.elementAt(i)).canAcceptAntelope)
total++;
}
if (total == 0)
return 1;
else
return total;
}
public String getName()
{
return name;
}
public void setName(String _name)
{
name = _name;
}
public String getSaveName()
{
return saveName;
}
public void setSaveName(String _saveName)
{
saveName = _saveName;
}
public String getXml()
{
return xml;
}
}
The table below shows all metrics for QueryResultItem.java.




