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 PeerAddress EMPTY_IPv4
           
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 right size.
PeerAddress(byte[] peerAddress, byte[] socketAddress)
          The format of the peer address can also be split.
PeerAddress(byte[] me, int offset)
          Creates a PeerAddress from a continuous byte array.
PeerAddress(Number160 id)
          If you only need to know the id
PeerAddress(Number160 id, byte[] me, int offset)
           
PeerAddress(Number160 id, org.jboss.netty.buffer.ChannelBuffer channelBuffer)
           
PeerAddress(Number160 id, InetAddress address)
          If you only need to know the id and InetAddress
PeerAddress(Number160 id, InetAddress address, int portTCP, int portUDP)
           
PeerAddress(Number160 id, InetAddress address, int portTCP, int portUDP, boolean firewalledUDP, boolean firewalledTCP)
          Creates a PeerAddress
PeerAddress(Number160 id, InetAddress address, int portTCP, int portUDP, int options)
           
PeerAddress(Number160 id, InetSocketAddress inetSocketAddress)
           
PeerAddress(Number160 id, PeerAddress parent)
           
PeerAddress(Number160 id, String address, int portTCP, int portUDP)
           
 
Method Summary
 PeerAddress changeAddress(InetAddress address)
           
 PeerAddress changeFirewalledTCP(boolean status)
           
 PeerAddress changeFirewalledUDP(boolean status)
           
 PeerAddress changePeerId(Number160 id2)
           
 PeerAddress changePorts(int portUDP, int portTCP)
           
 int compareTo(PeerAddress nodeAddress)
           
 InetSocketAddress createSocketTCP()
          Returns the socket address.
 InetSocketAddress createSocketUDP()
          Returns the socket address.
 boolean equals(Object obj)
           
 int expectedLength()
           
static int expectedLength(int options)
           
static int expectedSocketLength(int options)
           
 Number160 getID()
          The id of the peer.
 InetAddress getInetAddress()
          Returns the address or null if no address set
 byte getOptions()
           
 int getSocketAddressSize()
           
 int hashCode()
           
 boolean isFirewalledTCP()
           
static boolean isFirewalledTCP(int options)
           
 boolean isFirewalledUDP()
           
static boolean isFirewalledUDP(int options)
           
 boolean isIPv4()
           
 boolean isIPv6()
           
static boolean isNet6(int options)
           
 int offset()
          When deserializing, we need to know how much we deserialized from the constructor call.
 int portTCP()
           
 int portUDP()
           
 int readBytes()
           
 byte[] toByteArray()
          Serializes to a new array with the proper size
 int toByteArray(byte[] me, int offset)
          Serializes to an existing array.
 byte[] toByteArraySocketAddress()
           
 int toByteArraySocketAddress(byte[] me, int offset)
           
 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

EMPTY_IPv4

public static final PeerAddress EMPTY_IPv4
Constructor Detail

PeerAddress

public PeerAddress(byte[] me)
Creates a new peeraddress, where the byte array has to be in the rigth format and in the right 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)
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,
                   org.jboss.netty.buffer.ChannelBuffer channelBuffer)

PeerAddress

public PeerAddress(Number160 id,
                   byte[] me,
                   int offset)

PeerAddress

public PeerAddress(byte[] peerAddress,
                   byte[] socketAddress)
The format of the peer address can also be split.

Parameters:
peerAddress - The 160bit number for the peer ID
socketAddress - The socket address with type port and IP
Throws:
UnknownHostException

PeerAddress

public PeerAddress(Number160 id)
If you only need to know the id

Parameters:
id - The id of the peer

PeerAddress

public PeerAddress(Number160 id,
                   InetAddress address)
If you only need to know the id and InetAddress

Parameters:
id - The id of the peer
address - The InetAddress of the peer

PeerAddress

public PeerAddress(Number160 id,
                   InetAddress address,
                   int portTCP,
                   int portUDP,
                   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,
                   String address,
                   int portTCP,
                   int portUDP)
            throws UnknownHostException
Throws:
UnknownHostException

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,
                   int options)
Method Detail

offset

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

Returns:
The new offset

readBytes

public int readBytes()

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.

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.

toByteArraySocketAddress

public byte[] toByteArraySocketAddress()

toByteArraySocketAddress

public int toByteArraySocketAddress(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

getOptions

public byte getOptions()

isNet6

public static boolean isNet6(int options)

isFirewalledTCP

public static boolean isFirewalledTCP(int options)

isFirewalledUDP

public static boolean isFirewalledUDP(int options)

expectedLength

public static int expectedLength(int options)

expectedSocketLength

public static int expectedSocketLength(int options)

expectedLength

public int expectedLength()

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

isFirewalledUDP

public boolean isFirewalledUDP()

isFirewalledTCP

public boolean isFirewalledTCP()

isIPv6

public boolean isIPv6()

isIPv4

public boolean isIPv4()

changeFirewalledUDP

public PeerAddress changeFirewalledUDP(boolean status)

changeFirewalledTCP

public PeerAddress changeFirewalledTCP(boolean status)

changePorts

public PeerAddress changePorts(int portUDP,
                               int portTCP)

changeAddress

public PeerAddress changeAddress(InetAddress address)

changePeerId

public PeerAddress changePeerId(Number160 id2)

getSocketAddressSize

public int getSocketAddressSize()


Copyright © 2013. All Rights Reserved.