diff --git a/linda/eratosthene/Client.java b/linda/eratosthene/Client.java new file mode 100644 index 0000000..b7cbd9e --- /dev/null +++ b/linda/eratosthene/Client.java @@ -0,0 +1,38 @@ +package linda.eratosthene; + +import java.util.ArrayList; + +import linda.Tuple; +import linda.server.LindaClient; + +public class Client { + + public static void main(String[] args) { + LindaClient lc = new LindaClient(args[0]); + + int K = (int) lc.read(new Tuple(Integer.class)).get(0); + System.out.println("K = " + K); + + while (true) { + Tuple tuple_test = lc.tryTake(new Tuple("prime", "untested", Integer.class)); + System.out.println(tuple_test); + if (tuple_test == null) { + break; + } + int val = (int) tuple_test.get(2); + tuple_test.set(1, "tested"); + lc.write(tuple_test); + + int iter = 2; + while (iter * val < K) { + Tuple tuple_testPrime = lc.take(new Tuple(String.class, String.class, iter*val)); + tuple_testPrime.set(0, "notprime"); + tuple_testPrime.set(1, "tested"); + lc.write(tuple_testPrime); + iter++; + } + } + + } + +} diff --git a/linda/eratosthene/Server.java b/linda/eratosthene/Server.java new file mode 100644 index 0000000..97a28ac --- /dev/null +++ b/linda/eratosthene/Server.java @@ -0,0 +1,52 @@ +package linda.eratosthene; + +import java.net.MalformedURLException; +import java.rmi.Naming; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.util.ArrayList; + +import linda.Tuple; +import linda.server.LindaServer; + +public class Server { + + public static void main(String[] args) { + try { + + LindaServer ls = new LindaServer(); + + int K = Integer.parseInt(args[2]); + + ls.write(new Tuple(K)); + + for (int i = 2; i < K; i++) { + ls.write(new Tuple("prime", "untested", i)); + } + + LocateRegistry.createRegistry(Integer.parseInt(args[1])); + Naming.rebind(args[0], ls); + System.out.println("L'instance Linda a été publié sur le registre (" + args[0] + ") !\n"); + + while (null != ls.tryRead(new Tuple(String.class, "untested", Integer.class))) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + ArrayList ts = (ArrayList) ls.readAll(new Tuple("prime", String.class, Integer.class)); + Tuple primes = new Tuple(); + for (int i = 0; i < ts.size(); i++) { + primes.add((int) ts.get(i).get(2)); + } + + System.out.println(primes); + + } catch (RemoteException | MalformedURLException e) { + e.printStackTrace(); + } + } + +}