|
LookupLocatorDiscoveryIn Chapter 3 into a loop. The LookupLocatorDiscovery class provides a more satisfactory solution by providing the same event handling method as in the multicast case; that is, you supply a list of addresses, and when a lookup service is found at one of these addresses, a listener object is informed. The LookupLocatorDiscovery class is specified as follows : package net.jini.discovery; public class LookupLocatorDiscovery implements DiscoveryManagement, DiscoveryLocatorManagement { public LookupLocatorDiscovery(LookupLocator[] locators); public LookupLocator[] getDiscoveredLocators(); public LookupLocator[] getUndiscoveredLocators(); } Rewriting the unicast example from Chapter 3 using this utility class makes it look much like the example on multicast discovery from the same chapter. The similarity is that it now uses the same event model for lookup service discovery; the difference is that it uses a set of LookupLocator objects rather than a set of groups. package discoverymgt; import net.jini.discovery.LookupLocatorDiscovery; import net.jini.discovery.DiscoveryListener; import net.jini.discovery.DiscoveryEvent; import net.jini.core.lookup.ServiceRegistrar; import net.jini.core.discovery.LookupLocator; import java.net.MalformedURLException; /** * UnicastRegister.java */ public class UnicastRegister implements DiscoveryListener { static public void main(String argv[]) { new UnicastRegister(); // stay around long enough to receive replies try { Thread.currentThread().sleep(10000L); } catch(java.lang.InterruptedException e) { // do nothing } } public UnicastRegister() { LookupLocatorDiscovery discover = null; LookupLocator[] locators = null; try { locators = new LookupLocator[] {new LookupLocator("jini://localhost")}; } catch(MalformedURLException e) { e.printStackTrace(); System.exit(1); } try { discover = new LookupLocatorDiscovery(locators); } catch(Exception e) { System.err.println(e.toString()); e.printStackTrace(); System.exit(1); } discover.addDiscoveryListener(this); } public void discovered(DiscoveryEvent evt) { ServiceRegistrar[] registrars = evt.getRegistrars(); for (int n = 0; n < registrars.length; n++) { ServiceRegistrar registrar = registrars[n]; // the code takes separate routes from here for client or service System.out.println("found a service locator"); } } public void discarded(DiscoveryEvent evt) { } } // UnicastRegister |