feat: 🎨 Ajout de l'application erathostene

hein ?

il faut encore mettre les commentaires
This commit is contained in:
gdamms 2021-12-03 11:34:36 +01:00
parent 3eca435f34
commit 3695b78ece
2 changed files with 90 additions and 0 deletions

View file

@ -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++;
}
}
}
}

View file

@ -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<Tuple> ts = (ArrayList<Tuple>) 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();
}
}
}