diff --git a/Client/Joueur.java b/Client/Joueur.java new file mode 100644 index 0000000..905581f --- /dev/null +++ b/Client/Joueur.java @@ -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(); + } + } +} diff --git a/Client/ObjetDeItf.java b/Client/ObjetDeItf.java new file mode 100644 index 0000000..6795e85 --- /dev/null +++ b/Client/ObjetDeItf.java @@ -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; +} diff --git a/Serveur/Arbitre.java b/Serveur/Arbitre.java new file mode 100644 index 0000000..635acac --- /dev/null +++ b/Serveur/Arbitre.java @@ -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(); + } + } + +} diff --git a/Serveur/ObjetDe.java b/Serveur/ObjetDe.java new file mode 100644 index 0000000..5c6c198 --- /dev/null +++ b/Serveur/ObjetDe.java @@ -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; + } +} diff --git a/Serveur/ObjetDeItf.java b/Serveur/ObjetDeItf.java new file mode 100644 index 0000000..6795e85 --- /dev/null +++ b/Serveur/ObjetDeItf.java @@ -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; +} diff --git a/linda/server/LindaClient.java b/linda/server/LindaClient.java index 554c071..5befa29 100644 --- a/linda/server/LindaClient.java +++ b/linda/server/LindaClient.java @@ -16,31 +16,28 @@ import java.util.Collection; */ public class LindaClient implements Linda { - Linda lindaServer; + LindaRemote lindaServer; 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 { - LindaServer ls = (LindaServer) Naming.lookup(args[0]); - - 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) { + 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"); + } /** @@ -52,45 +49,87 @@ public class LindaClient implements Linda { */ public LindaClient(String serverURI) { try { - this.lindaServer = (Linda) Naming.lookup(serverURI); + this.lindaServer = (LindaRemote) Naming.lookup(serverURI); } catch (MalformedURLException | RemoteException | NotBoundException e) { e.printStackTrace(); } } public void write(Tuple t) { - lindaServer.write(t); + try { + lindaServer.write(t); + } catch (RemoteException e) { + e.printStackTrace(); + } } 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) { - return lindaServer.read(template); + try { + return lindaServer.read(template); + } catch (RemoteException e) { + e.printStackTrace(); + return null; + } } 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) { - return lindaServer.tryRead(template); + try { + return lindaServer.tryRead(template); + } catch (RemoteException e) { + e.printStackTrace(); + return null; + } } public Collection takeAll(Tuple template) { - return lindaServer.takeAll(template); + try { + return lindaServer.takeAll(template); + } catch (RemoteException e) { + e.printStackTrace(); + return null; + } } public Collection 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) { - lindaServer.eventRegister(mode, timing, template, callback); + try { + lindaServer.eventRegister(mode, timing, template, callback); + } catch (RemoteException e) { + e.printStackTrace(); + } } public void debug(String prefix) { - lindaServer.debug(prefix); + try { + lindaServer.debug(prefix); + } catch (RemoteException e) { + e.printStackTrace(); + } } } diff --git a/linda/server/LindaRemote.java b/linda/server/LindaRemote.java new file mode 100644 index 0000000..7266cc9 --- /dev/null +++ b/linda/server/LindaRemote.java @@ -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 takeAll(Tuple template) throws RemoteException; + + public Collection 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; +} diff --git a/linda/server/LindaServer.java b/linda/server/LindaServer.java index 2aad1b3..bcf4f03 100644 --- a/linda/server/LindaServer.java +++ b/linda/server/LindaServer.java @@ -1,9 +1,13 @@ package linda.server; -import java.rmi.*; -import java.rmi.server.UnicastRemoteObject; import java.util.Collection; +import java.rmi.server.UnicastRemoteObject; +import java.rmi.*; +import java.rmi.registry.*; +import java.io.*; + + import linda.Callback; import linda.Linda; import linda.Tuple; @@ -12,14 +16,15 @@ import linda.shm.CentralizedLinda; /** * lindaServer */ -public class LindaServer extends UnicastRemoteObject implements Linda { +public class LindaServer extends UnicastRemoteObject implements LindaRemote { Linda lindaInstance; public static void main(String[] args) { try { 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"); } catch (Exception e) { e.printStackTrace(); @@ -35,35 +40,35 @@ public class LindaServer extends UnicastRemoteObject implements Linda { 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); } - public Tuple read(Tuple template) { + public Tuple read(Tuple template) throws RemoteException { return lindaInstance.read(template); } - public Tuple tryTake(Tuple template) { + public Tuple tryTake(Tuple template) throws RemoteException { return lindaInstance.tryTake(template); } - public Tuple tryRead(Tuple template) { + public Tuple tryRead(Tuple template) throws RemoteException { return lindaInstance.tryRead(template); } - public Collection takeAll(Tuple template) { + public Collection takeAll(Tuple template) throws RemoteException { return lindaInstance.takeAll(template); } - public Collection readAll(Tuple template) { + public Collection readAll(Tuple template) throws RemoteException { 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); } - public void debug(String prefix) { + public void debug(String prefix) throws RemoteException { lindaInstance.debug(prefix); } } \ No newline at end of file