diff --git a/pitiupi.testSocket/dist/testSocket.jar b/pitiupi.testSocket/dist/testSocket.jar deleted file mode 100644 index 3b6438b..0000000 Binary files a/pitiupi.testSocket/dist/testSocket.jar and /dev/null differ diff --git a/pitiupi.testSocket/out/GUI/ChatMenu.class b/pitiupi.testSocket/out/GUI/ChatMenu.class new file mode 100644 index 0000000..65067fe Binary files /dev/null and b/pitiupi.testSocket/out/GUI/ChatMenu.class differ diff --git a/pitiupi.testSocket/out/GUI/ChatWindow.class b/pitiupi.testSocket/out/GUI/ChatWindow.class new file mode 100644 index 0000000..98660b9 Binary files /dev/null and b/pitiupi.testSocket/out/GUI/ChatWindow.class differ diff --git a/pitiupi.testSocket/out/GUI/PrivateChatWindow.class b/pitiupi.testSocket/out/GUI/PrivateChatWindow.class new file mode 100644 index 0000000..9415b28 Binary files /dev/null and b/pitiupi.testSocket/out/GUI/PrivateChatWindow.class differ diff --git a/pitiupi.testSocket/out/GUI/PublicChatWindow.class b/pitiupi.testSocket/out/GUI/PublicChatWindow.class new file mode 100644 index 0000000..01f5592 Binary files /dev/null and b/pitiupi.testSocket/out/GUI/PublicChatWindow.class differ diff --git a/pitiupi.testSocket/out/META-INF/services/pitiupi.plugin.Plugin b/pitiupi.testSocket/out/META-INF/services/pitiupi.plugin.Plugin index cdf4196..58a4118 100644 --- a/pitiupi.testSocket/out/META-INF/services/pitiupi.plugin.Plugin +++ b/pitiupi.testSocket/out/META-INF/services/pitiupi.plugin.Plugin @@ -1 +1 @@ -plugin.SocketTest +plugin.TestChat diff --git a/pitiupi.testSocket/out/control/ChatController.class b/pitiupi.testSocket/out/control/ChatController.class new file mode 100644 index 0000000..ca920e4 Binary files /dev/null and b/pitiupi.testSocket/out/control/ChatController.class differ diff --git a/pitiupi.testSocket/out/net/TestChatMessage.class b/pitiupi.testSocket/out/net/TestChatMessage.class new file mode 100644 index 0000000..02c09f3 Binary files /dev/null and b/pitiupi.testSocket/out/net/TestChatMessage.class differ diff --git a/pitiupi.testSocket/out/plugin/TestChat.class b/pitiupi.testSocket/out/plugin/TestChat.class new file mode 100644 index 0000000..6cbbaca Binary files /dev/null and b/pitiupi.testSocket/out/plugin/TestChat.class differ diff --git a/pitiupi.testSocket/src/GUI/ChatMenu.java b/pitiupi.testSocket/src/GUI/ChatMenu.java new file mode 100644 index 0000000..9b7e0e2 --- /dev/null +++ b/pitiupi.testSocket/src/GUI/ChatMenu.java @@ -0,0 +1,84 @@ +package GUI; + +import plugin.TestChat; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.util.ArrayList; +import java.util.List; + +public class ChatMenu extends JMenu { + private final TestChat testChat; + + public ChatMenu(TestChat testChat) { + this.testChat = testChat; + this.setText("MP-Chat"); + + JMenuItem startPrivateChat = new JMenuItem("start Private Chat"); + startPrivateChat.addActionListener(_ -> showPrivateChatDialog()); + this.add(startPrivateChat); + } + + public void newMenuItem(JMenuItem newMenuItem, ChatWindow chatWindow) { + newMenuItem.addActionListener(_ -> chatWindow.setVisible(true)); + + this.add(newMenuItem); + } + + public void removeMenuItem(JMenuItem removeMenuItem) { + this.remove(removeMenuItem); + } + + private void showPrivateChatDialog() { + JDialog dialog = new JDialog( + SwingUtilities.getWindowAncestor(this), + "Novo chat privado", + Dialog.ModalityType.APPLICATION_MODAL + ); + + JTextField ipField = new JTextField(); + + JButton startButton = new JButton("Iniciar"); + JButton cancelButton = new JButton("Cancelar"); + + JPanel contentPanel = new JPanel(new BorderLayout(10, 10)); + contentPanel.setBorder( + BorderFactory.createEmptyBorder(10, 10, 10, 10) + ); + + JPanel ipPanel = new JPanel(new BorderLayout(5, 5)); + ipPanel.add(new JLabel("IP:"), BorderLayout.WEST); + ipPanel.add(ipField, BorderLayout.CENTER); + + JPanel buttonsPanel = new JPanel( + new FlowLayout(FlowLayout.RIGHT) + ); + + buttonsPanel.add(cancelButton); + buttonsPanel.add(startButton); + + JLabel errorLabel = new JLabel("Não foi possível estabelecer a conexão."); + errorLabel.setForeground(Color.RED); + errorLabel.setVisible(false); + + contentPanel.add(errorLabel, BorderLayout.NORTH); + contentPanel.add(ipPanel, BorderLayout.CENTER); + contentPanel.add(buttonsPanel, BorderLayout.SOUTH); + + cancelButton.addActionListener(e -> dialog.dispose()); + + startButton.addActionListener(e -> { + String ip = ipField.getText(); + + if (testChat.openConnection(ip)) dialog.dispose(); + else errorLabel.setVisible(true); + + }); + + dialog.setContentPane(contentPanel); + dialog.pack(); + dialog.setLocationRelativeTo(SwingUtilities.getWindowAncestor(this)); + dialog.setVisible(true); + } +} diff --git a/pitiupi.testSocket/src/GUI/ChatWindow.java b/pitiupi.testSocket/src/GUI/ChatWindow.java new file mode 100644 index 0000000..9e9a0f6 --- /dev/null +++ b/pitiupi.testSocket/src/GUI/ChatWindow.java @@ -0,0 +1,94 @@ +package GUI; + +import net.TestChatMessage; +import pitiupi.GUI.MainWindow; + +import javax.swing.*; +import java.awt.*; +import java.net.InetAddress; +import java.net.UnknownHostException; + +public abstract class ChatWindow extends JFrame { + + protected final MainWindow mainWindow; + + private JTextArea receivedMessagesArea; + protected JTextField messageField; + + public ChatWindow(MainWindow mainWindow) { + this.mainWindow = mainWindow; + initComponents(); + } + + private void initComponents() { + setTitle("Socket Test"); + setSize(700, 500); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + + JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); + + mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + + initReceivedMessagesPanel(mainPanel); + initBottomPanel(mainPanel); + + setContentPane(mainPanel); + } + + private void initReceivedMessagesPanel(JPanel mainPanel) { + receivedMessagesArea = new JTextArea(); + receivedMessagesArea.setEditable(false); + receivedMessagesArea.setLineWrap(true); + receivedMessagesArea.setWrapStyleWord(true); + + JScrollPane messagesScrollPane = new JScrollPane(receivedMessagesArea); + + messagesScrollPane.setBorder(BorderFactory.createTitledBorder("Mensagens recebidas")); + + mainPanel.add(messagesScrollPane, BorderLayout.CENTER); + } + + private void initBottomPanel(JPanel mainPanel) { + messageField = new JTextField(); + + JPanel bottomPanel = new JPanel(new BorderLayout(10, 10)); + + bottomPanel.add(messageField, BorderLayout.CENTER); + + initButtons(bottomPanel); + + mainPanel.add(bottomPanel, BorderLayout.SOUTH); + } + + protected abstract void initButtons(JPanel bottomPanel); + + protected TestChatMessage createMessage(String tipoMensagem) { + try { + String nome = mainWindow.getUserName(); + String ip = InetAddress.getLocalHost().getHostAddress(); + String message = messageField.getText(); + + return new TestChatMessage(nome, ip, tipoMensagem, message); + + } catch (UnknownHostException e) { + throw new RuntimeException(e); + } + } + + public void receiveMessage(TestChatMessage msg) { + SwingUtilities.invokeLater(() -> { + String mensagem = String.format( + "[%s] %s (%s): %s%n", + msg.getTipoMensagem(), + msg.getNome(), + msg.getIp(), + msg.getMessage() + ); + + receivedMessagesArea.append(mensagem); + + receivedMessagesArea.setCaretPosition(receivedMessagesArea.getDocument().getLength()); + }); + } +} \ No newline at end of file diff --git a/pitiupi.testSocket/src/GUI/PrivateChatWindow.java b/pitiupi.testSocket/src/GUI/PrivateChatWindow.java new file mode 100644 index 0000000..8b809a9 --- /dev/null +++ b/pitiupi.testSocket/src/GUI/PrivateChatWindow.java @@ -0,0 +1,46 @@ +package GUI; + +import net.TestChatMessage; +import pitiupi.GUI.MainWindow; +import pitiupi.net.tcp.TcpConnection; + +import javax.swing.*; +import java.awt.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class PrivateChatWindow extends ChatWindow { + + private final TcpConnection connection; + + public PrivateChatWindow(MainWindow mainWindow, TcpConnection connection) { + this.connection = connection; + + super(mainWindow); + } + + @Override + protected void initButtons(JPanel bottomPanel) { + JButton tcpButton = new JButton("TCP"); + + tcpButton.addActionListener(e -> sendTCP()); + + JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); + + buttonsPanel.add(tcpButton); + + bottomPanel.add(buttonsPanel, BorderLayout.EAST); + } + + private void sendTCP() { + try { + TestChatMessage msg = createMessage("TCP Private"); + connection.send(msg.toByteArray()); + + } catch (Exception ex) { + Logger.getLogger( + PrivateChatWindow.class.getName() + ).log(Level.SEVERE, null, ex); + } + } +} \ No newline at end of file diff --git a/pitiupi.testSocket/src/GUI/PublicChatWindow.java b/pitiupi.testSocket/src/GUI/PublicChatWindow.java new file mode 100644 index 0000000..4c9e945 --- /dev/null +++ b/pitiupi.testSocket/src/GUI/PublicChatWindow.java @@ -0,0 +1,93 @@ +package GUI; + +import net.TestChatMessage; +import pitiupi.GUI.MainWindow; + +import javax.swing.*; +import java.awt.*; +import java.net.InetAddress; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class PublicChatWindow extends ChatWindow { + + private JTextField ipField; + + public PublicChatWindow(MainWindow mainWindow) { + super(mainWindow); + } + + @Override + protected void initButtons(JPanel bottomPanel) { + initIpField(bottomPanel); + initCommunicationButtons(bottomPanel); + } + + private void initIpField(JPanel bottomPanel) { + ipField = new JTextField(); + ipField.setPreferredSize(new Dimension(130, 30)); + + JPanel ipPanel = new JPanel(new BorderLayout(5, 5)); + + ipPanel.add(new JLabel("IP destino:"), BorderLayout.WEST); + + ipPanel.add(ipField, BorderLayout.CENTER); + + bottomPanel.add(ipPanel, BorderLayout.WEST); + } + + private void initCommunicationButtons(JPanel bottomPanel) { + JButton tcpButton = new JButton("TCP"); + JButton udpButton = new JButton("UDP"); + JButton multicastButton = new JButton("Multicast"); + + tcpButton.addActionListener(e -> sendTCP()); + udpButton.addActionListener(e -> sendUDP()); + multicastButton.addActionListener(e -> sendMulticast()); + + JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); + + buttonsPanel.add(tcpButton); + buttonsPanel.add(udpButton); + buttonsPanel.add(multicastButton); + + bottomPanel.add(buttonsPanel, BorderLayout.EAST); + } + + private void sendTCP() { + try { + TestChatMessage msg = createMessage("TCP Unicast"); + + InetAddress ip = InetAddress.getByName(ipField.getText()); + + mainWindow.getSocketTCP().send(msg, ip); + + } catch (Exception ex) { + Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private void sendUDP() { + try { + TestChatMessage msg = createMessage("UDP Unicast"); + + InetAddress ip = InetAddress.getByName(ipField.getText()); + + mainWindow.getSocketUDP().sendUnicast(msg, ip); + + } catch (Exception ex) { + Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private void sendMulticast() { + try { + TestChatMessage msg = createMessage("UDP Multicast"); + + mainWindow.getSocketUDP().sendMulticast(msg); + + } catch (Exception ex) { + Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex); + } + } +} \ No newline at end of file diff --git a/pitiupi.testSocket/src/GUI/SocketTestMenu.java b/pitiupi.testSocket/src/GUI/SocketTestMenu.java deleted file mode 100644 index 0f04bf9..0000000 --- a/pitiupi.testSocket/src/GUI/SocketTestMenu.java +++ /dev/null @@ -1,21 +0,0 @@ -package GUI; - -import javax.swing.*; -import java.awt.event.ActionEvent; - -public class SocketTestMenu extends JMenu { - private final SocketTestWindow socketTestWindow; - - public SocketTestMenu(SocketTestWindow socketTestWindow) { - this.socketTestWindow = socketTestWindow; - this.setText("MP-Chat"); - JMenuItem menuItem = new JMenuItem(); - menuItem.setText("Show Chat Window"); - menuItem.addActionListener(this::showTestWindow); - this.add(menuItem); - } - - private void showTestWindow(ActionEvent ae) { - this.socketTestWindow.setVisible(true); - } -} diff --git a/pitiupi.testSocket/src/GUI/SocketTestWindow.java b/pitiupi.testSocket/src/GUI/SocketTestWindow.java deleted file mode 100644 index e4d70d0..0000000 --- a/pitiupi.testSocket/src/GUI/SocketTestWindow.java +++ /dev/null @@ -1,151 +0,0 @@ -package GUI; - -import net.TestMessage; -import pitiupi.GUI.MainWindow; - -import javax.swing.*; -import java.awt.*; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.logging.Level; -import java.util.logging.Logger; - -public class SocketTestWindow extends JFrame { - private MainWindow mainWindow; - private JTextArea receivedMessagesArea; - - private JTextField ipField; - private JTextField messageField; - - private JButton tcpButton; - private JButton udpButton; - private JButton multicastButton; - - public SocketTestWindow(MainWindow mainWindow) { - this.mainWindow = mainWindow; - initComponents(); - } - - public void initComponents() { - setTitle("Socket Test"); - setSize(700, 500); - setLocationRelativeTo(null); - setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); - - JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); - - mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); - - initReceivedMessagesPanel(mainPanel); - initBottomPanel(mainPanel); - - setContentPane(mainPanel); - } - - private void initReceivedMessagesPanel(JPanel mainPanel) { - receivedMessagesArea = new JTextArea(); - receivedMessagesArea.setEditable(false); - receivedMessagesArea.setLineWrap(true); - receivedMessagesArea.setWrapStyleWord(true); - - JScrollPane messagesScrollPane = new JScrollPane(receivedMessagesArea); - - messagesScrollPane.setBorder(BorderFactory.createTitledBorder("Mensagens recebidas")); - - mainPanel.add(messagesScrollPane, BorderLayout.CENTER); - } - - private void initBottomPanel(JPanel mainPanel) { - ipField = new JTextField(); - ipField.setPreferredSize(new Dimension(130, 30)); - - JPanel ipPanel = new JPanel(new BorderLayout(5, 5)); - ipPanel.add(new JLabel("IP destino:"), BorderLayout.WEST); - ipPanel.add(ipField, BorderLayout.CENTER); - - messageField = new JTextField(); - - JPanel bottomPanel = new JPanel(new BorderLayout(10, 10)); - - bottomPanel.add(ipPanel, BorderLayout.WEST); - bottomPanel.add(messageField, BorderLayout.CENTER); - initButtons(bottomPanel); - - mainPanel.add(bottomPanel, BorderLayout.SOUTH); - } - - private void initButtons(JPanel bottomPanel) { - tcpButton = new JButton("TCP"); - udpButton = new JButton("UDP"); - multicastButton = new JButton("Multicast"); - - tcpButton.addActionListener(e -> sendTCP()); - udpButton.addActionListener(e -> sendUDP()); - multicastButton.addActionListener(e -> sendMulticast()); - - JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); - - buttonsPanel.add(tcpButton); - buttonsPanel.add(udpButton); - buttonsPanel.add(multicastButton); - - bottomPanel.add(buttonsPanel, BorderLayout.EAST); - } - - public void sendTCP() { - try { - TestMessage msg = createMessage("TCP Unicast"); - InetAddress ip = InetAddress.getByName(ipField.getText()); - mainWindow.getSocketTCP().send(msg, ip); - } catch (Exception ex) { - Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); - } - - } - public void sendUDP() { - try { - TestMessage msg = createMessage("UDP Unicast"); - InetAddress ip = InetAddress.getByName(ipField.getText()); - mainWindow.getSocketUDP().sendUnicast(msg, ip); - } catch (Exception ex) { - Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); - } - } - public void sendMulticast() { - try { - TestMessage msg = createMessage("UDP Multicast"); - mainWindow.getSocketUDP().sendMulticast(msg); - } catch (Exception ex) { - Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); - } - } - - private TestMessage createMessage(String tipoMensagem) { - try { - String nome = mainWindow.getUserName(); - String ip = InetAddress.getLocalHost().getHostAddress(); - String message = messageField.getText(); - return new TestMessage(nome, ip, tipoMensagem, message); - } catch (UnknownHostException e) { - throw new RuntimeException(e); - } - } - - public void receiveMessage(TestMessage msg) { - SwingUtilities.invokeLater(() -> { - String mensagem = String.format( - "[%s] %s (%s): %s%n", - msg.getTipoMensagem(), - msg.getNome(), - msg.getIp(), - msg.getMessage() - ); - - receivedMessagesArea.append(mensagem); - - receivedMessagesArea.setCaretPosition( - receivedMessagesArea.getDocument().getLength() - ); - }); - } -} diff --git a/pitiupi.testSocket/src/META-INF/services/pitiupi.plugin.Plugin b/pitiupi.testSocket/src/META-INF/services/pitiupi.plugin.Plugin index cdf4196..58a4118 100644 --- a/pitiupi.testSocket/src/META-INF/services/pitiupi.plugin.Plugin +++ b/pitiupi.testSocket/src/META-INF/services/pitiupi.plugin.Plugin @@ -1 +1 @@ -plugin.SocketTest +plugin.TestChat diff --git a/pitiupi.testSocket/src/control/ChatController.java b/pitiupi.testSocket/src/control/ChatController.java new file mode 100644 index 0000000..96a3361 --- /dev/null +++ b/pitiupi.testSocket/src/control/ChatController.java @@ -0,0 +1,62 @@ +package control; + +import GUI.ChatMenu; +import GUI.ChatWindow; +import GUI.PrivateChatWindow; +import GUI.PublicChatWindow; +import net.TestChatMessage; +import pitiupi.GUI.MainWindow; +import pitiupi.net.tcp.TcpConnection; + +import javax.swing.*; +import java.util.HashMap; + +public class ChatController { + MainWindow mainWindow; + ChatMenu chatMenu; + + PublicChatWindow publicChatWindow; + + private final HashMap chatMenus; + private final HashMap chatWindows; + + + public ChatController(MainWindow mainWindow, ChatMenu chatMenu) { + this.mainWindow = mainWindow; + publicChatWindow = new PublicChatWindow(mainWindow); + + chatMenus = new HashMap<>(); + chatWindows = new HashMap<>(); + + chatMenu.newMenuItem(new JMenuItem("Show global chat"), publicChatWindow); + } + + public void addChatWindow(TcpConnection tcpConnection) { + if (chatWindows.containsKey(tcpConnection)) return; + + PrivateChatWindow newChat = new PrivateChatWindow(mainWindow, tcpConnection); + chatWindows.put(tcpConnection, newChat); + + JMenuItem newMenuItem = new JMenuItem(); + chatMenus.put(tcpConnection, newMenuItem); + + newMenuItem.setText(tcpConnection.getAddress().toString()); + chatMenu.newMenuItem(newMenuItem, newChat); + } + + public void removeChatWindow(TcpConnection tcpConnection) { + ChatWindow removedWindow = chatWindows.remove(tcpConnection); + JMenuItem removedMenu = chatMenus.remove(tcpConnection); + + chatMenu.removeMenuItem(removedMenu); + removedWindow.setVisible(false); + removedWindow.dispose(); + } + + public void receivePublic(TestChatMessage message) { + publicChatWindow.receiveMessage(message); + } + public void receivePrivate(TcpConnection tcpConnection, TestChatMessage message) { + chatWindows.get(tcpConnection).receiveMessage(message); + } +} diff --git a/pitiupi.testSocket/src/net/TestMessage.java b/pitiupi.testSocket/src/net/TestChatMessage.java similarity index 76% rename from pitiupi.testSocket/src/net/TestMessage.java rename to pitiupi.testSocket/src/net/TestChatMessage.java index cb947d4..3b9a499 100644 --- a/pitiupi.testSocket/src/net/TestMessage.java +++ b/pitiupi.testSocket/src/net/TestChatMessage.java @@ -2,20 +2,20 @@ package net; import pitiupi.net.Message; -public class TestMessage extends Message { +public class TestChatMessage extends Message { private String nome; private String ip; private String tipoMensagem; private String message; - public TestMessage(String nome, String ip, String tipoMensagem, String message) { + public TestChatMessage(String nome, String ip, String tipoMensagem, String message) { this.nome = nome; this.ip = ip; this.tipoMensagem = tipoMensagem; this.message = message; } - public TestMessage(){} + public TestChatMessage(){} public String getNome() { return nome; diff --git a/pitiupi.testSocket/src/plugin/SocketTest.java b/pitiupi.testSocket/src/plugin/SocketTest.java deleted file mode 100644 index 145dbca..0000000 --- a/pitiupi.testSocket/src/plugin/SocketTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package plugin; - -import GUI.SocketTestMenu; -import GUI.SocketTestWindow; -import net.TestMessage; -import pitiupi.GUI.MainWindow; -import pitiupi.net.Message; -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 PrivateConnectionPlugin{ - SocketTestWindow socketTestWindow; - - @Override - public String getName() { - return "Test Socket"; - } - - @Override - public String getAuthor() { - return "Gustavo Henrique"; - } - - @Override - public String getVersion() { - return "0"; - } - - @Override - public String getIdentifier() { - return "Socket.Test.123456"; - } - - @Override - public void createPlugin(MainWindow mainWindow) { - this.socketTestWindow = new SocketTestWindow(mainWindow); - mainWindow.addMenu(new SocketTestMenu(socketTestWindow)); - } - - @Override - public Message getMessage() { - return new TestMessage(); - } - - @Override - public void receiveMessage(byte[] message) { - ByteArrayInputStream bis = new ByteArrayInputStream(message); - ObjectInputStream ois; - try { - ois = new ObjectInputStream(bis); - if (ois.readObject() instanceof TestMessage msg) { - this.socketTestWindow.receiveMessage(msg); - } - } catch (IOException | ClassNotFoundException ex) { - 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.testSocket/src/plugin/TestChat.java b/pitiupi.testSocket/src/plugin/TestChat.java new file mode 100644 index 0000000..de25242 --- /dev/null +++ b/pitiupi.testSocket/src/plugin/TestChat.java @@ -0,0 +1,100 @@ +package plugin; + +import GUI.ChatMenu; +import control.ChatController; +import net.TestChatMessage; +import pitiupi.GUI.MainWindow; +import pitiupi.net.Message; +import pitiupi.net.tcp.TcpConnection; +import pitiupi.plugin.PrivateConnectionPlugin; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.net.InetAddress; + +public class TestChat implements PrivateConnectionPlugin{ + ChatController chatController; + MainWindow mainWindow; + + @Override + public String getName() { + return "MultiProtocol chat"; + } + + @Override + public String getAuthor() { + return "Gustavo Henrique"; + } + + @Override + public String getVersion() { + return "0"; + } + + @Override + public String getIdentifier() { + return "Multi.Protocol.Chat.Pitiupi"; + } + + @Override + public void createPlugin(MainWindow mainWindow) { + this.mainWindow = mainWindow; + + ChatMenu chatMenu = new ChatMenu(this); + chatController = new ChatController(mainWindow, chatMenu); + mainWindow.addMenu(chatMenu); + } + + @Override + public Message getMessage() { + return new TestChatMessage(); + } + + @Override + public void receiveMessage(byte[] message) { + TestChatMessage testChatMessage = parseMessage(message); + chatController.receivePublic(testChatMessage); + } + + @Override + public void receivePrivateConnection(TcpConnection tcpConnection) { + chatController.addChatWindow(tcpConnection); + } + + @Override + public void receiveBytes(TcpConnection connection, byte[] bytes) { + TestChatMessage testChatMessage = parseMessage(bytes); + chatController.receivePrivate(connection, testChatMessage); + } + + @Override + public void onSocketClosed(TcpConnection tcpConnection) { + chatController.removeChatWindow(tcpConnection); + } + + public boolean openConnection(String ip) { + try { + mainWindow.getSocketTCP().createPrivateConnection(InetAddress.getByName(ip), getIdentifier(), getIdentifier()); + return true; + } + catch (Exception e) { + System.out.println("Error opening connection: " + e.getMessage()); + } + return false; + } + + private TestChatMessage parseMessage(byte[] bytes) { + ByteArrayInputStream bis = new ByteArrayInputStream(bytes); + ObjectInputStream ois; + try { + ois = new ObjectInputStream(bis); + if (ois.readObject() instanceof TestChatMessage msg) { + return msg; + } + } catch (IOException | ClassNotFoundException ex) { + System.out.println(ex.toString()); + } + return null; + } +} diff --git a/pitiupi/src/pitiupi/GUI/Login.java b/pitiupi/src/pitiupi/GUI/Login.java index 20b2a37..893161d 100644 --- a/pitiupi/src/pitiupi/GUI/Login.java +++ b/pitiupi/src/pitiupi/GUI/Login.java @@ -48,7 +48,6 @@ public final class Login extends JDialog { public void initComponents() { this.setTitle("Login"); - String nomeSalvo = prefs.get(PREF_NOME, System.getProperty("user.name")); int portaSalva = prefs.getInt(PREF_PORTA, 8000);