Carlisle's Jini Badge Locator Service (designed for him to understand Jini services).
Thanks also to
Jan Newmarch's Jini Tutorial for helping
this understanding
Four machines were used in the experiment.
shop110ab.cse: This machine provided the lookup service. None of the code I wrote was placed on the machine by me. Lookup service gets and passes on a serialized stub object.
balmy.cse: This machine provided the web service. Part of startup of the programs specifies that this machine will serve the byte codes needed by clients.
Software placed on balmy was:
GetPut.class:
Service_Stub.class:
starthttpcommand:
java -jar /usr/local/jini/jini1_0/lib/tools.jar -port 8080 -dir /home/carlisle -verbose
bandy.cse: This machine was a client that knew the location of "carlisle" and told the Service that he was in "Dunstan" . Files on this machine were
TestGetPut1.class
policy.all
GetPut.class
startclient1command
java -cp /usr/local/jini/jini1_0/lib/jini-core.jar:/usr/local/jini/jini1_0/lib/jiniext.jar:
/usr/local/jini/jini1_0/lib/sun-util.jar:/home/carlisle
-Djava.security.policy=/usr/local/jini/jini1_0/example/lookup/policy.all TestGetPut1
(note that the policy.all file came from a different place but could have been local copy).
barre.cse: This machine was a client that wanted to know the location of carlisle and is told that he is in Dunstan. Files on this machine were
TestGetPut2.class policy.all GetPut.class client.bat startclient2command
balsa.cse: This machine had two purposes. One to start up the database service and the other to be the database service. Files on this machine are:
Server.class
Service_Stub.class
GetPut.class
Service.class
startservicecommand
java -cp /usr/local/jini/jini1_0/lib/jini-core.jar:/usr/local/jini/jini1_0/lib/jini-ext.jar:
/usr/local/jini/jini1_0/lib/sun-util.jar:/home/carlisle
-Djava.security.policy=/usr/local/jini/jini1_0/example/lookup/policy.all
-Djava.rmi.server.codebase=http://balmy.cse:8080/ Server
OK….now for the code:
GetPut.java: the interface for client and service
//This file is compiled with the command: java GetPut.java
import java.lang.String;
import java.io.Serializable;
import java.rmi.Remote;
//probably should separate this interface for the two kinds of clients
public interface GetPut extends Serializable,Remote
{
public void isAt(String person, String location)
throws java.rmi.RemoteException;
public String whereIs(String person)
throws java.rmi.RemoteException;
}
TestGetPut1.java the first client that knows where carlisle is
Client files were compiled with the command
javac -classpath /opt/jini/jini1_0/lib/jinicore.jar:
/opt/jini/jini1_0/lib/jini-ext.jar:
/opt/jini/jini1_0/lib/sun-util.jar:/home/carlisle TestGetPut1.java TestGetPut2.java
import net.jini.discovery.*;
import net.jini.core.lookup.*;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
public class TestGetPut1 implements DiscoveryListener {
protected ServiceTemplate template;
protected LookupDiscovery disco;
public static void main(String[] argv) {
new TestGetPut1();
try {
Thread.currentThread().sleep(100000L);
}
catch(java.lang.InterruptedException e) {}
}
public TestGetPut1() {
System.setSecurityManager(new RMISecurityManager());
LookupDiscovery discover = null;
try {
// debug
System.out.println("Constructor ");
//end debug
discover = new LookupDiscovery(
LookupDiscovery.ALL_GROUPS);
}
catch(Exception e)
{ System.err.println(""+e);
System.exit(1);
}
discover.addDiscoveryListener(this);
}
public void discovered(DiscoveryEvent ev) {
ServiceRegistrar[] newregs = ev.getRegistrars();
// debug
System.out.println("entered discovered ");
// end debug
Class [] classes = new Class[]{GetPut.class};
GetPut o = null;
ServiceTemplate template =
new ServiceTemplate(null, classes, null);
for (int i=0 ; i<newregs.length ; i++) {
ServiceRegistrar r = newregs[i];
try {
o = (GetPut) r.lookup(template);
}
catch(java.rmi.RemoteException e) {
e.printStackTrace();
System.exit(2);
}
if(o == null) {
//debug
System.out.println("Continuing search ");
continue;
}
try {
o.isAt("Carlisle", "Dunstan");
}
catch(java.rmi.RemoteException e) {
System.err.println(""+e);
}
}
}
public void discarded(DiscoveryEvent ev) {}
}
Here's the client that tries to find carlisle
import net.jini.discovery.*;
import net.jini.core.lookup.*;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
public class TestGetPut2 implements DiscoveryListener {
protected ServiceTemplate template;
protected LookupDiscovery disco;
public static void main(String[] argv) {
new TestGetPut2();
try {
Thread.currentThread().sleep(100000L);
}
catch(java.lang.InterruptedException e) {}
}
public TestGetPut2() {
System.setSecurityManager(new RMISecurityManager());
LookupDiscovery discover = null;
try {
// debug
System.out.println("Constructor2 ");
//end debug
discover = new LookupDiscovery(
LookupDiscovery.ALL_GROUPS);
}
catch(Exception e)
{ System.err.println(""+e);
System.exit(1);}
discover.addDiscoveryListener(this);
}
public void discovered(DiscoveryEvent ev) {
ServiceRegistrar[] newregs = ev.getRegistrars();
// debug
System.out.println("entered discovered ");
// end debug
Class [] classes = new Class[]{GetPut.class};
GetPut o = null;
ServiceTemplate template =
new ServiceTemplate(null, classes, null);
for (int i=0 ; i<newregs.length ; i++) {
ServiceRegistrar r = newregs[i];
try {
o = (GetPut) r.lookup(template);
}
catch(java.rmi.RemoteException e) {
e.printStackTrace();
System.exit(2);
}
if(o == null) {
//debug
System.out.println("Continuing search ");
continue;
}
try {
String t = o.whereIs("Carlisle");
System.out.println(t);
}
catch(java.rmi.RemoteException e) {
System.err.println(""+e);
}
}
}
public void discarded(DiscoveryEvent ev) {}
}
The following file is the databases Service.java and was compiled with the command
javac -classpath /opt/jini/jini1_0/lib/jini-core.jar:/opt/jini/jini1_0/lib/jini-ext.jar:/opt/jini/jini1_0/lib/sun-util.jar:/home/carlisle Service.java
import java.util.Hashtable;
import java.lang.String;
import java.rmi.server.UnicastRemoteObject;
class Service extends UnicastRemoteObject
implements GetPut
{ protected Hashtable d;
public Service() throws java.rmi.RemoteException {
d = new Hashtable();
}
public void isAt(String person, String location)
throws java.rmi.RemoteException { d.put(person,location);}
public String whereIs(String person)
throws java.rmi.RemoteException { return (String)d.get(person);}
}
additionally the RMI stub is generated from this compiled class with the command
rmic –v1.2 –d /home/carlisle Service
The file Server.java follows. This program starts the service and registers it.
It is compiled with the command
javac -classpath /opt/jini/jini1_0/lib/jini- core.jar:/opt/jini/jini1_0/lib/jini-ext.jar:
/opt/jini/jini1_0/lib/sun-util.jar:/home/carlisle Server.java
import net.jini.discovery.*;
import net.jini.core.lookup.*;
import java.util.Hashtable;
import java.io.IOException;
import java.rmi.RMISecurityManager;
import com.sun.jini.lease.*;
import net.jini.core.lease.Lease;
public class Server implements
DiscoveryListener, LeaseListener {
protected Service service;
protected LeaseRenewalManager leaseManager =
new LeaseRenewalManager();
protected ServiceID serviceID = null;
public static void main(String args[]) {
new Server();
}
public Server() {
try {
service = new Service();
}
catch(Exception e) {
System.err.println(""+e);
System.exit(1);
}
System.setSecurityManager(
new RMISecurityManager());
LookupDiscovery discover = null;
try {
discover = new LookupDiscovery(
LookupDiscovery.ALL_GROUPS);
}
catch(Exception e) {
System.err.println(""+e);
System.exit(1);
}
discover.addDiscoveryListener(this);
}
public void discovered(DiscoveryEvent evt) {
ServiceRegistrar[] registrars = evt.getRegistrars();
for (int n=0 ; n<registrars.length ; n++) {
ServiceRegistrar registrar = registrars[n];
ServiceItem item = new ServiceItem(
null, service, null);
//debug
System.out.println("registered " + service.getClass().getName());
//enddebug
ServiceRegistration reg = null;
try {
reg = registrar.register(item,Lease.FOREVER);
}
catch(java.rmi.RemoteException e) {
System.err.println("" + e);
continue;
}
leaseManager.renewUntil(reg.getLease(),
Lease.FOREVER,this);
}
}
public void discarded(DiscoveryEvent evt) {}
public void notify(LeaseRenewalEvent evt) {
System.out.println("LeaseExpired " + evt); }
}