From c3907be8a097752bf884bdbd4b6cdc72a2a95ce0 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Mon, 14 Mar 2022 12:18:29 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20choix=20de=20l'interface=20pour=20l'?= =?UTF-8?q?=C3=A9coute=20multicast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 5 ++++ app/src/main/java/lightcontainer/App.java | 2 +- .../server/MulticastServerListener.java | 25 ++++++++++++++++++- .../lightcontainer/storage/Repository.java | 4 +++ .../java/lightcontainer/utils/NetChooser.java | 5 +++- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5345a80..3a8d723 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,3 +35,8 @@ tasks.named('test') { // Use JUnit Platform for unit tests. useJUnitPlatform() } + + +run { + standardInput = System.in +} \ No newline at end of file diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index ad2fbdf..b5e133d 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -36,7 +36,7 @@ public class App { // Initialisation du dispatcher et des servers FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); new UnicastServerListener(ffe, clientRep, protocolRep, repositoryStorage, repositoryStorage.getUnicastPort()); - new MulticastServerListener(ffe, storeRep, protocolRep, repositoryStorage.getMulticastIp(), repositoryStorage.getMulticastPort()); + new MulticastServerListener(ffe, storeRep, protocolRep, repositoryStorage.getMulticastIp(), repositoryStorage.getMulticastPort(), repositoryStorage.getNetworkInterface()); // close repo et client et server. diff --git a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java index 96342f0..36e6ba9 100644 --- a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java @@ -6,6 +6,7 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.reader.HelloRule; import lightcontainer.repository.FileFrontEnd; +import lightcontainer.utils.NetChooser; import java.io.IOException; import java.net.*; @@ -25,6 +26,7 @@ public class MulticastServerListener implements Runnable { // Variable private final String multicast_address; private final int multicast_port; + private final String network_interface; private FileFrontEnd ffe; private final MulticastSPR repository; private final ProtocolRepository protocolRep; @@ -33,12 +35,13 @@ public class MulticastServerListener implements Runnable { private MulticastSocket listener; // Constructor - public MulticastServerListener(FileFrontEnd ffe, MulticastSPR repository, ProtocolRepository protocolRep, String multicast_address, int multicast_port) { + public MulticastServerListener(FileFrontEnd ffe, MulticastSPR repository, ProtocolRepository protocolRep, String multicast_address, int multicast_port, String network_interface) { this.ffe = ffe; this.repository = repository; this.protocolRep = protocolRep; this.multicast_address = multicast_address; this.multicast_port = multicast_port; + this.network_interface = network_interface; repository.setServerListener(this); } @@ -54,6 +57,9 @@ public class MulticastServerListener implements Runnable { try { // Create a new listening socket this.listener = new MulticastSocket(this.multicast_port); + + this.selectInterface(); + // Create an identifier for the multicast group on the specified ip InetAddress listener_group = InetAddress.getByName(this.multicast_address); // Creation of a packet for the information received @@ -85,6 +91,23 @@ public class MulticastServerListener implements Runnable { } } + /** + * Permet de choisir l'interface réseau d'écoute en Multicast + */ + private void selectInterface() { + try { + this.listener.setNetworkInterface(NetworkInterface.getByName(network_interface)); + } catch (SocketException| NullPointerException e) { + NetChooser netChooser = new NetChooser(); + try { + this.listener.setNetworkInterface(netChooser.requestInterface()); + } catch (SocketException socketException) { + socketException.printStackTrace(); + this.stop(); + } + } + } + /** * Closes the MulticastSocket connection and aborts the listening and infinite listening loop. * diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 3561d73..362fac4 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -158,4 +158,8 @@ public class Repository { public List getStringifiedFilesOf(String login) { return this.appData.getStringifiedFilesOf(login); } + + public String getNetworkInterface() { + return this.appData.getAppConfig().getNetworkInterface(); + } } diff --git a/app/src/main/java/lightcontainer/utils/NetChooser.java b/app/src/main/java/lightcontainer/utils/NetChooser.java index f9ecd98..066b7fb 100644 --- a/app/src/main/java/lightcontainer/utils/NetChooser.java +++ b/app/src/main/java/lightcontainer/utils/NetChooser.java @@ -13,6 +13,9 @@ public class NetChooser { public NetChooser() { loadInterfaces(); + } + + public NetworkInterface requestInterface() { Scanner console = new Scanner(System.in); String[] allInterfaceNames = getInterfaces(); for(int index=0; index < allInterfaceNames.length; ++index) { @@ -21,7 +24,7 @@ public class NetChooser { System.out.printf("Select your interface :"); NetworkInterface selected = getInterfacesByIndex(console.nextInt()); System.out.printf("Selected interface: %s\n", selected.getDisplayName()); - + return selected; } private void loadInterfaces() {