1 Lecture 16 Jini Technology Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer...

Preview:

Citation preview

1

Lecture 16Jini Technology

Instructors: Fu-Chiung Cheng

(鄭福炯 )Associate Professor

Computer Science & EngineeringTatung Institute of Technologyemail: cheng@cse.ttit.edu.tw

http:// www.cse.ttit.edu.tw/fccheng

2

Three main players in Jini

• Service, such as a printer, a toaster, a marriage agency• Client which would like to make use of this service. • Lookup service (service locator) which acts as a broker/trader/locator between services and clients. • A network connecting all three of these (running TCP/IP)

ClientServiceProvider

ServiceLocator

TCP/IP

3

Service Registration

• Service to register itself with the lookup service. • Service sends a multicast message ( TCP port 4160). • Each lookup service will be listening on this port. • When the lookup service gets a request on this port, it sends an object by back to the service. • This object, known as a registrar, acts as a proxy to the lookup service, and runs in the service's JVM.• Any requests that the service needs to make of the lookup service are made through this proxy registrar. • Lookup service uses RMI.

4

Service Registration

Client

ServiceProvider

ServiceLocator

TCP/IP

registrarservice

1. lookup

2. send registrar

3. send service

5

Client Lookup

• Client sends a multicast lookup message• Lookup service send a registrar to the client • Client requests the service by copying the service object

6

Client Lookup

ClientServiceProvider

ServiceLocator

TCP/IP

registrar

service

1. lookup2. send registrar

3. send serviceservice

7

Server Structure

• A server application: A. prepare for discovery: B. discover a lookup service: C. create information about a service:

Entry objects D. export a service: Service registration E. renew leasing periodically: Leasing

8

Client Structure

• A server application: A. prepare for discovery: B. discover a lookup service: C. prepare a template for lookup search: Entry objects and Client search D. lookup a service: Client search E. call the service create information about a service:

9

Discovering a Lookup Service

• A client locates a service by querying a lookup service. • A service must register itself with the lookup service.• A client/service must first locate a lookup service. • Such a lookup service will usually have been started by some independent mechanism. • The search for a lookup service can be done either by unicast (RMI model) or by multicast.

10

Discovering a Lookup Service:Unicast discovery

• Unicast discovery can be used when you already know the machine on which the lookup service resides.• This is expected to be used for a lookup service that is outside of your local network, which you know the address of anyway (possibly advertised in some newsgroup, or maybe even on TV!).

11

Discovering a Lookup Service:Unicast discovery

• Constructors of LookupLocator

• URL examples: A. jini://host/ or jini://host:port/. B. If no port is given, it defaults to 4160. C. The host should be localhost or valid DNS name.

package net.jini.core.discovery;public Class LookupLocator { LookupLocator(java.lang.String url) throws java.net.MalformedURLException; LookupLocator(java.lang.String host,int port);}

12

public class HelloClient { public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); LookupLocator lookup =

new LookupLocator ("jini://140.129.20.87"); ServiceRegistrar registrar = lookup.getRegistrar (); Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("HelloServer"); ServiceTemplate template = new ServiceTemplate (null, null, serverAttributes); HelloInterface myServerInterface = (HelloInterface) registrar.lookup (template); System.out.println ( myServerInterface.sayHello () ); } }

13

Discovering a Lookup Service:Unicast discovery

• LookupLocator’s methods: A. String getHost(); return information about the hostname B. int getPort(); return information about the port C. public ServiceRegistrar getRegistrar() throws java.io.IOException, java.lang.ClassNotFoundException returns an object of class ServiceRegistrar.

14

Discovering a Lookup Service:Unicast discovery

• ServiceRegistrar’s methods: A. Object lookup(ServiceTemplate tmpl); return information about the available service obj B. ServiceMatches lookup(ServiceTemplate tmpl,

int maxMatches); return information about matched services• Entry classes are used to identify and matching service.

15

Discovering a Lookup Service:Unicast discovery

• Each service has a unique service id.• The id is unique over time and space with respect to all other service ids generated by all lookup services. • The id is a 128-bit value, to insure this uniqueness. • ID is to be set by the lookup service. • com.sun.jini.lookup.ServiceIDListener contains one method, serviceIDNotify. The lookup service calls this method to provide the service its service ID.

16

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

17

public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); HelloServer myServer = new HelloServer (); Entry[] identityingAttributes = new Entry[1]; identityingAttributes[0] = new Name("HelloServer"); JoinManager myManager = new JoinManager ( myServer, identityingAttributes, myServer, new LeaseRenewalManager () ); System.out.println ("Server has been Joined!"); } }

18

Discovering a Lookup Service:unicast discovery

• JoinManager: manages the join protocol for a service. • JoinManager discovers and keeps track of which lookup services to join, registers with them, keeps the registration leases renewed, and keeps the attributes up to date.

19

Compiling Programs

• $Javac -classpath \jdk1.2\jini1_0\lib\jini-core.jar;\jdk1.2\jini1_0\jini-ext.jar;\jdk1.2\jini1_0\lib\reggie.jar *.java• jar tvf jini-core.jar >jini-core.table net.jini.core….• jar tvf jini-ext.jar >jini-ext.table net.jini….• jar tvf reggie.jar > reggie.table com.sun.jini…..

20

Running SimpleService Program

• start lookup service see /jdk1.2/jini1_0/doc/example/StartingService.html or /jdk1.2/jini1_0/doc/example/StartingServiceGUI.html • java -cp /files/jini1_0/lib/jini-examples.jar com.sun.jini.example.service.StartService • run jini_tutorial/basic/SimpleService tcp-port (a service provider registers itself to a lookup service.)

21

Running HelloServer Program

• start lookup service see /jdk1.2/jini1_0/doc/example/StartingService.html or /jdk1.2/jini1_0/doc/example/StartingServiceGUI.html • >RunHelloServer (do not use start RunHelloServer … just doesn’t work) • >java HelloClient

22

Discovering a Lookup Service:Multicast discovery

• Three basic processes defined in specs: A. Discovery: Devices/clients locate and obtain reference to lookup services. B. Join: Device registers its services with lookup service. C. Lookup: Client locate and contact services.

23

Discovering entity(Client/serer)

Discovering a Lookup Service:Multicast discovery

TCP/IP

Multicastrequestclient

present announcement

Multicastresponse

server

Lookup service

Multicastrequestserver

Multicastresponse

client

TCP connection

24

Discovering a Lookup Service:Multicast discovery

• If the location of a lookup service is unknown, it is necessary to make a broadcast search for one. • The class LookupDiscovery in package net.jini.discovery is used for this. LookupDiscovery(java.lang.String[] groups)• The parameter to the LookupDiscovery constructor: 1. LookupDiscovery.ALL_GROUPS: discover all reachable lookup services. (This will be the normal case.) 2. LookupDiscovery.NO_GROUPS: the object is created, but no search is performed. (use method setGroups() later) 3. non-empty array of strings: This will search for that set of groups.

25

Discovering a Lookup Service:Multicast discovery

• Each instance of LookupDiscovery initiates a search for nearby lookup services. • The search continues as long as it is active (until the terminate() method is sent to it). • As qualifying lookup services are encountered, the LookupDiscovery object calls the discovered() method • As the LookupDiscovery object learns that qualifying lookup services have gone away, it calls the discarded() method.

26

DiscoveryListener (interface)• discovered() and discarded() methods are defined in DiscoveryListener interface when • Implementers must register their object with a LookupDiscovery object using addDiscoveryListener() to get the updates. • When a service goes away, the discarded() method is not immediately called because the LookupDiscovery object is not polling the service and does not know it has gone away. • The discarded() method is only called when a service requester attempts to call the service and receives a RemoteException.

27

public class MyServer extends UnicastRemoteObject implements MyServerInterface, ServiceIDListener, DiscoveryListener { LookupDiscovery discovery; ServiceRegistrar[] registrars; public MyServer () throws RemoteException { try { discovery = new LookupDiscovery

(LookupDiscovery.ALL_GROUPS); discovery.addDiscoveryListener (this); System.out.println ("\nserver: Begin search for a lookup " + "service..."); } catch (Exception e) { System.out.println ("MyServer(): " + e); } }

28

// MyServiceInterface public String sayHello () throws RemoteException { System.out.println ("server: The method sayHello()

In MyServer was " + "called"); return ("Hello World from MyServer!"); } public void serviceIDNotify (ServiceID sidIn) { System.out.println ("server: received ServiceID = "

+ sidIn); } // DiscoveryListener public void discovered (DiscoveryEvent evt) { int i, j; ServiceID id; Entry[] aeAttributes; ServiceTemplate template; ServiceMatches matches;

29

/* Get the array of registrar objects then pause to give the lookup server time to add MyServer to its list of services. ======================================== */ registrars = evt.getRegistrars (); System.out.println ("server: Found = " + registrars.length + " lookup service[s]..."); try {Thread.sleep (2000);} catch (Exception e) {}

/* Each cycle of this loop attempts to find MyServerInterface in one of the registrars that was passed to this method. ========================================== */ for (i = 0; i < registrars.length; i++) { id = registrars[i].getServiceID (); System.out.println ("\nserver: ServiceRegistrar = " + registrars[i]); System.out.println ("server: ServiceID = " + id);

30

aeAttributes = new Entry[1]; aeAttributes[0] = new Name ("MyServer"); template = new ServiceTemplate (null, null, aeAttributes);

try { System.out.println ("\nserver: lookup service's host "+

registrars[i].getLocator().getHost()); matches = registrars[i].lookup ( new ServiceTemplate (null, null, null), 50 ); System.out.println ("server: ServiceMatches = "

+ matches); System.out.println ("server: num matches = " + matches.totalMatches);

31

for (j = 0; j < matches.totalMatches; j++) { System.out.println ("server: svc item " + j + ": " + matches.items[j]); System.out.println ("server: svc object " + j + ": " + matches.items[j].service); } System.out.println ("*-----------------------------------------*"); } catch (Exception e) { System.out.println ("server: exception " + e); } } }

32

// DiscoveryListener public void discarded (DiscoveryEvent evt) { int i;

registrars = evt.getRegistrars (); System.out.println ("server: discarded() = " + registrars.length + " registrar[s]"); for (i = 0; i < registrars.length; i++) { System.out.println ("server: registrar = " +

registrars[i]); } }

33

public static void main (String[] args) { MyServer myServer; Entry[] aeAttributes; JoinManager joinmanager; try { /* Set 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 ());

/* Create the attributes (an array of entry objects) that describe this server and use it to register this server with the lookup service. JoinManager finds and registers with the lookup service. ============================================= */

34

aeAttributes = new Entry[1]; aeAttributes[0] = new Name("MyServer"); myServer = new MyServer (); joinmanager = new JoinManager ( myServer, aeAttributes, myServer, new LeaseRenewalManager () ); System.out.println ("server: JoinManager = " +

joinmanager);}

catch (Exception e) { System.out.println("server: MyServer.main(): Exception "

+ e); } } }

Recommended