FileDocCategorySizeDatePackage
ServerListener.javaAPI DocExample5809Thu Mar 16 11:52:46 GMT 2000None

ServerListener

public class ServerListener extends Object implements Runnable, DiscoveryListener

Fields Summary
private Hashtable
leases
private ServiceItem
item
private static final long
ltime
private static final int
mtime
private LookupDiscovery
ld
Constructors Summary
public ServerListener(LookupDiscovery ld, Object object)

        // The discovery object we're listening to

         
        item = new ServiceItem(null, object, null);
        this.ld = ld;
        // Start the new thread to renew the leases
        new Thread(this).start();
    
Methods Summary
public synchronized voiddiscarded(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]);
            }
        }
    
public synchronized voiddiscovered(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
                    leases.put(lookup[i], ret);
                    // There's a new lease, notify the renewal thread
                    notify();
                } catch (RemoteException ex) {
                    System.out.println("ServerListener error: " + ex);
                }
            }
            // else we were already registered in this service
        }
    
public synchronized voidrun()

        while (true) {
            long nextRenewal = Long.MAX_VALUE;
            long now = System.currentTimeMillis();

            Enumeration e = leases.keys();
            // Loop to renew all leases that are about to expire
            // and also to find the time when the next lease will
            // expire so we know when to run the loop again.
            while (e.hasMoreElements()) {
                ServiceRegistrar lookup = (ServiceRegistrar) e.nextElement();
                ServiceRegistration sr = (ServiceRegistration) leases.get(lookup);
                Lease l = sr.getLease();
                long expire = l.getExpiration();

                // See if the current lease has the minimum time.
                // If we can't renew it, discard that lookup service.
                // That will generate an event to the discarded() method
                // which will actually remove the lease from our list.
                try {
                    if (expire <= now + mtime) {
                        l.renew(ltime);
                        expire = l.getExpiration();
                    } 
                    if (nextRenewal > expire - mtime) {
                        nextRenewal = expire - mtime;
                    }
                } catch (LeaseDeniedException lex) {
                } catch (UnknownLeaseException lex) {
                    ld.discard(lookup);
                } catch (RemoteException ex) {
                    ld.discard(lookup);
                }
            }
            try {
                // Wait until the next renewal time. A new lease
                // will notify us prematurely in case the new
                // lease has a smaller time until it must be renewed
                wait(nextRenewal - now);
            } catch (InterruptedException ex) {};
        }