KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > p2p > registry > ServerListener


1 package org.objectweb.proactive.p2p.registry;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Hashtable JavaDoc;
6
7 import net.jini.discovery.LookupDiscovery;
8 import net.jini.discovery.DiscoveryListener;
9 import net.jini.discovery.DiscoveryEvent;
10 import net.jini.core.lookup.ServiceItem;
11 import net.jini.core.lookup.ServiceRegistrar;
12 import net.jini.core.lookup.ServiceRegistration;
13 import net.jini.core.lease.Lease;
14 import net.jini.core.lease.LeaseDeniedException;
15 import net.jini.core.lease.UnknownLeaseException;
16
17 /**
18 * This class represent the Server that manage the platform
19 */

20
21 public class ServerListener implements DiscoveryListener, Runnable JavaDoc {
22
23     // Hashtable of registration leases keyed by the lookup service
24
private Hashtable JavaDoc leases = new Hashtable JavaDoc();
25     private ServiceItem item; // Item to be registered with lookup
26
private static final long ltime = Lease.FOREVER;
27     private static final int mtime = 30*1000; // 30 seconds (Minimum lease)
28

29     private LookupDiscovery ld; // The discovery object we're listening to
30

31     public ServerListener(LookupDiscovery ld, Object JavaDoc object) {
32         item = new ServiceItem(null, object, null);
33         this.ld = ld;
34         // Start the new thread to renew the leases
35
new Thread JavaDoc(this).start();
36     }
37
38     // Automatically called when new lookup service(s) are discovered
39
public synchronized void discovered(DiscoveryEvent dev) {
40         ServiceRegistrar[] lookup = dev.getRegistrars();
41         // For each discovered service, see if we're already registered.
42
// If not, register
43
for (int i = 0; i < lookup.length; i++) {
44             if (leases.containsKey(lookup[i]) == false) {
45                 // Not already registered
46
try {
47                     //Register
48
ServiceRegistration ret = lookup[i].register(item, ltime);
49                     // You must assign the serviceID based on what the
50
// lookup service returns
51
if (item.serviceID == null) {
52                         item.serviceID = ret.getServiceID();
53                     }
54                     // Save this registration
55
leases.put(lookup[i], ret);
56                     // There's a new lease, notify the renewal thread
57
notify();
58                 } catch (RemoteException JavaDoc ex) {
59                     System.out.println("ServerListener error: " + ex);
60                 }
61             }
62             // else we were already registered in this service
63
}
64     }
65
66     // Automatically called when lookup service(s) are no longer available
67
public synchronized void discarded(DiscoveryEvent dev) {
68         ServiceRegistrar[] lookup = dev.getRegistrars();
69         for (int i = 0; i < lookup.length; i++) {
70             if (leases.containsKey(lookup[i]) == true) {
71                 // Remove the registration. If the lookup service comes
72
// back later, we'll re-register at that time.
73
leases.remove(lookup[i]);
74             }
75         }
76     }
77
78     public synchronized void run() {
79         while (true) {
80             long nextRenewal = Long.MAX_VALUE;
81             long now = System.currentTimeMillis();
82
83             Enumeration JavaDoc e = leases.keys();
84             // Loop to renew all leases that are about to expire
85
// and also to find the time when the next lease will
86
// expire so we know when to run the loop again.
87
while (e.hasMoreElements()) {
88                 ServiceRegistrar lookup = (ServiceRegistrar) e.nextElement();
89                 ServiceRegistration sr = (ServiceRegistration) leases.get(lookup);
90                 Lease l = sr.getLease();
91                 long expire = l.getExpiration();
92
93                 // See if the current lease has the minimum time.
94
// If we can't renew it, discard that lookup service.
95
// That will generate an event to the discarded() method
96
// which will actually remove the lease from our list.
97
try {
98                     if (expire <= now + mtime) {
99                         l.renew(ltime);
100                         expire = l.getExpiration();
101                     }
102                     if (nextRenewal > expire - mtime) {
103                         nextRenewal = expire - mtime;
104                     }
105                 } catch (LeaseDeniedException lex) {
106                 } catch (UnknownLeaseException lex) {
107                     ld.discard(lookup);
108                 } catch (RemoteException JavaDoc ex) {
109                     ld.discard(lookup);
110                 }
111             }
112             try {
113                 // Wait until the next renewal time. A new lease
114
// will notify us prematurely in case the new
115
// lease has a smaller time until it must be renewed
116
wait(nextRenewal - now);
117             } catch (InterruptedException JavaDoc ex) {};
118         }
119     }
120 }
121
Popular Tags