net.tomp2p.peers
Class PeerAddress

java.lang.Object
  extended by net.tomp2p.peers.PeerAddress
All Implemented Interfaces:
Serializable, Comparable<PeerAddress>

public final class PeerAddress
extends Object
implements Comparable<PeerAddress>, Serializable

A PeerAddress contains the node ID and how to contact this node using both TCP and UDP. This class is thread safe (or it does not matter if its not). The serialized size of this class is for IPv4 (20 + 4 + 1 + 4=29), for IPv6 (20 + 4 + 1 + 16=41)

Author:
Thomas Bocek
See Also:
Serialized Form

Field Summary
static int SIZE_IP_SOCKv4
           
static int SIZE_IP_SOCKv6
           
static int SIZE_IPv4
           
static int SIZE_IPv6
           
 
Constructor Summary
PeerAddress(byte[] me)
          Creates a new peeraddress, where the byte array has to be in the rigth format and in the rigth size.
PeerAddress(byte[] me, int offset)
          Creates a PeerAddress from a continuous byte array.
PeerAddress(Number160 id)
          This is usually used for debugging, the address will be null and ports -1
PeerAddress(Number160 id, InetAddress address, int portTCP, int portUDP)
           
PeerAddress(Number160 id, InetAddress address, int portTCP, int portUDP, boolean forwarded, boolean firewalledUDP, boolean firewalledTCP)
          Creates a PeerAddress
PeerAddress(Number160 id, InetAddress address, int portTCP, int portUDP, byte optionType)
           
PeerAddress(Number160 id, InetSocketAddress inetSocketAddress)
           
PeerAddress(Number160 id, PeerAddress parent)
           
 
Method Summary
 int compareTo(PeerAddress nodeAddress)
           
 PeerAddress copyWithDifferentId(Number160 id2)
           
 InetSocketAddress createSocketTCP()
          Returns the socket address.
 InetSocketAddress createSocketUDP()
          Returns the socket address.
 byte createType()
           
 boolean equals(Object obj)
           
static int expectedLength(int type)
           
 PeerAddress forward(InetAddress inetAddress)
           
 Number160 getID()
          The id of the peer.
 InetAddress getInetAddress()
          Returns the address or null if no address set
 byte[] getSocketAddress()
           
 int getSocketAddress(byte[] me, int offset)
           
 int getSocketAddressSize()
           
 int hashCode()
           
 boolean isFirewalledTCP()
           
static boolean isFirewalledTCP(int type)
           
 boolean isFirewalledUDP()
           
static boolean isFirewalledUDP(int type)
           
static boolean isForward(int type)
           
 boolean isForwarded()
           
 boolean isIPv6()
           
static boolean isNet6(int type)
           
 PeerAddress notFirewalledTCP()
           
 PeerAddress notFirewalledUDP()
           
 int offset()
          When deserializeg, we need to know how much we deserialized from the constructor call.
 PeerAddress ports(int portUDP, int portTCP)
           
 int portTCP()
           
 int portUDP()
           
 byte[] toByteArray()
          Serializes to a new array with the proper size
 int toByteArray(byte[] me, int offset)
          Serializes to an existing array.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SIZE_IP_SOCKv6

public static final int SIZE_IP_SOCKv6
See Also:
Constant Field Values

SIZE_IP_SOCKv4

public static final int SIZE_IP_SOCKv4
See Also:
Constant Field Values

SIZE_IPv6

public static final int SIZE_IPv6
See Also:
Constant Field Values

SIZE_IPv4

public static final int SIZE_IPv4
See Also:
Constant Field Values
Constructor Detail

PeerAddress

public PeerAddress(byte[] me)
            throws UnknownHostException
Creates a new peeraddress, where the byte array has to be in the rigth format and in the rigth size. The new offset can be accessed with offset().

Parameters:
me - The serialized array
Throws:
UnknownHostException - Using InetXAddress.getByAddress creates this exception.

PeerAddress

public PeerAddress(byte[] me,
                   int offset)
            throws UnknownHostException
Creates a PeerAddress from a continuous byte array. This is useful if you don't know the size beforehand. The new offset can be accessed with offset().

Parameters:
me - The serialized array
offset - the offset, where to start
Throws:
UnknownHostException - Using InetXAddress.getByAddress creates this exception.

PeerAddress

public PeerAddress(Number160 id)
This is usually used for debugging, the address will be null and ports -1

Parameters:
id - The id of the peer

PeerAddress

public PeerAddress(Number160 id,
                   InetAddress address,
                   int portTCP,
                   int portUDP,
                   boolean forwarded,
                   boolean firewalledUDP,
                   boolean firewalledTCP)
Creates a PeerAddress

Parameters:
id - The id of the peer
address - The address of the peer, how to reach this peer
portTCP - The tcp port how to reach the peer
portUDP - The udp port how to reach the peer

PeerAddress

public PeerAddress(Number160 id,
                   InetAddress address,
                   int portTCP,
                   int portUDP)

PeerAddress

public PeerAddress(Number160 id,
                   InetSocketAddress inetSocketAddress)

PeerAddress

public PeerAddress(Number160 id,
                   PeerAddress parent)

PeerAddress

public PeerAddress(Number160 id,
                   InetAddress address,
                   int portTCP,
                   int portUDP,
                   byte optionType)
Method Detail

offset

public int offset()
When deserializeg, we need to know how much we deserialized from the constructor call.

Returns:
The new offset

toByteArray

public byte[] toByteArray()
Serializes to a new array with the proper size

Returns:
The serialized representation.

toByteArray

public int toByteArray(byte[] me,
                       int offset)
Serializes to an existing array. Please note, that a serialized peeraddress can never start with 0 for the first byte

Parameters:
me - The array where the result should be stored
offset - The offset where to start to save the result in the byte array
Returns:
The new offest.

getSocketAddress

public byte[] getSocketAddress()

getSocketAddress

public int getSocketAddress(byte[] me,
                            int offset)

getInetAddress

public InetAddress getInetAddress()
Returns the address or null if no address set

Returns:
The address of this peer

createSocketTCP

public InetSocketAddress createSocketTCP()
Returns the socket address. The socket address will be created if necessary.

Returns:
The socket address how to reach this peer

createSocketUDP

public InetSocketAddress createSocketUDP()
Returns the socket address. The socket address will be created if necessary.

Returns:
The socket address how to reach this peer

getID

public Number160 getID()
The id of the peer. A peer cannot change its id.

Returns:
Id of the peer

createType

public byte createType()

isNet6

public static boolean isNet6(int type)

isForward

public static boolean isForward(int type)

isFirewalledTCP

public static boolean isFirewalledTCP(int type)

isFirewalledUDP

public static boolean isFirewalledUDP(int type)

expectedLength

public static int expectedLength(int type)

toString

public String toString()
Overrides:
toString in class Object

compareTo

public int compareTo(PeerAddress nodeAddress)
Specified by:
compareTo in interface Comparable<PeerAddress>

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

portTCP

public int portTCP()
Returns:
TCP port

portUDP

public int portUDP()
Returns:
UDP port

isForwarded

public boolean isForwarded()

isFirewalledUDP

public boolean isFirewalledUDP()

isFirewalledTCP

public boolean isFirewalledTCP()

isIPv6

public boolean isIPv6()

notFirewalledUDP

public PeerAddress notFirewalledUDP()

notFirewalledTCP

public PeerAddress notFirewalledTCP()

forward

public PeerAddress forward(InetAddress inetAddress)

ports

public PeerAddress ports(int portUDP,
                         int portTCP)

getSocketAddressSize

public int getSocketAddressSize()

copyWithDifferentId

public PeerAddress copyWithDifferentId(Number160 id2)


Copyright © 2011. All Rights Reserved.