Mini project - 2001/2 - PowerPoint PPT Presentation

About This Presentation
Title:

Mini project - 2001/2

Description:

directory -verbose& Rmi daemon: rmid -J-Dsun.rmi.activation.execPolicy=none -port 1100 ... from responding in a timely fashion to new discovery events. An ... – PowerPoint PPT presentation

Number of Views:714
Avg rating:3.0/5.0
Slides: 35
Provided by: ASS91
Category:
Tags: mini | project

less

Transcript and Presenter's Notes

Title: Mini project - 2001/2


1
Mini project - 2001/2 Jini - a new computing
paradigm
2
Our first jini steps basic topics 1.RMI
overview. 2.a first jini application hello
world.
rmi
rmi
rmi
3
RMI-remote method invocation
provides a way for Java applications running
on different JVMs/host computers to talk to
each other.
4
basic ideas/concepts 1.remote interface. 2.stubs
and skeletons - rmic.
client
server
Remote impl
Remote interface
stub
skeleton
5
3.serialization. 4.parameters and return
values. 5.dynamic code loading. 6.security
implications. 7.marshaled objects. 8.the
activation framework - rmid.
6
Jini basics
7
3 main components 1.service 2.client 3.jini
environment - rmi,web server, lookup
service...
8
service
  • Device that shares its capabilities with other
    devices
  • Devices Requirement
  • Memory CPU
  • Connection to Network
  • E.g. TV, Microwave,Printer,PC,Software

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

10
The Jini environment
11
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.
12
Our first jini application - hello
world! 1.compiling running the jini
env. 2.compiling running a)server. b)client.
13
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) 3.run a lookup server
(supplied with the reggie.jar
files in jini)
14
Running the environment command lines http
server java -jar java
archive /usr/local/Java/jini1_1/lib/tools.jar
jinis http -port 8087 -dir /usr/local/Java/jini1
_1/lib the servers root
directory -verbose Rmi
daemon rmid -J-Dsun.rmi.activation.execPolicynon
e -port 1100 -log /tmp/rmid_log rmi log
directory-recvering...
15
Running the environment command lines Lookup
server java -Djava.security.policy/usr/local/Ja
va/jini1_1 /example/lookup/policy.all
policy -jar /usr/local/Java/jini1_1/lib/reggie.jar
lookup service http//silver.cs.bgu.ac.il8087/re
ggie-dl.jar codebase /usr/local/Java/jini1_1/exam
ple/lookup/policy.all /tmp/reggie_log1 public
policy
16
Compiling the server and client programs Compile
the server class javac -classpath
/usr/local/Java/jini1_1/lib/jini-core.jar/usr/loc
al/Java/jini1_1/lib/jini-ext.jar/usr/local/Java/j
ini1_1/lib/sun-util.jar/users/studs/bsc/2000/liyb
oviz/files/service -d /users/studs/bsc/2000/liybov
iz/files/service /users/studs/bsc/2000/liyboviz/fi
les/corejini/chapter5/HelloWorldServiceInterface.j
ava /users/studs/bsc/2000/liyboviz/files/corejini/
chapter5/HelloWorldService.java Compile the
client class javac -classpath /usr/local/Java/jin
i1_1/lib/jini-core.jar/usr/local/Java/jini1_1/lib
/jini-ext.jar/usr/local/Java/jini1_1/lib/sun-util
.jar -d /users/studs/bsc/2000/liyboviz/files/clie
nt /users/studs/bsc/2000/liyboviz/files//corejini/
chapter5/HelloWorldServiceInterface.java
/users/studs/bsc/2000/liyboviz/files/corejini/chap
ter5/HelloWorldClient.java
17
Running - first the server, then the
client Server http java -jar /usr/local/Java/jin
i1_1/lib/tools.jar -port 8085 -dir
/users/studs/bsc/2000/liyboviz/files/service-dl
-verbose The Service java -cp
/usr/local/Java/jini1_1/lib/jini-core.jar/usr/loc
al/Java/jini1_1/lib/jini-ext.jar/usr/local/Java/j
ini1_1/lib/sun-util.jar/users/studs/bsc/2000/liyb
oviz/files/service -Djava.rmi.server.codebasehtt
p//silver.cs.bgu.ac.il8085/ -Djava.security.poli
cy/usr/local/Java/jini1_1/example/lookup /policy.
all corejini.chapter5.HelloWorldService
18
Running then, the client The Client java -cp
/usr/local/Java/jini1_1/lib/jini-core.jar /usr/lo
cal/Java/jini1_1/lib/jini-ext.jar /usr/local/Java
/jini1_1/lib/sun-util.jar /users/studs/bsc/2000/l
iyboviz/files/client -Djava.security.policy/usr/
local/Java/jini1_1/example/lookup /policy.all
corejini.chapter5.HelloWorldClient
19
Screen output

From the service gtdiscovered a lookup
service! gtset serviceID to e91bb-53434-ty7554 Fro
m the client gtGot matching service. gtIts message
is Hello, world!
20
Whats behind the screen?
21
The client/server known interface // This is the
interface that the service's proxy //
implements package corejini.chapter5 public
interface HelloWorldServiceInterface public
String getMessage()
22
The servers 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.chapter5 import
net.jini.discovery.DiscoveryListener import
net.jini.discovery.DiscoveryEvent import
net.jini.discovery.LookupDiscovery import
net.jini.core.lookup.ServiceItem import
net.jini.core.lookup.ServiceRegistrar import
net.jini.core.lookup.ServiceRegistration import
java.util.Hashtable import java.io.IOException i
mport java.io.Serializable import
java.rmi.RemoteException import
java.rmi.RMISecurityManager
23
// This is the proxy object that will be
downloaded // by clients. It's serializable and
implements // our well-known HelloWorldServiceInte
rface. class HelloWorldServiceProxy implements
Serializable, HelloWorldServiceInterface
public HelloWorldServiceProxy()
public String getMessage() return
"Hello, world!"
24
// HelloWorldService is the "wrapper" class
that // handles publishing the service
item. public class HelloWorldService implements
Runnable // 10 minute leases protected
final int LEASE_TIME 10 60 1000
protected Hashtable registrations new
Hashtable() protected ServiceItem item
protected LookupDiscovery disco // Inner
class to listen for discovery events class
Listener implements DiscoveryListener
// Called when we find a new lookup service.
public void discovered(DiscoveryEvent ev)
System.out.println("discovered a lookup
service!") ServiceRegistrar
newregs ev.getRegistrars() for
(int i0 iltnewregs.length i)
if (!registrations.containsKey(newregsi))
registerWithLookup(newregsi)

25
// Called ONLY when we explicitly discard a
// lookup service, not "automatically" when a
// lookup service goes down. Once
discovered, // there is NO ongoing
communication with a // lookup service.
public void discarded(DiscoveryEvent ev)
ServiceRegistrar deadregs
ev.getRegistrars() for (int i0
iltdeadregs.length i)
registrations.remove(deadregsi) public
HelloWorldService() throws IOException
item new ServiceItem(null, createProxy(),
null) // Set a security manager
if (System.getSecurityManager() null)
System.setSecurityManager(new
RMISecurityManager()) // Search for the
"public" group, which by // convention is
named by the empty string disco new
LookupDiscovery(new String "" ) //
Install a listener. disco.addDiscoveryListen
er(new Listener())
26
protected HelloWorldServiceInterface
createProxy() return new
HelloWorldServiceProxy() // 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
registerWithLookup(ServiceRegistrar registrar)
ServiceRegistration registration null
try registration
registrar.register(item, LEASE_TIME)
catch (RemoteException ex)
System.out.println("Couldn't register "
ex.getMessage()) return if
(item.serviceID null)
item.serviceID registration.getServiceID()
System.out.println("Set serviceID to "
item.serviceID) registrations.put(regist
rar, registration)
27
// 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 (InterruptedException ex)
// Create a new HelloWorldService
and start // its thread. public static
void main(String args) try
HelloWorldService hws new HelloWorldService()
new Thread(hws).start()
catch (IOException ex)
System.out.println("Couldn't create service "
ex.getMessage())

28
The clients code
// A simple Client to exercise the
HelloWorldService package corejini.chapter5 imp
ort net.jini.discovery.DiscoveryListener import
net.jini.discovery.DiscoveryEvent import
net.jini.discovery.LookupDiscovery import
net.jini.core.lookup.ServiceRegistrar import
net.jini.core.lookup.ServiceTemplate import
java.util.Vector import java.io.IOException impo
rt java.rmi.RemoteException import
java.rmi.RMISecurityManager
29
public class HelloWorldClient implements Runnable
protected ServiceTemplate template
protected LookupDiscovery disco // An
inner class to implement DiscoveryListener
class Listener implements DiscoveryListener
public void discovered(DiscoveryEvent ev)
ServiceRegistrar newregs
ev.getRegistrars() for (int i0
iltnewregs.length i)
lookForService(newregsi)
public void discarded(DiscoveryEvent
ev)
30
public HelloWorldClient() throws IOException
Class types HelloWorldServiceInterface
.class template new
ServiceTemplate(null, types, null)
// Set a security manager if
(System.getSecurityManager() null)
System.setSecurityManager(new
RMISecurityManager())
// Only search the public group disco
new LookupDiscovery(new String "" )
// Install a listener
disco.addDiscoveryListener(new Listener())

31
// Once we've found a new lookup service,
search // for proxies that implement //
HelloWorldServiceInterface protected Object
lookForService(ServiceRegistrar lusvc)
Object o null try o
lusvc.lookup(template) catch
(RemoteException ex)
System.err.println("Error doing lookup "
ex.getMessage()) 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 "
((HelloWorldServiceInterface)
o).getMessage()) return o
32
// 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
(InterruptedException ex)
// Create a HelloWorldClient and start its
thread public static void main(String args)
try HelloWorldClient hwc
new HelloWorldClient() new
Thread(hwc).start() catch (IOException
ex) System.out.println("Couldn't
create client "
ex.getMessage())
33
A policy file example grant // Allow
everything for now permission java.security.AllPe
rmission
34
The End
To be continued...
Write a Comment
User Comments (0)
About PowerShow.com