Integra recebimento de heartbeat no socket multicast

This commit is contained in:
tonyhcj 2026-07-17 21:48:48 -03:00
parent 076b4fcb43
commit eff5fd2f12
1 changed files with 21 additions and 2 deletions

View File

@ -17,10 +17,12 @@ import java.net.UnknownHostException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import pitiupi.plugin.Plugin; import pitiupi.plugin.Plugin;
import pitiupi.net.HeartbeatMessage;
/** /**
* *
* @author flavio * @author flavio
* @author tony
*/ */
public class Socket extends Thread { public class Socket extends Thread {
@ -57,9 +59,15 @@ public class Socket extends Thread {
public void receive() throws UnknownHostException, IOException, ClassNotFoundException { public void receive() throws UnknownHostException, IOException, ClassNotFoundException {
byte[] buf = new byte[256000]; byte[] buf = new byte[256000];
while (true) { while (true) {
// Receive the information and print it.
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length); DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
multicastSocket.receive(msgPacket); multicastSocket.receive(msgPacket);
HeartbeatMessage heartbeat = tryParseHeartbeat(msgPacket.getData());
if (heartbeat != null) {
main.getHeartbeatManager().receiveHeartbeat(heartbeat, msgPacket.getAddress());
continue;
}
for (Plugin plugin : main.getPlugins()) { for (Plugin plugin : main.getPlugins()) {
System.out.println(msgPacket.getAddress().toString()); System.out.println(msgPacket.getAddress().toString());
plugin.receiveMessage(msgPacket.getData()); plugin.receiveMessage(msgPacket.getData());
@ -67,6 +75,17 @@ public class Socket extends Thread {
} }
} }
private HeartbeatMessage tryParseHeartbeat(byte[] data) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ObjectInput in = new ObjectInputStream(bais);
Object obj = in.readObject();
return (obj instanceof HeartbeatMessage) ? (HeartbeatMessage) obj : null;
} catch (IOException | ClassNotFoundException | ClassCastException ex) {
return null;
}
}
@Override @Override
public void run() { public void run() {
try { try {