CS 696 Emerging Technologies: Java Distributed Computing Spring Semester, 1999 Client-side Server, Unicast Join & Leases |
||
---|---|---|
© 1999, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 25-Mar-99 |
Jini Services
Some topics we need to cover:
Server & Proxies
Note the presentation of Server Proxies follows very closely to the Simple example in Jan Newmarch’s Jini Tutorial at: http://pandonia.canberra.edu.au/java/jini/tutorial/Jini.xml. The server provides a different service in this example. A few other details relating to RMI usage and registration are also changed. It is instructive to look at both examples.
In designing a Jini service there are several different strategies:
Client-side Server
Will use an encryption server as an example
The classes:
package whitney.jini.examples.serverProxy; public interface EncryptionInterface { public String[] encodeTypes(); public String encode( String encodeType, String plainText ); public String decode( String encodeType, String encodedText); }
DirectEncryptionServer
package whitney.jini.examples.serverProxy; import java.io.Serializable; import sdsu.io.XorOutputStream; import sdsu.io.XorOutputStream; public class DirectEncryptionServer implements EncryptionInterface, Serializable { private static final String XOR = "xor"; private byte mask; private String[] encodeTypes = { XOR }; public DirectEncryptionServer( byte aMask ) { mask = aMask; } public String[] encodeTypes() { return encodeTypes; } public String encode( String encodeType, String plainText ) { if ( encodeType.equals( XOR) ) return xorText( plainText ); else // just an example. Save space by not using exception return ""; } public String decode( String encodeType, String encodedText) { if ( encodeType.equals( XOR) ) return xorText( encodedText ); else // just an example. Save space by not using exception return ""; }
DirectEncryptionServer Continued
private String xorText(String text) { byte inputBytes[] = text.getBytes(); byte[] xorBytes = new byte[inputBytes.length]; for (int k = 0; k < inputBytes.length;k++) xorBytes[k ] =(byte) (inputBytes[k] ^ mask); return new String(xorBytes); } }
DirectEncryptionClient
package whitney.jini.examples.serverProxy; import net.jini.core.entry.Entry; import net.jini.core.lookup.ServiceTemplate; import net.jini.core.lookup.ServiceRegistrar; import net.jini.core.discovery.LookupLocator; import net.jini.lookup.entry.Name; import java.rmi.RMISecurityManager; public class DirectEncryptionClient{ public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); LookupLocator lookup = new LookupLocator ("jini://eli.sdsu.edu"); ServiceRegistrar registrar = lookup.getRegistrar (); Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("EncryptionService"); ServiceTemplate template = new ServiceTemplate (null, null, serverAttributes); EncryptionInterface encoder = (EncryptionInterface) registrar.lookup (template); String testText = "Hi Mom"; String encodedText = encoder.encode( "xor", testText ); String decodedText = encoder.encode( "xor", encodedText ); System.out.println ( "Original Text: " + testText ); System.out.println ( "Encoded Text: " + encodedText ); System.out.println ( "Decoded Text: " + decodedText ); } }
RegisterEncryptionService
package whitney.jini.examples.serverProxy; import com.sun.jini.lease.LeaseRenewalManager; import java.rmi.RMISecurityManager; import java.util.Date; import net.jini.core.discovery.LookupLocator; import net.jini.core.entry.Entry; import net.jini.core.lease.Lease; import net.jini.core.lookup.ServiceID; import net.jini.core.lookup.ServiceItem; import net.jini.core.lookup.ServiceRegistrar; import net.jini.core.lookup.ServiceRegistration; import net.jini.lookup.entry.Name; public class RegisterEncryptionService { public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("EncryptionService"); ServiceID serverID = null; DirectEncryptionServer theServer = new DirectEncryptionServer((byte) 11); ServiceItem encryptServer = new ServiceItem( serverID, theServer, serverAttributes); LookupLocator lookup = new LookupLocator ("jini://eli.sdsu.edu"); ServiceRegistrar registrar = lookup.getRegistrar (); ServiceRegistration serverReg = registrar.register(encryptServer, 1000 * 60 ); System.out.println ( "Server registered" );
RegisterEncryptionService Continued
Lease serverLease = serverReg.getLease(); LeaseRenewalManager manageLease = new LeaseRenewalManager( serverLease, Lease.FOREVER, null ); serverID = serverReg.getServiceID(); long now = (new Date()).getTime(); long leaseDuration = (serverLease.getExpiration() - now)/1000; System.out.println ( "Server id: " + serverID ); System.out.println ( "Server lease: " + serverLease ); System.out.println ( "Lease duration in seconds " + leaseDuration ); Thread.sleep( 1000 * 60 * 15 ); System.out.println ( "Server going down: "); manageLease.cancel(serverLease); } }
RegisterEncryptionService Explained
Set the security manager to allow downloading of reggie code
System.setSecurityManager (new RMISecurityManager ());Prepare to register server with lookup service using unicast join
// Entry is used described the attributes of the service Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("EncryptionService"); ServiceID serverID = null; // Create the server DirectEncryptionServer theServer = new DirectEncryptionServer((byte) 11); // Service items contain the data needed to register the service ServiceItem encryptServer = new ServiceItem( serverID, theServer, serverAttributes);
RegisterEncryptionService Explained Continued
Perform the unicast join
// Find the lookup service LookupLocator lookup = new LookupLocator ("jini://eli.sdsu.edu"); ServiceRegistrar registrar = lookup.getRegistrar ();
// Register the server, the second arugment of register is the // requested lease time ServiceRegistration serverReg = registrar.register(encryptServer, 1000 * 60 );
Lease serverLease = serverReg.getLease(); LeaseRenewalManager manageLease = new LeaseRenewalManager( serverLease, Lease.FOREVER, null );Print out some information about the lease & server registration
serverID = serverReg.getServiceID(); long now = (new Date()).getTime(); long leaseDuration = (serverLease.getExpiration() - now)/1000; System.out.println ( "Server id: " + serverID ); System.out.println ( "Server lease: " + serverLease ); System.out.println ( "Lease duration in seconds " + leaseDuration );
RegisterEncryptionService Explained Continued Keep the server object & LeaseRenewalManager alive
Thread.sleep( 1000 * 60 * 15 );Be polite and cancel the lease.
System.out.println ( "Server going down: "); manageLease.cancel(serverLease);
DirectEncryptionClient Explained
Contact the lookup service
LookupLocator lookup = new LookupLocator ("jini://eli.sdsu.edu"); ServiceRegistrar registrar = lookup.getRegistrar ();Request a DirectEncryptionServer object
Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("EncryptionService"); ServiceTemplate template = new ServiceTemplate (null, null, serverAttributes); EncryptionInterface encoder = (EncryptionInterface) registrar.lookup (template);
String testText = "Hi Mom"; String encodedText = encoder.encode( "xor", testText ); String decodedText = encoder.encode( "xor", encodedText );
Running the Example
Class Location
Client Machine needs:
Starting the programs
Jini components Start rmid