📄 jiniuseragent.java
字号:
import java.rmi.*;
import java.io.IOException;
import java.util.Vector;
// Jini classes used to find and bind to a Jini service
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.discovery.DiscoveryEvent;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.DiscoveryManagement;
import net.jini.discovery.LookupDiscovery;
import net.jini.core.lookup.ServiceMatches;
import net.jini.core.lookup.ServiceRegistrar;
public class JiniUserAgent {
public JiniUserAgent() {}
public static void main(String args[]) {
// set security manager if not set
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
JiniUserAgent userAgent = new JiniUserAgent();
// create a service template to represent the required service
ServiceTemplate template;
// We pass a class that identifies an interface rather than a name
Class[] types = { Class.forName("Agent") };
// We do not constrain our service search other than interface type
// null indicates match anything on service ID and attributes
template = new ServiceTemplate(null, types, null);
// call the lookup passing our template
Agent agent = (Agent)userAgent.lookup(template);
// Now call the JiniAgentService
System.out.println(agent.talk());
} catch (Exception e) {
System.err.println("Agent exception: " +
e.getMessage());
e.printStackTrace();
}
}
// method to find a service given a service template
private Object lookup(ServiceTemplate template) throws IOException
{
// Your internal class ServiceListener does the actual work
ServiceListener serviceListener = new ServiceListener(template);
return serviceListener.lookup();
}
}
class ServiceListener implements DiscoveryListener {
// Our discovery management support
private DiscoveryManagement mgt;
// A vector to hold all services discovered
private Vector services = new Vector();
// A template used to indicate the service requested by the client
private ServiceTemplate template;
public ServiceListener(ServiceTemplate template) throws IOException {
super();
this.template = template;
// sequence to ensure all lookups are heard
mgt = new LookupDiscovery(LookupDiscovery.NO_GROUPS);
// we implement the listener interface
mgt.addDiscoveryListener(this);
// now set all groups
((LookupDiscovery)mgt).setGroups(LookupDiscovery.ALL_GROUPS);
}
// client will wait until matching service is discovered
public synchronized Object lookup() {
while(services.size() == 0) {
try {
wait();
} catch (InterruptedException ex) {}
}
// just return the first match found
return ((ServiceItem)services.elementAt(0)).service;
}
public synchronized void discovered(DiscoveryEvent de) {
System.out.println("Discovered LUS: ");
ServiceRegistrar[] registrars = de.getRegistrars();
for(int i=0; i < registrars.length; i++) {
try {
System.out.println("URL: " + registrars[i].getLocator().toString());
System.out.println("ID: " + registrars[i].getServiceID());
String groups[] = registrars[i].getGroups();
// simply display the first group returned
System.out.println("GROUPS: " + groups[0]);
ServiceMatches sm = registrars[i].lookup(template, Integer.MAX_VALUE);
System.out.println("Matching services found ------: " + sm.totalMatches);
System.out.println("");
for(int j=0; j < sm.items.length; j++) {
// Process each ServiceItem
if(sm.items[j].service != null) {
services.addElement(sm.items[j]);
} else {
System.out.println("Service item null" + sm.items[j].service);
}
}
// notify the client a matching service has been found
System.out.println("Notifying...");
notifyAll();
} catch(Exception e) { e.printStackTrace(); }
}
}
// we ignore discarded LUS objects in this example
public void discarded(DiscoveryEvent de) {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -