WIP bug fix
This commit is contained in:
1 parent
57059c3ef0
commit
ba2f41daf7
14 files changed
+72
-78
No files matched your search
@@ -66,7 +66,7 @@ public class HeartbeatManager {
|
||||
private void announce() {
|
||||
try {
|
||||
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
|
||||
mainWindow.getSocketUDP().sendMulticast(msg.toByteArray());
|
||||
mainWindow.getSocketUDP().sendMulticast(msg);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(HeartbeatManager.class.getName()).log(Level.WARNING, "Falha ao enviar heartbeat", ex);
|
||||
}
|
||||
|
||||
@@ -316,47 +316,25 @@ public class SocketTCP extends Thread {
|
||||
while (running) {
|
||||
try {
|
||||
byte[] bytes = connection.receive();
|
||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
||||
|
||||
Object object = ois.readObject();
|
||||
if (object instanceof Message message) {
|
||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(bytes);
|
||||
|
||||
if (sysMessage == null) {
|
||||
for (Plugin plugin : main.getPlugins()) {
|
||||
plugin.receiveMessage(message);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (sysMessage.getType()) {
|
||||
case CONNECTION_CLOSED, INTERNAL_ERROR -> {
|
||||
connectionManager.closeConnection(connection);
|
||||
return;
|
||||
}
|
||||
case MESSAGE_REJECTED -> {
|
||||
logger.log(Level.SEVERE, "Somehow sent something that was not an Message");
|
||||
}
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
else {
|
||||
SysInfoMessage.sendSystemInfo(
|
||||
connection,
|
||||
SysInfoType.MESSAGE_REJECTED,
|
||||
"Public messages must be instances of Message"
|
||||
);
|
||||
if (sysMessage == null) {
|
||||
for (Plugin plugin : main.getPlugins()) {
|
||||
plugin.receiveMessage(bytes);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
SysInfoMessage.sendSystemInfo(
|
||||
connection,
|
||||
SysInfoType.INTERNAL_ERROR,
|
||||
"class Message not found."
|
||||
);
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
connectionManager.closeConnection(connection);
|
||||
|
||||
switch (sysMessage.getType()) {
|
||||
case CONNECTION_CLOSED, INTERNAL_ERROR -> {
|
||||
connectionManager.closeConnection(connection);
|
||||
return;
|
||||
}
|
||||
case MESSAGE_REJECTED -> {
|
||||
logger.log(Level.SEVERE, "Somehow sent something that was not an Message");
|
||||
}
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
catch (SocketTimeoutException ex) {
|
||||
|
||||
@@ -48,11 +48,4 @@ public class SysInfoMessage extends Message {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static SysInfoMessage parseSystemInfo(Message message) {
|
||||
if (message instanceof SysInfoMessage) {
|
||||
return (SysInfoMessage) message;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public class SocketUDP extends Thread {
|
||||
* @param msg mensagem serializada a ser enviada.
|
||||
* @throws IOException caso ocorra um erro durante o envio.
|
||||
*/
|
||||
public void sendMulticast(byte[] msg) throws IOException {
|
||||
public void sendMulticast(Message msg) throws IOException {
|
||||
send(msg, this.address);
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public class SocketUDP extends Thread {
|
||||
* @param destinationAddress endereço IP do peer destinatário.
|
||||
* @throws IOException caso ocorra um erro durante o envio.
|
||||
*/
|
||||
public void sendUnicast(byte[] msg, InetAddress destinationAddress) throws IOException {
|
||||
public void sendUnicast(Message msg, InetAddress destinationAddress) throws IOException {
|
||||
send(msg, destinationAddress);
|
||||
}
|
||||
|
||||
@@ -96,9 +96,10 @@ public class SocketUDP extends Thread {
|
||||
* @param destinationAddress endereço de destino.
|
||||
* @throws IOException caso ocorra um erro durante o envio.
|
||||
*/
|
||||
private void send(byte[] msg, InetAddress destinationAddress) throws IOException {
|
||||
private void send(Message msg, InetAddress destinationAddress) throws IOException {
|
||||
DatagramPacket msgPacket;
|
||||
msgPacket = new DatagramPacket(msg, msg.length, destinationAddress, this.main.getPort());
|
||||
byte[] buf = msg.toByteArray();
|
||||
msgPacket = new DatagramPacket(buf, buf.length, destinationAddress, this.main.getPort());
|
||||
multicastSocket.send(msgPacket);
|
||||
}
|
||||
|
||||
@@ -149,14 +150,9 @@ public class SocketUDP extends Thread {
|
||||
continue;
|
||||
}
|
||||
|
||||
Message message = tryParseMessage(msgPacket.getData());
|
||||
if (message == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (Plugin plugin : main.getPlugins()) {
|
||||
System.out.println(msgPacket.getAddress().toString());
|
||||
plugin.receiveMessage(message);
|
||||
plugin.receiveMessage(msgPacket.getData());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -179,20 +175,6 @@ public class SocketUDP extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
private Message tryParseMessage(byte[] data) {
|
||||
try {
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(data);
|
||||
ObjectInputStream ois = new ObjectInputStream(bis);
|
||||
|
||||
Object object = ois.readObject();
|
||||
|
||||
if (object instanceof Message message) return message;
|
||||
return null;
|
||||
} catch (IOException | ClassNotFoundException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inicia a recepção de mensagens UDP.
|
||||
*/
|
||||
|
||||
@@ -47,6 +47,6 @@ public interface Plugin {
|
||||
*
|
||||
* @param message mensagem serializada recebida.
|
||||
*/
|
||||
public void receiveMessage(Message message);
|
||||
public void receiveMessage(byte[] message);
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user