Flylib.com

Books Software

 
 
 

Chapter 17: LEGO MINDSTORMS

previous chapter table of contents next chapter
  

Chapter 17: LEGO MINDSTORMS

Overview

LEGO MINDSTORMS IS A "ROBOTICS INVENTION SYSTEM" that allows you to build LEGO toys with a programmable computer. This chapter looks at the issues involved in interfacing with a specialized hardware device, using MINDSTORMS as an example.

  

previous chapter table of contents next chapter
  

Further Uses of Entries

The primary intention of entries is to provide extra information about services so that clients can decide whether or not they are the services the client wants to use. An expectation in this is that the information in an entry is primarily static. However, entries are objects, and they could also implement behavior as well as state. This should not be used to extend the behavior of a service, since all service behavior should be captured in the service interface specification.

A good example of a "non-static" entry is ServiceType , which is an abstract subclass of AbstractEntry . This contains "human oriented" information about a service, and contains abstract methods , such as String getDisplayName() . This method is intended to provide a localized name for the service. Localization (for example, producing an appropriate French name for the service for French-speaking communities) can only be done on the client side and will require code to be executed in the client to examine the locale and produce a name .

Another use is to define the user interface for a service. Services do not have or require user interfaces for human users, since they are defined by Java interfaces that can be called by any other Java objects. However, some services may wish to offer a way of interacting with themselves by means of a user interface, and this involves much executable code. Since it is not part of the service itself, this should be left in suitable Entry objects. This topic is looked at in detail in Chapter 19.

  

previous chapter table of contents next chapter
  

LookupDiscoveryManager

An application (client or service) that wants to use a set of lookup services at fixed, known addresses, and also to use whatever lookup services it can find by multicast, can use the LookupDiscoveryManager utility class. Most of the methods of this class come from its interfaces:

package net.jini.discovery;
public class LookupDiscoveryManager implements DiscoveryManagement,
                                               DiscoveryGroupManagement,
                                               DiscoveryLocatorManagement {
    public LookupDiscoveryManager(String[] groups,
                                  LookupLocator[] locators,
                                  DiscoveryListener listener)
                                      throws IOException;
}

This class differs from LookupDiscovery and LookupLocatorDiscovery in that it insists on a DiscoveryListener in its constructor. Programs using this class can follow the same event model as the last example:

package discoverymgt;
import net.jini.discovery.LookupDiscoveryManager;
import net.jini.discovery.DiscoveryGroupManagement;
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;
import java.io.IOException;
import java.rmi.RemoteException;
/**
 * AllcastRegister.java
 */
public class AllcastRegister implements DiscoveryListener {
    static public void main(String argv[]) {
        new AllcastRegister();
        // stay around long enough to receive replies
        try {
            Thread.currentThread().sleep(10000L);
        } catch(java.lang.InterruptedException e) {
            // do nothing
        }
    }
    public AllcastRegister() {
        LookupDiscoveryManager discover = null;
        LookupLocator[] locators = null;
        try {
            locators = new LookupLocator[] {new LookupLocator("jini://localhost")};
        } catch(MalformedURLException e) {
            e.printStackTrace();
            System.exit(1);
        }
        try {
            discover = new _
LookupDiscoveryManager(DiscoveryGroupManagement.ALL_GROUPS,
                                                  locators,
                                                     this);
        } catch(IOException e) {
            System.err.println(e.toString());
            e.printStackTrace();
            System.exit(1);
        }
    }
    public void discovered(DiscoveryEvent evt) {
        ServiceRegistrar[] registrars = evt.getRegistrars();
        for (int n = 0; n < registrars.length; n++) {
            ServiceRegistrar registrar = registrars[n];
            try {
                System.out.println("found a service locator at " +
                               registrar.getLocator().getHost());
            } catch(RemoteException e) {
                e.printStackTrace();
                continue;
            }
            // the code takes separate routes from here for client or service
          }
    }
    public void discarded(DiscoveryEvent evt) {
    }
} // AllcastRegister