diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..601f39c --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ 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 aa63180..e85d0bf 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -3,12 +3,26 @@ */ package lightcontainer; -public class App { - public String getGreeting() { - return "Hello World!"; - } +import lightcontainer.domains.StoreMulticastRunnable; +public class App { public static void main(String[] args) { - System.out.println(new App().getGreeting()); + System.out.println("Hello World !"); + + int port = 42500; + String ip = "226.0.0.1"; + + StoreMulticastRunnable multicast = new StoreMulticastRunnable(ip, port); + (new Thread(multicast)).start(); + + // Sleep + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // Close + multicast.stop(); } } diff --git a/app/src/main/java/lightcontainer/domains/StoreMulticastRunnable.java b/app/src/main/java/lightcontainer/domains/StoreMulticastRunnable.java new file mode 100644 index 0000000..a8ba557 --- /dev/null +++ b/app/src/main/java/lightcontainer/domains/StoreMulticastRunnable.java @@ -0,0 +1,69 @@ +package lightcontainer.domains; + +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; + +/** + * StoreMulticastRunnable + * + * Class listening to the announcement of new StoreBackEnd. + * Allowing it to be used as a storage unit. + * + * @version 1.0 + * + * @see Runnable + * @author Jérémi NIHART + */ +public class StoreMulticastRunnable implements Runnable { + // Variable + private final String multicast_address; + private final int multicast_port; + + private final byte[] buffer = new byte[256]; + private MulticastSocket listener; + + // Constructor + public StoreMulticastRunnable(String multicast_address, int multicast_port) { + this.multicast_address = multicast_address; + this.multicast_port = multicast_port; + } + + /** + * Start Multicast listening on indicated port and IP group. + * + * @since 1.0 + * + * @see MulticastSocket#receive(DatagramPacket) + * @see DatagramPacket + */ + @Override + public void run() { + try { + // Create a new listening socket + this.listener = new MulticastSocket(this.multicast_port); + // 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 + DatagramPacket packet = new DatagramPacket(buffer, buffer.length); + // Add the listener to the multicast group + listener.joinGroup(listener_group); + while(true) { + listener.receive(packet); + String data = new String(packet.getData(), 0, packet.getLength()); + System.out.println(data); // TODO ajouter un controller et lui signaler qu'il y a un nouveau StoreBackEnd + } + } catch (Exception ignore) { } + } + + /** + * Closes the MulticastSocket connection and aborts the listening and infinite listening loop. + * + * @since 1.0 + * + * @see StoreMulticastRunnable#run() + */ + public void stop() { + this.listener.close(); + } +} diff --git a/app/src/test/java/lightcontainer/AppTest.java b/app/src/test/java/lightcontainer/AppTest.java index f435009..9d64c1d 100644 --- a/app/src/test/java/lightcontainer/AppTest.java +++ b/app/src/test/java/lightcontainer/AppTest.java @@ -7,8 +7,8 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class AppTest { - @Test void appHasAGreeting() { - App classUnderTest = new App(); - assertNotNull(classUnderTest.getGreeting(), "app should have a greeting"); - } +// @Test void appHasAGreeting() { +// App classUnderTest = new App(); +// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting"); +// } }