Developing A Simple Jini Client
Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................The Java 2 compiler and API - JDK 1.2
.........................................
The Jini 1.x System Software

This simple Jini client Queries the Jini Service for it's current time in milliseconds by invoking it's getTime() method. It then computes the time taken for the message to reach it and prints it out in milliseconds.

The Steps involved in developing a Jini Client are

1. Code the Jini Client
2. Start the Client

1. Code the Jini Client

MyClient.java
import net.jini.core.entry.*;
import net.jini.core.lookup.*;
import net.jini.core.discovery.*;
import net.jini.lookup.*;
import net.jini.lookup.entry.*;
import net.jini.discovery.*;

import java.rmi.*;
/***************************************************************************

 MyClient -- The Jini Client Class

 ***************************************************************************/

class MyClient {
 public static void main (String[] args) {
  int iPort;
  String sHost;
  Entry[] aeAttributes;
  LookupLocator lookup;
  ServiceID id;
  ServiceRegistrar registrar;
  ServiceTemplate template;

  MyServerInterface myServerInterface;

  try {
  
/*
   Setting the security manager to allow the RMI class loader
   to go to the codebase for classes that are not available
   locally.
   ========================================================== */

   System.setSecurityManager (new RMISecurityManager ());
 

   /*
   Find the Jini lookup service (reggie) and print its location.
   ============================================================= */

   lookup = new LookupLocator ("jini://localhost");
   sHost = lookup.getHost ();
   iPort = lookup.getPort ();

   System.out.println ();
   System.out.println ("client: LookupLocator = " + lookup);
   System.out.println ("client: LookupLocator.host = " + sHost);
   System.out.println ("client: LookupLocator.port = " + iPort);
 

   /*
   Get the lookup service's ServiceRegistrar (the class by which
   interaction with the lookup service is possible).
   ============================================================= */

   registrar = lookup.getRegistrar ();
   id = registrar.getServiceID ();

   System.out.println ("client: ServiceRegistrar = " + registrar);
   System.out.println ("client: ServiceID = " + id);
 

   /*
   Perform a search on the lookup server to find the service
   that has the name attribute of "MyServer". The lookup
   service returns an interface object to the service.
   ========================================================= */

   aeAttributes = new Entry[1];
   aeAttributes[0] = new Name ("MyServer");
   template = new ServiceTemplate (null, null, aeAttributes);
   myServerInterface = (MyServerInterface) registrar.lookup (template);

   System.out.println ("client: ServiceTemplate = " + template);
   System.out.println ("client: Service object = " + myServerInterface);
 

   /*
   If the correct object was returned, call one of its methods.
   ============================================================ */

   if (myServerInterface instanceof MyServerInterface) {
    long startTime = myServerInterface.getTime();
    System.out.println ("client: Time at server is " + startTime);
    long stopTime = System.currentTimeMillis();
    System.out.println ("client: Time at client is " + stopTime);
    float difference= stopTime-startTime;
    System.out.println ("client: Time for message to traverse the net is --->"
                        + difference + " msecs<---");
   }
  } catch (Exception e) { System.out.println ("client: MyClient.main() exception: " + e); }
 }
}
 

 

4. Startup the Client

H:\jini\TimeServer>
H:\jini\TimeServer>set CLASSPATH=.;G:\sun\jini\jini1_0\lib\jini-core.jar;G:\sun\jini\jini1_0\lib\jini-ext.jar;
G:\sun\jini\jini1_0\lib\mahalo.jar;G:\sun\jini\jini1_0\lib\mahalo-dl.jar;G:\sun\jini\jini1_0\lib\reggie.jar;
G:\sun\jini\jini1_0\lib\reggie-dl.jar;G:\sun\jini\jini1_0\lib\sun-util.jar;G:\sun\jini\jini1_0\lib\tools.jar;
%CLASSPATH%

H:\jini\TimeServer>
H:\jini\TimeServer>java -Djava.security.policy=H:\jini\TimeServer\policy.all -Djava.rmi.server.codebase=http://localhost/ MyClient

client: LookupLocator = jini://localhost/
client: LookupLocator.host = localhost
client: LookupLocator.port = 4160
client: ServiceRegistrar = com.sun.jini.reggie.RegistrarProxy@2cd27317
client: ServiceID = 88fc7d88-8ea6-4db9-9cac-ddeab6249ecc
client: ServiceTemplate = net.jini.core.lookup.ServiceTemplate@f82dd4d7
client: Service object = MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3980](remote),objID:[4c85d4c7:d5f
eec9ba4:-8000, 0]]]]
client: Time at server is 919104970506
client: Time at client is 919104970506
client: Time for message to traverse the net is --->0.0 msecs<---

H:\jini\TimeServer>java -Djava.security.policy=H:\jini\TimeServer\policy.all -Djava.rmi.server.codebase=http://localhost/ MyClient

client: LookupLocator = jini://localhost/
client: LookupLocator.host = localhost
client: LookupLocator.port = 4160
client: ServiceRegistrar = com.sun.jini.reggie.RegistrarProxy@2cd27317
client: ServiceID = 88fc7d88-8ea6-4db9-9cac-ddeab6249ecc
client: ServiceTemplate = net.jini.core.lookup.ServiceTemplate@f82dd4b8
client: Service object = MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3980](remote),objID:[4c85d4c7:d5feec9ba4:-8000, 0]]]]
client: Time at server is 919105007119
client: Time at client is 919105007119
client: Time for message to traverse the net is --->0.0 msecs<---

H:\jini\TimeServer>java -Djava.security.policy=H:\jini\TimeServer\policy.all -Djava.rmi.server.codebase=http://localhost/ MyClient

client: LookupLocator = jini://localhost/
client: LookupLocator.host = localhost
client: LookupLocator.port = 4160
client: ServiceRegistrar = com.sun.jini.reggie.RegistrarProxy@2cd27317
client: ServiceID = 88fc7d88-8ea6-4db9-9cac-ddeab6249ecc
client: ServiceTemplate = net.jini.core.lookup.ServiceTemplate@f821d4bf
client: Service object = MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3980](remote),objID:[4c85d4c7:d5feec9ba4:-8000, 0]]]]
client: Time at server is 919105010724
client: Time at client is 919105010734
client: Time for message to traverse the net is --->10.0 msecs<---

H:\jini\TimeServer>java -Djava.security.policy=H:\jini\TimeServer\policy.all -Djava.rmi.server.codebase=http://localhost/ MyClient

client: LookupLocator = jini://localhost/
client: LookupLocator.host = localhost
client: LookupLocator.port = 4160
client: ServiceRegistrar = com.sun.jini.reggie.RegistrarProxy@2cd27317
client: ServiceID = 88fc7d88-8ea6-4db9-9cac-ddeab6249ecc
client: ServiceTemplate = net.jini.core.lookup.ServiceTemplate@fb79d483
client: Service object = MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3980](remote),objID:[4c85d4c7:d5feec9ba4:-8000, 0]]]]
client: Time at server is 919105014360
client: Time at client is 919105014370
client: Time for message to traverse the net is --->10.0 msecs<---

H:\jini\TimeServer>java -Djava.security.policy=H:\jini\TimeServer\policy.all -Djava.rmi.server.codebase=http://localhost/ MyClient

client: LookupLocator = jini://localhost/
client: LookupLocator.host = localhost
client: LookupLocator.port = 4160
client: ServiceRegistrar = com.sun.jini.reggie.RegistrarProxy@2cd27317
client: ServiceID = 88fc7d88-8ea6-4db9-9cac-ddeab6249ecc
client: ServiceTemplate = net.jini.core.lookup.ServiceTemplate@f82dd486
client: Service object = MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3980](remote),objID:[4c85d4c7:d5feec9ba4:-8000, 0]]]]
client: Time at server is 919105017744
client: Time at client is 919105017754
client: Time for message to traverse the net is --->10.0 msecs<---

H:\jini\TimeServer>java -Djava.security.policy=H:\jini\TimeServer\policy.all -Djava.rmi.server.codebase=http://localhost/ MyClient

client: LookupLocator = jini://localhost/
client: LookupLocator.host = localhost
client: LookupLocator.port = 4160
client: ServiceRegistrar = com.sun.jini.reggie.RegistrarProxy@2cd27317
client: ServiceID = 88fc7d88-8ea6-4db9-9cac-ddeab6249ecc
client: ServiceTemplate = net.jini.core.lookup.ServiceTemplate@f82dd48a
client: Service object = MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3980](remote),objID:[4c85d4c7:d5feec9ba4:-8000, 0]]]]
client: Time at server is 919105021370
client: Time at client is 919105021380
client: Time for message to traverse the net is --->10.0 msecs<---

H:\jini\TimeServer>java -Djava.security.policy=H:\jini\TimeServer\policy.all -Djava.rmi.server.codebase=http://localhost/ MyClient

client: LookupLocator = jini://localhost/
client: LookupLocator.host = localhost
client: LookupLocator.port = 4160
client: ServiceRegistrar = com.sun.jini.reggie.RegistrarProxy@2cd27317
client: ServiceID = 88fc7d88-8ea6-4db9-9cac-ddeab6249ecc
client: ServiceTemplate = net.jini.core.lookup.ServiceTemplate@fb79d489
client: Service object = MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3980](remote),objID:[4c85d4c7:d5feec9ba4:-8000, 0]]]]
client: Time at server is 919105024785
client: Time at client is 919105024785
client: Time for message to traverse the net is --->0.0 msecs<---

H:\jini\TimeServer>




 

click here to go to the
Developing a Simple Jini Service Page...
click here to go to
My Jini HomePage...

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since February 12,1999.

Last Updated : Feb 12, '99

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-99, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site,are trademarks of their respective owners.