From e414adca3b5ff285476dfbcb6525acd9da9d337c Mon Sep 17 00:00:00 2001 From: gdamms Date: Tue, 30 Nov 2021 17:39:17 +0100 Subject: [PATCH] :) --- linda/server/LindaClient.java | 93 +++++++++++++++++++++++++++++---- linda/server/LindaServer.java | 69 ++++++++++++++++++++++++ linda/shm/CentralizedLinda.java | 57 +++++++++++++------- linda/test/BasicTestCB.java | 6 +-- 4 files changed, 193 insertions(+), 32 deletions(-) create mode 100644 linda/server/LindaServer.java diff --git a/linda/server/LindaClient.java b/linda/server/LindaClient.java index ea2587b..554c071 100644 --- a/linda/server/LindaClient.java +++ b/linda/server/LindaClient.java @@ -3,19 +3,94 @@ package linda.server; import linda.Callback; import linda.Linda; import linda.Tuple; +import linda.shm.CentralizedLinda; -/** Client part of a client/server implementation of Linda. - * It implements the Linda interface and propagates everything to the server it is connected to. - * */ +import java.net.MalformedURLException; +import java.rmi.*; +import java.util.Collection; + +/** + * Client part of a client/server implementation of Linda. + * It implements the Linda interface and propagates everything to the server it + * is connected to. + */ public class LindaClient implements Linda { - - /** Initializes the Linda implementation. - * @param serverURI the URI of the server, e.g. "rmi://localhost:4000/LindaServer" or "//localhost:4000/LindaServer". + + Linda lindaServer; + + public static void main(String[] args) { + 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) { + e.printStackTrace(); + } + } + + /** + * Initializes the Linda implementation. + * + * @param serverURI the URI of the server, e.g. + * "rmi://localhost:4000/LindaServer" or + * "//localhost:4000/LindaServer". */ public LindaClient(String serverURI) { - // TO BE COMPLETED + try { + this.lindaServer = (Linda) Naming.lookup(serverURI); + } catch (MalformedURLException | RemoteException | NotBoundException e) { + e.printStackTrace(); + } } - - // TO BE COMPLETED + public void write(Tuple t) { + lindaServer.write(t); + } + + public Tuple take(Tuple template) { + return lindaServer.take(template); + } + + public Tuple read(Tuple template) { + return lindaServer.read(template); + } + + public Tuple tryTake(Tuple template) { + return lindaServer.tryTake(template); + } + + public Tuple tryRead(Tuple template) { + return lindaServer.tryRead(template); + } + + public Collection takeAll(Tuple template) { + return lindaServer.takeAll(template); + } + + public Collection readAll(Tuple template) { + return lindaServer.readAll(template); + } + + public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) { + lindaServer.eventRegister(mode, timing, template, callback); + } + + public void debug(String prefix) { + lindaServer.debug(prefix); + } } diff --git a/linda/server/LindaServer.java b/linda/server/LindaServer.java new file mode 100644 index 0000000..2aad1b3 --- /dev/null +++ b/linda/server/LindaServer.java @@ -0,0 +1,69 @@ +package linda.server; + +import java.rmi.*; +import java.rmi.server.UnicastRemoteObject; +import java.util.Collection; + +import linda.Callback; +import linda.Linda; +import linda.Tuple; +import linda.shm.CentralizedLinda; + +/** + * lindaServer + */ +public class LindaServer extends UnicastRemoteObject implements Linda { + + Linda lindaInstance; + + public static void main(String[] args) { + try { + LindaServer ls = new LindaServer(); + Naming.bind(args[0], ls); + System.out.println("L'instance Linda a été publié sur le registre (" + args[0] + ") !\n"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public LindaServer() throws RemoteException { + this.lindaInstance = new CentralizedLinda(); + } + + public void write(Tuple t) { + lindaInstance.write(t); + System.out.println("Le tuple " + t + " a été ajouté au TupleSpace !"); + } + + public Tuple take(Tuple template) { + return lindaInstance.take(template); + } + + public Tuple read(Tuple template) { + return lindaInstance.read(template); + } + + public Tuple tryTake(Tuple template) { + return lindaInstance.tryTake(template); + } + + public Tuple tryRead(Tuple template) { + return lindaInstance.tryRead(template); + } + + public Collection takeAll(Tuple template) { + return lindaInstance.takeAll(template); + } + + public Collection readAll(Tuple template) { + return lindaInstance.readAll(template); + } + + public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) { + lindaInstance.eventRegister(mode, timing, template, callback); + } + + public void debug(String prefix) { + lindaInstance.debug(prefix); + } +} \ No newline at end of file diff --git a/linda/shm/CentralizedLinda.java b/linda/shm/CentralizedLinda.java index 253fd29..78039a7 100644 --- a/linda/shm/CentralizedLinda.java +++ b/linda/shm/CentralizedLinda.java @@ -227,30 +227,30 @@ public class CentralizedLinda implements Linda { try { while (!found) { - // Waiting for reading access - requestReading(); + // Waiting for access + switch (mode) { + case READ: + requestReading(); + break; + case TAKE: + requestWriting(); + break; + } // Find the tuple in the tuple list for (index = 0; index < tuples.size(); index++) { if (tuples.get(index).matches(template)) { // Tuple matching - - switch (timing) { - case IMMEDIATE: - // Tuple found - found = true; - break; - - case FUTURE: - // Is tuple unknown - for (Tuple knownTuple : knownTuples) { - if (knownTuple != tuples.get(index)) { - found = true; - break; - } + if (timing == eventTiming.IMMEDIATE || knownTuples.isEmpty()) { + found = true; + } else { + for (Tuple knownTuple : knownTuples) { + if (knownTuple != tuples.get(index)) { + found = true; + break; } - break; + } } // Rebreak to end searching @@ -275,8 +275,15 @@ public class CentralizedLinda implements Linda { } } - // End reading - endReading(); + // End access + switch (mode) { + case READ: + endReading(); + break; + case TAKE: + endWriting(); + break; + } } // Callback with the result tuple @@ -290,7 +297,17 @@ public class CentralizedLinda implements Linda { } public void debug(String prefix) { - System.out.println(prefix + tuples); + try { + // Waiting for reading access + requestReading(); + + System.out.println(prefix + tuples); + + // End reading + endReading(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } private void requestReading() throws InterruptedException { diff --git a/linda/test/BasicTestCB.java b/linda/test/BasicTestCB.java index cc8b049..1d666fb 100644 --- a/linda/test/BasicTestCB.java +++ b/linda/test/BasicTestCB.java @@ -27,14 +27,14 @@ public class BasicTestCB { // linda = new linda.server.LindaClient("//localhost:4000/MonServeur"); cbmotif = new Tuple(Integer.class, String.class); - linda.eventRegister(eventMode.READ, eventTiming.IMMEDIATE, cbmotif, new MyCallback()); linda.debug("(1.1)"); Tuple t1 = new Tuple(1, "a"); - System.out.println("(2) write: " + t1); + System.out.println("(1) write: " + t1); linda.write(t1); linda.debug("(1.2)"); + linda.eventRegister(eventMode.READ, eventTiming.FUTURE, cbmotif, new MyCallback()); try { Thread.sleep(5000); } catch (InterruptedException e) { @@ -53,7 +53,7 @@ public class BasicTestCB { linda.debug("(3.1)"); Tuple t3 = new Tuple(3, "c"); - System.out.println("(2) write: " + t3); + System.out.println("(3) write: " + t3); linda.write(t3); linda.debug("(3.2)");