FileDocCategorySizeDatePackage
ServerListener.javaAPI DocExample3592Thu Mar 16 11:52:44 GMT 2000None

ServerListener.java

/*
 *
 * Copyright (c) 2000 Scott Oaks and Henry Wong. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and
 * without fee is hereby granted.
 *
 * This sample source code is provided for example only,
 * on an unsupported, as-is basis. 
 *
 * AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  AUTHOR
 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
 * HIGH RISK ACTIVITIES.
 */



import java.rmi.*;
import java.util.*;
import net.jini.discovery.*;
import net.jini.core.lookup.*;

public class ServerListener implements DiscoveryListener {
    // Hashtable of registration leases keyed by the lookup service
    private Hashtable leases = new Hashtable();
    private ServiceItem item;          // Item to be registered with lookup
    private static final int ltime = 15*60*1000;    // 15 Minutes

    public ServerListener(Object object) {
        item = new ServiceItem(null, object, null);
    }

    // Automatically called when new lookup service(s) are discovered
    public synchronized void discovered (DiscoveryEvent dev) {
        ServiceRegistrar[] lookup = dev.getRegistrars();
        // For each discovered service, see if we're already registered.
        // If not, register
        for (int i = 0; i < lookup.length; i++) {
            if (leases.containsKey(lookup[i]) == false) {
                // Not already registered
                try {
                    // Register
                    ServiceRegistration ret = lookup[i].register (item, ltime);
                    // You must assign the serviceID based on what the
                    // lookup service returns
                    if (item.serviceID == null) {
                        item.serviceID = ret.getServiceID();
                    }
                    // Save this registration
                    // Note that we don't actually renew the leases yet
                    leases.put (lookup[i], ret);
                } catch (RemoteException ex) {
                    System.out.println("ServerListener error: " + ex);
                }
            }
            // else we were already registered in this service
        }
    }

    // Automatically called when lookup service(s) are no longer available
    public synchronized void discarded (DiscoveryEvent dev) {
        ServiceRegistrar[] lookup = dev.getRegistrars();
        for (int i = 0; i < lookup.length; i++) {
            if (leases.containsKey(lookup[i]) == true) {
                // Remove the registration. If the lookup service comes
                // back later, we'll re-register at that time.
                leases.remove(lookup[i]);
            }
        }
    }
}