This commit is contained in:
gdamms 2021-11-30 17:39:17 +01:00
parent 4c48b21211
commit e414adca3b
4 changed files with 193 additions and 32 deletions

View file

@ -3,19 +3,94 @@ package linda.server;
import linda.Callback; import linda.Callback;
import linda.Linda; import linda.Linda;
import linda.Tuple; import linda.Tuple;
import linda.shm.CentralizedLinda;
/** Client part of a client/server implementation of Linda. import java.net.MalformedURLException;
* It implements the Linda interface and propagates everything to the server it is connected to. 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 { public class LindaClient implements Linda {
/** Initializes the Linda implementation. Linda lindaServer;
* @param serverURI the URI of the server, e.g. "rmi://localhost:4000/LindaServer" or "//localhost:4000/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) { 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<Tuple> takeAll(Tuple template) {
return lindaServer.takeAll(template);
}
public Collection<Tuple> 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);
}
} }

View file

@ -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<Tuple> takeAll(Tuple template) {
return lindaInstance.takeAll(template);
}
public Collection<Tuple> 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);
}
}

View file

@ -227,30 +227,30 @@ public class CentralizedLinda implements Linda {
try { try {
while (!found) { while (!found) {
// Waiting for reading access // Waiting for access
switch (mode) {
case READ:
requestReading(); requestReading();
break;
case TAKE:
requestWriting();
break;
}
// Find the tuple in the tuple list // Find 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)) {
// Tuple matching // Tuple matching
if (timing == eventTiming.IMMEDIATE || knownTuples.isEmpty()) {
switch (timing) {
case IMMEDIATE:
// Tuple found
found = true; found = true;
break; } else {
case FUTURE:
// Is tuple unknown
for (Tuple knownTuple : knownTuples) { for (Tuple knownTuple : knownTuples) {
if (knownTuple != tuples.get(index)) { if (knownTuple != tuples.get(index)) {
found = true; found = true;
break; break;
} }
} }
break;
} }
// Rebreak to end searching // Rebreak to end searching
@ -275,8 +275,15 @@ public class CentralizedLinda implements Linda {
} }
} }
// End reading // End access
switch (mode) {
case READ:
endReading(); endReading();
break;
case TAKE:
endWriting();
break;
}
} }
// Callback with the result tuple // Callback with the result tuple
@ -290,7 +297,17 @@ public class CentralizedLinda implements Linda {
} }
public void debug(String prefix) { public void debug(String prefix) {
try {
// Waiting for reading access
requestReading();
System.out.println(prefix + tuples); System.out.println(prefix + tuples);
// End reading
endReading();
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
private void requestReading() throws InterruptedException { private void requestReading() throws InterruptedException {

View file

@ -27,14 +27,14 @@ public class BasicTestCB {
// linda = new linda.server.LindaClient("//localhost:4000/MonServeur"); // linda = new linda.server.LindaClient("//localhost:4000/MonServeur");
cbmotif = new Tuple(Integer.class, String.class); cbmotif = new Tuple(Integer.class, String.class);
linda.eventRegister(eventMode.READ, eventTiming.IMMEDIATE, cbmotif, new MyCallback());
linda.debug("(1.1)"); linda.debug("(1.1)");
Tuple t1 = new Tuple(1, "a"); Tuple t1 = new Tuple(1, "a");
System.out.println("(2) write: " + t1); System.out.println("(1) write: " + t1);
linda.write(t1); linda.write(t1);
linda.debug("(1.2)"); linda.debug("(1.2)");
linda.eventRegister(eventMode.READ, eventTiming.FUTURE, cbmotif, new MyCallback());
try { try {
Thread.sleep(5000); Thread.sleep(5000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -53,7 +53,7 @@ public class BasicTestCB {
linda.debug("(3.1)"); linda.debug("(3.1)");
Tuple t3 = new Tuple(3, "c"); Tuple t3 = new Tuple(3, "c");
System.out.println("(2) write: " + t3); System.out.println("(3) write: " + t3);
linda.write(t3); linda.write(t3);
linda.debug("(3.2)"); linda.debug("(3.2)");