Add StoreMulticastRunnable.java, Multicast receiver
This commit is contained in:
parent
fe4e80a148
commit
f91067504e
6
.idea/inspectionProfiles/Project_Default.xml
Normal file
6
.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="InfiniteLoopStatement" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
@ -3,12 +3,26 @@
|
|||||||
*/
|
*/
|
||||||
package lightcontainer;
|
package lightcontainer;
|
||||||
|
|
||||||
|
import lightcontainer.domains.StoreMulticastRunnable;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public String getGreeting() {
|
public static void main(String[] args) {
|
||||||
return "Hello World!";
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
// Close
|
||||||
System.out.println(new App().getGreeting());
|
multicast.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 <j.nihart@student.helmo.be>
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
@ -7,8 +7,8 @@ import org.junit.jupiter.api.Test;
|
|||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class AppTest {
|
class AppTest {
|
||||||
@Test void appHasAGreeting() {
|
// @Test void appHasAGreeting() {
|
||||||
App classUnderTest = new App();
|
// App classUnderTest = new App();
|
||||||
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user