projet-donnees-reparties/linda/shm/CentralizedLinda.java

241 lines
6.9 KiB
Java
Raw Normal View History

2021-11-27 16:50:33 +00:00
package linda.shm;
import linda.Callback;
import linda.Linda;
import linda.Tuple;
2021-11-27 19:24:42 +00:00
import java.util.ArrayList;
import java.util.Collection;
2021-12-17 08:33:01 +00:00
import java.util.Collections;
2021-11-27 19:24:42 +00:00
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
2021-12-17 08:33:01 +00:00
class Reveil implements Callback {
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
static Lock lock = new ReentrantLock();
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
Tuple template;
Condition condition;
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
Reveil(Tuple template) {
this.template = template;
this.condition = lock.newCondition();
}
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
public void call(Tuple t) {
return;
2021-11-27 19:24:42 +00:00
}
2021-12-17 08:33:01 +00:00
void sleep() {
2021-11-27 19:24:42 +00:00
try {
2021-12-17 08:33:01 +00:00
condition.await();
2021-11-27 19:24:42 +00:00
} catch (InterruptedException e) {
e.printStackTrace();
}
}
2021-12-17 08:33:01 +00:00
void reveil(Tuple t) {
if (t.matches(this.template)) {
condition.signal();
}
}
}
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
/** Shared memory implementation of Linda. */
public class CentralizedLinda implements Linda {
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
List<Tuple> tuples;
List<Reveil> reveils;
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
public CentralizedLinda() {
this.tuples = Collections.synchronizedList(new ArrayList<Tuple>());
this.reveils = Collections.synchronizedList(new ArrayList<Reveil>());
}
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
public void write(Tuple t) {
tuples.add(t);
reveils.forEach(r -> r.reveil(t));
}
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
public Tuple take(Tuple template) {
Tuple result = null;
Reveil reveil = new Reveil(template);
result = tryTake(template);
while (result == null) {
reveil.sleep();
result = tryTake(template);
2021-11-27 19:24:42 +00:00
}
2021-12-17 08:33:01 +00:00
2021-11-27 19:24:42 +00:00
return result;
2021-11-27 16:50:33 +00:00
}
2021-11-27 19:24:42 +00:00
public Tuple read(Tuple template) {
Tuple result = null;
2021-12-17 08:33:01 +00:00
Reveil reveil = new Reveil(template);
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
result = tryRead(template);
while (result == null) {
reveil.sleep();
result = tryRead(template);
2021-11-27 19:24:42 +00:00
}
return result;
}
public Tuple tryTake(Tuple template) {
Tuple result = null;
2021-12-17 08:33:01 +00:00
// Extract the tuple from the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
if (tuples.get(i).matches(template)) {
result = tuples.remove(i);
break;
2021-11-27 19:24:42 +00:00
}
}
return result;
}
public Tuple tryRead(Tuple template) {
Tuple result = null;
2021-12-17 08:33:01 +00:00
// Get the tuple from the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
if (result.matches(template)) {
break;
2021-11-27 19:24:42 +00:00
}
}
return result;
}
public Collection<Tuple> takeAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
2021-12-17 08:33:01 +00:00
Tuple result = new Tuple();
2021-11-27 19:24:42 +00:00
2021-12-17 08:33:01 +00:00
// Extract the tuples in the tuple list
while (result != null) {
result = tryTake(template);
if (result != null) {
results.add(result);
2021-11-27 19:24:42 +00:00
}
}
return results;
}
public Collection<Tuple> readAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
2021-12-17 08:33:01 +00:00
Tuple result;
// Extract the tuples in the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
if (result.matches(template))
results.add(result);
2021-11-27 19:24:42 +00:00
}
return results;
}
public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) {
2021-11-28 11:44:46 +00:00
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) {
2021-11-30 16:39:17 +00:00
// Waiting for access
switch (mode) {
case READ:
requestReading();
break;
case TAKE:
requestWriting();
break;
}
2021-11-28 11:44:46 +00:00
// Find the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
// Tuple matching
2021-11-30 16:39:17 +00:00
if (timing == eventTiming.IMMEDIATE || knownTuples.isEmpty()) {
found = true;
} else {
for (Tuple knownTuple : knownTuples) {
if (knownTuple != tuples.get(index)) {
found = true;
break;
2021-11-28 11:44:46 +00:00
}
2021-11-30 16:39:17 +00:00
}
2021-11-28 11:44:46 +00:00
}
// 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;
}
}
2021-11-30 16:39:17 +00:00
// End access
switch (mode) {
case READ:
endReading();
break;
case TAKE:
endWriting();
break;
}
2021-11-28 11:44:46 +00:00
}
// Callback with the result tuple
callback.call(result);
2021-11-27 19:24:42 +00:00
2021-11-28 11:44:46 +00:00
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
2021-11-27 19:24:42 +00:00
}
public void debug(String prefix) {
2021-11-30 16:39:17 +00:00
try {
// Waiting for reading access
requestReading();
System.out.println(prefix + tuples);
// End reading
endReading();
} catch (InterruptedException e) {
e.printStackTrace();
}
2021-11-27 19:24:42 +00:00
}
2021-11-27 16:50:33 +00:00
}