Posted by:h_q_x
Posted on:2003-03-25 21:28
package corejini.chapter5;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.DiscoveryEvent;
import net.jini.discovery.LookupDiscovery;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceRegistration;
import net.jini.core.entry.Entry;
import net.jini.lookup.entry.*;
import net.jini.core.event.*;
import java.util.HashMap;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.Remote;
import java.rmi.RMISecurityManager;
import java.rmi.MarshalledObject;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.event.EventListenerList;
// This is the proxy object that will be downloaded
// by clients. It's serializable and implements
// our well-known HelloWorldServiceInterface.
add here for EVENT GENERATOR!!!
interface EventGena extends Remote{
public EventRegistration register(RemoteEventListener listener,
long leaseLength)
throws RemoteException;
}
// HelloWorldService is the "wrapper" class that
// handles publishing the service item.
public class HelloWorldServicea extends UnicastRemoteObject implements EventGena,Runnable {
// 10 minute leases
protected final int LEASE_TIME = 1000 * 60 * 1000;
protected HashMap registrations = new HashMap();
protected ServiceItem item;
protected LookupDiscovery disco;
//here r the variants for the EVENT GEN
protected long lastEventSeqNo = 0;
EventListenerList listenerList = new EventListenerList();
// Inner class to listen for discovery events
class Listener implements DiscoveryListener {
// Called when we find a new lookup service.
public void discovered(DiscoveryEvent ev) {
System.out.println("discovered a lookup service!");
ServiceRegistrar[] newregs = ev.getRegistrars();
for (int i=0 ; i
if (!registrations.containsKey(newregs[i])) {
registerWithLookup(newregs[i]);
}
}
}
// Called ONLY when we explicitly discard a
// lookup service, not "automatically" when a
// lookup service goes down. Once discovered,
// there is NO ongoing communication with a
// lookup service.
public void discarded(DiscoveryEvent ev) {
ServiceRegistrar[] deadregs = ev.getRegistrars();
for (int i=0 ; i
registrations.remove(deadregs[i]);
}
}
}
public HelloWorldServicea() throws IOException {
item = new ServiceItem(null, this
, null);
// Set a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
// Search for the "public" group, which by
// convention is named by the empty string
disco = new LookupDiscovery(new String[] { "" });
// Install a listener.
disco.addDiscoveryListener(new Listener());
}
// This work involves remote calls, and may take a
// while to complete. Thus, since it's called from
// discovered(), it will prevent us from responding
// in a timely fashion to new discovery events. An
// improvement would be to spin off a separate short-
// lived thread to do the work.
synchronized void registerWithLookup(ServiceRegistrar
registrar) {
ServiceRegistration registration = null;
System.out.println("enter lookup register...");
try {
registration = registrar.register(item, LEASE_TIME);
} catch (RemoteException ex) {
System.out.println("Couldn't register: " + ex.getMessage());
return;
} catch (Exception ex){
System.out.println("Couldn't register: " + ex.getMessage());
return;
}
// If this is our first registration, use the
// service ID returned to us. Ideally, we should
// save this ID so that it can be used after
// restarts of the service
if (item.serviceID == null) {
item.serviceID = registration.getServiceID();
System.out.println("Set serviceID to " + item.serviceID);
}
System.out.println("serviceID is set to " + item.serviceID);
registrations.put(registrar, registration);
}
//here is the method for EVENT GEN
public synchronized EventRegistration register(
RemoteEventListener listener,
long leaseLength){
EventRegistration evtreg = new EventRegistration(1L,this,null,0);
listenerList.add(RemoteEventListener.class, listener);
return evtreg;
//
}
//here is the one to send those EVENT
protected void sendEvent(){
RemoteEvent remoteEvent = null;
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int n = listeners.length - 2; n >= 0; n -= 2) {
if (listeners[n] == RemoteEventListener.class) {
RemoteEventListener listener =
(RemoteEventListener) listeners[n+1];
if (remoteEvent == null) {
remoteEvent = new RemoteEvent(this, 1L,
lastEventSeqNo++, null);
}
try {
listener.notify(remoteEvent);
} catch(UnknownEventException e) {
e.printStackTrace();
} catch(java.rmi.RemoteException e) {
e.printStackTrace();
}
}
}
}
// This thread does nothing but sleep, but it
// makes sure the VM doesn't exit.
public void run() {
while (true) {
try {
Thread.sleep(1000000);
sendEvent();
} catch (InterruptedException ex) {
}
}
}
// Create a new HelloWorldService and start
// its thread.
public static void main(String args[]) {
try {
HelloWorldServicea hws =
new HelloWorldServicea();
new Thread(hws).start();
} catch (IOException ex) {
System.out.println("Couldn't create service: " +
ex.getMessage());
}
}
}