179.

previous chapter table of contents next chapter
  

Unicast Discovery

Unicast discovery can be used when you know the machine on which the lookup service resides and can ask for it directly. This approach is expected to be used for a lookup service that is outside of your local network, but that you know the address of anyway (such as your home network while you are at work, or a network identified in a newsgroup or email message, or maybe even one advertised on TV).

Unicast discovery relies on a single class, LookupLocator , which is described in the next section. Basic use of this class is illustrated in the sections on the InvalidLookupLocator program. The InvalidLookupLocator should be treated as an introductory Jini program that you can build and run without having to worry about network issues. Connecting to a lookup service using the network is done with the getRegistrar ()method of LookupLocator , and an example program using this is shown in the UnicastRegistrar program in the "Get Registrar" section.

LookupLocator

The LookupLocator class in the net.jini. core .discovery package is used for unicast discovery of a lookup service. There are two constructors:

 package net.jini.core.discovery; public class LookupLocator {     LookupLocator(java.lang.String url)                   throws java.net.MalformedURLException;     LookupLocator(java.lang.String host,int port); } 

For the first constructor, the url parameter follows the standard URL syntax of "protocol://host" or "protocol://host:port". The protocol is jini . If no port is given, it defaults to 4160. The host should be a valid DNS name (such as pandonia.canberra.edu.au or an IP address (such as 137.92.11.13 ). So for example, jini://pandonia.canberra.edu.au:4160 may be given as the URL for the first constructor. No unicast discovery is performed at this stage, though, so any rubbish could be entered. Only a check for the syntactic validity of the URL is performed. The first constructor will throw an exception if it discovers a syntax error. This syntactic check is not even done for the second constructor, which takes a host name and port separately.

InvalidLookupLocator

The following program creates some objects with valid and invalid host/URLs. They are only checked for syntactic validity rather than existence as URLs. That is, no network lookups are performed. This should be treated as a basic example to get you started building and running a simple Jini program.

 package basic; import net.jini.core.discovery.LookupLocator; /**  * InvalidLookupLocator.java  */ public class InvalidLookupLocator {        static public void main(String argv[]) {     new InvalidLookupLocator();     }     public InvalidLookupLocator() {     LookupLocator lookup;    // this is valid    try {        lookup = new LookupLocator("jini://localhost");        System.out.println("First lookup creation succeeded");     } catch(java.net.MalformedURLException e) {         System.err.println("First lookup failed: " + e.toString());     }     // this is probably an invalid URL,     // but the URL is syntactically okay     try {         lookup = new LookupLocator("jini://ABCDEFG.org");         System.out.println("Second lookup creation succeeded");     } catch(java.net.MalformedURLException e) {         System.err.println("Second lookup failed: " + e.toString());     }     // this IS a malformed URL, and should throw an exception     try {         lookup = new LookupLocator("A:B:C://ABCDEFG.org");         System.out.println("Third lookup creation succeeded");     } catch(java.net.MalformedURLException e) {         System.err.println("Third lookup failed: " + e.toString());     }     // this is valid, but no check is made anyway     lookup = new LookupLocator("localhost", 80);     System.out.println("Fourth lookup creation succeeded");     } } // InvalidLookupLocator 

Running the InvalidLookupLocator

All Jini programs will need to be compiled using the JDK 1.2 compiler. Jini programs will not compile or run under JDK 1.1 (any versions).

The InvalidLookupLocator program defines the InvalidLookupLocator class in the basic package. The source code will be in the InvalidLookupLocator.java file in the basic subdirectory. From the parent directory, this can be compiled by a command such as this:

 javac basic/InvalidLookupLocator.java 

This will leave the class file also in the basic subdirectory.

When you compile the source code, the CLASSPATH will need to include the jini-core.jar Jini file. Similarly, when a service is run, this Jini file will need to be in its CLASSPATH , and when a client runs, it will also need this file in its CLASSPATH . The reason for this repetition is that the service and the client are two separate applications, running in two separate JVMs, and quite likely will be on two separate computers.

The InvalidLookupLocator has no additional requirements. It does not perform any network calls and does not require any additional service to be running. It can be run simply by entering this command:

 java -classpath ... basic.InvalidLookupLocator 

Information from the LookupLocator

Two of the methods of LookupLocator are these:

 String getHost(); int getPort(); 

These methods will return information about the hostname that the locator will use, and the port it will connect on or is already connected on. This is just the information fed into the constructor or left to default values, though. It doesn't offer anything new for unicasting . This information will be useful in the multicast situation, though, if you need to find out where the lookup service is.

getRegistrar

Search and lookup is performed by the getRegistrar() method of the LookupLocator , which returns an object of class ServiceRegistrar .

 public ServiceRegistrar getRegistrar()              throws java.io.IOException,java.lang.ClassNotFoundException 

The ServiceRegistrar class is discussed in detail later. This class performs network lookup on the URL given in the LookupLocator constructor.

UML sequence diagrams are useful for showing the timelines of object existence and the method calls that are made from one object to another. The timeline reads down, and the method calls and their returns read across. A UML sequence diagram augmented with a jagged arrow showing the network connection is shown in Figure 3-1. The UnicastRegister object makes a new() call to create a LookupLocator , and this call returns a lookup object. The getRegistrar() method call is then made on the lookup object, and this causes network activity. As a result of this, a ServiceRegistrar object is created in some manner by the lookup object, and this is returned from the method as the registrar .

click to expand
Figure 3-1: UML sequence diagram for lookup

The UnicastRegistrar program that implements Figure 3-1 and performs the network connection to get a ServiceRegistrar object is as follows:

 package basic; import net.jini.core.discovery.LookupLocator; import net.jini.core.lookup.ServiceRegistrar; /**  * UnicastRegistrar.java  */ public class UnicastRegister {     static public void main(String argv[]) {         new UnicastRegister();     }     public UnicastRegister() {       LookupLocator lookup = null;     ServiceRegistrar registrar = null;         try {             lookup = new LookupLocator("jini:// www.jini.canberra.edu.au");         } catch(java.net.MalformedURLException e) {             System.err.println("Lookup failed: " + e.toString());         System.exit(1);         }     try {         registrar = lookup.getRegistrar();     } catch (java.io.IOException e) {             System.err.println("Registrar search failed: " + e.toString());         System.exit(1);     } catch (java.lang.ClassNotFoundException e) {             System.err.println("Registrar search failed: " + e.toString());         System.exit(1);     }     System.out.println("Registrar found");     // the code takes separate routes from here for client or service     } } // UnicastRegister 

The registrar object will be used in different ways for clients and services: the services will use it to register themselves , and the clients will use it to locate services.

Note  

This program might not run as is, due to security issues. If that is the case, see the first section of Chapter 12 .

Running the UnicastRegister

When the UnicastRegistrar program in the previous section program needs to be compiled and run, it has to have the file jini-code.jar in its CLASSPATH . When run, it will attempt to connect to the service locator, so obviously the service locator needs to be running on the machine specified in order for this to happen. Otherwise, the program will throw an exception and terminate. In this case, the host specified is www.jini.canberra.edu.au . It could, however, be any machine accessible on the local or remote network (as long as it is running a service locator). For example, to connect to the service locator running on my current workstation, the parameter for LookupLocator would be jini://pandonia.canberra.edu.au .

The UnicastRegister program will receive a ServiceRegistrar from the service locator. However, it does so with a simple readObject() on a socket connected to the service locator, so it does not need any additional support services, such as rmiregistry or rmid . The program can be run by this command:

 java basic.UnicastRegister 

The CLASSPATH for the UnicastRegister program should contain the Jini jar files as well as the path to basic/UnicastRegister.class .

  


A Programmer[ap]s Guide to Jini Technology
A Programmer[ap]s Guide to Jini Technology
ISBN: 1893115801
EAN: N/A
Year: 2000
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net