From 39e0b3383ce07ae4d79d873335013af4cbdc1331 Mon Sep 17 00:00:00 2001 From: gdamms Date: Sun, 28 Nov 2021 12:44:46 +0100 Subject: [PATCH] eventRegister du Centralized --- linda/shm/CentralizedLinda.java | 80 +++++++++++++++++++++++++++++-- linda/test/BasicTestCB.java | 67 ++++++++++++++++++++++++++ linda/test/BasicTestCallback.java | 6 +-- 3 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 linda/test/BasicTestCB.java diff --git a/linda/shm/CentralizedLinda.java b/linda/shm/CentralizedLinda.java index 62a102b..253fd29 100644 --- a/linda/shm/CentralizedLinda.java +++ b/linda/shm/CentralizedLinda.java @@ -104,7 +104,7 @@ public class CentralizedLinda implements Linda { } if (found) { - // Result found, remove it from the tuple list + // Result found, get it from the tuple list result = tuples.get(index); } @@ -126,7 +126,7 @@ public class CentralizedLinda implements Linda { // Wait for writing access requestWriting(); - // Find the tuple in the tuple list + // Extract the tuple in the tuple list for (index = 0; index < tuples.size(); index++) { if (tuples.get(index).matches(template)) { result = tuples.remove(index); @@ -175,10 +175,10 @@ public class CentralizedLinda implements Linda { // Wait for writing access requestWriting(); - // Find the tuple in the tuple list + // Extract the tuples in the tuple list for (index = 0; index < tuples.size(); index++) { if (tuples.get(index).matches(template)) { - results.add(tuples.get(index)); + results.add(tuples.remove(index)); } } @@ -199,7 +199,7 @@ public class CentralizedLinda implements Linda { // Waiting for reading access requestReading(); - // Find the tuple in the tuple list + // Find the tuples in the tuple list for (index = 0; index < tuples.size(); index++) { if (tuples.get(index).matches(template)) { results.add(tuples.get(index)); @@ -216,7 +216,77 @@ public class CentralizedLinda implements Linda { } public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) { + new Thread() { + public void run() { + Tuple result = null; + boolean found = false; + int index; + // Get known tuples for FUTURE timing + List knownTuples = (List) readAll(template); + + try { + while (!found) { + // Waiting for reading access + requestReading(); + + // 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; + } + } + break; + } + + // Rebreak to end searching + if (found) { + break; + } + } + } + + if (found) { + // Result found + switch (mode) { + case READ: + // Get it from the tuple list + result = tuples.get(index); + break; + + case TAKE: + // Remove it from the tuple list + result = tuples.remove(index); + break; + } + } + + // End reading + endReading(); + } + + // Callback with the result tuple + callback.call(result); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }.start(); } public void debug(String prefix) { diff --git a/linda/test/BasicTestCB.java b/linda/test/BasicTestCB.java new file mode 100644 index 0000000..cc8b049 --- /dev/null +++ b/linda/test/BasicTestCB.java @@ -0,0 +1,67 @@ + +package linda.test; + +import linda.*; +import linda.Linda.eventMode; +import linda.Linda.eventTiming; + +public class BasicTestCB { + + private static Linda linda; + private static Tuple cbmotif; + + private static class MyCallback implements Callback { + public void call(Tuple t) { + System.out.println("CB got " + t); + linda.eventRegister(eventMode.TAKE, eventTiming.IMMEDIATE, cbmotif, this); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + System.out.println("CB done with " + t); + } + } + + public static void main(String[] a) { + linda = new linda.shm.CentralizedLinda(); + // 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); + linda.write(t1); + linda.debug("(1.2)"); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + } + + linda.debug("(2.1)"); + Tuple t2 = new Tuple(2, "b"); + System.out.println("(2) write: " + t2); + linda.write(t2); + linda.debug("(2.2)"); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + } + + linda.debug("(3.1)"); + Tuple t3 = new Tuple(3, "c"); + System.out.println("(2) write: " + t3); + linda.write(t3); + linda.debug("(3.2)"); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + } + + } + +} diff --git a/linda/test/BasicTestCallback.java b/linda/test/BasicTestCallback.java index 43e2395..312fbd0 100644 --- a/linda/test/BasicTestCallback.java +++ b/linda/test/BasicTestCallback.java @@ -9,16 +9,16 @@ public class BasicTestCallback { private static Linda linda; private static Tuple cbmotif; - + private static class MyCallback implements Callback { public void call(Tuple t) { - System.out.println("CB got "+t); + System.out.println("CB got " + t); linda.eventRegister(eventMode.TAKE, eventTiming.IMMEDIATE, cbmotif, this); try { Thread.sleep(1000); } catch (InterruptedException e) { } - System.out.println("CB done with "+t); + System.out.println("CB done with " + t); } }