LimeWire Consolidated API

com.limegroup.gnutella
Class HostCatcher

java.lang.Object
  extended by com.limegroup.gnutella.HostCatcher
All Implemented Interfaces:
Bootstrapper.Listener, Service

public class HostCatcher
extends Object
implements Service, Bootstrapper.Listener

The host catcher collects the addresses of Gnutella and DHT hosts from ping replies and bootstrap servers. Collected addresses are stored in a file between sessions. The servent may attempt to connect to these addresses as necessary to maintain full connectivity. Hosts that are known to be ultrapeers are preferred when caching and returning addresses.


Nested Class Summary
static interface HostCatcher.EndpointObserver
          Simple callback for having an endpoint added.
 
Field Summary
protected static int EXPIRED_HOSTS_SIZE
           
protected static int PROBATION_HOSTS_SIZE
           
 
Constructor Summary
protected HostCatcher(ScheduledExecutorService backgroundExecutor, ConnectionServices connectionServices, com.google.inject.Provider<ConnectionManager> connectionManager, com.google.inject.Provider<UDPService> udpService, com.google.inject.Provider<DHTManager> dhtManager, com.google.inject.Provider<QueryUnicaster> queryUnicaster, com.google.inject.Provider<IPFilter> ipFilter, UniqueHostPinger uniqueHostPinger, NetworkInstanceUtils networkInstanceUtils, Bootstrapper bootstrapper)
           
 
Method Summary
 int add(Collection<? extends Endpoint> endpoints)
          Adds a collection of hosts to the catcher.
 boolean add(Endpoint e, boolean forceHighPriority)
          Adds a host, possibly ejecting others from the cache.
 boolean add(Endpoint e, boolean forceHighPriority, String locale)
          Adds a host with known locale.
 boolean add(PingReply pr)
          Adds hosts from a ping reply, possibly ejecting others from the cache.
 void connect()
          Informs the host catcher that we have (re)connected to the network; reads the host file and starts pinging.
 void doneWithConnect(Endpoint e, boolean success)
          Notifies the catcher that a connection fetcher has finished attempting a connection to the given host.
 void expireHost(Endpoint host)
          Adds the specified host to the group of expired hosts.
protected  Endpoint getAnEndpoint()
          Removes and returns a host, blocking if one is not immediately available.
 void getAnEndpoint(HostCatcher.EndpointObserver observer)
          Passes the next available host to the given EndpointObserver.
 Endpoint getAnEndpointImmediate(HostCatcher.EndpointObserver observer)
          Returns a host immediately if one is available.
protected  ExtendedEndpoint getAnEndpointInternal()
          Removes and returns the best available host, or null if no hosts are available.
 List<ExtendedEndpoint> getDHTSupportEndpoint(int minVersion)
          Gets a (possibly empty) list of hosts that support the DHT.
 int getNumHosts()
          Returns the number of hosts the catcher knows about.
protected  int getNumUltrapeerHosts()
          Returns the number of hosts that are known to be ultrapeers.
protected  Iterator<ExtendedEndpoint> getPermanentHosts()
          Returns an iterator of the set of permanent hosts.
 String getServiceName()
           
 Collection<IpPort> getUltrapeersWithFreeLeafSlots(String locale, int num)
          Returns a collection containing up to the specified number of hosts that have advertised free leaf slots, matching the specified locale if possible.
 Collection<IpPort> getUltrapeersWithFreeUltrapeerSlots(String locale, int num)
          Returns a collection containing up to the specified number of hosts that have advertised free ultrapeer slots, matching the specified locale if possible.
 int handleHosts(Collection<? extends Endpoint> hosts)
          Receives hosts from the bootstrapper, returning the number used.
 void initialize()
           
 boolean isHostTLSCapable(IpPort ipp)
          Returns true if the given host is known to be TLS-capable.
 boolean isValidHost(Endpoint host)
          Determines whether a host is valid for adding to the catcher.
 boolean needsHosts()
          Returns true if the host catcher needs hosts from the bootstrapper.
 void noInternetConnection()
          Resets all recorded failures on the assumption that they were caused by having no internet connection
 void putHostOnProbation(Endpoint host)
          Adds the specified host to the group of hosts currently on "probation." These are hosts that are on the network but that have rejected a connection attempt.
 void removeEndpointObserver(HostCatcher.EndpointObserver observer)
          Removes an observer that is waiting for an endpoint.
 void reset()
          Resets the state of the host catcher (only for testing).
 void sendMessageToAllHosts(Message m, MessageListener listener, Cancellable c)
          Sends a ping to every host the catcher knows about (used for discovering DHT-capable hosts).
 void start()
          Schedules background threads.
 void stop()
          Shuts down background threads and saves the host file.
protected  void write()
          Writes the host file to the default location.
protected  void write(File hostFile)
          Writes hosts to the specified file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EXPIRED_HOSTS_SIZE

protected static final int EXPIRED_HOSTS_SIZE
See Also:
Constant Field Values

PROBATION_HOSTS_SIZE

protected static final int PROBATION_HOSTS_SIZE
See Also:
Constant Field Values
Constructor Detail

HostCatcher

@Inject
protected HostCatcher(ScheduledExecutorService backgroundExecutor,
                             ConnectionServices connectionServices,
                             com.google.inject.Provider<ConnectionManager> connectionManager,
                             com.google.inject.Provider<UDPService> udpService,
                             com.google.inject.Provider<DHTManager> dhtManager,
                             com.google.inject.Provider<QueryUnicaster> queryUnicaster,
                             com.google.inject.Provider<IPFilter> ipFilter,
                             UniqueHostPinger uniqueHostPinger,
                             NetworkInstanceUtils networkInstanceUtils,
                             Bootstrapper bootstrapper)
Method Detail

start

public void start()
Schedules background threads.

Specified by:
start in interface Service

getServiceName

public String getServiceName()
Specified by:
getServiceName in interface Service

initialize

public void initialize()
Specified by:
initialize in interface Service

stop

public void stop()
Shuts down background threads and saves the host file.

Specified by:
stop in interface Service

connect

public void connect()
Informs the host catcher that we have (re)connected to the network; reads the host file and starts pinging.


sendMessageToAllHosts

public void sendMessageToAllHosts(Message m,
                                  MessageListener listener,
                                  Cancellable c)
Sends a ping to every host the catcher knows about (used for discovering DHT-capable hosts).


getDHTSupportEndpoint

public List<ExtendedEndpoint> getDHTSupportEndpoint(int minVersion)
Gets a (possibly empty) list of hosts that support the DHT. Active nodes are returned first, then passive nodes, then inactive nodes. Note: this method is slow and is not meant to be used often.

Parameters:
minVersion - the minimum DHT version, 0 to return all versions.

write

protected void write()
Writes the host file to the default location. Package access for testing.


write

protected void write(File hostFile)
              throws IOException
Writes hosts to the specified file. Package access for testing.

Throws:
IOException

add

public boolean add(PingReply pr)
Adds hosts from a ping reply, possibly ejecting others from the cache. Also adds any UHCs in the ping reply to the UDPHostCache, active and passive DHT nodes to the DHT manager, and unicast endpoints to the unicast manager.

Returns:
true if any hosts were added to the catcher

add

public int add(Collection<? extends Endpoint> endpoints)
Adds a collection of hosts to the catcher.

Returns:
the number of hosts added

add

public boolean add(Endpoint e,
                   boolean forceHighPriority)
Adds a host, possibly ejecting others from the cache.

Parameters:
e - the host to add
forceHighPriority - true if the host should have high priority
Returns:
true if the host was added

add

public boolean add(Endpoint e,
                   boolean forceHighPriority,
                   String locale)
Adds a host with known locale. This is used when a handshake is rejected because of the locale.

Returns:
true if the host was added

isValidHost

public boolean isValidHost(Endpoint host)
Determines whether a host is valid for adding to the catcher.


isHostTLSCapable

public boolean isHostTLSCapable(IpPort ipp)
Returns true if the given host is known to be TLS-capable.


getAnEndpoint

public void getAnEndpoint(HostCatcher.EndpointObserver observer)
Passes the next available host to the given EndpointObserver. If no host is immediately available, the observer is added to a list of waiting observers.


getAnEndpointImmediate

public Endpoint getAnEndpointImmediate(HostCatcher.EndpointObserver observer)
Returns a host immediately if one is available. Otherwise the observer is added to a list of waiting observers and null is returned. If the observer is null and no host is available, this method returns null and schedules no future callback.


removeEndpointObserver

public void removeEndpointObserver(HostCatcher.EndpointObserver observer)
Removes an observer that is waiting for an endpoint.


getAnEndpoint

protected Endpoint getAnEndpoint()
                          throws InterruptedException
Removes and returns a host, blocking if one is not immediately available. Package access for testing.

Throws:
InterruptedException - if the calling thread is interrupted

doneWithConnect

public void doneWithConnect(Endpoint e,
                            boolean success)
Notifies the catcher that a connection fetcher has finished attempting a connection to the given host. This allows the catcher to update the host's connection history.


getAnEndpointInternal

protected ExtendedEndpoint getAnEndpointInternal()
Removes and returns the best available host, or null if no hosts are available. Protected so tests can override it. LOCKING: this


getNumHosts

public int getNumHosts()
Returns the number of hosts the catcher knows about.


getNumUltrapeerHosts

protected int getNumUltrapeerHosts()
Returns the number of hosts that are known to be ultrapeers. Package access for testing.


getPermanentHosts

protected Iterator<ExtendedEndpoint> getPermanentHosts()
Returns an iterator of the set of permanent hosts. Package access for testing. THIS MUST NOT BE MODIFIED WHILE THE ITERATOR IS IN USE.


getUltrapeersWithFreeUltrapeerSlots

public Collection<IpPort> getUltrapeersWithFreeUltrapeerSlots(String locale,
                                                              int num)
Returns a collection containing up to the specified number of hosts that have advertised free ultrapeer slots, matching the specified locale if possible. The returned collection can be modified.


getUltrapeersWithFreeLeafSlots

public Collection<IpPort> getUltrapeersWithFreeLeafSlots(String locale,
                                                         int num)
Returns a collection containing up to the specified number of hosts that have advertised free leaf slots, matching the specified locale if possible. The returned collection can be modified.


noInternetConnection

public void noInternetConnection()
Resets all recorded failures on the assumption that they were caused by having no internet connection


reset

public void reset()
Resets the state of the host catcher (only for testing).


putHostOnProbation

public void putHostOnProbation(Endpoint host)
Adds the specified host to the group of hosts currently on "probation." These are hosts that are on the network but that have rejected a connection attempt. They will periodically be re-activated as needed.


expireHost

public void expireHost(Endpoint host)
Adds the specified host to the group of expired hosts. These are hosts that we have been unable to create a TCP connection to, let alone a Gnutella connection.


needsHosts

public boolean needsHosts()
Returns true if the host catcher needs hosts from the bootstrapper.

Specified by:
needsHosts in interface Bootstrapper.Listener

handleHosts

public int handleHosts(Collection<? extends Endpoint> hosts)
Receives hosts from the bootstrapper, returning the number used.

Specified by:
handleHosts in interface Bootstrapper.Listener

LimeWire Consolidated API

Copyright © 2009. All Rights Reserved.