From 70d0d0e196155a317354c150be1a0c9ce5aa4d71 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 15 Mar 2018 19:52:26 +0100 Subject: [PATCH] TD3: Add HelloWorld RMI package. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TD3/exemples/javarmi/hello/HelloClient.java | 21 +++++++++++++++++++ TD3/exemples/javarmi/hello/HelloImpl.java | 15 +++++++++++++ .../javarmi/hello/HelloInterface.java | 10 +++++++++ TD3/exemples/javarmi/hello/HelloServer.java | 18 ++++++++++++++++ TD3/exemples/javarmi/hello/security.policy | 4 ++++ 5 files changed, 68 insertions(+) create mode 100644 TD3/exemples/javarmi/hello/HelloClient.java create mode 100644 TD3/exemples/javarmi/hello/HelloImpl.java create mode 100644 TD3/exemples/javarmi/hello/HelloInterface.java create mode 100644 TD3/exemples/javarmi/hello/HelloServer.java create mode 100644 TD3/exemples/javarmi/hello/security.policy diff --git a/TD3/exemples/javarmi/hello/HelloClient.java b/TD3/exemples/javarmi/hello/HelloClient.java new file mode 100644 index 0000000..0d0eeea --- /dev/null +++ b/TD3/exemples/javarmi/hello/HelloClient.java @@ -0,0 +1,21 @@ +package exemples.javarmi.hello; +import java.rmi.Naming; +import java.rmi.RMISecurityManager; + +public class HelloClient { + public static void main(String[] args){ + /* lance le SecurityManager */ + System.setSecurityManager(new RMISecurityManager()); + try { + String server = args[0]; + int port = Integer.parseInt(args[1]); + /* cherche référence objet distant */ + HelloInterface hello = (HelloInterface)Naming.lookup("rmi://"+server+":"+port+"/Hello1"); + /* appel de méthode à distance */ + System.out.println(hello.sayHello()); + } + catch (Exception e) { + System.out.println ("Erreur client : " + e); + } + } +} diff --git a/TD3/exemples/javarmi/hello/HelloImpl.java b/TD3/exemples/javarmi/hello/HelloImpl.java new file mode 100644 index 0000000..76ee108 --- /dev/null +++ b/TD3/exemples/javarmi/hello/HelloImpl.java @@ -0,0 +1,15 @@ +package exemples.javarmi.hello; +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; + +public class HelloImpl extends UnicastRemoteObject implements HelloInterface { + private static final long serialVersionUID = 6586708515447619453L; + private String message; + + public HelloImpl(String s) throws RemoteException { + message = s; + } + public String sayHello() throws RemoteException { + return message; + } +} diff --git a/TD3/exemples/javarmi/hello/HelloInterface.java b/TD3/exemples/javarmi/hello/HelloInterface.java new file mode 100644 index 0000000..a1dc126 --- /dev/null +++ b/TD3/exemples/javarmi/hello/HelloInterface.java @@ -0,0 +1,10 @@ +package exemples.javarmi.hello; +import java.rmi.Remote; +import java.rmi.RemoteException; + +public interface HelloInterface extends Remote { + /** + * méthode imprimant un message prédéfini dans l'objet appelé + */ + public String sayHello() throws RemoteException; +} diff --git a/TD3/exemples/javarmi/hello/HelloServer.java b/TD3/exemples/javarmi/hello/HelloServer.java new file mode 100644 index 0000000..ad08111 --- /dev/null +++ b/TD3/exemples/javarmi/hello/HelloServer.java @@ -0,0 +1,18 @@ +package exemples.javarmi.hello; +import java.rmi.Naming; +import java.rmi.RMISecurityManager; + +public class HelloServer { + public static void main (String[] args) { + /* lancer SecurityManager */ + System.setSecurityManager(new RMISecurityManager()); + try { + /* créer une instance de la classe Hello et l'enregistrer dans le serveur de noms */ + Naming.rebind("Hello1", new HelloImpl("Hello world")); + System.out.println("Serveur prêt"); + } + catch (Exception e) { + System.out.println("Erreur serveur : " + e); + } + } +} diff --git a/TD3/exemples/javarmi/hello/security.policy b/TD3/exemples/javarmi/hello/security.policy new file mode 100644 index 0000000..8ae3813 --- /dev/null +++ b/TD3/exemples/javarmi/hello/security.policy @@ -0,0 +1,4 @@ +grant { + permission java.net.SocketPermission "*:1024-65535", "connect, accept, resolve"; + permission java.net.SocketPermission "*:80", "connect"; +}; -- 2.34.1