From 3eca435f34b799679b7a002d3c0c9e81eabd6600 Mon Sep 17 00:00:00 2001 From: gdamms Date: Fri, 3 Dec 2021 09:09:08 +0100 Subject: [PATCH] feat: :fire: un test client-server qui tue --- Client/Joueur.java | 19 ------ Client/ObjetDeItf.java | 7 -- Serveur/Arbitre.java | 21 ------ Serveur/ObjetDe.java | 24 ------- Serveur/ObjetDeItf.java | 7 -- linda/server/LindaServer.java | 1 - linda/test/ClientTest.java | 118 ++++++++++++++++++++++++++++++++++ linda/test/ClientTest1.java | 30 --------- 8 files changed, 118 insertions(+), 109 deletions(-) delete mode 100644 Client/Joueur.java delete mode 100644 Client/ObjetDeItf.java delete mode 100644 Serveur/Arbitre.java delete mode 100644 Serveur/ObjetDe.java delete mode 100644 Serveur/ObjetDeItf.java create mode 100644 linda/test/ClientTest.java delete mode 100644 linda/test/ClientTest1.java diff --git a/Client/Joueur.java b/Client/Joueur.java deleted file mode 100644 index 905581f..0000000 --- a/Client/Joueur.java +++ /dev/null @@ -1,19 +0,0 @@ -import java.rmi.*; -import java.io.*; - -public class Joueur { - - public static void main(String[] args) { - try { - ObjetDeItf de = (ObjetDeItf) Naming.lookup("//localhost:4000/Michel"); - System.out.println(de); - int nb1 = de.getRandom(); - int nb2 = de.getRandom(); - int nb3 = de.getRandom(); - int nbCalls = de.getCalls(); - System.out.println("Vous avez tiré les valeurs " + nb1 + ", " + nb2 + ", " + nb3 + ". Actuellement il y a " + nbCalls + " lancers de dés."); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/Client/ObjetDeItf.java b/Client/ObjetDeItf.java deleted file mode 100644 index 6795e85..0000000 --- a/Client/ObjetDeItf.java +++ /dev/null @@ -1,7 +0,0 @@ -import java.rmi.*; -import java.io.*; - -public interface ObjetDeItf extends Remote { - public int getRandom() throws RemoteException; - public int getCalls() throws RemoteException; -} diff --git a/Serveur/Arbitre.java b/Serveur/Arbitre.java deleted file mode 100644 index 635acac..0000000 --- a/Serveur/Arbitre.java +++ /dev/null @@ -1,21 +0,0 @@ -import java.rmi.*; -import java.rmi.registry.*; -import java.io.*; - -public class Arbitre { - - public static void main(String[] args) { - try { - ObjetDe de = new ObjetDe(); - Registry registry = LocateRegistry.createRegistry(4000); - Naming.rebind("rmi://localhost:4000/Michel", de); - - // Si on utilise la commande rmiregistry dans le terminal, on peut alors remplacer les deux lignes précédentes par : - //Naming.bind("rmi://localhost/Michel", de); - System.out.println("L'objet a été publié"); - } catch (Exception e) { - e.printStackTrace(); - } - } - -} diff --git a/Serveur/ObjetDe.java b/Serveur/ObjetDe.java deleted file mode 100644 index 5c6c198..0000000 --- a/Serveur/ObjetDe.java +++ /dev/null @@ -1,24 +0,0 @@ -import java.rmi.*; -import java.rmi.server.*; -import java.io.*; -import java.util.*; - -public class ObjetDe extends UnicastRemoteObject implements ObjetDeItf { - - private Random de; - private int calls; - - public ObjetDe() throws RemoteException { - de = new Random(); - calls = 0; - } - - public int getRandom() throws RemoteException { - calls++; - return 1 + de.nextInt(6); - } - - public int getCalls() throws RemoteException { - return calls; - } -} diff --git a/Serveur/ObjetDeItf.java b/Serveur/ObjetDeItf.java deleted file mode 100644 index 6795e85..0000000 --- a/Serveur/ObjetDeItf.java +++ /dev/null @@ -1,7 +0,0 @@ -import java.rmi.*; -import java.io.*; - -public interface ObjetDeItf extends Remote { - public int getRandom() throws RemoteException; - public int getCalls() throws RemoteException; -} diff --git a/linda/server/LindaServer.java b/linda/server/LindaServer.java index 36d2637..34c23b1 100644 --- a/linda/server/LindaServer.java +++ b/linda/server/LindaServer.java @@ -4,7 +4,6 @@ import java.util.Collection; import java.rmi.server.UnicastRemoteObject; import java.rmi.*; -import java.rmi.registry.*; import linda.Callback; import linda.Linda; diff --git a/linda/test/ClientTest.java b/linda/test/ClientTest.java new file mode 100644 index 0000000..557288c --- /dev/null +++ b/linda/test/ClientTest.java @@ -0,0 +1,118 @@ +package linda.test; + +import linda.server.LindaClient; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Serializable; +import java.util.ArrayList; + +import linda.Callback; +import linda.Tuple; + +public class ClientTest implements Callback, Serializable { + + public static void main(String[] args) { + + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + LindaClient lc = new LindaClient(args[0]); + + while (true) { + afficherCommande(); + lireCommande(reader, lc); + } + + } + + static void afficherCommande() { + System.out.println("===================="); + System.out.println("Commandes :"); + System.out.println("w : write tuple"); + System.out.println("r : read tuple"); + System.out.println("t : take tuple"); + System.out.println("tr : tryRead tuple"); + System.out.println("tt : tryTake tuple"); + System.out.println("ra : readAll tuple"); + System.out.println("ta : takeAll tuple"); + System.out.println("\nPour les templates :"); + System.out.println("I : int"); + System.out.println("S : string"); + System.out.print("--------------------\n> "); + } + + static void lireCommande(BufferedReader reader, LindaClient lc) { + try { + String ligne = reader.readLine(); + String[] args = ligne.split(" "); + + if (args.length < 2) { + System.out.println("Il faut indiquer la commande et un tuple ! Ex : > r 1 S\n"); + } else { + + Serializable[] objets = new Serializable[args.length-1]; + + for (int i = 1; i < args.length; i++) { + if (args[i].equals("I")) { + objets[i-1] = Integer.class; + } else if (args[i].equals("S")) { + objets[i-1] = String.class; + } else { + try { + objets[i-1] = Integer.parseInt(args[i]); + } catch (NumberFormatException e) { + objets[i-1] = args[i]; + } + } + } + + Tuple t = new Tuple(objets); + + if (args[0].equals("w")) { + lc.write(t); + System.out.println("Tuple " + t + " écrit."); + } else if (args[0].equals("r")) { + System.out.println("Attente de la lecture d'un tuple " + t + "."); + t = lc.read(t); + System.out.println("Tuple " + t + " lu."); + } else if (args[0].equals("t")) { + System.out.println("Attente de la prise d'un tuple " + t + "."); + t = lc.take(t); + System.out.println("Tuple " + t + " pris."); + } else if (args[0].equals("tr")) { + t = lc.tryRead(t); + System.out.println("Tuple " + t + " lu."); + } else if (args[0].equals("tt")) { + t = lc.tryTake(t); + System.out.println("Tuple " + t + " pris."); + } else if (args[0].equals("ra")) { + ArrayList ts = (ArrayList) lc.readAll(t); + System.out.print("Tuples "); + for (Tuple ti : ts) { + System.out.print(ti + " "); + } + System.out.println(" lus."); + } else if (args[0].equals("ta")) { + ArrayList ts = (ArrayList) lc.takeAll(t); + System.out.print("Tuples "); + for (Tuple ti : ts) { + System.out.print(ti + " "); + } + System.out.println(" pris."); + } else { + System.out.println("Il faut indiquer la commande et un tuple ! Ex : > r 1 S\n"); + } + + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void call(Tuple t) { + System.out.println("Tuple " + t + " lu"); + } + +} diff --git a/linda/test/ClientTest1.java b/linda/test/ClientTest1.java deleted file mode 100644 index 4f3a121..0000000 --- a/linda/test/ClientTest1.java +++ /dev/null @@ -1,30 +0,0 @@ -package linda.test; - -import linda.server.LindaClient; -import linda.Tuple; - -public class ClientTest1 { - - public static void main(String[] args) { - - LindaClient lc = new LindaClient(args[0]); - - Tuple t1 = new Tuple(1, "a"); - System.out.print("Tuple " + t1 + " envoyé"); - lc.write(t1); - System.out.print("Tuple " + t1 + " écrit"); - - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - Tuple template = new Tuple(Integer.class, String.class); - System.out.print("Lecture " + template + " envoyé"); - Tuple t2 = lc.read(template); - System.out.print("Tuple " + t2 + " lu"); - - } - -}