diff --git a/pitiupi.testSocket/dist/testSocket.jar b/pitiupi.testSocket/dist/testSocket.jar new file mode 100644 index 0000000..3b6438b Binary files /dev/null and b/pitiupi.testSocket/dist/testSocket.jar differ diff --git a/pitiupi.testSocket/out/GUI/SocketTestMenu.class b/pitiupi.testSocket/out/GUI/SocketTestMenu.class new file mode 100644 index 0000000..724eae7 Binary files /dev/null and b/pitiupi.testSocket/out/GUI/SocketTestMenu.class differ diff --git a/pitiupi.testSocket/out/GUI/SocketTestWindow.class b/pitiupi.testSocket/out/GUI/SocketTestWindow.class new file mode 100644 index 0000000..b782ecd Binary files /dev/null and b/pitiupi.testSocket/out/GUI/SocketTestWindow.class differ diff --git a/pitiupi.testSocket/out/net/TestMessage.class b/pitiupi.testSocket/out/net/TestMessage.class new file mode 100644 index 0000000..d472376 Binary files /dev/null and b/pitiupi.testSocket/out/net/TestMessage.class differ diff --git a/pitiupi.testSocket/out/plugin/SocketTest.class b/pitiupi.testSocket/out/plugin/SocketTest.class new file mode 100644 index 0000000..549bbe9 Binary files /dev/null and b/pitiupi.testSocket/out/plugin/SocketTest.class differ diff --git a/pitiupi.testSocket/pitiupi.testSocket.iml b/pitiupi.testSocket/pitiupi.testSocket.iml new file mode 100644 index 0000000..4e2cbb3 --- /dev/null +++ b/pitiupi.testSocket/pitiupi.testSocket.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pitiupi.testSocket/src/GUI/SocketTestMenu.java b/pitiupi.testSocket/src/GUI/SocketTestMenu.java index 8b4984f..0f04bf9 100644 --- a/pitiupi.testSocket/src/GUI/SocketTestMenu.java +++ b/pitiupi.testSocket/src/GUI/SocketTestMenu.java @@ -8,7 +8,7 @@ public class SocketTestMenu extends JMenu { public SocketTestMenu(SocketTestWindow socketTestWindow) { this.socketTestWindow = socketTestWindow; - this.setText("Chat"); + this.setText("MP-Chat"); JMenuItem menuItem = new JMenuItem(); menuItem.setText("Show Chat Window"); menuItem.addActionListener(this::showTestWindow); diff --git a/pitiupi.testSocket/src/GUI/SocketTestWindow.java b/pitiupi.testSocket/src/GUI/SocketTestWindow.java index 32f3944..e4d70d0 100644 --- a/pitiupi.testSocket/src/GUI/SocketTestWindow.java +++ b/pitiupi.testSocket/src/GUI/SocketTestWindow.java @@ -96,7 +96,7 @@ public class SocketTestWindow extends JFrame { try { TestMessage msg = createMessage("TCP Unicast"); InetAddress ip = InetAddress.getByName(ipField.getText()); - mainWindow.getSocketTCP().send(msg.toByteArray(), ip); + mainWindow.getSocketTCP().send(msg, ip); } catch (Exception ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } @@ -106,7 +106,7 @@ public class SocketTestWindow extends JFrame { try { TestMessage msg = createMessage("UDP Unicast"); InetAddress ip = InetAddress.getByName(ipField.getText()); - mainWindow.getSocketUDP().sendUnicast(msg.toByteArray(), ip); + mainWindow.getSocketUDP().sendUnicast(msg, ip); } catch (Exception ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } @@ -114,7 +114,7 @@ public class SocketTestWindow extends JFrame { public void sendMulticast() { try { TestMessage msg = createMessage("UDP Multicast"); - mainWindow.getSocketUDP().sendMulticast(msg.toByteArray()); + mainWindow.getSocketUDP().sendMulticast(msg); } catch (Exception ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } diff --git a/pitiupi.testSocket/src/plugin/SocketTest.java b/pitiupi.testSocket/src/plugin/SocketTest.java index 66ef6f0..145dbca 100644 --- a/pitiupi.testSocket/src/plugin/SocketTest.java +++ b/pitiupi.testSocket/src/plugin/SocketTest.java @@ -5,13 +5,14 @@ import GUI.SocketTestWindow; import net.TestMessage; import pitiupi.GUI.MainWindow; import pitiupi.net.Message; -import pitiupi.plugin.Plugin; +import pitiupi.net.tcp.TcpConnection; +import pitiupi.plugin.PrivateConnectionPlugin; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; -public class SocketTest implements Plugin{ +public class SocketTest implements PrivateConnectionPlugin{ SocketTestWindow socketTestWindow; @Override @@ -29,6 +30,11 @@ public class SocketTest implements Plugin{ return "0"; } + @Override + public String getIdentifier() { + return "Socket.Test.123456"; + } + @Override public void createPlugin(MainWindow mainWindow) { this.socketTestWindow = new SocketTestWindow(mainWindow); @@ -41,8 +47,8 @@ public class SocketTest implements Plugin{ } @Override - public void receiveMessage(byte[] bytes) { - ByteArrayInputStream bis = new ByteArrayInputStream(bytes); + public void receiveMessage(byte[] message) { + ByteArrayInputStream bis = new ByteArrayInputStream(message); ObjectInputStream ois; try { ois = new ObjectInputStream(bis); @@ -53,4 +59,19 @@ public class SocketTest implements Plugin{ 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) { + + } } diff --git a/pitiupi/src/pitiupi/control/HeartbeatManager.java b/pitiupi/src/pitiupi/control/HeartbeatManager.java index 1cfa480..08b21be 100644 --- a/pitiupi/src/pitiupi/control/HeartbeatManager.java +++ b/pitiupi/src/pitiupi/control/HeartbeatManager.java @@ -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); } diff --git a/pitiupi/src/pitiupi/net/tcp/SocketTCP.java b/pitiupi/src/pitiupi/net/tcp/SocketTCP.java index 326d23d..074c36c 100644 --- a/pitiupi/src/pitiupi/net/tcp/SocketTCP.java +++ b/pitiupi/src/pitiupi/net/tcp/SocketTCP.java @@ -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) { diff --git a/pitiupi/src/pitiupi/net/tcp/SysInfoMessage.java b/pitiupi/src/pitiupi/net/tcp/SysInfoMessage.java index e10c9b7..9e149eb 100644 --- a/pitiupi/src/pitiupi/net/tcp/SysInfoMessage.java +++ b/pitiupi/src/pitiupi/net/tcp/SysInfoMessage.java @@ -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; - } } diff --git a/pitiupi/src/pitiupi/net/udp/SocketUDP.java b/pitiupi/src/pitiupi/net/udp/SocketUDP.java index da9fa5f..a4a42f2 100644 --- a/pitiupi/src/pitiupi/net/udp/SocketUDP.java +++ b/pitiupi/src/pitiupi/net/udp/SocketUDP.java @@ -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. */ diff --git a/pitiupi/src/pitiupi/plugin/Plugin.java b/pitiupi/src/pitiupi/plugin/Plugin.java index 6159842..108deb1 100644 --- a/pitiupi/src/pitiupi/plugin/Plugin.java +++ b/pitiupi/src/pitiupi/plugin/Plugin.java @@ -47,6 +47,6 @@ public interface Plugin { * * @param message mensagem serializada recebida. */ - public void receiveMessage(Message message); + public void receiveMessage(byte[] message); } \ No newline at end of file