feat: 🔥 server client qui fonctionne enfin

This commit is contained in:
gdamms 2021-12-02 21:57:01 +01:00
parent e414adca3b
commit b86ea38420
8 changed files with 194 additions and 42 deletions

19
Client/Joueur.java Normal file
View file

@ -0,0 +1,19 @@
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();
}
}
}

7
Client/ObjetDeItf.java Normal file
View file

@ -0,0 +1,7 @@
import java.rmi.*;
import java.io.*;
public interface ObjetDeItf extends Remote {
public int getRandom() throws RemoteException;
public int getCalls() throws RemoteException;
}

21
Serveur/Arbitre.java Normal file
View file

@ -0,0 +1,21 @@
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();
}
}
}

24
Serveur/ObjetDe.java Normal file
View file

@ -0,0 +1,24 @@
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;
}
}

7
Serveur/ObjetDeItf.java Normal file
View file

@ -0,0 +1,7 @@
import java.rmi.*;
import java.io.*;
public interface ObjetDeItf extends Remote {
public int getRandom() throws RemoteException;
public int getCalls() throws RemoteException;
}

View file

@ -16,31 +16,28 @@ import java.util.Collection;
*/ */
public class LindaClient implements Linda { public class LindaClient implements Linda {
Linda lindaServer; LindaRemote lindaServer;
public static void main(String[] args) { 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 { try {
LindaServer ls = (LindaServer) Naming.lookup(args[0]); Thread.sleep(3000);
} catch (InterruptedException e) {
Tuple t1 = new Tuple(1, "a");
System.out.print("Tuple " + t1 + " envoyé");
ls.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 = ls.read(template);
System.out.print("Tuple " + t2 + " lu");
} catch (MalformedURLException | RemoteException | NotBoundException e) {
e.printStackTrace(); 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");
} }
/** /**
@ -52,45 +49,87 @@ public class LindaClient implements Linda {
*/ */
public LindaClient(String serverURI) { public LindaClient(String serverURI) {
try { try {
this.lindaServer = (Linda) Naming.lookup(serverURI); this.lindaServer = (LindaRemote) Naming.lookup(serverURI);
} catch (MalformedURLException | RemoteException | NotBoundException e) { } catch (MalformedURLException | RemoteException | NotBoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void write(Tuple t) { public void write(Tuple t) {
lindaServer.write(t); try {
lindaServer.write(t);
} catch (RemoteException e) {
e.printStackTrace();
}
} }
public Tuple take(Tuple template) { public Tuple take(Tuple template) {
return lindaServer.take(template); try {
return lindaServer.take(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
} }
public Tuple read(Tuple template) { public Tuple read(Tuple template) {
return lindaServer.read(template); try {
return lindaServer.read(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
} }
public Tuple tryTake(Tuple template) { public Tuple tryTake(Tuple template) {
return lindaServer.tryTake(template); try {
return lindaServer.tryTake(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
} }
public Tuple tryRead(Tuple template) { public Tuple tryRead(Tuple template) {
return lindaServer.tryRead(template); try {
return lindaServer.tryRead(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
} }
public Collection<Tuple> takeAll(Tuple template) { public Collection<Tuple> takeAll(Tuple template) {
return lindaServer.takeAll(template); try {
return lindaServer.takeAll(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
} }
public Collection<Tuple> readAll(Tuple template) { public Collection<Tuple> readAll(Tuple template) {
return lindaServer.readAll(template); try {
return lindaServer.readAll(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
} }
public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) { public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) {
lindaServer.eventRegister(mode, timing, template, callback); try {
lindaServer.eventRegister(mode, timing, template, callback);
} catch (RemoteException e) {
e.printStackTrace();
}
} }
public void debug(String prefix) { public void debug(String prefix) {
lindaServer.debug(prefix); try {
lindaServer.debug(prefix);
} catch (RemoteException e) {
e.printStackTrace();
}
} }
} }

View file

@ -0,0 +1,30 @@
package linda.server;
import linda.Linda;
import linda.Tuple;
import linda.Callback;
import java.util.Collection;
import java.rmi.*;
public interface LindaRemote extends Remote {
public void write(Tuple t) throws RemoteException;
public Tuple take(Tuple template) throws RemoteException;
public Tuple read(Tuple template) throws RemoteException;
public Tuple tryTake(Tuple template) throws RemoteException;
public Tuple tryRead(Tuple template) throws RemoteException;
public Collection<Tuple> takeAll(Tuple template) throws RemoteException;
public Collection<Tuple> readAll(Tuple template) throws RemoteException;
public void eventRegister(Linda.eventMode mode, Linda.eventTiming timing, Tuple template, Callback callback) throws RemoteException;
public void debug(String prefix) throws RemoteException;
}

View file

@ -1,9 +1,13 @@
package linda.server; package linda.server;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collection; import java.util.Collection;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.rmi.registry.*;
import java.io.*;
import linda.Callback; import linda.Callback;
import linda.Linda; import linda.Linda;
import linda.Tuple; import linda.Tuple;
@ -12,14 +16,15 @@ import linda.shm.CentralizedLinda;
/** /**
* lindaServer * lindaServer
*/ */
public class LindaServer extends UnicastRemoteObject implements Linda { public class LindaServer extends UnicastRemoteObject implements LindaRemote {
Linda lindaInstance; Linda lindaInstance;
public static void main(String[] args) { public static void main(String[] args) {
try { try {
LindaServer ls = new LindaServer(); LindaServer ls = new LindaServer();
Naming.bind(args[0], ls); Registry registry = LocateRegistry.createRegistry(Integer.parseInt(args[1]));
Naming.rebind(args[0], ls);
System.out.println("L'instance Linda a été publié sur le registre (" + args[0] + ") !\n"); System.out.println("L'instance Linda a été publié sur le registre (" + args[0] + ") !\n");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -35,35 +40,35 @@ public class LindaServer extends UnicastRemoteObject implements Linda {
System.out.println("Le tuple " + t + " a été ajouté au TupleSpace !"); System.out.println("Le tuple " + t + " a été ajouté au TupleSpace !");
} }
public Tuple take(Tuple template) { public Tuple take(Tuple template) throws RemoteException {
return lindaInstance.take(template); return lindaInstance.take(template);
} }
public Tuple read(Tuple template) { public Tuple read(Tuple template) throws RemoteException {
return lindaInstance.read(template); return lindaInstance.read(template);
} }
public Tuple tryTake(Tuple template) { public Tuple tryTake(Tuple template) throws RemoteException {
return lindaInstance.tryTake(template); return lindaInstance.tryTake(template);
} }
public Tuple tryRead(Tuple template) { public Tuple tryRead(Tuple template) throws RemoteException {
return lindaInstance.tryRead(template); return lindaInstance.tryRead(template);
} }
public Collection<Tuple> takeAll(Tuple template) { public Collection<Tuple> takeAll(Tuple template) throws RemoteException {
return lindaInstance.takeAll(template); return lindaInstance.takeAll(template);
} }
public Collection<Tuple> readAll(Tuple template) { public Collection<Tuple> readAll(Tuple template) throws RemoteException {
return lindaInstance.readAll(template); return lindaInstance.readAll(template);
} }
public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) { public void eventRegister(Linda.eventMode mode, Linda.eventTiming timing, Tuple template, Callback callback) throws RemoteException {
lindaInstance.eventRegister(mode, timing, template, callback); lindaInstance.eventRegister(mode, timing, template, callback);
} }
public void debug(String prefix) { public void debug(String prefix) throws RemoteException {
lindaInstance.debug(prefix); lindaInstance.debug(prefix);
} }
} }