WIP bug fix

This commit is contained in:
GustavoHMDS committed 2026-09-28 18:59:20 -03:00
1 parent 57059c3ef0
commit ba2f41daf7
14 files changed
+72 -78

No files matched your search

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+20
View File
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../pitiupi/dist/pitiupi.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
@@ -8,7 +8,7 @@ public class SocketTestMenu extends JMenu {
public SocketTestMenu(SocketTestWindow socketTestWindow) { public SocketTestMenu(SocketTestWindow socketTestWindow) {
this.socketTestWindow = socketTestWindow; this.socketTestWindow = socketTestWindow;
this.setText("Chat"); this.setText("MP-Chat");
JMenuItem menuItem = new JMenuItem(); JMenuItem menuItem = new JMenuItem();
menuItem.setText("Show Chat Window"); menuItem.setText("Show Chat Window");
menuItem.addActionListener(this::showTestWindow); menuItem.addActionListener(this::showTestWindow);
@@ -96,7 +96,7 @@ public class SocketTestWindow extends JFrame {
try { try {
TestMessage msg = createMessage("TCP Unicast"); TestMessage msg = createMessage("TCP Unicast");
InetAddress ip = InetAddress.getByName(ipField.getText()); InetAddress ip = InetAddress.getByName(ipField.getText());
mainWindow.getSocketTCP().send(msg.toByteArray(), ip); mainWindow.getSocketTCP().send(msg, ip);
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
} }
@@ -106,7 +106,7 @@ public class SocketTestWindow extends JFrame {
try { try {
TestMessage msg = createMessage("UDP Unicast"); TestMessage msg = createMessage("UDP Unicast");
InetAddress ip = InetAddress.getByName(ipField.getText()); InetAddress ip = InetAddress.getByName(ipField.getText());
mainWindow.getSocketUDP().sendUnicast(msg.toByteArray(), ip); mainWindow.getSocketUDP().sendUnicast(msg, ip);
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
} }
@@ -114,7 +114,7 @@ public class SocketTestWindow extends JFrame {
public void sendMulticast() { public void sendMulticast() {
try { try {
TestMessage msg = createMessage("UDP Multicast"); TestMessage msg = createMessage("UDP Multicast");
mainWindow.getSocketUDP().sendMulticast(msg.toByteArray()); mainWindow.getSocketUDP().sendMulticast(msg);
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
} }
+25 -4
View File
@@ -5,13 +5,14 @@ import GUI.SocketTestWindow;
import net.TestMessage; import net.TestMessage;
import pitiupi.GUI.MainWindow; import pitiupi.GUI.MainWindow;
import pitiupi.net.Message; import pitiupi.net.Message;
import pitiupi.plugin.Plugin; import pitiupi.net.tcp.TcpConnection;
import pitiupi.plugin.PrivateConnectionPlugin;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
public class SocketTest implements Plugin{ public class SocketTest implements PrivateConnectionPlugin{
SocketTestWindow socketTestWindow; SocketTestWindow socketTestWindow;
@Override @Override
@@ -29,6 +30,11 @@ public class SocketTest implements Plugin{
return "0"; return "0";
} }
@Override
public String getIdentifier() {
return "Socket.Test.123456";
}
@Override @Override
public void createPlugin(MainWindow mainWindow) { public void createPlugin(MainWindow mainWindow) {
this.socketTestWindow = new SocketTestWindow(mainWindow); this.socketTestWindow = new SocketTestWindow(mainWindow);
@@ -41,8 +47,8 @@ public class SocketTest implements Plugin{
} }
@Override @Override
public void receiveMessage(byte[] bytes) { public void receiveMessage(byte[] message) {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ByteArrayInputStream bis = new ByteArrayInputStream(message);
ObjectInputStream ois; ObjectInputStream ois;
try { try {
ois = new ObjectInputStream(bis); ois = new ObjectInputStream(bis);
@@ -53,4 +59,19 @@ public class SocketTest implements Plugin{
System.out.println(ex.toString()); System.out.println(ex.toString());
} }
} }
@Override
public void receivePrivateConnection(TcpConnection tcpConnection) {
}
@Override
public void receiveBytes(TcpConnection connection, byte[] bytes) {
}
@Override
public void onSocketClosed(TcpConnection tcpConnection) {
}
} }
@@ -66,7 +66,7 @@ public class HeartbeatManager {
private void announce() { private void announce() {
try { try {
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId); HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
mainWindow.getSocketUDP().sendMulticast(msg.toByteArray()); mainWindow.getSocketUDP().sendMulticast(msg);
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(HeartbeatManager.class.getName()).log(Level.WARNING, "Falha ao enviar heartbeat", ex); Logger.getLogger(HeartbeatManager.class.getName()).log(Level.WARNING, "Falha ao enviar heartbeat", ex);
} }
+15 -37
View File
@@ -316,47 +316,25 @@ public class SocketTCP extends Thread {
while (running) { while (running) {
try { try {
byte[] bytes = connection.receive(); byte[] bytes = connection.receive();
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis)) {
Object object = ois.readObject(); SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(bytes);
if (object instanceof Message message) {
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
if (sysMessage == null) { if (sysMessage == null) {
for (Plugin plugin : main.getPlugins()) { for (Plugin plugin : main.getPlugins()) {
plugin.receiveMessage(message); plugin.receiveMessage(bytes);
}
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"
);
} }
continue;
} }
catch (ClassNotFoundException ex) {
SysInfoMessage.sendSystemInfo( switch (sysMessage.getType()) {
connection, case CONNECTION_CLOSED, INTERNAL_ERROR -> {
SysInfoType.INTERNAL_ERROR, connectionManager.closeConnection(connection);
"class Message not found." return;
); }
logger.log(Level.SEVERE, null, ex); case MESSAGE_REJECTED -> {
connectionManager.closeConnection(connection); logger.log(Level.SEVERE, "Somehow sent something that was not an Message");
}
default -> {}
} }
} }
catch (SocketTimeoutException ex) { catch (SocketTimeoutException ex) {
@@ -48,11 +48,4 @@ public class SysInfoMessage extends Message {
} }
return null; return null;
} }
public static SysInfoMessage parseSystemInfo(Message message) {
if (message instanceof SysInfoMessage) {
return (SysInfoMessage) message;
}
return null;
}
} }
+6 -24
View File
@@ -74,7 +74,7 @@ public class SocketUDP extends Thread {
* @param msg mensagem serializada a ser enviada. * @param msg mensagem serializada a ser enviada.
* @throws IOException caso ocorra um erro durante o envio. * @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); send(msg, this.address);
} }
@@ -85,7 +85,7 @@ public class SocketUDP extends Thread {
* @param destinationAddress endereço IP do peer destinatário. * @param destinationAddress endereço IP do peer destinatário.
* @throws IOException caso ocorra um erro durante o envio. * @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); send(msg, destinationAddress);
} }
@@ -96,9 +96,10 @@ public class SocketUDP extends Thread {
* @param destinationAddress endereço de destino. * @param destinationAddress endereço de destino.
* @throws IOException caso ocorra um erro durante o envio. * @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; 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); multicastSocket.send(msgPacket);
} }
@@ -149,14 +150,9 @@ public class SocketUDP extends Thread {
continue; continue;
} }
Message message = tryParseMessage(msgPacket.getData());
if (message == null) {
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(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. * Inicia a recepção de mensagens UDP.
*/ */
+1 -1
View File
@@ -47,6 +47,6 @@ public interface Plugin {
* *
* @param message mensagem serializada recebida. * @param message mensagem serializada recebida.
*/ */
public void receiveMessage(Message message); public void receiveMessage(byte[] message);
} }