Скачать презентацию Mini project — 2001 2 Jini — a new Скачать презентацию Mini project — 2001 2 Jini — a new

1d6108aab3ac86ffa32ea9e31213085a.ppt

  • Количество слайдов: 34

Mini project - 2001/2 Jini - a new computing paradigm Mini project - 2001/2 Jini - a new computing paradigm

Our first jini steps… basic topics: 1. RMI overview. rmi mi r 2. a Our first jini steps… basic topics: 1. RMI overview. rmi mi r 2. a first jini application: “hello world”.

RMI-remote method invocation provides a way for Java applications running on different JVM’s/host computers RMI-remote method invocation provides a way for Java applications running on different JVM’s/host computers to “talk” to each other.

basic ideas/concepts: 1. remote interface. . 2 stubs and skeletons - rmic. client server basic ideas/concepts: 1. remote interface. . 2 stubs and skeletons - rmic. client server Remote interface Remote impl stub skeleton

. 3 serialization. . 4 parameters and return values. . 5 dynamic code loading. . 3 serialization. . 4 parameters and return values. . 5 dynamic code loading. . 6 security implications. . 7 marshaled objects. . 8 the activation framework - rmid.

Jini basics Jini basics

3 main components 1. service 2. client 3. jini environment rmi, web server, lookup 3 main components 1. service 2. client 3. jini environment rmi, web server, lookup service. . .

service • Device that shares its capabilities with other devices • Devices Requirement – service • Device that shares its capabilities with other devices • Devices Requirement – Memory & CPU – Connection to Network • E. g. TV, Microwave, Printer, PC, Software

client • A device that uses services • Requirement – Memory & CPU – client • A device that uses services • Requirement – Memory & CPU – Connection to Network • E. g. PDA, WAP Phone, PC, Coke Machine

The Jini environment Service Provider Jini Lookup Service Consumer The Jini environment Service Provider Jini Lookup Service Consumer

How does it works? 1. create the jini environment-lookup server online. 2. services discover, How does it works? 1. create the jini environment-lookup server online. 2. services discover, join and register on lookup servers. 3. clients discover and finds matching and downloading services, then using them.

Our first jini application hello world! 1. compiling & running the jini env. 2. Our first jini application hello world! 1. compiling & running the jini env. 2. compiling & running a)server. b)client.

Creating The jini environment: 1. run a http server (jini provides a simple but Creating The jini environment: 1. run a http server (jini provides a simple but efficient one) 2. run a rmi daemon (exists on Java 2)

Running the environment command lines: http server: java -jar java archive /usr/local/Java/jini 1_1/lib/tools. jar Running the environment command lines: http server: java -jar java archive /usr/local/Java/jini 1_1/lib/tools. jar jini’s http -port 8087 -dir /usr/local/Java/jini 1_1/lib the server’s root directory -verbose& Rmi daemon: rmid -J-Dsun. rmi. activation. exec. Policy=none -port 1100

Running the environment command lines: Lookup server: java -Djava. security. policy=/usr/local/Java/jini 1_1 /example/lookup/policy. all Running the environment command lines: Lookup server: java -Djava. security. policy=/usr/local/Java/jini 1_1 /example/lookup/policy. all policy -jar /usr/local/Java/jini 1_1/lib/reggie. jar lookup service http: //silver. cs. bgu. ac. il: 8087/reggie-dl. jar codebase /usr/local/Java/jini 1_1/example/lookup/policy. all /tmp/reggie_log 1 public& policy

Compiling the server and client programs: Compile the server class: javac -classpath /usr/local/Java/jini 1_1/lib/jinicore. Compiling the server and client programs: Compile the server class: javac -classpath /usr/local/Java/jini 1_1/lib/jinicore. jar: /usr/local/Java/jini 1_1/lib/jiniext. jar: /usr/local/Java/jini 1_1/lib/sunutil. jar: /users/studs/bsc/2000/liyboviz/files/service -d /users/studs/bsc/2000/liyboviz/files/service /users/studs/bsc/2000/liyboviz/files/corejini/chapter 5/Hello. World. S ervice. Interface. java /users/studs/bsc/2000/liyboviz/files/corejini/chapter 5/Hello. World. S ervice. java Compile the client class: javac -classpath /usr/local/Java/jini 1_1/lib/jinicore. jar: /usr/local/Java/jini 1_1/lib/jiniext. jar: /usr/local/Java/jini 1_1/lib/sun-util. jar: -d /users/studs/bsc/2000/liyboviz/files/client /users/studs/bsc/2000/liyboviz/files//corejini/chapter 5/Hello. World Service. Interface. java /users/studs/bsc/2000/liyboviz/files/corejini/chapter 5/Hello. World. C

Running - first the server, then the client: Server http: java -jar /usr/local/Java/jini 1_1/lib/tools. Running - first the server, then the client: Server http: java -jar /usr/local/Java/jini 1_1/lib/tools. jar -port 8085 -dir /users/studs/bsc/2000/liyboviz/files/service-dl -verbose& The Service: java -cp /usr/local/Java/jini 1_1/lib/jinicore. jar: /usr/local/Java/jini 1_1/lib/jiniext. jar: /usr/local/Java/jini 1_1/lib/sunutil. jar: /users/studs/bsc/2000/liyboviz/files/service -Djava. rmi. server. codebase=http: //silver. cs. bgu. ac. il: 8085/ -Djava. security. policy=/usr/local/Java/jini 1_1/example/lookup /policy. all corejini. chapter 5. Hello. World. Service&

Running – then, the client: The Client: java -cp /usr/local/Java/jini 1_1/lib/jini-core. jar: /usr/local/Java/jini 1_1/lib/jini-ext. Running – then, the client: The Client: java -cp /usr/local/Java/jini 1_1/lib/jini-core. jar: /usr/local/Java/jini 1_1/lib/jini-ext. jar: /usr/local/Java/jini 1_1/lib/sun-util. jar: /users/studs/bsc/2000/liyboviz/files/client -Djava. security. policy=/usr/local/Java/jini 1_1/example/lookup /policy. all corejini. chapter 5. Hello. World. Client&

Screen output From the service: >discovered a lookup service! >set service. ID to e Screen output From the service: >discovered a lookup service! >set service. ID to e 91 bb-53434 -ty 7554 From the client: >Got matching service. >Its message is: Hello, world!

What’s behind the screen? What’s behind the screen?

The client/server known interface: // This is the interface that the service's proxy // The client/server known interface: // This is the interface that the service's proxy // implements package corejini. chapter 5; public interface Hello. World. Service. Interface { public String get. Message(); }

The server’s code: // This is the first iteration of a Hello, World // The server’s code: // This is the first iteration of a Hello, World // service--it publishes a proxy that returns // a string when asked by clients. package corejini. chapter 5; import net. jini. discovery. Discovery. Listener; import net. jini. discovery. Discovery. Event; import net. jini. discovery. Lookup. Discovery; import net. jini. core. lookup. Service. Item; import net. jini. core. lookup. Service. Registrar; import net. jini. core. lookup. Service. Registration; import java. util. Hashtable; import java. io. IOException; import java. io. Serializable; import java. rmi. Remote. Exception; import java. rmi. RMISecurity. Manager;

//This is the proxy object that will be downloaded //by clients. It's serializable and //This is the proxy object that will be downloaded //by clients. It's serializable and implements //our well-known Hello. World. Service. Interface. class Hello. World. Service. Proxy implements Serializable, Hello. World. Service. Interface { public Hello. World. Service. Proxy() { } public String get. Message() { return "Hello, world!"; } }

//Hello. World. Service is the //Hello. World. Service is the "wrapper" class that //handles publishing the service item. public class Hello. World. Service implements Runnable { // 10 minute leases protected final int LEASE_TIME = 10 * 60 * 1000; protected Hashtable registrations = new Hashtable(); protected Service. Item item; protected Lookup. Discovery disco; // Inner class to listen for discovery events class Listener implements Discovery. Listener { // Called when we find a new lookup service. public void discovered(Discovery. Event ev) { System. out. println("discovered a lookup service!"); Service. Registrar[] newregs = ev. get. Registrars(); for (int i=0 ; i

// Called ONLY when we explicitly discard a // lookup service, not // Called ONLY when we explicitly discard a // lookup service, not "automatically" when a // lookup service goes down. // there is NO ongoing communication with a // lookup service. Once discovered, public void discarded(Discovery. Event ev) { Service. Registrar[] deadregs = ev. get. Registrars(); for (int i=0 ; i

protected Hello. World. Service. Interface create. Proxy} () return new Hello. World. Service. Proxy{; protected Hello. World. Service. Interface create. Proxy} () return new Hello. World. Service. Proxy{; () // This work involves remote calls, and may take a while to complete // Thus, since it's called from discovered(), it will prevent us // from responding in a timely fashion to new discovery events. An // improvement would be to spin off a separate short- // lived thread to do the work. protected synchronized void register. With. Lookup(Service. Registrar registrar) { Service. Registration registration = null; try { registration = registrar. register(item, LEASE_TIME); } catch (Remote. Exception ex) { System. out. println("Couldn't register: " + ex. get. Message()); return; } if (item. service. ID == null) { item. service. ID = registration. get. Service. ID(); System. out. println("Set service. ID to " + item. service. ID); } registrations. put(registrar, registration); }

//This thread does nothing but sleep, but it // makes sure the VM doesn't //This thread does nothing but sleep, but it // makes sure the VM doesn't exit. public void run() { while (true) { try { Thread. sleep(1000000); } catch (Interrupted. Exception ex) { }}} // Create a new Hello. World. Service and start // its thread. public static void main(String args[]) { try { Hello. World. Service hws = new Hello. World. Service(); new Thread(hws). start(); } catch (IOException ex) { System. out. println("Couldn't create service: " + ex. get. Message()); }}}

The client’s code: //A simple Client to exercise the Hello. World. Service package corejini. The client’s code: //A simple Client to exercise the Hello. World. Service package corejini. chapter 5; import net. jini. discovery. Discovery. Listener; import net. jini. discovery. Discovery. Event; import net. jini. discovery. Lookup. Discovery; import net. jini. core. lookup. Service. Registrar; import net. jini. core. lookup. Service. Template; import java. util. Vector; import java. io. IOException; import java. rmi. Remote. Exception; import java. rmi. RMISecurity. Manager;

public class Hello. World. Client implements Runnable { protected Service. Template template; protected Lookup. public class Hello. World. Client implements Runnable { protected Service. Template template; protected Lookup. Discovery disco; // An inner class to implement Discovery. Listener class Listener implements Discovery. Listener { public void discovered(Discovery. Event ev) { Service. Registrar[] newregs = ev. get. Registrars(); for (int i=0 ; i

public Hello. World. Client() throws IOException { Class[] types = { Hello. World. Service. public Hello. World. Client() throws IOException { Class[] types = { Hello. World. Service. Interface. class }; template = new Service. Template(null, types, null); // Set a security manager if (System. get. Security. Manager() == null) { System. set. Security. Manager(new RMISecurity. Manager()); } // Only search the public group disco = new Lookup. Discovery(new String[] { "" }); // Install a listener disco. add. Discovery. Listener(new Listener()); }

// Once we've found a new lookup service, search // // for proxies that // Once we've found a new lookup service, search // // for proxies that implement Hello. World. Service. Interface protected Object look. For. Service(Service. Registrar lusvc) { Object o = null; try { o = lusvc. lookup(template); } catch (Remote. Exception ex) { System. err. println("Error doing lookup: " + ex. get. Message()); return null; } if (o == null) { System. err. println("No matching service. "); return null; } System. out. println("Got a matching service. "); System. out. println("It's message is: " + ((Hello. World. Service. Interface) o). get. Message()); return o; }

// This thread does nothing--it simply keeps the // VM from exiting while we // This thread does nothing--it simply keeps the // VM from exiting while we do discovery. public void run() { while (true) { try { Thread. sleep(1000000); } catch (Interrupted. Exception ex) { }}} // Create a Hello. World. Client and start its thread public static void main(String args[]) { try { Hello. World. Client hwc = new Hello. World. Client(); new Thread(hwc). start(); } catch (IOException ex) { System. out. println("Couldn't create client: " + ex. get. Message()); }}}

A policy file example grant { // Allow everything for now permission java. security. A policy file example grant { // Allow everything for now permission java. security. All. Permission; };

The End To be continued. . . The End To be continued. . .