CS 696 Emerging Technologies: Java Distributed Computing Spring Semester, 1999 More Reggie |
||
---|---|---|
© 1999, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 19-Apr-99 |
Reggie Contents
Many of you seem to be running reggies what do not have any services registered in them. The following program will display services registered with a reggie. Before you spend all day trying to determine why your client does not work use this program to see if your server actually registered with your reggie. You provide the group(s) of the reggie(s) you are interested in on the command line. The program will contact all reggies in the given group(s). It will display the entries of all services that contain a Name entry. If you want to display all services in the reggie replace "new ServiceTemplate (null, null, serverAttributes)" with "new ServiceTemplate (null, null, null) " in the method displayService. A sample command line is:
java ReggieContents –groups whitney.rohan.sdsu.edu,whitney.moria.sdsu.edu import java.io.IOException; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.util.StringTokenizer; import net.jini.core.entry.Entry; import net.jini.core.lookup.ServiceItem; import net.jini.core.lookup.ServiceMatches; import net.jini.core.lookup.ServiceRegistrar; import net.jini.core.lookup.ServiceRegistration; import net.jini.core.lookup.ServiceTemplate; import net.jini.discovery.DiscoveryEvent; import net.jini.discovery.DiscoveryListener; import net.jini.discovery.LookupDiscovery; import net.jini.lookup.entry.Name; import sdsu.util.ProgramProperties; public class ReggieContents implements DiscoveryListener { private static final int MILLSECS_PER_MINUTE = 1000 * 60; LookupDiscovery findLookupServices; public static void main( String[] arguments ) throws IOException, InterruptedException { ProgramProperties flags = new ProgramProperties( arguments); String groupsString = flags.getString( "groups", "ALL" ); ReggieContents myReggie = new ReggieContents( groupsString ); Thread.currentThread().sleep( MILLSECS_PER_MINUTE * 1 ); myReggie.terminate(); } public ReggieContents( String groupList) throws IOException { formateGroupList( groupList ) ; findLookupServices = new LookupDiscovery( formateGroupList( groupList ) ); findLookupServices.addDiscoveryListener( this ); System.out.println( "Start looking"); }
ReggieContents Continued
public void terminate() { findLookupServices.terminate(); } private String[] formateGroupList( String groupList ) { if (groupList.equals( "ALL") ) return LookupDiscovery.ALL_GROUPS; if ( groupList.indexOf( ',' ) < 0 ) return new String[] { groupList.trim() }; StringTokenizer groups = new StringTokenizer( groupList, ",'"); String[] formatedGroups = new String[ groups.countTokens() ]; for ( int k = 0; k < formatedGroups.length; k++ ) { formatedGroups[k] = groups.nextToken().trim(); } return formatedGroups; } public void discovered(DiscoveryEvent lookupService ) { displayEvent( lookupService); } public void discarded(DiscoveryEvent lookupService) { displayEvent( lookupService); } private void displayEvent( DiscoveryEvent event) { try { ServiceRegistrar lookupServices[] = event.getRegistrars(); for (int i = 0; i < lookupServices.length; i++) displayService(lookupServices[i]); } catch (Exception lookupProblem) { lookupProblem.printStackTrace(); } }
ReggieContents Continued
private void displayService(ServiceRegistrar service) throws RemoteException { System.out.println("Registrar " + service.getLocator() ); String[] serviceGroups = service.getGroups(); if ( serviceGroups.length > 0 ) { System.out.println( "\tGroups of registrar "); for (int k=0; k< serviceGroups.length; k++ ) System.out.println( "\t\t" + serviceGroups[k] ); } Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name (); ServiceTemplate matchAll = new ServiceTemplate (null, null, serverAttributes); ServiceMatches reggieSevices = service.lookup( matchAll, 10 ); System.out.println( "\tNumber of matches: " + reggieSevices.totalMatches ); ServiceItem[] foundItems = reggieSevices.items; for ( int k = 0; k < foundItems.length;k++ ) displayServiceItem( foundItems[k]); } private void displayServiceItem( ServiceItem item ) { Entry[] discriptors = item.attributeSets; System.out.print( "\t\t" ); for (int k = 0 ; k < discriptors.length; k++ ) System.out.print( discriptors[k].toString() + ", "); System.out.println(); } }
Discover-Join HelloWorld Example
The following example uses the Discover and Join in the client and server. A client on moria can use a server on rohan. This has been tested.
Source Code
HelloInterface
package whitney.jini.examples.discoveryJoin; import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloInterface extends Remote { public String sayHello() throws RemoteException; }
HelloServer
package whitney.jini.examples.discoveryJoin; import com.sun.jini.lease.LeaseRenewalManager; import com.sun.jini.lookup.JoinManager; import com.sun.jini.lookup.ServiceIDListener; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.rmi.server.UnicastRemoteObject; import java.util.StringTokenizer; import net.jini.core.entry.Entry; import net.jini.core.lookup.ServiceID; import net.jini.discovery.LookupDiscovery; import net.jini.lookup.entry.Name; import sdsu.util.ProgramProperties; public class HelloServer extends UnicastRemoteObject implements HelloInterface, ServiceIDListener { private ServiceID myID; public HelloServer() throws RemoteException { } public String sayHello () throws RemoteException { return ("Hello World from Jini Hello server!"); } public void serviceIDNotify (ServiceID uniqueID) { myID = uniqueID; System.out.println("server: ID set: " + myID ); }
HelloServer Continued public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); HelloServer myServer = new HelloServer (); ProgramProperties flags = new ProgramProperties( args); String groupsString = flags.getString( "groups", "NONE" ); String[] groups = formateGroupList( groupsString ); Entry[] identityingAttributes = new Entry[1]; identityingAttributes[0] = new Name("HelloServer"); JoinManager myManager = new JoinManager ( myServer, identityingAttributes, groups, null, myServer, new LeaseRenewalManager () ); System.out.println ("Server has been Joined!"); } private static String[] formateGroupList( String groupList ) { if (groupList.equals( "NONE") ) { System.out.println( "Usage: java HelloServer -groups=group1,group2 " ); System.exit( 0 ); } if ( groupList.indexOf( ',' ) < 0 ) return new String[] { groupList.trim() }; StringTokenizer groups = new StringTokenizer( groupList, ",'"); String[] formatedGroups = new String[ groups.countTokens() ]; for ( int k = 0; k < formatedGroups.length; k++ ) { formatedGroups[k] = groups.nextToken().trim(); } return formatedGroups; } }
HelloClient
package whitney.jini.examples.discoveryJoin; import java.io.IOException; import java.rmi.RMISecurityManager; import net.jini.core.discovery.LookupLocator; import net.jini.core.entry.Entry; import net.jini.core.lookup.ServiceRegistrar; import net.jini.core.lookup.ServiceTemplate; import net.jini.discovery.DiscoveryEvent; import net.jini.discovery.DiscoveryListener; import net.jini.discovery.LookupDiscovery; import net.jini.lookup.entry.Name; import sdsu.util.ProgramProperties; public class HelloClient implements DiscoveryListener { public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); ProgramProperties flags = new ProgramProperties( args); if ( !flags.containsKey( "group") ) { System.out.println( "Usage: java HelloClient -group=groupName" ); System.exit(0); } String groupsString = flags.getString( "group" ); new HelloClient( groupsString ); // discovery nneds some time to work Thread.currentThread().sleep( 1000 * 15 ); } public HelloClient( String group ) throws IOException { String[] serverGroup = { group }; LookupDiscovery findAllLookupServices = new LookupDiscovery( serverGroup ); findAllLookupServices.addDiscoveryListener( this ); } public void discovered(DiscoveryEvent lookupService ) { findServer( lookupService); } public void discarded(DiscoveryEvent lookupService) { // don’t care about this}
HelloClient Continued
private void findServer( DiscoveryEvent event) { Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("HelloServer"); ServiceTemplate template = new ServiceTemplate (null, null, serverAttributes); try { ServiceRegistrar lookupServices[] = event.getRegistrars(); for (int k = 0; k < lookupServices.length; k++) { HelloInterface myServerInterface = (HelloInterface) lookupServices[k].lookup (template); if (myServerInterface != null ){ System.out.println ( myServerInterface.sayHello () ); } else{ System.out.println ( "No server yet" ); } } } catch (Exception lookupProblem) { lookupProblem.printStackTrace(); } } }
Running the Example
The following is how I ran the example on rohan. I assume that rmid and reggie are not running before I start. I also assume that all rmid log files and all reggie files have been deleted.
javac Hello*.java
rmid -C-Djava.rmi.server.hostname=rohan.sdsu.edu -port 4536 &
#/bin/csh #Start jini services switch ($#) case 0: case 1: echo "Usage: reggie httpPort rmidPort" exit 2 endsw set httpPort=$1; set rmidPort=$2; set jiniDir=/opt/jini1_0/lib set jiniLog=~whitney/jini_data/jini_logs set jiniData=~whitney/jini_data rm -R $jiniLog/reggie java -Djava.rmi.server.hostname=rohan.sdsu.edu -Djava.rmi.activation.port=$rmidPort -jar $jiniDir/reggie.jar http://eli.sdsu.edu:$httpPort/reggie-dl.jar $jiniData/policy $jiniLog/reggie ´whoami´"."´hostname´"."´domainname´ &
echo "reggie started"
Running the Example Continued
java -Djava.rmi.server.hostname=rohan.sdsu.edu whitney.jini.examples.discoveryJoin.HelloServer -groups=whitney.rohan.sdsu.edu &
java whitney.jini.examples.discoveryJoin.HelloClient -group=whitney.rohan.sdsu.edu