org.p2psockets
Class P2PNameService

java.lang.Object
  extended byorg.p2psockets.P2PNameService

public class P2PNameService
extends java.lang.Object

This class provides a "Name Service" facade to the JXTA network. It provides a standard way to resolve a hostname, port, or IP address into a JXTA socket that can be used to communicate with the given endpoint (the 'resolve' set of methods). It also provides a set of methods to 'bind' a given hostname, port, or IP address together, and returns a JXTA server socket that can be used to service clients that communicate with this endpoint. Implementation Notes: The IP address we generate is created from a random number generator, using no seed. We use all four bytes. If the localhost or "any" interfaces are requested, we generate a random IP address and then cache the value so that the same local IP address is always returned for this session (though it will clear out if the JVM is restarted). This class has been extensively refactored to port to JXTA 2.3.1. We used to have an application peer group nested inside the net peer group; inside of this group we would create another peer group for each host name. When I tried to port P2P Sockets to JXTA 2.3.1 I ran into all sorts of strange problems that I could not solve, so to get around them I decided to drastically simplify how P2P Sockets works. Now we only have one peer group, the net peer group, that we publish all of our JXTA Server Socket ads into. We set the Name tag for all of these ads as follows: appName/IPAddress/hostName:port For example: Paper Airplane/33.66.23.88/www.nike.laborpolicy:80 Threading: this class is threadsafe because it is both static and has no private variables.

Version:
0.5
Author:
Brad GNUberg, bkn3@columbia.edu

Field Summary
static java.lang.String NAME_FIELD
          The field that holds the domain name.
static java.lang.String PEER_SERVICE_PREFIX
          This is an optional prefix, such as "www.", that will be added to automatically generated peer domain names, such as "www.bradgnuberg.peer".
static java.lang.String PEER_SERVICE_SUFFIX
          To make it easy to differentiate peer services from peer group services, all peer domain names must end with this ending.
 
Constructor Summary
P2PNameService()
           
 
Method Summary
protected static void becomeRendezvous(net.jxta.peergroup.PeerGroup inGroup)
          If we create a new P2P server peer group or publish a port ad into the group then we automatically become a rendezvous server.
static net.jxta.socket.JxtaServerSocket bind(P2PSocketAddress endpoint, net.jxta.peergroup.PeerGroup hostParentPeerGroup)
          Creates and binds the given P2PSocketAddress to run in the PeerGroup 'hostParentPeerGroup'.
protected static net.jxta.protocol.PipeAdvertisement createAndPublishPipe(java.lang.String advName, net.jxta.peergroup.PeerGroup startServerInGroup)
           
protected static net.jxta.protocol.PipeAdvertisement createPipeAdvertisement(java.lang.String advName, net.jxta.peergroup.PeerGroup parentPeerGroup)
           
static java.lang.String findHostName(java.lang.String ipAddress)
          This method will search on the network to find an advertisement for the given IP address and discover the host name for that IP address.
static java.lang.String findHostName(java.lang.String ipAddress, net.jxta.peergroup.PeerGroup searchInPeerGroup)
          This method will search on the network to find an advertisement for the given IP address and discover the host name for that IP address.
static byte[] findIPAddress(java.lang.String host)
          This method will search on the network to find an advertisement for the given host and discover the IP address for that host.
static byte[] findIPAddress(java.lang.String host, net.jxta.peergroup.PeerGroup searchInPeerGroup)
          This method will search on the network to find an advertisement for the given host and discover the IP address for that host.
protected static byte[] generateHash(java.lang.String clearTextID, java.lang.String function)
           
protected static net.jxta.pipe.PipeID getPipeID(java.lang.String advName, net.jxta.peergroup.PeerGroup parentPeerGroup)
           
static boolean isHostNameTaken(java.lang.String hostName)
          Determines if the given host name is already being used on the peer network.
static boolean isIPAddressTaken(java.lang.String ipAddress)
          Determines if the given IP address is already taken.
static boolean isPortTaken(java.lang.String hostName, int port)
          Determines if the given port for the given host name is already being used in the peer network.
static net.jxta.socket.JxtaSocket resolve(byte[] ipAddress, int port, net.jxta.peergroup.PeerGroup hostParentPeerGroup)
          Resolves the given IP address and port name in the given peer group to a socket that can be used to communicate with this service.
static net.jxta.socket.JxtaSocket resolve(P2PSocketAddress endpoint, net.jxta.peergroup.PeerGroup hostParentPeerGroup)
          Resolves the given remote socket address in the given peer group to a socket that can be used to communicate with this service.
static net.jxta.socket.JxtaSocket resolve(java.lang.String host, int port, net.jxta.peergroup.PeerGroup hostParentPeerGroup)
          Resolves the given host and port name in the given peer group to a socket that can be used to communicate with this service.
protected static net.jxta.protocol.PipeAdvertisement searchForPipeAdvertisement(java.lang.String advName, net.jxta.peergroup.PeerGroup parentPeerGroup)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PEER_SERVICE_SUFFIX

public static final java.lang.String PEER_SERVICE_SUFFIX
To make it easy to differentiate peer services from peer group services, all peer domain names must end with this ending.

See Also:
Constant Field Values

PEER_SERVICE_PREFIX

public static final java.lang.String PEER_SERVICE_PREFIX
This is an optional prefix, such as "www.", that will be added to automatically generated peer domain names, such as "www.bradgnuberg.peer".

See Also:
Constant Field Values

NAME_FIELD

public static final java.lang.String NAME_FIELD
The field that holds the domain name.

See Also:
Constant Field Values
Constructor Detail

P2PNameService

public P2PNameService()
Method Detail

bind

public static net.jxta.socket.JxtaServerSocket bind(P2PSocketAddress endpoint,
                                                    net.jxta.peergroup.PeerGroup hostParentPeerGroup)
                                             throws java.io.IOException
Creates and binds the given P2PSocketAddress to run in the PeerGroup 'hostParentPeerGroup'. This creates and starts the server socket that will host requests for the given host and port. If the port is 0, we search several times to find an unused port address above 1024 If the host is "localhost", then this server socket is simply started on the local host with a host name automatically generated from the peer name, with an ending of ".peer". If 'endpoint' is null than we just assume the server socket will run on the local machine as a peer-service.

Throws:
java.io.IOException

resolve

public static net.jxta.socket.JxtaSocket resolve(P2PSocketAddress endpoint,
                                                 net.jxta.peergroup.PeerGroup hostParentPeerGroup)
                                          throws java.io.IOException
Resolves the given remote socket address in the given peer group to a socket that can be used to communicate with this service.

Throws:
java.io.IOException

resolve

public static net.jxta.socket.JxtaSocket resolve(java.lang.String host,
                                                 int port,
                                                 net.jxta.peergroup.PeerGroup hostParentPeerGroup)
                                          throws java.io.IOException
Resolves the given host and port name in the given peer group to a socket that can be used to communicate with this service.

Throws:
java.io.IOException

resolve

public static net.jxta.socket.JxtaSocket resolve(byte[] ipAddress,
                                                 int port,
                                                 net.jxta.peergroup.PeerGroup hostParentPeerGroup)
                                          throws java.io.IOException
Resolves the given IP address and port name in the given peer group to a socket that can be used to communicate with this service.

Throws:
java.io.IOException

findIPAddress

public static byte[] findIPAddress(java.lang.String host)
                            throws java.io.IOException,
                                   java.net.UnknownHostException
This method will search on the network to find an advertisement for the given host and discover the IP address for that host. If none is found than null is returned. If 'host' is null or has the value 'localhost' than the local host name is generated and an IP address is returned for that.

Throws:
java.io.IOException
java.net.UnknownHostException

findIPAddress

public static byte[] findIPAddress(java.lang.String host,
                                   net.jxta.peergroup.PeerGroup searchInPeerGroup)
                            throws java.io.IOException,
                                   java.net.UnknownHostException
This method will search on the network to find an advertisement for the given host and discover the IP address for that host. If none is found than null is returned. If 'host' is null or has the value 'localhost' than the local host name is generated and an IP address is returned for that.

Throws:
java.io.IOException
java.net.UnknownHostException

findHostName

public static java.lang.String findHostName(java.lang.String ipAddress)
                                     throws java.io.IOException,
                                            java.net.UnknownHostException
This method will search on the network to find an advertisement for the given IP address and discover the host name for that IP address. If none is found than null is returned.

Throws:
java.io.IOException
java.net.UnknownHostException

findHostName

public static java.lang.String findHostName(java.lang.String ipAddress,
                                            net.jxta.peergroup.PeerGroup searchInPeerGroup)
                                     throws java.io.IOException,
                                            java.net.UnknownHostException
This method will search on the network to find an advertisement for the given IP address and discover the host name for that IP address. If none is found than null is returned.

Throws:
java.io.IOException
java.net.UnknownHostException

isHostNameTaken

public static boolean isHostNameTaken(java.lang.String hostName)
                               throws java.io.IOException
Determines if the given host name is already being used on the peer network.

Throws:
java.io.IOException

isPortTaken

public static boolean isPortTaken(java.lang.String hostName,
                                  int port)
                           throws java.io.IOException
Determines if the given port for the given host name is already being used in the peer network.

Throws:
java.io.IOException

isIPAddressTaken

public static boolean isIPAddressTaken(java.lang.String ipAddress)
                                throws java.io.IOException
Determines if the given IP address is already taken.

Throws:
java.io.IOException

createAndPublishPipe

protected static net.jxta.protocol.PipeAdvertisement createAndPublishPipe(java.lang.String advName,
                                                                          net.jxta.peergroup.PeerGroup startServerInGroup)
                                                                   throws java.lang.Exception
Throws:
java.lang.Exception

createPipeAdvertisement

protected static net.jxta.protocol.PipeAdvertisement createPipeAdvertisement(java.lang.String advName,
                                                                             net.jxta.peergroup.PeerGroup parentPeerGroup)
                                                                      throws java.lang.Exception
Throws:
java.lang.Exception

searchForPipeAdvertisement

protected static net.jxta.protocol.PipeAdvertisement searchForPipeAdvertisement(java.lang.String advName,
                                                                                net.jxta.peergroup.PeerGroup parentPeerGroup)

getPipeID

protected static net.jxta.pipe.PipeID getPipeID(java.lang.String advName,
                                                net.jxta.peergroup.PeerGroup parentPeerGroup)
                                         throws java.lang.Exception
Throws:
java.lang.Exception

becomeRendezvous

protected static void becomeRendezvous(net.jxta.peergroup.PeerGroup inGroup)
                                throws java.lang.Exception
If we create a new P2P server peer group or publish a port ad into the group then we automatically become a rendezvous server.

Throws:
java.lang.Exception

generateHash

protected static byte[] generateHash(java.lang.String clearTextID,
                                     java.lang.String function)