Bài giảng lập trình mạng java remote method invocation GV nguyễn xuân vinh

26 311 0
Bài giảng lập trình mạng  java remote method invocation   GV  nguyễn xuân vinh

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

KHOA CÔNG NGHỆ THÔNG TIN /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM Java Remote Method Invocation Presenter: Nguyễn Xuân Vinh Information Technology Faculty Nong Lam University GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN “The network is the computer”  Consider the following program organization: /26 04/12/15 MÔN: LẬP TRÌNH MẠNG method call SomeClass AnotherClass returned object computer computer  If the network is the computer, we ought to be able to put the two classes on different computers /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM Parameter marshalling KHOA CÔNG NGHỆ THÔNG TIN /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM Calling the remote getDescription method KHOA CÔNG NGHỆ THÔNG TIN /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN RMI and other technologies  CORBA (Common Object Request Broker Architecture) was used for a long time  CORBA supports object transmission between virtually any languages  Objects have to be described in IDL (Interface Definition Language), which looks a lot like C++ data definitions  CORBA is complex and flaky  CORBA has fallen out of favor  Microsoft supported CORBA, then COM, now NET  RMI is purely Java-specific  Java to Java communications only  As a result, RMI is much simpler than CORBA /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN What is needed for RMI  Java makes RMI (Remote Method Invocation) fairly easy, but there are some extra steps  To send a message to a remote “server object,”  The “client object” has to find the object Do this by looking it up in a registry  The client object then has to marshal the parameters (prepare them for transmission) Java requires Serializable parameters The server object has to unmarshal its parameters, its computation, and marshal its response  The client object has to unmarshal the response  Much of this is done for you by special software /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Terminology  A remote object is an object on another computer  The client object is the object making the request (sending a message to the other object)  The server object is the object receiving the request  As usual, “client” and “server” can easily trade roles (each can make requests of the other)  The rmiregistry is a special server that looks up objects by name  Hopefully, the name is unique!  rmic is a special compiler for creating stub (client) and skeleton (server) classes KHOA CÔNG NGHỆ THÔNG TIN Processes  For RMI, you need to be running three processes  The Client  The Server  The Object Registry, rmiregistry, which is like a DNS service for objects  You also need TCP/IP active /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Interfaces  Interfaces define behavior  Classes define implementation  Therefore,  In order to use a remote object, the client must know its behavior (interface), but does not need to know its implementation (class)  In order to provide an object, the server must know both its interface (behavior) and its class (implementation)  In short,  The interface must be available to both client and server  The class of any transmitted object must be on both client and server  The class whose method is being used should only be on the server /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Classes  A Remote class is one whose instances can be accessed remotely  On the computer where it is defined, instances of this class can be accessed just like any other object  On other computers, the remote object can be accessed via object handles  A Serializable class is one whose instances can be marshaled (turned into a linear sequence of bits)  Serializable objects can be transmitted from one computer to another  It probably isn’t a good idea for an object to be both remote and serializable 10 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 10 12 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Remote interfaces and class  A Remote class has two parts:  The interface (used by both client and server): Must be public Must extend the interface java.rmi.Remote Every method in the interface must declare that it throws java.rmi.RemoteException (other exceptions may also be thrown)  The class itself (used only by the server): Must implement the Remote interface Should extend java.rmi.server.UnicastRemoteObject May have locally accessible methods that are not in its Remote interface 12 KHOA CÔNG NGHỆ THÔNG TIN Remote vs Serializable  A Remote object lives on another computer (such as the Server)  You can send messages to a Remote object and get responses back from the object  All you need to know about the Remote object is its interface  Remote objects don’t pose much of a security issue  You can transmit a copy of a Serializable object between computers  The receiving object needs to know how the object is implemented; it needs the class as well as the interface  There is a way to transmit the class definition  Accepting classes does pose a security issue 13 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 13 KHOA CÔNG NGHỆ THÔNG TIN Security  It isn’t safe for the client to use somebody else’s code on some random server  System.setSecurityManager(new RMISecurityManager());  The security policy of RMISecurityManager is the same as that of the default SecurityManager  Your client program should use a more conservative security manager than the default  Most discussions of RMI assume you should this on both the client and the server  Unless your server also acts as a client, it isn’t really necessary on the server 14 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 14 KHOA CÔNG NGHỆ THÔNG TIN The server class  The class that defines the server object should extend UnicastRemoteObject  This makes a connection with exactly one other computer  If you must extend some other class, you can use exportObject() instead  Sun does not provide a MulticastRemoteObject class  The server class needs to register its server object:  String url = "rmi://" + host + ":" + port + "/" + objectName;  The default port is 1099  Naming.rebind(url, object);  Every remotely available method must throw a RemoteException (because connections can fail)  Every remotely available method should be synchronized 15 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 15 04/12/15 /26 Example: HelloWorld Java.rmi.Remote extends () HelloInterface HelloServer (3) sayHello(“Vinh”) (1) bin d HelloClient () up ok 16 KHOA CÔNG NGHỆ THÔNG TIN lo (2) MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM HelloInterface implements HelloImpl RMI RMIRegistry Registry UnicastRemoteObject extends KHOA CÔNG NGHỆ THÔNG TIN HelloInterface: interface import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloInterface extends Remote { public String sayHello(String name)throws RemoteException; } 17 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 17 KHOA CÔNG NGHỆ THÔNG TIN HelloImpl: class implement HelloInterface import java.rmi.*; import java.rmi.server.*; public class HelloImpl extends UnicastRemoteObject implements HelloInterface { private String message; // Strings are serializable MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM public HelloImpl() throws RemoteException { super(); } public Hello (String msg) throws RemoteException { message = msg; 04/12/15 } 18 public String sayHello(String name) throws RemoteException { /26 } return “Hello “ + name + “ ” + message; } 18 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN HelloServer: binding RMI remote object import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.RemoteException; public class HelloServer { Registry reg = LocateRegistry.createRegistry(1234) reg.rebind("rmi://localhost:1234/hello", hello) public static void main(String[] args) { MÔN: LẬP TRÌNH MẠNG try { HelloInterface hello = new HelloImpl(“Welcome to HelloWorld RMI”); try { Naming.rebind("rmi://localhost:1234/hello", hello); System.out.println("Hello Server is ready."); } catch (MalformedURLException e) { e.printStackTrace(); } 04/12/15 } catch (RemoteException e) { System.out.println("Hello Server failed: " + e); 19 /26 } } } 19 KHOA CÔNG NGHỆ THÔNG TIN HelloClient: lookup and process RMI object public class HelloClient { public static void main (String[] args) { HelloInterface hello; String name = "rmi://localhost:1234/hello"; try { hello = (HelloInterface) Naming.lookup(name); System.out.println(hello.sayHello(“Nguyen Xuan Vinh”)); } catch (Exception e) { System.out.println("HelloClient exception: " + e); } Registry reg = LocateRegistry.createRegistry(1234) } reg.rebind("rmi://localhost:1234/hello", hello) } 20 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 20 KHOA CÔNG NGHỆ THÔNG TIN rmic  The class that implements the remote object should be compiled as usual  Then, it should be compiled with rmic:  rmic HelloImpl  This will generate files HelloImpl_Stub.class and HelloImpl_Skel.class  These classes the actual communication  The “Stub” class must be copied to the client area  The “Skel” was needed in SDK 1.1 but is no longer necessary 21 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 21 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Trying RMI In three different terminal windows: Run the registry program: rmiregistry Run the server program: java HelloServer Run the client program: java HelloClient If all goes well, you should get the message: 22 /26 04/12/15 Hello Nguyen Xuan Vinh! Welcome to HelloWorld RMI! 22 23 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Example by CLI  Compile all *.java to *.class javac *.java  Compile stub and skeleton* (*:used in JDK1.1) rmic HelloImpl  Start RMI Registry start rmiregistry  Start server java -Djava.security.policy=server.policy HelloServer  Run client application java HelloClient server.policy server.policy grant { permission java.net.SocketPermission "127.0.0.1:*", "connect,resolve"; permission java.net.SocketPermission "127.0.0.1:*", "accept"; }; KHOA CÔNG NGHỆ THÔNG TIN Summary Start the registry server, rmiregistry Start the object server The object server registers an object, with a name, with the registry server Start the client The client looks up the object in the registry server The client makes a request The request actually goes to the Stub class The Stub classes on client and server talk to each other The client’s Stub class returns the result 24 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 24 KHOA CÔNG NGHỆ THÔNG TIN References Trail: RMI by Ann Wollrath and Jim Waldo  http://java.sun.com/docs/books/tutorial/rmi/index.html Fundamentals of RMI Short Course by jGuru  http://developer.java.sun.com/developer/onlineTraining/ rmi/RMI.html Java RMI Tutorial by Ken Baclawski  http://www.eg.bucknell.edu/~cs379/DistributedSystems/ rmi_tut.html  http://www.cs.rutgers.edu/~pxk/rutgers/notes/pdf/rb-rmi.pdf 25 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 25 26 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM HỎI ĐÁP KHOA CÔNG NGHỆ THÔNG TIN [...]... } 17 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 17 KHOA CÔNG NGHỆ THÔNG TIN HelloImpl: class implement HelloInterface import java. rmi.*; import java. rmi.server.*; public class HelloImpl extends UnicastRemoteObject implements HelloInterface { private String message; // Strings are serializable MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM... longer necessary 21 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 21 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Trying RMI In three different terminal windows: 1 Run the registry program: rmiregistry 1 Run the server program: java HelloServer 1 Run the client program: java HelloClient If all goes well, you... MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM HelloInterface implements HelloImpl RMI RMIRegistry Registry UnicastRemoteObject extends KHOA CÔNG NGHỆ THÔNG TIN HelloInterface: interface import java. rmi .Remote; import java. rmi.RemoteException; public interface HelloInterface extends Remote { public String sayHello(String name)throws RemoteException; } 17 /26 04/12/15 MÔN: LẬP... not declare any methods  The class must have a no-argument constructor  All fields of the class must be serializable: either primitive types or Serializable objects Exception: Fields marked transient will be ignored during serialization 11 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 11 12 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI... Naming.rebind(url, object);  Every remotely available method must throw a RemoteException (because connections can fail)  Every remotely available method should be synchronized 15 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 15 04/12/15 /26 Example: HelloWorld Java. rmi .Remote extends () HelloInterface HelloServer (3) sayHello( Vinh ) (1) bin d HelloClient ()... http://www.eg.bucknell.edu/~cs379/DistributedSystems/ rmi_tut.html  http://www.cs.rutgers.edu/~pxk/rutgers/notes/pdf/rb-rmi.pdf 25 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 25 26 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM HỎI ĐÁP KHOA CÔNG NGHỆ THÔNG TIN ... 22 /26 04/12/15 Hello Nguyen Xuan Vinh! Welcome to HelloWorld RMI! 22 23 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Example by CLI  Compile all * .java to *.class javac * .java  Compile stub and skeleton* (*:used in JDK1.1) rmic HelloImpl  Start RMI Registry start rmiregistry  Start server java -Djava.security.policy=server.policy... returns the result 1 2 24 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG 2 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 24 KHOA CÔNG NGHỆ THÔNG TIN References Trail: RMI by Ann Wollrath and Jim Waldo  http:/ /java. sun.com/docs/books/tutorial/rmi/index.html Fundamentals of RMI Short Course by jGuru  http://developer .java. sun.com/developer/onlineTraining/ rmi/RMI.html Java RMI Tutorial by Ken Baclawski  http://www.eg.bucknell.edu/~cs379/DistributedSystems/... HelloImpl() throws RemoteException { super(); } public Hello (String msg) throws RemoteException { message = msg; 04/12/15 } 18 public String sayHello(String name) throws RemoteException { /26 } return “Hello “ + name + “ ” + message; } 18 GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN HelloServer: binding RMI remote object import java. net.MalformedURLException; import java. rmi.Naming;... THÔNG TIN Remote interfaces and class  A Remote class has two parts:  The interface (used by both client and server): Must be public Must extend the interface java. rmi .Remote Every method in the interface must declare that it throws java. rmi.RemoteException (other exceptions may also be thrown)  The class itself (used only by the server): Must implement the Remote interface Should extend java. rmi.server.UnicastRemoteObject ... MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 11 12 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Remote. .. 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM Parameter marshalling KHOA CÔNG NGHỆ THÔNG TIN /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI... is no longer necessary 21 /26 04/12/15 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM 21 MÔN: LẬP TRÌNH MẠNG GV: NGUYỄN XUÂN VINH TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG

Ngày đăng: 04/12/2015, 15:17

Từ khóa liên quan

Mục lục

  • Java Remote Method Invocation

  • “The network is the computer”

  • Parameter marshalling

  • Calling the remote getDescription method

  • RMI and other technologies

  • What is needed for RMI

  • Terminology

  • Processes

  • Interfaces

  • Classes

  • Conditions for serializability

  • Remote interfaces and class

  • Remote vs. Serializable

  • Security

  • The server class

  • Example: HelloWorld

  • HelloInterface: interface

  • HelloImpl: class implement HelloInterface

  • HelloServer: binding RMI remote object

  • HelloClient: lookup and process RMI object

Tài liệu cùng người dùng

Tài liệu liên quan