Remote Method Invocation
Prof. Dr. Ralf Lämmel
Universität Koblenz-Landau
Software Languages Team
https://github.com/101co...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Elevator speech
What if the objects of ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Non-distributed programming
Caller and ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Distributed programming
Caller and call...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
See package “helloworld” of javaRmiSamp...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
A more interesting scenario: a banking
...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Communication between different Java ru...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Stubs and skeletons
• Client invokes a ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Remote-method invocation
Host mymac.foo...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Parameters and return values
Remote obj...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
How to refer to / look up
remote object...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Naming service cont’d
Host mymac.foo.ed...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Designing a client/server app
(Summary)...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Designing a client/server app
(Detailed...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Step 1:
Design an interface for service...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Step 2:
Implement the service.
import j...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Step 4:
Implement a server to contact f...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Name format for binding
rmi://<host_nam...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Availability of registry
• Binding requ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Usage of “localhost”
• localhost may fa...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Step 5:
Implement a client to invoke se...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Step 6:
Start the server.
JavaRmi> java...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Step 7:
Run the client.
JavaRmi> java h...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Components
of an RMI-based application
...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Security of RMI
• Useful constrains:
– ...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
– A non-C/S-based version as a referenc...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
DEMO
http://101companies.org/wiki/
Cont...
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Summary
• RMI easily blends with OOP.
–...
Upcoming SlideShare
Loading in...5
×

Remote method invocation (as part of the the PTT lecture)

562

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
562
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Remote method invocation (as part of the the PTT lecture)

  1. 1. Remote Method Invocation Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team https://github.com/101companies/101repo/tree/master/languages/AspectJ/javaRmiSamples Non-101samples available here: http://101companies.org/wiki/ Contribution:javaRmi
  2. 2. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Elevator speech What if the objects of interests are no longer located on the same computer, i.e., the objects are distributed instead? How to communicate between different computers in an OOP-friendly manner? Basically, we want to continue to hold references to objects (perhaps remote objects), and send them messages (i.e., perform method calls) as before. We will be looking into Java RMI as a technology of choice. Alternative options include Corba, WebServices, and SOA.
  3. 3. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Non-distributed programming Caller and callee are on the same machine. MyMachine MyMachine Object1 (Client) Object2 (Server) Method Invocation - Arguments are evaluated. - Caller location is pushed onto stack. - Callee method is executed. - Result is returned. - Caller location resumes.
  4. 4. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Distributed programming Caller and callee are on different machines. Host mymac.foo.edu Host myserver.bar.edu Object1 (Client) Remote Method Invocation Client holds on a server proxy. Args + result go over the network. In demo code, client and server may be on the same machine, perhaps even on the same JVM, but the invocation is handled (conceptually) over the network. Object2 (Server)
  5. 5. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) See package “helloworld” of javaRmiSamples (see 1st slide). Start the server as described in “Makefile”. Start the client by running the “Client” class. Observe how 42 was remotely incremented. DEMO A first look at the “hello world” of RMI
  6. 6. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) A more interesting scenario: a banking application to access account data remotely at a teller (ATM). Host koblenz1.db.de Host server.db.de Teller Bank Customer Account
  7. 7. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Communication between different Java runtimes on the same machine or different machines, eventually involves the network layer of the OS. JRE JRE Host OS Network layer JRE JRE Host OS Network layer network cable
  8. 8. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Stubs and skeletons • Client invokes a remote method on “stub” object. • The stub sends the call to the server-side “skeleton”. • That is, the stub: – opens a socket to the remote server, – marshals the method parameters, – forwards the data stream to the skeleton, – awaits and unmarshals result data stream from skeleton. • The skeleton contains a method that: – receives the remote calls, – unmarshals the method parameters, – invokes the actual remote (by now, local) implementation, and – marshals the result and delivers it back to the stub.
  9. 9. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Remote-method invocation Host mymac.foo.edu Host myserver.bar.edu Object1 (Client) Object2 (Server) Remote Object Stub: serves as Remote Reference odd(3) 01101 odd(3) true10010true Same interface as remote object Serialized method name, parameters and return value Remote Object Skeleton
  10. 10. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Parameters and return values Remote objects – by reference Serializable objects – by copy Others – cannot be passed (exception)
  11. 11. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) How to refer to / look up remote objects? Remote Object C Remote Object A Remote Object B Naming “X” “Y” “Z” Host myserver.bar.edu We need a naming service! A server-side directory that associates remote objects with names (URLs)
  12. 12. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Naming service cont’d Host mymac.foo.edu Host myserver.bar.edu ObjectY Naming “X” “Y” “Z” lookup(“Y”) Remote reference to server
  13. 13. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Designing a client/server app (Summary) Server Bind 1+ “service” objects via registry Client Looks up and interacts with service objects For example (see “helloworld” demo): Service: increment operation
  14. 14. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Designing a client/server app (Detailed steps) 1. Design an interface for service (remote object). 2. Implement the service. 3. (Generate stub and skeleton classes.) 4. Implement a server to contact for binding. 5. Implement a client to invoke service. 6. Start the server. 7. Run the client. Done automatically.
  15. 15. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Step 1: Design an interface for service (remote object). import java.rmi.Remote; import java.rmi.RemoteException; public interface Service extends Remote { public long inc(long x) throws RemoteException; } This is a regular interface except for the special base interface and the special throws declaration.
  16. 16. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Step 2: Implement the service. import java.rmi.server.UnicastRemoteObject; import java.rmi.RemoteException; public class ServiceImpl extends UnicastRemoteObject implements Service { // Needed for serialization private static final long serialVersionUID = 6102178242852627613L; // Needed because of exception public ServiceImpl() throws RemoteException { super(); } public long inc(long x) throws RemoteException { return ++x; } }
  17. 17. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Step 4: Implement a server to contact for binding. import java.rmi.Naming; public class Server { public Server() { try { Service s = new ServiceImpl(); Naming.rebind("rmi://localhost/Service", s); } catch (Exception e) { System.out.println("Trouble: " + e); } } public static void main(String args[]) { new Server(); } } Remote object Register with naming service
  18. 18. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Name format for binding rmi://<host_name> [:<name_service_port>] /<service_name> Thus, URLs of a specific form are used for addressing remote objects.
  19. 19. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Availability of registry • Binding requires that the registry is locally running. • Registry can be started programmatically as follows: import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; LocateRegistry.createRegistry(Registry.REGISTRY_PORT); • Default TCP/IP port: 1099
  20. 20. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Usage of “localhost” • localhost may fail to work in binding. • This depends on the network configuration. • Alternatively, one can use the following idiom: InetAddress addr = InetAddress.getLocalHost(); String hostname = addr.getHostName(); Naming.rebind("rmi://"+hostname+"/Service", s);
  21. 21. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Step 5: Implement a client to invoke service. import java.rmi.Naming; public class Client { public static void main(String[] args) { try { Service s = (Service) Naming.lookup("rmi://localhost/Service"); System.out.println(s.inc(41)); } catch (Exception e) { ... } } }
  22. 22. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Step 6: Start the server. JavaRmi> java helloworld.Server Server must be killed, e.g., with CTRL-C One may want to run the server (and the client) with a security policy. If you are using Eclipse, you cannot (easily) run both server and client simultaneously from within Eclipse. One of the two would be started from the command line.
  23. 23. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Step 7: Run the client. JavaRmi> java helloworld.Client 42
  24. 24. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Components of an RMI-based application • Services of remote objects (interfaces) • Implementation of services (classes) • Proxies for client-side (classes, generated) • A server that binds services (class) • A client that looks up services (class) • The RMI registry (component of RMI layer) • Security policies for client and server (optional)
  25. 25. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Security of RMI • Useful constrains: – Service provision (“May an app provide services?”) – Naming lookups (“May an IP address look up services?”) – Remote invocations (“May an IP address invoke ...?”) • Use a non-default security manager: System.setSecurityManager(new SecurityManager()); • Assign a security policy to an application: java -Djava.security.policy=mySecurity.policy Server
  26. 26. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) – A non-C/S-based version as a reference: package “banking.local”. – The C/S-based version: package “banking.remote”. – Think of a refactoring to derive the latter from the former. DEMO An RMI-based banking app
  27. 27. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) DEMO http://101companies.org/wiki/ Contribution:javaRmi This implementation is interesting in so far that it readies all data objects for RMI. Further, the operation “total” is provided as a service, but “cut” is not. Thus, the client must implement “cut”, which essentially means that all company, department, and employee objects end up as proxies on the client.
  28. 28. (C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Summary • RMI easily blends with OOP. – A simple form of distributed programming is enabled this way. – Client/Server applications are enabled this way. • RMI – semantics and concepts: – Use local proxy objects for access to remote objects. – Un-/marshal arguments and results for messages on the wire. – Bind objects to names (URLs) that can be used for lookup. • RMI – programming idioms: – Remote objects are looked up from server-side registry. – Remote objects may also be returned by RMI calls. – Remote objects may be created by factory methods. • Omissions: – Multiplicity of clients – Other distribution technologies: Corba, WebServices, SOA, HTTP, ...

×