com.limegroup.gnutella.altlocs
Class AlternateLocation

java.lang.Object
  extended bycom.limegroup.gnutella.altlocs.AlternateLocation
All Implemented Interfaces:
java.lang.Comparable, HTTPHeaderValue

public final class AlternateLocation
extends java.lang.Object
implements HTTPHeaderValue, java.lang.Comparable

This class encapsulates the data for an alternate resource location, as specified in HUGE v0.93. This also provides utility methods for such operations as comparing alternate locations based on the date they were stored.


Field Summary
static java.lang.String ALT_VENDOR
          The vendor to use.
 
Method Summary
 int compareTo(java.lang.Object obj)
          The idea is that this is smaller than any AlternateLocation who has a greater value of _count.
static AlternateLocation create(RemoteFileDesc rfd)
          Creates a new AlternateLocation for the data stored in a RemoteFileDesc.
static AlternateLocation create(java.lang.String location)
          Constructs a new AlternateLocation instance based on the specified string argument.
static AlternateLocation create(java.lang.String location, URN urn)
          Constructs a new AlternateLocation instance based on the specified string argument and URN.
static AlternateLocation create(java.net.URL url)
          Creates a new AlternateLocation instance for the given URL instance.
static AlternateLocation create(URN urn)
          Creates a new AlternateLocation for a file stored locally with the specified URN.
 AlternateLocation createClone()
          could return null
 RemoteFileDesc createRemoteFileDesc(int size)
          Creates a new RemoteFileDesc from this AlternateLocation
 boolean equals(java.lang.Object obj)
          Overrides the equals method to accurately compare AlternateLocation instances.
 int getCount()
          Accessor to find if this has been demoted
 boolean getDemoted()
          package access, accessor to the value of _demoted
 Endpoint getHost()
          Returns the host/port of this alternate location as an endpoint.
 URN getSHA1Urn()
          Accessor for the SHA1 urn for this AlternateLocation.
 java.net.URL getUrl()
          Returns an instance of the URL instance for this alternate location.
 int hashCode()
          Overrides the hashCode method of Object to meet the contract of hashCode.
 java.lang.String httpStringValue()
          Returns a string representation of the HTTP header value for this class.
 void increment()
          increment the count.
 void setOld()
          to set this._old to true, meaning that this is a location from the old mesh.
 java.lang.String toString()
          Overrides toString to return a string representation of this AlternateLocation, namely the url and the date.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ALT_VENDOR

public static final java.lang.String ALT_VENDOR
The vendor to use.

See Also:
Constant Field Values
Method Detail

create

public static AlternateLocation create(java.lang.String location)
                                throws java.io.IOException
Constructs a new AlternateLocation instance based on the specified string argument.

Parameters:
location - a string containing a single alternate location, including a full URL for a file and an optional date
Throws:
IOException - if there is any problem constructing the new instance from the specified string, or if the argument is either null or the empty string -- we could (should?) throw NullPointerException here, but since we're already forcing the caller to catch IOException, we might as well throw in in both cases
java.io.IOException

create

public static AlternateLocation create(java.lang.String location,
                                       URN urn)
                                throws java.io.IOException
Constructs a new AlternateLocation instance based on the specified string argument and URN.

Parameters:
location - a string containing one of the following: "http://my.address.com:port#/uri-res/N2R?urn:sha:SHA1LETTERS" "1.2.3.4[:6346]" If the first is given, then the SHA1 in the string MUST match the SHA1 given.
Throws:
IOException - if there is any problem constructing the new instance.
java.io.IOException

create

public static AlternateLocation create(java.net.URL url)
                                throws java.net.MalformedURLException,
                                       java.io.IOException
Creates a new AlternateLocation instance for the given URL instance. This constructor creates an alternate location with the current date and time as its timestamp. This can be used, for example, for newly uploaded files.

Parameters:
url - the URL instance for the resource
Throws:
NullPointerException - if the url argument is null
MalformedURLException - if a copy of the supplied URL instance cannot be successfully created
IOException - if the url argument is not a valid location for any reason
java.net.MalformedURLException
java.io.IOException

create

public static AlternateLocation create(RemoteFileDesc rfd)
                                throws java.io.IOException
Creates a new AlternateLocation for the data stored in a RemoteFileDesc.

Parameters:
rfd - the RemoteFileDesc to use in creating the AlternateLocation
Returns:
a new AlternateLocation
Throws:
IOException - if the rfd does not contain a valid urn or if it's a private address
NullPointerException - if the rfd is null
IOException - if the port is invalid
java.io.IOException

create

public static AlternateLocation create(URN urn)
Creates a new AlternateLocation for a file stored locally with the specified URN.

Parameters:
urn - the URN of the locally stored file

getUrl

public java.net.URL getUrl()
Returns an instance of the URL instance for this alternate location.

Returns:
a URL instance corresponding to the URL for this alternate location, or null if an instance could not be created

getHost

public Endpoint getHost()
Returns the host/port of this alternate location as an endpoint.


getSHA1Urn

public URN getSHA1Urn()
Accessor for the SHA1 urn for this AlternateLocation.

Returns:
the SHA1 urn for the this AlternateLocation

getCount

public int getCount()
Accessor to find if this has been demoted


getDemoted

public boolean getDemoted()
package access, accessor to the value of _demoted


httpStringValue

public java.lang.String httpStringValue()
Description copied from interface: HTTPHeaderValue
Returns a string representation of the HTTP header value for this class.

Specified by:
httpStringValue in interface HTTPHeaderValue
Returns:
a string representation of the HTTP header value for this class

createRemoteFileDesc

public RemoteFileDesc createRemoteFileDesc(int size)
Creates a new RemoteFileDesc from this AlternateLocation

Parameters:
size - the size of the file for the new RemoteFileDesc -- this is necessary to make sure the download bucketing works correctly
Returns:
new RemoteFileDesc based off of this, or null if the RemoteFileDesc could not be created

increment

public void increment()
increment the count.

See Also:
demote

setOld

public void setOld()
to set this._old to true, meaning that this is a location from the old mesh.


createClone

public AlternateLocation createClone()
could return null


equals

public boolean equals(java.lang.Object obj)
Overrides the equals method to accurately compare AlternateLocation instances. AlternateLocations are equal if their URLs are equal.

Parameters:
obj - the Object instance to compare to
Returns:
true if the URL of this AlternateLocation is equal to the URL of the AlternateLocation location argument, and otherwise returns false

compareTo

public int compareTo(java.lang.Object obj)
The idea is that this is smaller than any AlternateLocation who has a greater value of _count. There is one exception to this rule -- a demoted AlternateLocation has a higher value irrespective of count.

This is because we want to have a sorted set of AlternateLocation where any demoted AlternateLocation is put at the end of the list because it probably does not work.

Further we want to get AlternateLocations with smaller counts to be propogated more, since this will serve to get better load balancing of uploader.

Specified by:
compareTo in interface java.lang.Comparable

hashCode

public int hashCode()
Overrides the hashCode method of Object to meet the contract of hashCode. Since we override equals, it is necessary to also override hashcode to ensure that two "equal" alternate locations return the same hashCode, less we unleash unknown havoc on the hash-based collections.

Returns:
a hash code value for this object

toString

public java.lang.String toString()
Overrides toString to return a string representation of this AlternateLocation, namely the url and the date.

Returns:
the string representation of this alternate location