From 3a8c8f1926734c79be64af8debbc807ee2eabb5e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 16 Mar 2018 16:22:09 +0100 Subject: [PATCH] TD3: Add code skeleton for the hostel reservations though RMI. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TD3/hotel/ChaineHotels.java | 52 +++++++++++++++++ TD3/hotel/ChaineHotelsClientRMI.java | 78 +++++++++++++++++++++++++ TD3/hotel/ChaineHotelsInterface.java | 21 +++++++ TD3/hotel/ChaineHotelsServeurRMI.java | 69 ++++++++++++++++++++++ TD3/hotel/ExceptionChambresIndispo.java | 28 +++++++++ TD3/hotel/ExceptionNumReservation.java | 28 +++++++++ TD3/hotel/Infos.java | 77 ++++++++++++++++++++++++ TD3/hotel/Readme.txt | 47 +++++++++++++++ TD3/hotel/security.policy | 23 ++++++++ 9 files changed, 423 insertions(+) create mode 100644 TD3/hotel/ChaineHotels.java create mode 100644 TD3/hotel/ChaineHotelsClientRMI.java create mode 100644 TD3/hotel/ChaineHotelsInterface.java create mode 100644 TD3/hotel/ChaineHotelsServeurRMI.java create mode 100644 TD3/hotel/ExceptionChambresIndispo.java create mode 100644 TD3/hotel/ExceptionNumReservation.java create mode 100644 TD3/hotel/Infos.java create mode 100644 TD3/hotel/Readme.txt create mode 100644 TD3/hotel/security.policy diff --git a/TD3/hotel/ChaineHotels.java b/TD3/hotel/ChaineHotels.java new file mode 100644 index 0000000..990cfb7 --- /dev/null +++ b/TD3/hotel/ChaineHotels.java @@ -0,0 +1,52 @@ +package hotel; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; +import java.util.HashMap; + + +/** + * Applications reparties + * TP - Exercice + * Chaine d'hotels + * + * Classe d'un futur objet serveur (servant) de l'application chaine d'hotels + * + * @author Toto + * @version 1.0 + */ +public class ChaineHotels { //TODO completer la declaration de la classe + + //TODO completer la declaration de chaque methode + + private static final long serialVersionUID = -2066714609302506667L; + + HashMap lesReservations = null; + Integer numLibre; + + public ChaineHotels() { + this.lesReservations = new HashMap(); + this.numLibre = 0; + } + + public void annuler(int numReservation) { + if(this.lesReservations.containsKey(new Integer(numReservation))==false) + throw new ExceptionNumReservation(); + else this.lesReservations.remove(new Integer(numReservation)); + } + + public Infos lister(int numReservation) { + if(this.lesReservations.containsKey(new Integer(numReservation))==false) + throw new ExceptionNumReservation(); + else return this.lesReservations.get(new Integer(numReservation)); + } + + public int reserver(String nomClient, String nomHotel, String date, int nbchambres) { + //il faudrait verifier que les chambres sont disponibles... + Infos infos = new Infos(nomClient, nomHotel, date, nbchambres); + this.numLibre++; + if(this.lesReservations.put(new Integer(this.numLibre), infos) == null) return 0; + else return this.lesReservations.size(); + } + +} diff --git a/TD3/hotel/ChaineHotelsClientRMI.java b/TD3/hotel/ChaineHotelsClientRMI.java new file mode 100644 index 0000000..d6b3235 --- /dev/null +++ b/TD3/hotel/ChaineHotelsClientRMI.java @@ -0,0 +1,78 @@ +package hotel; + +import java.io.IOException; +import java.rmi.Naming; +import java.rmi.NotBoundException; +import java.rmi.RMISecurityManager; + + +/** + * Applications reparties + * TP - Exercice + * Chaine d'hotels + * + * Programme client RMI + * + * @author Toto + * @version 1.0 + */ +public class ChaineHotelsClientRMI { + + + /** + * Nom par defaut de la machine ou tourne le registre RMI + */ + private static String DEFAULT_HOST = "localhost"; + + /** + * Port par defaut du registre RMI distant + */ + private static int DEFAULT_REGISTRY_PORT = 1099; + + + /** + * Programme principal (les arguments sont optionnels) + * @param args[0] Nom du serveur + * @param args[1] Port du serveur + */ + public static void main(String[] args) { + String server = null; + int port = -1; + + if(args.length != 2) { + System.out.println("Usage: java ChaineHotelsClientRMI NomServeur PortServeur"); + System.out.println("Utilisation du nom de serveur et du port par defaut ("+DEFAULT_HOST+":"+DEFAULT_REGISTRY_PORT+")"); + server = DEFAULT_HOST; + port = DEFAULT_REGISTRY_PORT; + } + else { + try { + server = args[0]; + port = Integer.parseInt(args[1]); + } + catch(NumberFormatException nfe) { + nfe.printStackTrace(); + System.exit(1); + } + } + + try { + + /********************/ + //TODO + /********************/ + + } catch (ExceptionChambresIndispo ec) { + System.err.println("Erreur : "+ec); + } catch (ExceptionNumReservation en) { + System.err.println("Erreur : "+en); + } catch (IOException ioe) { + System.err.println("Erreur : "+ioe); + ioe.printStackTrace(); + } catch (NotBoundException nbe) { + System.err.println("Erreur : "+nbe); + nbe.printStackTrace(); + } + } + +} diff --git a/TD3/hotel/ChaineHotelsInterface.java b/TD3/hotel/ChaineHotelsInterface.java new file mode 100644 index 0000000..d4c4af0 --- /dev/null +++ b/TD3/hotel/ChaineHotelsInterface.java @@ -0,0 +1,21 @@ +package hotel; + +import java.rmi.Remote; +import java.rmi.RemoteException; + + +/** + * Applications reparties + * TP - Exercice + * Chaine d'hotels + * + * Interface de la chaine d'hotel + * + * @author Toto + * @version 1.0 + */ +public interface ChaineHotelsInterface { //TODO completer la declaration + + //TODO completer l'interface + +} diff --git a/TD3/hotel/ChaineHotelsServeurRMI.java b/TD3/hotel/ChaineHotelsServeurRMI.java new file mode 100644 index 0000000..eb93ba3 --- /dev/null +++ b/TD3/hotel/ChaineHotelsServeurRMI.java @@ -0,0 +1,69 @@ +package hotel; + +import java.net.MalformedURLException; +import java.rmi.Naming; +import java.rmi.RMISecurityManager; +import java.rmi.RemoteException; + +/** + * Applications reparties + * TP - Exercice + * Chaine d'hotels + * + * Programme serveur RMI + * + * @author Toto + * @version 1.0 + */ +public class ChaineHotelsServeurRMI { + + + private static int DEFAULT_REGISTRY_PORT = 1099; + + + /** + * Constructeur par defaut + */ + public ChaineHotelsServeurRMI() { + } + + + /** + * Programme principal + * args[0] Port du registre rmi + */ + public static void main(String[] args) { + ChaineHotels hotels; + int port = -1; + + if(args.length != 1) { + System.out.println("Usage: java ChaineHotelServeurRMI Port"); + System.out.println("Utilisation du port par defaut ("+DEFAULT_REGISTRY_PORT+")"); + port = DEFAULT_REGISTRY_PORT; + } + else { + try { + port = Integer.parseInt(args[0]); + } + catch(NumberFormatException nfe) { + nfe.printStackTrace(); + System.exit(1); + } + } + + try { + /*************************/ + //TODO + /*************************/ + } + catch (RemoteException e) { + System.err.println("Serveur : erreur\n"+e.getMessage()); + e.printStackTrace(); + } + catch (MalformedURLException e) { + System.err.println("Serveur : erreur\n"+e.getMessage()); + e.printStackTrace(); + } + } + +} diff --git a/TD3/hotel/ExceptionChambresIndispo.java b/TD3/hotel/ExceptionChambresIndispo.java new file mode 100644 index 0000000..0cf161e --- /dev/null +++ b/TD3/hotel/ExceptionChambresIndispo.java @@ -0,0 +1,28 @@ +package hotel; + +/** + * Applications reparties + * TP - Exercice + * Chaine d'hotels + * + * Exception liee a la non disponibilite + * + * @author Toto + * @version 1.0 + */ +public class ExceptionChambresIndispo extends Exception { + + private static final long serialVersionUID = 5269715160970930660L; + + String message; + + public ExceptionChambresIndispo() { + super(); + message = "Erreur: chambre(s) indisponible(s)"; + } + + public String toString() { + return message; + } + +} diff --git a/TD3/hotel/ExceptionNumReservation.java b/TD3/hotel/ExceptionNumReservation.java new file mode 100644 index 0000000..cb8dbde --- /dev/null +++ b/TD3/hotel/ExceptionNumReservation.java @@ -0,0 +1,28 @@ +package hotel; + +/** + * Applications reparties + * TP - Exercice + * Chaine d'hotels + * + * Exception liee a un numero de reservation inexistant + * + * @author Toto + * @version 1.0 + */ +public class ExceptionNumReservation extends Exception { + + private static final long serialVersionUID = -6656122783469441587L; + + String message; + + public ExceptionNumReservation() { + super(); + message = "Erreur: numero de reservation incorrect"; + } + + public String toString() { + return message; + } + +} diff --git a/TD3/hotel/Infos.java b/TD3/hotel/Infos.java new file mode 100644 index 0000000..3c881c0 --- /dev/null +++ b/TD3/hotel/Infos.java @@ -0,0 +1,77 @@ +package hotel; + +import java.io.Serializable; + + +/** + * Applications reparties + * TP - Exercice + * Chaine d'hotels + * + * Classe regroupant les informations d'une reservation + * + * @author Toto + * @version 1.0 + */ +public class Infos implements Serializable { + + private static final long serialVersionUID = -6199402168457446943L; + + private String nomClient; + private String nomHotel; + private String date; + private int nbchambres; + + public Infos() { + this.nomClient = ""; + this.nomHotel = ""; + this.date = ""; + this.nbchambres = 0; + } + + public Infos(String client, String hotel, String date, int nb) { + this.nomClient = client; + this.nomHotel = hotel; + this.date = date; + this.nbchambres = nb; + } + + public String getNomClient() { + return this.nomClient; + } + + public void setNomClient(String nom) { + this.nomClient = nom; + } + + public String getNomHotel() { + return this.nomHotel; + } + + public void setNomHotel(String nom) { + this.nomHotel = nom; + } + + public String getDate() { + return this.date; + } + + public void setDate(String d) { + this.date = d; + } + + public int getNbChambres() { + return this.nbchambres; + } + + public void setNbChambres(int nb) { + this.nbchambres = nb; + } + + public String toString() { + String str = ""; + str = str + this.nomClient + " " + this.nomHotel + " " + this.date + " " + this.nbchambres; + return str; + } + +} diff --git a/TD3/hotel/Readme.txt b/TD3/hotel/Readme.txt new file mode 100644 index 0000000..5a52233 --- /dev/null +++ b/TD3/hotel/Readme.txt @@ -0,0 +1,47 @@ + + +# 1) Completer les classes : +# ChaineHotelsInterface, ChaineHotels, ChaineHotelsServeurRMI, ChaineHotelsClientRMI + + + + +# 2) Pour la compilation: + +#Se placer dans le dossier CONTENANT le dossier "hotel" + +#compilation : +javac hotel/*.java + +#Generation des souches : +rmic hotel.ChaineHotels + +#Creation d'un fichier texte "security.policy" contenant la politique de sécurité retenue +# (ici on autorise tout... dangereux !) + + + + +# 3) Execution de l'application : + +#On suppose que l’élève e00000 a place le dossier "hotel" dans un dossier "SR" + +#On suppose que le serveur tourne sur la machine 134.192.30.6 +# et le registre utilise le port par défaut 1099 (le registre rmi tourne aussi sur la machine 134.192.30.6) + + +#Sur la machine "serveur" : + +#Lancement du registre rmi : +rmiregistry + +#Se placer dans le dossier "SR" + +#Lancement du programme serveur : +java -cp . -Djava.security.policy=/filer/eleves/e00000/SR/hotel/security.policy -Djava.rmi.server.codebase=file:/filer/eleves/e00000/SR/ hotel.ChaineHotelsServeurRMI + + +#Sur la machine "client" : + +#Lancement du programme client : +java -cp . -Djava.security.policy=/filer/eleves/e00000/SR/hotel/security.policy -Djava.rmi.server.codebase=file:/filer/eleves/e00000/SR/ hotel.ChaineHotelsClientRMI 134.192.30.6 1099 diff --git a/TD3/hotel/security.policy b/TD3/hotel/security.policy new file mode 100644 index 0000000..3264aac --- /dev/null +++ b/TD3/hotel/security.policy @@ -0,0 +1,23 @@ +// +// For more information see: +// http://java.sun.com/docs/books/tutorial/rmi/running.html +// http://java.sun.com/j2se/1.5.0/docs/guide/security/PolicyFiles.html +// + +grant { + permission java.security.AllPermission; + + // Other options: + // permission java.net.SocketPermission "127.0.0.1:1024-", "accept, connect, listen, resolve"; + // permission java.net.SocketPermission "localhost:1024-", "accept, connect, listen, resolve"; + + // From http://java.sun.com/docs/books/tutorial/rmi/running.html + // Copyright 1995-2005 Sun Microsystems, Inc. Reprinted with permission + + // permission java.net.SocketPermission "*:1024-65535", "connect,accept"; + // permission java.net.SocketPermission "*:80", "connect"; + + // permission java.net.SocketPermission "*:1024-65535", "connect,accept"; + // permission java.io.FilePermission "c:\\home\\ann\\public_html\\classes\\-", "read"; + // permission java.io.FilePermission "c:\\home\\jones\\public_html\\classes\\-", "read"; +}; -- 2.34.1