feat: 🔥 un test client-server qui tue

This commit is contained in:
gdamms 2021-12-03 09:09:08 +01:00
parent 85e8629816
commit 3eca435f34
8 changed files with 118 additions and 109 deletions

View file

@ -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();
}
}
}

View file

@ -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;
}

View file

@ -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();
}
}
}

View file

@ -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;
}
}

View file

@ -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;
}

View file

@ -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;

118
linda/test/ClientTest.java Normal file
View file

@ -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<Tuple> ts = (ArrayList<Tuple>) 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<Tuple> ts = (ArrayList<Tuple>) 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");
}
}

View file

@ -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");
}
}