jini, third verse richard chapman october 13, 1999
TRANSCRIPT
![Page 1: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/1.jpg)
Jini, third verse
Richard ChapmanOctober 13, 1999
![Page 2: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/2.jpg)
The interface
// HelloWorldServiceInterface.java
Package corejini.chapter5;
public interface HelloWorldServiceInterface {
public String getMessage(); }
![Page 3: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/3.jpg)
Code for the serviceHelloWorldService.java
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;
![Page 4: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/4.jpg)
Service code, contd
import jini.core.lookup.ServiceRegistration;
import java.util.Hashtable; import java.io.IOException; import java.io.Serializable; import java.rmi.RemoteException;import java.rmi.RMISecurityManager;
![Page 5: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/5.jpg)
Service code, proxy object
class HelloworldServiceProxy implementsSerializable,HelloWorldServiceInterface
{ public HelloWorldServiceProxy() { }public String getMessage() {
return “Hello, world!”; }}
![Page 6: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/6.jpg)
Service code, wrapper class
public class HelloWorldSerivce implementsRunnable {
protected final int LEASE_TIME=10*60*1000;protected HashTable registrations =
new Hashtable(); protected ServiceItem item;protected LookupDiscovery disco;
![Page 7: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/7.jpg)
Listen for discovery eventsclass Listener implements DiscoveryListener {public void discovered(DiscoveryEvent ev) { System.out.println(
“discovered a loookup service!”); ServiceRegistrar[] newregs =
ev.getRegistrars(); for (int i=0;i<newregs.length; i++) { if(!registrations.containsKey(newregs[I])) {
registerWithLookup(newregs[i]); } } }
![Page 8: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/8.jpg)
To discard a lookup service
public void discarded(DiscoveryEvent ev) { ServiceRegistrar[] deadregs = ev.getRegistrars();
for (int i=0; i<deadregs.length; i++) { registrations.remove(deadregs[i]);
}}// end of class Listener from previous slide ago
}
![Page 9: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/9.jpg)
HelloWorldService constructor
public HelloWorldService() throws IOException{item =
new ServiceItem(null,createProxy(),null); //set a security managerif (System.getSecurityManager()==null) {
System.setSecurityManager(new RMISecurityManager());
}// search for the “public group”, named by””disco =
new LookupDiscovery(new String[] {“”});
![Page 10: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/10.jpg)
Lookup
Lookup service
Service item
Proxy
attribute
attribute Service item
![Page 11: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/11.jpg)
HelloWorldService, cont’d//install a listenerdisco.addDiscoveryListener(new Listener());
}
protected HelloWorldServiceInterface createProxy() { return new HelloWorldServiceProxy();
}
![Page 12: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/12.jpg)
HelloWorldService, cont’dprotected synchronized void registerWithLookup(ServiceRegistrar registrar) { ServiceRegistration registration = null; try { registration = registrar.register(item,
LEASE_TIME); } catch(RemoteException ex) {
System.out.println(“Couldn’t register:” +
ex.getMessage()); return; }
![Page 13: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/13.jpg)
HelloWorldService, cont’dif (item.serviceID == null) {
item.serviceID = registration.getServiceID();
System.out.println(“Set serviceID to “+item.serviceID);
}registrations.put(registrar,registration);
// end of registerWithLookup from prev. slide}
![Page 14: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/14.jpg)
Thread to keep the JVM alive
// thread in HelloWorldService to make sure // the JVM doesn’t exitpublic void run() {
while (true) { try {
Thread.sleep(1000000); } catch (InterrruptedException ex)
{ }
} }
![Page 15: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/15.jpg)
Create service, start thread
public static void main(String args[]) { try {
HelloWorldService hws = new HelloWorldService(); new Thread(hws).start();
} catch (IOException ex) { System.out.println(“Couldn’t”+
“create service:”+ex.getMessage());}
}
// finally the end of HelloWorldService}
![Page 16: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/16.jpg)
Client code//needs most of the imports that the serverdid, plus
import net.jini.core.lookup.ServiceTemplate; import java.util.Vector;
![Page 17: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/17.jpg)
Client codepublic class HelloWorldClient implements Runnable { protected ServiceTemplate template; protected LookupDiscovery disco;
class Listener implements DiscoveryListener{public void discovered(DiscoveryEvent ev){ServiceRegistrar[] newregs = ev.getRegistrars(); for (int i=0;i<newregs.length;i++){lookForService(newRegs[i]); }}public void discarded(DiscoveryEvent ev){}
}
![Page 18: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/18.jpg)
Client code
public HelloWorldClient() throws IOException {Class[] types = {HelloWorldServiceInterface.class};template = new ServiceTemplate(null,types,
null); if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
![Page 19: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/19.jpg)
Client code
// only search the public groupdisco = new LookupDiscovery(new String[] {“”});
//install a listenerdisco.addDiscoveryListener(new Listener());
// end of HelloWorldClient}
![Page 20: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/20.jpg)
Look for proxies
protected Object lookforService(ServiceRegistrar lusvc) { Object o = null; try {
o = lusvc.lookup(template); } catch (RemoteException ex) {
System.err.println(“Error doing”+“lookup: ”+ex.getMessage());
return null; }
![Page 21: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/21.jpg)
Look for proxies
if (o==null) { System.err.println(“No matching”+
“service”); return null;
} System.out.println(“Got a matching”+
”service”); System.out.println(“It’s message is “+
((HelloWorldServiceInterface) o).getMessage()); return o; }
![Page 22: Jini, third verse Richard Chapman October 13, 1999](https://reader033.vdocuments.us/reader033/viewer/2022051515/551c57e25503467b488b55f8/html5/thumbnails/22.jpg)
Client, cont’d// client needs a thread “run” exactly // like server, to keep JVM alivepublic void run() {
while (true) { .... //method to start the thread for clent public static void main(String args[]) { try { HelloWorldClient hwc = new
HelloWorldClient(); new Thread(hwc).start;
} catch (IOException ex) { System.out.println(“Couldn’t create cli:”+
ex.getMessage()); }}}