SDSU CS 696 Emerging Technologies: Java Distributed Computing
Spring Semester, 1999
More Reggie
Previous    Lecture Notes Index    Next    
© 1999, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 19-Apr-99

Contents of Doc 27, More Reggie


References


Jini API Documentation

Local HTML Copy: http://www-rohan.sdsu.edu/doc/jini/doc/api/index.html

rmid documentation

Local HTML Copy http://www-rohan.sdsu.edu/doc/java/jdk1.2/docs/tooldocs/solaris/rmid.html


Doc 27, More Reggie Slide # 2

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");
   }

Doc 27, More Reggie Slide # 3
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();
      }
   }

Doc 27, More Reggie Slide # 4
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();
   }
}

Doc 27, More Reggie Slide # 5

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 );
   }

Doc 27, More Reggie Slide # 6
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;
   }
}

Doc 27, More Reggie Slide # 7

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}

Doc 27, More Reggie Slide # 8
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();
      }
   }
}

Doc 27, More Reggie Slide # 9

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


rmic whitney.jini.examples.discoveryJoin.HelloServer

Note that rmic does not see jar files that are standard extensions to the jdk. Rmic does not see the jini jar file that are added to /opt/jdk1.2/jre/lib/ext/. Hence I had to add /opt/jini1_0/lib/sun-util.jar to my classpath before rmic would generate the stub.


   rmid -C-Djava.rmi.server.hostname=rohan.sdsu.edu -port 4536 &

Recall that there is a problem with rohan’s configuration and rmi clients. The -C-D flag tells rmid to set the java.rmi.server.hostname property on all child JVMs that is starts. See http://www-rohan.sdsu.edu/doc/java/jdk1.2/docs/tooldocs/solaris/rmid.html for more details. This insures that reggie is properly configured. If you had set this property in assignment 2 you could have and moria clients talk you your rohan activatable server. If rmid throws exceptions when you start it, check for old log files.

Doc 27, More Reggie Slide # 10
Running the Example Continued

Here is the script file that I use to start reggie. Note that I am using a web server on eli.

#/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"

Doc 27, More Reggie Slide # 11
Running the Example Continued


java -Djava.rmi.server.hostname=rohan.sdsu.edu 
   whitney.jini.examples.discoveryJoin.HelloServer
   -groups=whitney.rohan.sdsu.edu &

The -D flag is needed for clients on different machines due to rohan’s configuration problems. When you register this server with reggie, you should see output like:
server: ID set: 49adedd1-fcd7-440d-8761-ccebd6281a47
If this does not happen the server was NOT registered with a reggie.

java whitney.jini.examples.discoveryJoin.HelloClient 
   -group=whitney.rohan.sdsu.edu

Given this setup you can run the client on moria. This stub class has to be either on the client side or dynamically downloaded to the client.


Copyright ©, All rights reserved.
1999 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 19-Apr-99    Next