diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 397587f..d5111fa 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -18,10 +18,10 @@ public class App { // -- Unicast client port private static final int UNICAST_PORT = 8500; // -- Multicast listener ip, port - private static final String MULTICAST_IP = "226.0.0.1"; + private static final String MULTICAST_IP = "226.66.66.1"; private static final int MULTICAST_PORT = 42500; - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { // Create all repository ClientHandlerRepository clientRep = new ClientHandlerRepository(); StoreProcessorRepository storeRep = new StoreProcessorRepository(); @@ -30,11 +30,15 @@ public class App { protocolRep.addReader(new HelloRule()); new UnicastServerListener(clientRep, UNICAST_PORT); - new MulticastServerListener(storeRep, protocolRep, MULTICAST_IP, MULTICAST_PORT); - + MulticastServerListener multiCast = new MulticastServerListener(storeRep, protocolRep, MULTICAST_IP, MULTICAST_PORT); + multiCast.run(); FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep); // close repo et client et server. + + Thread.sleep(60000); + + multiCast.stop(); clientRep.close(); storeRep.close(); } diff --git a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java index f9b56a0..55c4d2e 100644 --- a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java +++ b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java @@ -5,6 +5,7 @@ import lightcontainer.interfaces.StoreProcessorFFE; import java.io.*; import java.net.Socket; import java.nio.charset.StandardCharsets; +import java.util.Objects; /** * StoreProcessor @@ -21,6 +22,7 @@ import java.nio.charset.StandardCharsets; * @author Jérémi NIHART */ public class StoreProcessor implements Runnable, AutoCloseable { + private String domain; // Variables private final StoreProcessorFFE fileFrontEnd; private final Socket store; @@ -30,7 +32,8 @@ public class StoreProcessor implements Runnable, AutoCloseable { private PrintWriter writer; // Constructor - public StoreProcessor(Socket socket, StoreProcessorFFE ffe) { + public StoreProcessor(Socket socket, String domain, StoreProcessorFFE ffe) { + this.domain = domain; this.fileFrontEnd = ffe; this.store = socket; this.client_run = false; @@ -92,4 +95,17 @@ public class StoreProcessor implements Runnable, AutoCloseable { } catch (IOException ignored) { } } } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + StoreProcessor that = (StoreProcessor) o; + return Objects.equals(domain, that.domain); + } + + @Override + public int hashCode() { + return Objects.hash(domain); + } } diff --git a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java index 33dee9a..9f0ffc7 100644 --- a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java @@ -65,13 +65,16 @@ public class MulticastServerListener implements Runnable { try { // TODO Récupérer le port du message du packet et le setup (add description of the line). HelloRule.Result readerResult = protocolRep.executeReader(data); + System.out.printf("Réception en multicast : Domain=%s | Port=%d\n", readerResult.getDomain(), readerResult.getPort()); Socket socket = new Socket(packet.getAddress(), readerResult.getPort()); // Create the store processor - StoreProcessor storeProcessor = new StoreProcessor(socket, null); // TODO : Voir comment on procède get via repo ou ici ?! + StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), null); // TODO : Voir comment on procède get via repo ou ici ?! // Add the store processor to its repository + this.repository.addStore(storeProcessor); } catch (IOException ignore) { + ignore.printStackTrace(); } } } catch (Exception ignore) { diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java index ba9567b..e3a0dc2 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java @@ -45,7 +45,6 @@ public class HelloRule extends ProtocolReader { String domain = data[DOMAIN]; int port = Integer.parseInt(data[PORT]); - System.out.printf("Regle Hello avec domain=%s et port=%s\n", domain, port); return new HelloRule.Result(domain, port); } diff --git a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java index 7ed9a74..b97aa29 100644 --- a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java +++ b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java @@ -5,7 +5,9 @@ import lightcontainer.domains.server.MulticastServerListener; import lightcontainer.interfaces.MulticastSPR; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; // TODO : C'est genre un ClientHandlerManager quoi hein, normal qu'il fasse blinder de chose ;) /** * StoreProcessorRepository @@ -21,12 +23,12 @@ import java.util.List; */ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR { // Variables - private final List handlers; + private final Set handlers; private MulticastServerListener server; // Constructor public StoreProcessorRepository() { - this.handlers = new ArrayList<>(); + this.handlers = new HashSet<>(); } /** @@ -49,6 +51,7 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR { @Override public void addStore(StoreProcessor store) { this.handlers.add(store); + System.out.println(handlers.size()); } /**