LimeWire Consolidated API

com.limegroup.gnutella
Class UDPService

java.lang.Object
  extended by com.limegroup.gnutella.UDPService
All Implemented Interfaces:
IOErrorObserver, ReadObserver, ReadWriteObserver, Shutdownable, WriteObserver

public class UDPService
extends Object
implements ReadWriteObserver

This class handles UDP messaging services. It both sends and receives messages, routing received messages to their appropriate handlers. This also handles issues related to the GUESS proposal, such as making sure that the UDP and TCP port match and sending UDP acks for queries.

See Also:
UDPReplyHandler, MessageRouter, QueryUnicaster

Constructor Summary
UDPService(NetworkManager networkManager, com.google.inject.Provider<MessageDispatcher> messageDispatcher, com.google.inject.Provider<IPFilter> ipFilter, com.google.inject.Provider<ConnectionManager> connectionManager, com.google.inject.Provider<MessageRouter> messageRouter, com.google.inject.Provider<Acceptor> acceptor, com.google.inject.Provider<QueryUnicaster> queryUnicaster, ScheduledExecutorService backgroundExecutor, ConnectionServices connectionServices, MessageFactory messageFactory, PingRequestFactory pingRequestFactory, NetworkInstanceUtils networkInstanceUtils, EventBroadcaster<FirewallTransferStatusEvent> fwtStatusBroadcaster, ListenerSupport<UDPSocketChannelConnectionEvent> channelEventListenerSupport)
           
 
Method Summary
 boolean canDoFWT()
           
 boolean canReceiveSolicited()
          Returns whether or not this node is capable of receiving SOLICITED UDP packets.
 boolean canReceiveUnsolicited()
          Returns whether or not this node is capable of receiving UNSOLICITED UDP packets.
 GUID getConnectBackGUID()
           
 long getLastReceivedTime()
           
 GUID getSolicitedGUID()
           
 int getStableUDPPort()
           
 void handleIOException(IOException iox)
          Notification that an IOException occurred while reading/writing.
 void handleRead()
          Notification that a read can happen.
 boolean handleWrite()
          Notification that a write can happen.
 boolean isGUESSCapable()
          Returns whether or not this node is capable of sending its own GUESS queries.
 boolean isListening()
          Returns whether or not the UDP socket is listening for incoming messsages.
 int lastReportedPort()
           
static void mutateGUID(byte[] guid, InetAddress ip, int port)
           
 DatagramSocket newListeningSocket(int port)
          Returns a new DatagramSocket that is bound to the given port.
 boolean portStable()
           
protected  void processMessage(Message message, InetSocketAddress addr)
          Processes a single message.
 int receivedIpPong()
           
protected  void scheduleServices()
          Schedules IncomingValidator & PeriodicPinger for periodic use.
 void send(ByteBuffer buffer, InetSocketAddress addr, boolean custom)
           
 void send(Message msg, InetAddress ip, int port)
          Sends the Message via UDP to the port and IP address specified.
 void send(Message msg, InetSocketAddress addr)
          Sends the specified Message to the specified host.
 void send(Message msg, IpPort host)
          Sends the specified Message to the specified host.
 void setListeningSocket(DatagramSocket datagramSocket)
          Changes the DatagramSocket used for sending/receiving.
 void setReceiveSolicited(boolean value)
          Sets whether or not this node is capable of receiving SOLICITED UDP packets.
 void shutdown()
          Shuts down this service.
 void start()
          Starts listening for UDP messages & allowing UDP messages to be written.
 String toString()
          Overrides Object.toString to give more informative information about the class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UDPService

@Inject
public UDPService(NetworkManager networkManager,
                         com.google.inject.Provider<MessageDispatcher> messageDispatcher,
                         com.google.inject.Provider<IPFilter> ipFilter,
                         com.google.inject.Provider<ConnectionManager> connectionManager,
                         com.google.inject.Provider<MessageRouter> messageRouter,
                         com.google.inject.Provider<Acceptor> acceptor,
                         com.google.inject.Provider<QueryUnicaster> queryUnicaster,
                         ScheduledExecutorService backgroundExecutor,
                         ConnectionServices connectionServices,
                         MessageFactory messageFactory,
                         PingRequestFactory pingRequestFactory,
                         NetworkInstanceUtils networkInstanceUtils,
                         EventBroadcaster<FirewallTransferStatusEvent> fwtStatusBroadcaster,
                         ListenerSupport<UDPSocketChannelConnectionEvent> channelEventListenerSupport)
Method Detail

scheduleServices

protected void scheduleServices()
Schedules IncomingValidator & PeriodicPinger for periodic use.


getConnectBackGUID

public GUID getConnectBackGUID()
Returns:
The GUID to send for UDPConnectBack attempts....

getSolicitedGUID

public GUID getSolicitedGUID()
Returns:
The GUID to send for Solicited Ping attempts....

start

public void start()
Starts listening for UDP messages & allowing UDP messages to be written.


newListeningSocket

public DatagramSocket newListeningSocket(int port)
                                  throws IOException
Returns a new DatagramSocket that is bound to the given port. This value should be passed to setListeningSocket(DatagramSocket) to commit to the new port. If setListeningSocket is NOT called, you should close the return socket.

Returns:
a new DatagramSocket that is bound to the specified port.
Throws:
IOException - Thrown if the DatagramSocket could not be created.

setListeningSocket

public void setListeningSocket(DatagramSocket datagramSocket)
Changes the DatagramSocket used for sending/receiving. Typically called by Acceptor to commit to the new port.

Parameters:
datagramSocket - the new listening socket, which must be be the return value of newListeningSocket(int). A value of null disables UDP sending and receiving.

shutdown

public void shutdown()
Shuts down this service.

Specified by:
shutdown in interface Shutdownable

handleRead

public void handleRead()
                throws IOException
Notification that a read can happen.

Specified by:
handleRead in interface ReadObserver
Throws:
IOException

handleIOException

public void handleIOException(IOException iox)
Notification that an IOException occurred while reading/writing.

Specified by:
handleIOException in interface IOErrorObserver

processMessage

protected void processMessage(Message message,
                              InetSocketAddress addr)
Processes a single message.


mutateGUID

public static void mutateGUID(byte[] guid,
                              InetAddress ip,
                              int port)

send

public void send(Message msg,
                 IpPort host)
Sends the specified Message to the specified host.

Parameters:
msg - the Message to send
host - the host to send the message to

send

public void send(Message msg,
                 InetAddress ip,
                 int port)
Sends the Message via UDP to the port and IP address specified. This method should not be called if the client is not GUESS enabled.

Parameters:
msg - the Message to send
ip - the InetAddress to send to
port - the port to send to
err - an ErrorCallback if you want to be notified errors
Throws:
IllegalArgumentException - if msg, ip, or err is null.

send

public void send(Message msg,
                 InetSocketAddress addr)
Sends the specified Message to the specified host.

Parameters:
msg - the Message to send
host - the host to send the message to

send

public void send(ByteBuffer buffer,
                 InetSocketAddress addr,
                 boolean custom)

handleWrite

public boolean handleWrite()
                    throws IOException
Notification that a write can happen.

Specified by:
handleWrite in interface WriteObserver
Returns:
true there is still data to be written, otherwise false.
Throws:
IOException

isGUESSCapable

public boolean isGUESSCapable()
Returns whether or not this node is capable of sending its own GUESS queries. This would not be the case only if this node has not successfully received an incoming UDP packet.

Returns:
true if this node is capable of running its own GUESS queries, false otherwise

canReceiveUnsolicited

public boolean canReceiveUnsolicited()
Returns whether or not this node is capable of receiving UNSOLICITED UDP packets. It is false until a UDP ConnectBack ping has been received.

Returns:
true if this node has accepted a UNSOLICITED UDP packet.

canReceiveSolicited

public boolean canReceiveSolicited()
Returns whether or not this node is capable of receiving SOLICITED UDP packets.

Returns:
true if this node has accepted a SOLICITED UDP packet.

canDoFWT

public boolean canDoFWT()
Returns:
whether this node can do Firewall-to-firewall transfers. Until we get back any udp packet, the answer is no. If we have received an udp packet but are not connected, or haven't received a pong carrying ip info yet, see if we ever disabled fwt in the past. If we are connected and have gotten a single ip pong, our port must be the same as our tcp port or our forced tcp port. If we have received more than one ip pong, they must all report the same port.

portStable

public boolean portStable()

receivedIpPong

public int receivedIpPong()

lastReportedPort

public int lastReportedPort()

getStableUDPPort

public int getStableUDPPort()
Returns:
the stable UDP port as seen from the outside. If we have received more than one IPPongs and they report the same port, we return that. If we have received just one IPpong, and if its address matches either our local port or external port, return that. If we have not received any IPpongs, return whatever RouterService thinks our port is.

setReceiveSolicited

public void setReceiveSolicited(boolean value)
Sets whether or not this node is capable of receiving SOLICITED UDP packets. This is useful for testing UDPConnections.


getLastReceivedTime

public long getLastReceivedTime()

isListening

public boolean isListening()
Returns whether or not the UDP socket is listening for incoming messsages.

Returns:
true if the UDP socket is listening for incoming UDP messages, false otherwise

toString

public String toString()
Overrides Object.toString to give more informative information about the class.

Overrides:
toString in class Object
Returns:
the DatagramSocket data

LimeWire Consolidated API

Copyright © 2009. All Rights Reserved.