Arquivos faltando no commit anterior

This commit is contained in:
GustavoHMDS committed 2026-10-07 17:13:03 -03:00
1 parent 88b75ad75f
commit 7a699cd280
9 files changed
+391 -56

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
+5
View File
@@ -0,0 +1,5 @@
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-26" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/pitiupi.mpChat.iml" filepath="$PROJECT_DIR$/pitiupi.mpChat.iml" />
</modules>
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
@@ -0,0 +1 @@
plugin.MultiProtocolChat
+172 -14
View File
@@ -3,41 +3,199 @@ package GUI;
import net.MpChatMessage; import net.MpChatMessage;
import pitiupi.control.ConnectionManager; import pitiupi.control.ConnectionManager;
import pitiupi.net.tcp.TcpConnection; import pitiupi.net.tcp.TcpConnection;
import pitiupi.plugin.Pitiupi;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class PrivateChatWindow extends ChatWindow { public class PrivateChatWindow extends JFrame {
private final Pitiupi pitiupi;
private final Map<TcpConnection, PrivateChatPanel> chats;
private final JPanel chatPanel;
private final CardLayout cardLayout;
private final DefaultListModel<TcpConnection> connectionListModel;
private final JList<TcpConnection> connectionList;
public PrivateChatWindow(Pitiupi pitiupi) {
this.pitiupi = pitiupi;
this.chats = new HashMap<>();
setTitle("Chats privados");
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));
connectionListModel = new DefaultListModel<>();
connectionList = new JList<>(connectionListModel);
connectionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
connectionList.setPreferredSize(new Dimension(180, 0));
connectionList.addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
TcpConnection connection = connectionList.getSelectedValue();
if (connection != null) {
showChat(connection);
}
}
});
JScrollPane connectionScrollPane = new JScrollPane(connectionList);
connectionScrollPane.setBorder(
BorderFactory.createTitledBorder("Usuários")
);
mainPanel.add(connectionScrollPane, BorderLayout.WEST);
cardLayout = new CardLayout();
chatPanel = new JPanel(cardLayout);
mainPanel.add(chatPanel, BorderLayout.CENTER);
setContentPane(mainPanel);
}
public void addConnection(TcpConnection connection) {
if (chats.containsKey(connection)) {
return;
}
PrivateChatPanel panel = new PrivateChatPanel(connection);
chats.put(connection, panel);
connectionListModel.addElement(connection);
String cardName = getCardName(connection);
chatPanel.add(panel, cardName);
if (connectionList.getSelectedValue() == null) {
connectionList.setSelectedValue(connection, true);
}
}
public void removeConnection(TcpConnection connection) {
PrivateChatPanel panel = chats.remove(connection);
if (panel == null) {
return;
}
chatPanel.remove(panel);
connectionListModel.removeElement(connection);
if (!connectionListModel.isEmpty()) {
connectionList.setSelectedIndex(0);
}
}
public void receiveMessage(TcpConnection connection, MpChatMessage message) {
PrivateChatPanel panel = chats.get(connection);
if (panel == null) {
return;
}
panel.receiveMessage(message);
}
private void showChat(TcpConnection connection) {
cardLayout.show(chatPanel, getCardName(connection));
}
private String getCardName(TcpConnection connection) {
return Integer.toHexString(System.identityHashCode(connection));
}
private class PrivateChatPanel extends JPanel {
private final TcpConnection connection; private final TcpConnection connection;
public PrivateChatWindow(ConnectionManager ConnectionManager, TcpConnection connection) { private final JTextArea receivedMessagesArea;
super(ConnectionManager); private final JTextField messageField;
public PrivateChatPanel(TcpConnection connection) {
this.connection = connection; this.connection = connection;
}
@Override setLayout(new BorderLayout(10, 10));
protected void initButtons(JPanel bottomPanel) {
receivedMessagesArea = new JTextArea();
receivedMessagesArea.setEditable(false);
receivedMessagesArea.setLineWrap(true);
receivedMessagesArea.setWrapStyleWord(true);
JScrollPane messagesScrollPane =
new JScrollPane(receivedMessagesArea);
messagesScrollPane.setBorder(
BorderFactory.createTitledBorder("Mensagens")
);
add(messagesScrollPane, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel(new BorderLayout(10, 10));
messageField = new JTextField();
JButton tcpButton = new JButton("TCP"); JButton tcpButton = new JButton("TCP");
tcpButton.addActionListener(e -> sendTCP()); tcpButton.addActionListener(e -> sendTCP());
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); bottomPanel.add(messageField, BorderLayout.CENTER);
bottomPanel.add(tcpButton, BorderLayout.EAST);
buttonsPanel.add(tcpButton); add(bottomPanel, BorderLayout.SOUTH);
bottomPanel.add(buttonsPanel, BorderLayout.EAST);
} }
private void sendTCP() { private void sendTCP() {
try { try {
MpChatMessage msg = createMessage("TCP Private"); String nome = pitiupi.getUserName();
connection.send(msg.toByteArray());
MpChatMessage message = new MpChatMessage(
nome,
InetAddress.getLocalHost().toString(),
"TCP Private",
messageField.getText()
);
connection.send(message.toByteArray());
messageField.setText("");
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(PrivateChatWindow.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(
PrivateChatWindow.class.getName()
).log(Level.SEVERE, null, ex);
}
}
public void receiveMessage(MpChatMessage message) {
SwingUtilities.invokeLater(() -> {
String texto = String.format(
"[%s] %s (%s): %s%n",
message.getTipoMensagem(),
message.getNome(),
message.getIp(),
message.getMessage()
);
receivedMessagesArea.append(texto);
receivedMessagesArea.setCaretPosition(
receivedMessagesArea.getDocument().getLength()
);
});
} }
} }
} }
+139 -16
View File
@@ -2,36 +2,95 @@ package GUI;
import net.MpChatMessage; import net.MpChatMessage;
import pitiupi.control.ConnectionManager; import pitiupi.control.ConnectionManager;
import pitiupi.plugin.Pitiupi;
import javax.swing.*; import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*; import java.awt.*;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class PublicChatWindow extends ChatWindow { public class PublicChatWindow extends JFrame {
private final Pitiupi pitiupi;
private JTextPane receivedMessagesArea;
private JTextField messageField;
private JTextField ipField; private JTextField ipField;
public PublicChatWindow(ConnectionManager connectionManager) { public PublicChatWindow(Pitiupi pitiupi) {
super(connectionManager); this.pitiupi = pitiupi;
initComponents();
} }
@Override private void initComponents() {
protected void initButtons(JPanel bottomPanel) { setTitle("Public Chat");
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 JTextPane();
receivedMessagesArea.setEditable(false);
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);
initIpField(bottomPanel); initIpField(bottomPanel);
initCommunicationButtons(bottomPanel); initCommunicationButtons(bottomPanel);
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
} }
private void initIpField(JPanel bottomPanel) { private void initIpField(JPanel bottomPanel) {
ipField = new JTextField(); ipField = new JTextField();
ipField.setPreferredSize(new Dimension(130, 30)); ipField.setPreferredSize(new Dimension(130, 30));
JPanel ipPanel = new JPanel(new BorderLayout(5, 5)); JPanel ipPanel = new JPanel(
new BorderLayout(5, 5)
);
ipPanel.add(new JLabel("IP destino:"), BorderLayout.WEST); ipPanel.add(
new JLabel("IP destino:"),
BorderLayout.WEST
);
ipPanel.add(ipField, BorderLayout.CENTER); ipPanel.add(
ipField,
BorderLayout.CENTER
);
bottomPanel.add(ipPanel, BorderLayout.WEST); bottomPanel.add(ipPanel, BorderLayout.WEST);
} }
@@ -45,13 +104,71 @@ public class PublicChatWindow extends ChatWindow {
udpButton.addActionListener(e -> sendUDP()); udpButton.addActionListener(e -> sendUDP());
multicastButton.addActionListener(e -> sendMulticast()); multicastButton.addActionListener(e -> sendMulticast());
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); JPanel buttonsPanel = new JPanel(
new FlowLayout(FlowLayout.RIGHT, 5, 0)
);
buttonsPanel.add(tcpButton); buttonsPanel.add(tcpButton);
buttonsPanel.add(udpButton); buttonsPanel.add(udpButton);
buttonsPanel.add(multicastButton); buttonsPanel.add(multicastButton);
bottomPanel.add(buttonsPanel, BorderLayout.EAST); bottomPanel.add(
buttonsPanel,
BorderLayout.EAST
);
}
private MpChatMessage createMessage(String tipoMensagem) {
try {
String nome = pitiupi.getUserName();
String ip = InetAddress.getLocalHost().getHostAddress();
String message = messageField.getText();
return new MpChatMessage(
nome,
ip,
tipoMensagem,
message
);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public void receiveMessage(MpChatMessage msg) {
SwingUtilities.invokeLater(() -> {
String mensagem = String.format(
"[%s] %s (%s): %s%n",
msg.getTipoMensagem(),
msg.getNome(),
msg.getIp(),
msg.getMessage()
);
StyledDocument document = receivedMessagesArea.getStyledDocument();
SimpleAttributeSet attributes = new SimpleAttributeSet();
if (msg.getTipoMensagem().equals("TCP Unicast") || msg.getTipoMensagem().equals("UDP Unicast")) {
StyleConstants.setForeground(attributes, new Color(128, 0, 128));
}
try {
document.insertString(
document.getLength(),
mensagem,
attributes
);
receivedMessagesArea.setCaretPosition(
document.getLength()
);
} catch (BadLocationException ex) {
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex);
}
});
} }
private void sendTCP() { private void sendTCP() {
@@ -60,10 +177,12 @@ public class PublicChatWindow extends ChatWindow {
InetAddress ip = InetAddress.getByName(ipField.getText()); InetAddress ip = InetAddress.getByName(ipField.getText());
connectionManager.sendTcp(msg, ip); pitiupi.sendTcp(msg, ip);
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(
PublicChatWindow.class.getName()
).log(Level.SEVERE, null, ex);
} }
} }
@@ -73,10 +192,12 @@ public class PublicChatWindow extends ChatWindow {
InetAddress ip = InetAddress.getByName(ipField.getText()); InetAddress ip = InetAddress.getByName(ipField.getText());
connectionManager.sendUdpUnicast(msg, ip); pitiupi.sendUdpUnicast(msg, ip);
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(
PublicChatWindow.class.getName()
).log(Level.SEVERE, null, ex);
} }
} }
@@ -84,10 +205,12 @@ public class PublicChatWindow extends ChatWindow {
try { try {
MpChatMessage msg = createMessage("UDP Multicast"); MpChatMessage msg = createMessage("UDP Multicast");
connectionManager.sendUdpMulticast(msg); pitiupi.sendUdpMulticast(msg);
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(
PublicChatWindow.class.getName()
).log(Level.SEVERE, null, ex);
} }
} }
} }
+22 -1
View File
@@ -1,4 +1,25 @@
package GUI; package GUI;
public class UnicastMpChatMenu { import pitiupi.models.Menus.UnicastMenus.UnicastActionMenu;
import pitiupi.models.User;
import plugin.MultiProtocolChat;
import javax.swing.*;
public class UnicastMpChatMenu extends UnicastActionMenu {
private final MultiProtocolChat multiProtocolChat;
public UnicastMpChatMenu(MultiProtocolChat multiProtocolChat) {
this.multiProtocolChat = multiProtocolChat;
}
@Override
public void onClick(User user) {
multiProtocolChat.openConnection(user.getAddress().toString());
}
@Override
public String getText() {
return "start Private Chat";
}
} }
@@ -1,24 +1,28 @@
package plugin; package plugin;
import GUI.ChatMenu; import GUI.MulticastMpChatMenu;
import control.ChatController; import GUI.PrivateChatWindow;
import GUI.PublicChatWindow;
import GUI.UnicastMpChatMenu;
import net.MpChatMessage; import net.MpChatMessage;
import pitiupi.control.ConnectionManager; import pitiupi.models.Menus.MenuCategory;
import pitiupi.control.GuiManager;
import pitiupi.models.Messages.Message; import pitiupi.models.Messages.Message;
import pitiupi.net.tcp.TcpConnection; import pitiupi.net.tcp.TcpConnection;
import pitiupi.models.Plugin; import pitiupi.models.Plugin;
import pitiupi.plugin.Pitiupi;
import javax.swing.*; import javax.swing.*;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.List;
public class MultiProtocolChat extends Plugin { public class MultiProtocolChat extends Plugin {
ChatController chatController; Pitiupi pitiupi;
ConnectionManager connectionManager;
GuiManager guiManager; PublicChatWindow publicChatWindow;
PrivateChatWindow privateChatWindow;
@Override @Override
public String getName() { public String getName() {
@@ -41,30 +45,36 @@ public class MultiProtocolChat extends Plugin {
} }
@Override @Override
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager) { public void createPlugin(Pitiupi pitiupi) {
this.connectionManager = connectionManager; this.pitiupi = pitiupi;
this.guiManager = guiManager;
ChatMenu chatMenu = new ChatMenu(this); publicChatWindow = new PublicChatWindow(pitiupi);
chatController = new ChatController(connectionManager, chatMenu); privateChatWindow = new PrivateChatWindow(pitiupi);
guiManager.addMulticastMenu(chatMenu);
MulticastMpChatMenu multicastMpChatMenu = new MulticastMpChatMenu(publicChatWindow);
UnicastMpChatMenu unicastMpChatMenu = new UnicastMpChatMenu(this);
pitiupi.createMenu(MenuCategory.Social, multicastMpChatMenu);
pitiupi.createMenu(MenuCategory.Social, unicastMpChatMenu);
} }
@Override @Override
public Message getMessage() { public List<Class<? extends Message>> getMessages() {
return new MpChatMessage(); return List.of(MpChatMessage.class);
} }
@Override @Override
public void receiveMessage(byte[] message) { public void receiveMessage(Message message) {
MpChatMessage mpChatMessage = parseMessage(message); if (message instanceof MpChatMessage mpChatMessage) {
chatController.receivePublic(mpChatMessage); publicChatWindow.receiveMessage(mpChatMessage);
} }
}
@Override @Override
public boolean receivePrivateConnection(TcpConnection connection) { public boolean receivePrivateConnection(TcpConnection connection) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
chatController.addChatWindow(connection); privateChatWindow.addConnection(connection);
}); });
return true; return true;
} }
@@ -72,25 +82,23 @@ public class MultiProtocolChat extends Plugin {
@Override @Override
public boolean receiveBytes(TcpConnection connection, byte[] bytes) { public boolean receiveBytes(TcpConnection connection, byte[] bytes) {
MpChatMessage mpChatMessage = parseMessage(bytes); MpChatMessage mpChatMessage = parseMessage(bytes);
chatController.receivePrivate(connection, mpChatMessage); privateChatWindow.receiveMessage(connection, mpChatMessage);
return true; return true;
} }
@Override @Override
public boolean onSocketClosed(TcpConnection connection) { public boolean onSocketClosed(TcpConnection connection) {
chatController.removeChatWindow(connection); privateChatWindow.removeConnection(connection);
return true; return true;
} }
public boolean openConnection(String ip) { public void openConnection(String ip) {
try { try {
connectionManager.connectPluginTcp(InetAddress.getByName(ip), getIdentifier(), getIdentifier()); pitiupi.connectTcp(InetAddress.getByName(ip), getIdentifier(), getIdentifier());
return true;
} }
catch (Exception e) { catch (Exception e) {
System.out.println("Error opening connection: " + e.getMessage()); System.out.println("Error opening connection: " + e.getMessage());
} }
return false;
} }
private MpChatMessage parseMessage(byte[] bytes) { private MpChatMessage parseMessage(byte[] bytes) {