projet-donnees-reparties/linda/server/LindaClient.java

138 lines
3.5 KiB
Java
Raw Normal View History

2021-11-27 16:50:33 +00:00
package linda.server;
import linda.Callback;
import linda.Linda;
import linda.Tuple;
2021-11-30 16:39:17 +00:00
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.
*/
2021-11-27 16:50:33 +00:00
public class LindaClient implements Linda {
2021-11-30 16:39:17 +00:00
LindaRemote lindaServer;
2021-11-30 16:39:17 +00:00
public static void main(String[] args) {
2021-12-02 20:59:23 +00:00
<<<<<<< HEAD
2021-12-02 20:59:23 +00:00
=======
>>>>>>> 4a7bd702d3a89d65aa8cb02471b5d126846a9062
LindaClient lc = new LindaClient(args[0]);
2021-11-30 16:39:17 +00:00
Tuple t1 = new Tuple(1, "a");
System.out.print("Tuple " + t1 + " envoyé");
lc.write(t1);
System.out.print("Tuple " + t1 + " écrit");
2021-11-30 16:39:17 +00:00
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
2021-11-30 16:39:17 +00:00
e.printStackTrace();
}
Tuple template = new Tuple(Integer.class, String.class);
System.out.print("Lecture " + template + " envoyé");
Tuple t2 = lc.read(template);
System.out.print("Tuple " + t2 + " lu");
2021-11-30 16:39:17 +00:00
}
/**
* Initializes the Linda implementation.
*
* @param serverURI the URI of the server, e.g.
* "rmi://localhost:4000/LindaServer" or
* "//localhost:4000/LindaServer".
2021-11-27 16:50:33 +00:00
*/
public LindaClient(String serverURI) {
2021-11-30 16:39:17 +00:00
try {
this.lindaServer = (LindaRemote) Naming.lookup(serverURI);
2021-11-30 16:39:17 +00:00
} catch (MalformedURLException | RemoteException | NotBoundException e) {
e.printStackTrace();
}
}
public void write(Tuple t) {
try {
lindaServer.write(t);
} catch (RemoteException e) {
e.printStackTrace();
}
2021-11-30 16:39:17 +00:00
}
public Tuple take(Tuple template) {
try {
return lindaServer.take(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
2021-11-27 16:50:33 +00:00
}
2021-11-30 16:39:17 +00:00
public Tuple read(Tuple template) {
try {
return lindaServer.read(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
2021-11-30 16:39:17 +00:00
}
public Tuple tryTake(Tuple template) {
try {
return lindaServer.tryTake(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
2021-11-30 16:39:17 +00:00
}
public Tuple tryRead(Tuple template) {
try {
return lindaServer.tryRead(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
2021-11-30 16:39:17 +00:00
}
public Collection<Tuple> takeAll(Tuple template) {
try {
return lindaServer.takeAll(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
2021-11-30 16:39:17 +00:00
}
public Collection<Tuple> readAll(Tuple template) {
try {
return lindaServer.readAll(template);
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
2021-11-30 16:39:17 +00:00
}
public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) {
try {
lindaServer.eventRegister(mode, timing, template, callback);
} catch (RemoteException e) {
e.printStackTrace();
}
2021-11-30 16:39:17 +00:00
}
public void debug(String prefix) {
try {
lindaServer.debug(prefix);
} catch (RemoteException e) {
e.printStackTrace();
}
2021-11-30 16:39:17 +00:00
}
2021-11-27 16:50:33 +00:00
}