eventRegister du Centralized

This commit is contained in:
gdamms 2021-11-28 12:44:46 +01:00
parent db7103bb50
commit 39e0b3383c
3 changed files with 145 additions and 8 deletions

View file

@ -104,7 +104,7 @@ public class CentralizedLinda implements Linda {
} }
if (found) { if (found) {
// Result found, remove it from the tuple list // Result found, get it from the tuple list
result = tuples.get(index); result = tuples.get(index);
} }
@ -126,7 +126,7 @@ public class CentralizedLinda implements Linda {
// Wait for writing access // Wait for writing access
requestWriting(); requestWriting();
// Find the tuple in the tuple list // Extract the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) { for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) { if (tuples.get(index).matches(template)) {
result = tuples.remove(index); result = tuples.remove(index);
@ -175,10 +175,10 @@ public class CentralizedLinda implements Linda {
// Wait for writing access // Wait for writing access
requestWriting(); requestWriting();
// Find the tuple in the tuple list // Extract the tuples in the tuple list
for (index = 0; index < tuples.size(); index++) { for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) { 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 // Waiting for reading access
requestReading(); requestReading();
// Find the tuple in the tuple list // Find the tuples in the tuple list
for (index = 0; index < tuples.size(); index++) { for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) { if (tuples.get(index).matches(template)) {
results.add(tuples.get(index)); 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) { 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<Tuple> knownTuples = (List<Tuple>) 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) { public void debug(String prefix) {

View file

@ -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) {
}
}
}

View file

@ -12,13 +12,13 @@ public class BasicTestCallback {
private static class MyCallback implements Callback { private static class MyCallback implements Callback {
public void call(Tuple t) { 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); linda.eventRegister(eventMode.TAKE, eventTiming.IMMEDIATE, cbmotif, this);
try { try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
System.out.println("CB done with "+t); System.out.println("CB done with " + t);
} }
} }