intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Lập trình mạng nâng cao: Java Remote Method Invocation - Nguyễn Xuân Vinh

Chia sẻ: Ngocnga Ngocnga | Ngày: | Loại File: PPT | Số trang:26

63
lượt xem
3
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

This chapter presents the following content: Start the registry server, rmiregistry; start the object server; start the client; the client makes a request. Inviting you to refer.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Lập trình mạng nâng cao: Java Remote Method Invocation - Nguyễn Xuân Vinh

  1. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM GV: NGUYỄN XUÂN VINH KHOA CÔNG NGHỆ THÔNG TIN Java MÔN: LẬP TRÌNH MẠNG 2 Remote Method Invocation 15/01/16 Presenter: Nguyễn Xuân Vinh Information Technology Faculty /26 Nong Lam University 1
  2. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN “The network is the computer” GV: NGUYỄN XUÂN VINH  Consider the following program organization: method call MÔN: LẬP TRÌNH MẠNG 2 SomeClass AnotherClass returned object computer 1 computer 2  If the network is the computer, we ought to be able to put the two  classes on different computers 15/01/16 /26 2
  3. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Parameter marshalling GV: NGUYỄN XUÂN VINH MÔN: LẬP TRÌNH MẠNG 2 15/01/16 /26 3
  4. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Calling the remote getDescription method GV: NGUYỄN XUÂN VINH MÔN: LẬP TRÌNH MẠNG 2 15/01/16 /26 4
  5. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN RMI and other technologies GV: NGUYỄN XUÂN VINH  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  MÔN: LẬP TRÌNH MẠNG 2 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 15/01/16 /26 5
  6. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN What is needed for RMI GV: NGUYỄN XUÂN VINH  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 MÔN: LẬP TRÌNH MẠNG 2  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, do its  computation, and marshal its response  The client object has to unmarshal the response  Much of this is done for you by special software 15/01/16 /26 6
  7. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Terminology GV: NGUYỄN XUÂN VINH  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 MÔN: LẬP TRÌNH MẠNG 2  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 15/01/16 /26 7
  8. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Processes GV: NGUYỄN XUÂN VINH  For RMI, you need to be running three processes  The Client  The Server  The Object Registry, rmiregistry, which is like a DNS service for  MÔN: LẬP TRÌNH MẠNG 2 objects  You also need TCP/IP active 15/01/16 /26 8 8
  9. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Interfaces GV: NGUYỄN XUÂN VINH  Interfaces define behavior  Classes define implementation  Therefore, MÔN: LẬP TRÌNH MẠNG 2  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 15/01/16  The class whose method is being used should only be on the server /26 9 9
  10. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Classes GV: NGUYỄN XUÂN VINH  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 MÔN: LẬP TRÌNH MẠNG 2  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 15/01/16 /26 10 10
  11. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Conditions for serializability GV: NGUYỄN XUÂN VINH If an object is to be serialized:  The class must be declared as public  The class must implement Serializable MÔN: LẬP TRÌNH MẠNG 2 However, Serializable does 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  15/01/16 serialization /26 11 11
  12. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Remote interfaces and class GV: NGUYỄN XUÂN VINH  A Remote class has two parts:   The interface (used by both client and server): Must be public  Must extend the interface java.rmi.Remote MÔN: LẬP TRÌNH MẠNG 2 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 15/01/16 May have locally accessible methods that are not in its  Remote interface /26 12 12
  13. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Remote vs. Serializable GV: NGUYỄN XUÂN VINH  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 MÔN: LẬP TRÌNH MẠNG 2  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 15/01/16 /26 13 13
  14. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Security GV: NGUYỄN XUÂN VINH  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 MÔN: LẬP TRÌNH MẠNG 2  Your client program should use a more conservative security  manager than the default  Most discussions of RMI assume you should do this on both the  client and the server  Unless your server also acts as a client, it isn’t really necessary  on the server 15/01/16 /26 14 14
  15. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN The server class GV: NGUYỄN XUÂN VINH  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 MÔN: LẬP TRÌNH MẠNG 2  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) 15/01/16  Every remotely available method should be synchronized /26 15 15
  16. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN Example: HelloWorld GV: NGUYỄN XUÂN VINH HelloClient HelloServer (3) sayHello(“Vinh”) HelloInterface MÔN: LẬP TRÌNH MẠNG 2 Java.rmi.Remo te extends (2) lo o HelloInterface bi 1) () nd ku ( p() implements UnicastRemoteObj ect HelloImpl extends 15/01/16 RMI RMIRegistry Registry /26 16
  17. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN HelloInterface: interface GV: NGUYỄN XUÂN VINH import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloInterface extends Remote { public String sayHello(String name)throws RemoteException; } MÔN: LẬP TRÌNH MẠNG 2 15/01/16 /26 17 17
  18. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN HelloImpl: class implement HelloInterface GV: NGUYỄN XUÂN VINH 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 public HelloImpl() throws RemoteException { super(); } public Hello (String msg) throws RemoteException { message = msg; } 15/01/16 public String sayHello(String name) throws RemoteException { return “Hello “ + name + “. ” + message; } /26 } 18 18
  19. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN HelloServer: binding RMI remote object GV: NGUYỄN XUÂN VINH import java.net.MalformedURLException; import java.rmi.Naming; Registry reg = import java.rmi.RemoteException; LocateRegistry.createRegistry(1234) public class HelloServer { reg.rebind("rmi://localhost:1234/hello", hello) public static void main(String[] args)  {  try { MÔN: LẬP TRÌNH MẠNG 2 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(); } } catch (RemoteException e) { 15/01/16 System.out.println("Hello Server failed: " + e); } /26 } } 19 19
  20. TRƯỜNG ĐẠI HỌC NÔNG LÂM TP.HCM KHOA CÔNG NGHỆ THÔNG TIN HelloClient: lookup and process RMI object GV: NGUYỄN XUÂN VINH 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”)); MÔN: LẬP TRÌNH MẠNG 2 } catch (Exception e) { System.out.println("HelloClient exception: " + e); } } Registry reg = } LocateRegistry.createRegistry(1234) reg.rebind("rmi://localhost:1234/hello", hello) 15/01/16 /26 20 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD


ERROR:connection to 10.20.1.100:9315 failed (errno=111, msg=Connection refused)
ERROR:connection to 10.20.1.100:9315 failed (errno=111, msg=Connection refused)

 

Đồng bộ tài khoản
2=>2