1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems...

30
1 Jini Tutorial, Part 3 Jini Programming

Transcript of 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems...

Page 1: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

1

Jini Tutorial, Part 3

Jini Programming

Page 2: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

2

Tutorial outline Part 1 Introduction

Distributed systems Java basics Remote Method Invocation (RMI)

Part 2 Overview of Jini Goals Architecture Basic concepts, protocols

Part 3 Jini Programming

Part 4 Advanced topics Mobile Jini access Adaptive user interfaces Security

Page 3: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

3

Contents

Software overview – architecture, packages

Programming the basic steps discovery, join (service registration) and lookup (finding services)

Deployment – where to put what

Page 4: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

4

The key steps of operation

Every service and client will have to discover one or more lookup services

Every service will have to register with (join) the discovered lookup service(s)

Every client will have to search for (look up) services in the lookup service(s)

First we look at the programming of these fundamental steps of operation

Page 5: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

5

Jini package structure

The naming of Jini packages follow the inverted domain name naming convention jini.net.xxx

The core classes are in packages jini.net.core.*, stored in jini-core.jar

Classes building on core classes are in jini.net.*, stored in file jini-ext.jar

Non-standard utility, helper classes are in com.sun.jini.*, stored in file sun-util.jar

Page 6: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

6

The programmer’s view of Jini

The essential steps of creating a system of Jini services and clients

1. Create well-known interfaces (if required)

2. Create services that implement the interfaces

3. Provide discovery in services and clients

4. Services: Program service registration5. Clients: Program service lookup

Page 7: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

7

How we should proceed

Simple service example to show these steps

1. Defining service interface2. Creating the service3. Programming the Unicast Discovery4. Service registration5. Service lookup6. Multicast discovery7. The interesting parts

Page 8: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

8

Creating the interface

First we define the well-known interface for our sample service

public interface GreetingInterface {

public void hello();

}

Page 9: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

9

Creating the Service Proxy Then we create the proxy class the client will

see

public class GreetingServiceProxy implements Serializable, GreetingInterface {

public GreetingServiceProxy (){};

public void hello(){ System.out.println(“Welcome to Jini

World!”);}

}

Page 10: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

10

What we want to achieve

Service AService A

Lookup ServiceLookup Service

Service Proxy

registrarregistrar

registrationregistration

register( )

Page 11: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

11

Unicast discovery

LookupLocator lookup = null;

ServiceRegistrar registrar = null;

 

lookup = new LookupLocator(“jini://hostname”);

registrar = lookup.getRegistrar();

Page 12: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

12

Service registration

Once we have a proxy to the lookup service, we can register the service ServiceRegistration registration = null;

//create serviceItem (no ID, no attributes)

try{ // register for 100 seconds

registration = registrar.register(serviceItem,

100000L); } catch (java.rmi.RemoteException e){

}

ServiceItem serviceItem = new ServiceItem(null, new GreetingServiceProxy(), null);

Page 13: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

13

Putting the service together

import net.jini.core.discovery.*;import net.jini.core.lookup.*; public class GreetingService { static public void main(String argv[]) {

LookupLocator lookup = null; //object for discoveryServiceRegistrar registrar = null; //lookup serv. proxy

try { lookup = new LookupLocator("jini://hostname");} catch(java.net.MalformedURLException e) {...}// perform unicast discoverytry { registrar = lookup.getRegistrar(100000);}catch ...}

Page 14: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

14

Putting the service together

...

ServiceRegistration registration = null;

//create serviceItem (no ID, no attributes)

ServiceItem serviceItem = new ServiceItem(null, new GreetingServiceProxy(), null);

try{ // register for 100 seconds

registration = registrar.register(serviceItem,

100000L); }catch (java.rmi.RemoteException e){

}} // GreetingService

Page 15: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

15

The client side

Lookup ServiceLookup ServiceClient 1Client 1

Found proxy

registrarregistrar lookup( )

Interface

Template

Template

Unicastdiscovery

Page 16: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

16

Programming the service lookup

System.setSecurityManager( new RMISecurityManager() );

// create template for service search...GreetingServiceInterface returnedService = null;try{ returnedService = (GreetingServiceInterface)

registrar.lookup(template);}catch (java.rmi.RemoteException e){ ...}returnedService.hello();

}

Page 17: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

17

Creating the template

Class[] serviceTypes = new Class[1];

try {

serviceTypes[0] = Class.forName( "GreetingServiceInterface");

}catch (ClassNotFoundException e){

...

}

ServiceTemplate template = new ServiceTemplate(null, serviceTypes, null);

Page 18: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

18

Using Attibutes

Why use attributes The type (interface) of a service will only

say ‘what’ we can do with a service. We also need information on the properties

of the service (eg type and speed of a printer, supercomputer

specialising in climate modelling, storage capacity, location of service)

You can use standard Jini attributes Address, Comment, Location, Name,

ServiceInfo, ServiceType, Status And/or create your own particular ones

Page 19: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

19

Attributes

Entry[] attribSet = new Entry[3];

attribSet[0] = new Location(“G", “002", “South Building");

attribSet[1] = new Location(“1st", “107", "North Building");

attribSet[2] = new Name(“Greeting Service for Euro-Par participants");

ServiceTemplate newTemplate = new ServiceTemplate(null, serviceTypes, attribSet);

Since attributes are Java objects and downloaded with the service, they can be used for transferring any type of static or dynamic information from the service to the client

Page 20: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

20

Is that all? What’s missing?

We know how to discover a lookup service, register and lookup services

BUT, we can only discover a known lookup service, which is not discovery at all, only connecting to it Multicast discovery to deal with unknown

lookup services

Page 21: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

21

Programming the multicast discovery – Step 1

LookupDiscovery discover = null;

try{

discover = new LookupDiscovery(LookupDiscovery.ALL_GROUPS);

}catch ...

This implements the process shown in the next figure.

Lookup Service NLookup Service N

Discovering entityDiscovering entity Lookup Service …Lookup Service …

Lookup Service 2Lookup Service 2

Lookup Service 1Lookup Service 1

multicast request

Page 22: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

22

Programming the multicast discovery – Step 2

Asynchronous responses are handled by a listener object (implementing the DiscoveryListener interface)

discover.addDiscoveryListener(listener);

try{

Thread.currentThread().sleep(10000L);

}

catch ...

}

Page 23: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

23

Handling responses in the listener

Two methods for joining and leaving

public void discovered(DiscoveryEvent e){

//Lookup service discovered

}

public void discarded(DiscoveryEvent e){

//Clean up after lookup service discarded");

}

Page 24: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

24

Step 3 -- Handling registration Multicast version of the registration process

Note the use of event and the number of lookup services

public void discovered(DiscoveryEvent event){

ServiceRegistrar[] registrars = event.getRegistrars();ServiceRegistration[] registration = null;ServiceItem serviceItem = new ServiceItem(null, new GreetingServiceProxy(), null); for (int i = 0; i < registrars.length; i++){ try {

registration[i] = registrars[i].register(serviceItem, 100000L);

} catch ... }}

}

Page 25: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

25

Creating a ‘real’ service proxy

Creating a new proxy that talks to the backend service

public class GreetingServiceProxy implements Serializable, GreetingInterface {

protected GreetingBackEnd be;

public GreetingServiceProxy ( be ){

this.be = be;

};

public void hello(){

String str = be.getString();

System.out.println(str);

}

}

Page 26: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

26

Modifying the service

The interface for the remote proxypublic interface GreetingBackEnd extends Remote{

public String getString();}

We also need to create the Backend Remote object that will receive RMI calls from the proxypublic class BackEnd extends UnicastRemoteObject implements GreetingBackEnd{

public String getString(){ return “Welcome to Euro-Par”;

}}

Page 27: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

27

Modifying registration

The service will publish a proxy initialised with a reference to the back end service (the remote object)

BackEnd be = new BackEnd();

GreetingServiceProxy proxy = new GreetingServiceProxy(be);

...

ServiceItem serviceItem = new ServiceItem(null, proxy, null);

Page 28: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

28

Where are we now?

We have seen the programming of the discovery, join, lookup protocols

Shortcomings: We have no leasing for our service Only up and running services will be

located – latecomers (both lookup and other services) are missed. We need notification (events) when this happen

Page 29: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

29

Service computerService computer

Client deviceClient device

Lookup service computerLookup service computer

Deployment

RMI DaemonRMI Daemon

HTTP Server for exporting LS

classes

HTTP Server for exporting LS

classes

Lookup ServiceLookup Service

ClientClientServiceService

HTTP Server for exporting

service classes

HTTP Server for exporting

service classes

RMI DaemonRMI Daemon

HTTP Server for exporting client classes

HTTP Server for exporting client classes

Page 30: 1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

30

Summary

Overviewed the programming steps of the Discovery, Join and Lookup protocols

Have used a simple and real proxy to a Jini service

Looked at the use of attributes Overviewed deployment issues We look at the more advanced

issues in the next part