copyright The Portico Project 2008.

org.portico.core
Class MulticastRegistry

java.lang.Object
  extended by org.portico.core.MulticastRegistry
All Implemented Interfaces:
Runnable

public class MulticastRegistry
extends Object
implements Runnable

This class represents a registry containing information that is made available via a multicast channel. Generally speaking, this is Bootstrap subclass information that can be accessed by remote clients through multicast, thus providing some sort of automatic discovery mechanism. For example, the org.portico.binding.jsop bindings register the bootstrap information with the multicast registry so that clients using JSOP can automatically discover the ip address and port information needed to contact it.

The registry is basically a Map. Requests and responses are sent around as strings, with a request containing a key that is used to find information in the Map. Note that the registry is not explicitly reserved for bootstraps and can be used to store any information that one wishes to make available via multicast discovery.

Requests

Requests are multicast packets that contain a string prefixed with PorticoConstants.MRR_REQUEST. Each request is also sent with a serial number that is used in any response (so the client can recognize the response to its request and not confuse other responses with the information it desires).

When a request is received, the prefix is stripped off, and then the serial number is also removed and stored. The remainder of the string is assumed to be the key for the information the client is looking for.

An example of an incoming request package is:

 "drreq:123:jsop"
   (1)  (2)  (3)
 
Where (1) is the request prefix from PorticoConstants.MRR_REQUEST, (2) is the serial number to send back with the response, and (3) is the key that the client is trying to find associated information for.

Responses

If there is a value for the received key in the registry, a new response packet containing the response string is sent. The structure of this is as follows:

 "drresp:123:192.168.0.30:20913"
   (1)   (2)         (3)
 
Here, (1) is the response prefix held in PorticoConstants.MRR_RESPONSE. Second is the serial number that was sent with the request (so that the client can identify the packet as the response to its particular request). (3) is the value that was associated with the key in the registry.

Error Responses

If there is an error (such as there being no information for the key in the request), an error packet is sent back. This package is once again a string, and its structure is:

 "drrerror:123:no value for key"
    (1)    (2)       (3)
 
In this packet, (1) is the error response header and (2) is the serial number of the request. (3) is a textual description of the problem.


Field Summary
private  org.apache.log4j.Logger logger
           
private  InetAddress multicastGroup
           
private  int multicastPort
           
private  MulticastSocket multicastSocket
           
private  Map<String,String> registry
           
private  boolean running
           
private  Thread workerThread
           
 
Constructor Summary
MulticastRegistry(RTIExec rtiexec)
          Creates a new multicast registry that is to be stored in the given RTIExec.
 
Method Summary
private  String getRequestKey(String request)
          This method is much the same as getSerialNumber(String) except that it extracts the key from the request, not the serial number.
private  String getSerialNumber(String request)
          Extracts the serial number from the given request string.
 boolean isRegistered(String key)
          Returns true if there is a value for the given key, false otherwise
 boolean register(String key, String value)
          Register the given information under the given name with the registry.
 void run()
          Executes a loop listening for incoming requests and answering back with the appropriate information.
 void shutdown()
          This method shuts down the multicast registry.
 void startup()
          Start the multicast registry.
 String unregister(String key)
          Unregister the information for the given key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private org.apache.log4j.Logger logger

running

private volatile boolean running

registry

private Map<String,String> registry

multicastPort

private int multicastPort

multicastGroup

private InetAddress multicastGroup

multicastSocket

private MulticastSocket multicastSocket

workerThread

private Thread workerThread
Constructor Detail

MulticastRegistry

public MulticastRegistry(RTIExec rtiexec)
Creates a new multicast registry that is to be stored in the given RTIExec. No reference to the RTI is kept, it is just used to create a logger with the appropriate RTI execution name in it.

Once created, the registry still needs to be started and stopped.

Method Detail

register

public boolean register(String key,
                        String value)
Register the given information under the given name with the registry. If there is already an entry for that key, the request will be ignored and false returned. If the registration is successful, true will be returned.


unregister

public String unregister(String key)
Unregister the information for the given key. The unregistered information will be returned. If there is no information for that key, null will be returned.


isRegistered

public boolean isRegistered(String key)
Returns true if there is a value for the given key, false otherwise


startup

public void startup()
Start the multicast registry. This will attach us to the multicast port and start a new thread to handle remote requests for information. If the registry is already running, this method will just return right away. If startup fails, information will be logged, but no exception will be thrown.


shutdown

public void shutdown()
This method shuts down the multicast registry. It will close off the multicast connection and wait for the worker thread to stop before it returns. If there is an error shutting down, a message will be logged, but no exception will be thrown.


run

public void run()
Executes a loop listening for incoming requests and answering back with the appropriate information. If the thread is interrupted, it will disconnect from the multicast socket and return. See the class level comment for MulticastRegistry for a description of the formatting involved in the various packets.

Specified by:
run in interface Runnable

getSerialNumber

private String getSerialNumber(String request)
                        throws IllegalArgumentException
Extracts the serial number from the given request string. If the request is not propertly formatted, an IllegalArgumentException is thrown (containing a description of the error in its message property)

Throws:
IllegalArgumentException

getRequestKey

private String getRequestKey(String request)
                      throws IllegalArgumentException
This method is much the same as getSerialNumber(String) except that it extracts the key from the request, not the serial number.

Throws:
IllegalArgumentException

copyright The Portico Project 2008.