OpenNapUser.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.opennap.user |
![]() |
![]() |
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.opennap.user;
import javax.swing.Action;
import org.xnap.peer.AbstractPeer;
import org.xnap.plugin.opennap.OpenNapPlugin;
import org.xnap.plugin.opennap.action.OpenNapAddToHotlistAction;
import org.xnap.plugin.opennap.action.OpenNapBrowseAction;
import org.xnap.plugin.opennap.action.OpenNapChatAction;
import org.xnap.plugin.opennap.action.OpenNapWhoisAction;
import org.xnap.plugin.opennap.net.OpenNapServer;
import org.xnap.plugin.opennap.net.msg.MessageHandler;
import org.xnap.plugin.opennap.net.msg.client.PrivateMessage;
import org.xnap.plugin.opennap.net.msg.client.WhoisRequestMessage;
import org.xnap.util.Preferences;
public class OpenNapUser extends AbstractPeer {
//--- Constant(s) ---
public static final int WHOIS_REQUERY_INTERVALL = 2 * 60 * 1000;
public static final int WHOIS_RESENT_INTERVALL = 10 * 60 * 1000;
//--- Data field(s) ---
private int connectDuration;
private boolean fakeLocalFileCount;
private int downloadCount = 0;
private String host;
private long lastSeen;
/**
* Milli seconds when last whois query for this user was sent.
*/
private long lastWhoisSent = 0;
/**
* Milli seconds when last whois query for this user was received.
*/
private long lastWhoisResponse = 0;
private String level;
/**
* The global instance of this user.
*/
private OpenNapGlobalUser parent;
private int port;
private OpenNapServer server;
private String status;
private int uploadCount = 0;
//--- Constructor(s) ---
public OpenNapUser(String name, OpenNapServer server)
{
super(name);
this.server = server;
fakeLocalFileCount = name.equals(server.getNick());
}
//--- Method(s) ---
/**
* Returns true, if this user's client support direct browse.
*/
public boolean canDirectBrowse()
{
String client = getClientInfo();
if (client != null) {
client.toLowerCase();
return (client.startsWith("xnap")
|| client.startsWith("lopster")
|| client.startsWith("2get")
|| client.startsWith("utatane")
|| client.startsWith("napchan")
|| client.startsWith("nap"));
}
return false;
}
/**
* Returns true, if the name of <code>o</code> equals the name of the user.
*/
public boolean equals(Object o)
{
if (o instanceof OpenNapUser) {
OpenNapUser user = (OpenNapUser)o;
return getName().equals(user.getName());
}
return false;
}
public Action[] getActions()
{
return new Action[] {
new OpenNapBrowseAction(this),
new OpenNapChatAction(this),
new OpenNapWhoisAction(this),
new OpenNapAddToHotlistAction(this),
};
}
public int getConnectDuration()
{
return connectDuration;
}
/**
* Returns the download count as received by a whois response.
*/
public int getDownloadCount()
{
return downloadCount;
}
public String getHost()
{
return host;
}
public long getLastSeen()
{
return lastSeen;
}
public String getLevel()
{
return level;
}
public int getLocalDownloadCount()
{
return getParent().getDownloadCount();
}
public int getLocalUploadCount()
{
return getParent().getUploadCount();
}
public OpenNapGlobalUser getParent()
{
if (parent == null) {
synchronized (this) {
if (parent == null) {
parent = OpenNapPlugin.getUserManager().get(getName());
}
}
}
return parent;
}
public int getPort()
{
return port;
}
public OpenNapServer getServer()
{
return server;
}
public String getStatus()
{
return status;
}
public int getUploadCount()
{
return uploadCount;
}
/**
* @return true, if download requests are permanently denied
*/
public boolean isDownloadDenied()
{
if (getParent().getMaxDownloads() == 0) {
return true;
}
return false;
}
/**
* @return true, if download requests are temporarily denied
*/
public boolean isDownloadLimitReached()
{
int maxDownloads = getParent().getMaxDownloads();
if (maxDownloads == OpenNapGlobalUser.TRANSFER_DEFAULT) {
Preferences prefs = Preferences.getInstance();
return prefs.getLimitDownloadsPerUser()
? getLocalDownloadCount() >= prefs.getMaxDownloadsPerUser()
: false;
}
else {
return getLocalDownloadCount() >= parent.getMaxDownloads();
}
}
/**
* Checks if peer shares enough to be allowed to download from
* us. Serves to block leechers. Automatically calls
* {@link #update()} if needed.
*
* @return true, if uploads are permanently denied
*/
public boolean isUploadDenied()
{
if (getParent().getMaxUploads() == 0) {
return true;
}
Preferences prefs = Preferences.getInstance();
if (prefs.getUseMinimumShares()) {
if (update() && getFileCount() < prefs.getMinimumShares()) {
// send message to peer
if (prefs.getSendMinimumSharesMessage()
&& !getParent().isMinimumSharesMessageSent()) {
getParent().setMinimumSharesMessageSent(true);
PrivateMessage msg = new PrivateMessage
(getName(), prefs.getMinimumSharesMessage());
MessageHandler.send(server, msg);
}
return true;
}
}
return false;
}
/**
* @return true, if uploads are temporarily denied
*/
public boolean isUploadLimitReached()
{
int maxUploads = getParent().getMaxUploads();
if (maxUploads == OpenNapGlobalUser.TRANSFER_DEFAULT) {
Preferences prefs = Preferences.getInstance();
return prefs.getLimitDownloadsPerUser()
? getLocalUploadCount() >= prefs.getMaxUploadsPerUser()
: false;
}
else {
return getLocalUploadCount() >= parent.getMaxUploads();
}
}
public boolean isUpToDate()
{
return (System.currentTimeMillis() - lastWhoisResponse
< WHOIS_RESENT_INTERVALL);
}
/**
* Called by message class when whois query was received.
*/
public void notifyWhoisReceived()
{
lastWhoisResponse = System.currentTimeMillis();
lastWhoisSent = 0;
}
public void setConnectDuration(int newValue)
{
connectDuration = newValue;
}
public void setDownloadCount(int newValue)
{
downloadCount = newValue;
}
public void setHost(String newValue)
{
this.host = newValue;
}
public void setLastSeen(long newValue)
{
lastSeen = newValue;
}
public void setLevel(String newValue)
{
level = newValue;
}
public void setPort(int newValue)
{
port = newValue;
}
/**
* Maps OpenNap types to real values.
*/
public void setLinkSpeed(int newValue)
{
switch (newValue) {
case 0:
super.setLinkSpeed(0);
break;
case 1:
super.setLinkSpeed(14);
break;
case 2:
super.setLinkSpeed(28);
break;
case 3:
super.setLinkSpeed(33);
break;
case 4:
super.setLinkSpeed(56);
break;
case 5:
super.setLinkSpeed(64);
break;
case 7:
super.setLinkSpeed(500);
break;
case 8:
super.setLinkSpeed(768);
break;
case 9:
super.setLinkSpeed(3000);
break;
case 10:
super.setLinkSpeed(5000);
break;
default:
super.setLinkSpeed(0);
}
}
public void setStatus(String newValue)
{
status = newValue;
}
public void setUploadCount(int newValue)
{
uploadCount = newValue;
}
public boolean shouldRequery()
{
return (System.currentTimeMillis() - lastWhoisSent
> WHOIS_REQUERY_INTERVALL);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(getName());
if (server != null) {
sb.append("@");
sb.append(server.getHost());
}
return sb.toString();
}
/**
* Sends a whois query to the server if not up to date.
*
* @return true, if up to date; false, if not.
*/
public boolean update(boolean force)
{
if (force || !isUpToDate()) {
if (shouldRequery()) {
MessageHandler.send(server,
new WhoisRequestMessage(getName()));
lastWhoisSent = System.currentTimeMillis();
}
return false;
}
return true;
}
public boolean update()
{
return update(false);
}
//--- Inner Class(es) ---
}
The table below shows all metrics for OpenNapUser.java.




