33 changed files with 463 additions and 237 deletions

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>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-40
View File
@@ -1,40 +0,0 @@
package GUI;
import pitiupi.models.Menus.MenuCategory;
import pitiupi.models.Menus.MulticastMenus.MulticastSubMenu;
import plugin.MultiProtocolChat;
import javax.swing.*;
import java.awt.*;
public class ChatMenu extends MulticastSubMenu {
private final MultiProtocolChat multiProtocolChat;
public ChatMenu(MultiProtocolChat multiProtocolChat) {
this.multiProtocolChat = multiProtocolChat;
this.setText("MP-Chat");
JMenuItem startPrivateChat = new JMenuItem("start Private Chat");
startPrivateChat.addActionListener(e -> showPrivateChatDialog());
this.add(startPrivateChat);
}
public void newMenuItem(JMenuItem newMenuItem, ChatWindow chatWindow) {
newMenuItem.addActionListener(e -> chatWindow.setVisible(true));
this.add(newMenuItem);
}
public void removeMenuItem(JMenuItem removeMenuItem) {
this.remove(removeMenuItem);
}
private void showPrivateChatDialog() {
}
@Override
public String getText() {
return "MP-Chat";
}
}
-94
View File
@@ -1,94 +0,0 @@
package GUI;
import net.MpChatMessage;
import pitiupi.control.ConnectionManager;
import javax.swing.*;
import java.awt.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
public abstract class ChatWindow extends JFrame {
protected final ConnectionManager connectionManager;
private JTextArea receivedMessagesArea;
protected JTextField messageField;
public ChatWindow(ConnectionManager connectionManager) {
this.connectionManager = connectionManager;
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 MpChatMessage createMessage(String tipoMensagem) {
try {
String nome = connectionManager.getUserName();
String ip = InetAddress.getLocalHost().getHostAddress();
String message = messageField.getText();
return new MpChatMessage(nome, ip, tipoMensagem, message);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
public void receiveMessage(MpChatMessage 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());
});
}
}
@@ -2,10 +2,10 @@ package GUI;
import pitiupi.models.Menus.MulticastMenus.MulticastActionMenu; import pitiupi.models.Menus.MulticastMenus.MulticastActionMenu;
public class PublicChatMenu extends MulticastActionMenu { public class MulticastMpChatMenu extends MulticastActionMenu {
PublicChatWindow publicChatWindow; PublicChatWindow publicChatWindow;
PublicChatMenu(PublicChatWindow publicChatWindow) { public MulticastMpChatMenu(PublicChatWindow publicChatWindow) {
this.publicChatWindow = publicChatWindow; this.publicChatWindow = publicChatWindow;
} }
@@ -14,8 +14,9 @@ public class PublicChatMenu extends MulticastActionMenu {
publicChatWindow.setVisible(true); publicChatWindow.setVisible(true);
} }
@Override @Override
public String getText() { public String getText() {
return ""; return "MP-Chat";
} }
} }
@@ -1,38 +0,0 @@
package GUI;
import control.ChatController;
import pitiupi.models.Menus.UnicastMenus.UnicastActionMenu;
import pitiupi.models.User;
import plugin.MultiProtocolChat;
import javax.swing.*;
import java.awt.*;
import java.net.InetAddress;
public class PrivateChatMenu extends UnicastActionMenu {
PrivateChatWindow privateChatWindow;
private final MultiProtocolChat multiProtocolChat;
private final ChatController chatController;
public PrivateChatMenu(PrivateChatWindow PrivateChatWindow, MultiProtocolChat multiProtocolChat, ChatController chatController) {
this.privateChatWindow = PrivateChatWindow;
this.multiProtocolChat = multiProtocolChat;
this.chatController = chatController;
}
@Override
public void onClick(User user) {
if (multiProtocolChat.openConnection(user.getAddress())
else errorLabel.setVisible(true);
dialog.setContentPane(contentPanel);
dialog.pack();
dialog.setLocationRelativeTo(SwingUtilities.getWindowAncestor(privateChatWindow));
dialog.setVisible(true);
}
@Override
public String getText() {
return "start Private Chat";
}
}
+94 -15
View File
@@ -1,43 +1,122 @@
package GUI; package GUI;
import net.MpChatMessage; import net.MpChatMessage;
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.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 TcpConnection connection; private final TcpConnection connection;
public PrivateChatWindow(ConnectionManager ConnectionManager, TcpConnection connection) { private final JTextArea receivedMessagesArea;
super(ConnectionManager); private final JTextField messageField;
this.connection = connection;
} public PrivateChatWindow(
Pitiupi pitiupi,
TcpConnection connection
) {
this.pitiupi = pitiupi;
this.connection = connection;
setTitle("Chat privado");
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)
);
receivedMessagesArea = new JTextArea();
receivedMessagesArea.setEditable(false);
receivedMessagesArea.setLineWrap(true);
receivedMessagesArea.setWrapStyleWord(true);
JScrollPane messagesScrollPane =
new JScrollPane(receivedMessagesArea);
messagesScrollPane.setBorder(
BorderFactory.createTitledBorder("Mensagens")
);
mainPanel.add(
messagesScrollPane,
BorderLayout.CENTER
);
JPanel bottomPanel = new JPanel(
new BorderLayout(10, 10)
);
messageField = new JTextField();
@Override
protected void initButtons(JPanel bottomPanel) {
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
);
buttonsPanel.add(tcpButton); bottomPanel.add(
tcpButton,
BorderLayout.EAST
);
bottomPanel.add(buttonsPanel, BorderLayout.EAST); mainPanel.add(
bottomPanel,
BorderLayout.SOUTH
);
setContentPane(mainPanel);
} }
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().getHostAddress(),
"TCP Private",
messageField.getText()
);
connection.send(message.toByteArray());
receiveMessage(message);
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()
);
});
}
} }
+141 -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,13 @@ 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);
receiveMessage(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);
} }
} }
@@ -73,10 +193,13 @@ 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);
receiveMessage(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);
} }
} }
@@ -84,10 +207,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);
} }
} }
} }
@@ -0,0 +1,26 @@
package GUI;
import control.PrivateChatController;
import pitiupi.models.Menus.UnicastMenus.UnicastActionMenu;
import pitiupi.models.User;
public class UnicastMpChatMenu extends UnicastActionMenu {
private final PrivateChatController controller;
public UnicastMpChatMenu(
PrivateChatController controller
) {
this.controller = controller;
}
@Override
public void onClick(User user) {
controller.openChat(user);
}
@Override
public String getText() {
return "Start Private Chat";
}
}
@@ -0,0 +1,116 @@
package control;
import GUI.PrivateChatWindow;
import net.MpChatMessage;
import pitiupi.models.User;
import pitiupi.net.tcp.TcpConnection;
import pitiupi.plugin.Pitiupi;
import plugin.MultiProtocolChat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class PrivateChatController {
private final Pitiupi pitiupi;
private final MultiProtocolChat multiProtocolChat;
/*
* Relaciona o endereço do usuário à janela de chat.
*/
private final Map<String, PrivateChatWindow> windows;
/*
* Endereços para os quais uma conexão já foi solicitada,
* mas ainda não chegou.
*/
private final Set<String> pendingConnections;
public PrivateChatController(
Pitiupi pitiupi,
MultiProtocolChat multiProtocolChat
) {
this.pitiupi = pitiupi;
this.multiProtocolChat = multiProtocolChat;
this.windows = new HashMap<>();
this.pendingConnections = new HashSet<>();
}
public void openChat(User user) {
String address = user.getAddress().getHostAddress();
PrivateChatWindow window = windows.get(address);
/*
* Já existe uma janela para esse usuário.
*/
if (window != null) {
window.setVisible(true);
window.toFront();
window.requestFocus();
return;
}
/*
* Já estamos esperando uma conexão para esse usuário.
*/
if (pendingConnections.contains(address)) {
return;
}
/*
* Ainda não existe conexão.
*/
pendingConnections.add(address);
multiProtocolChat.openConnection(address);
}
public void receiveMessage(TcpConnection connection, MpChatMessage message) {
String address = connection.getAddress().toString();
PrivateChatWindow window = windows.get(address);
if (window == null) {
return;
}
window.receiveMessage(message);
}
/**
* Deve ser chamado quando uma nova conexão privada chegar.
*/
public void connectionReceived(TcpConnection connection) {
String address = connection.getAddress().toString();
pendingConnections.remove(address);
PrivateChatWindow window = new PrivateChatWindow(
pitiupi,
connection
);
windows.put(address, window);
window.setVisible(true);
window.toFront();
window.requestFocus();
}
/**
* Fecha a janela e remove o relacionamento.
*/
public void closeChat(TcpConnection connection) {
String address = connection.getAddress().toString();
PrivateChatWindow window = windows.remove(address);
if (window != null) {
window.dispose();
}
}
}
@@ -1,24 +1,29 @@
package plugin; package plugin;
import GUI.ChatMenu; import GUI.MulticastMpChatMenu;
import control.ChatController; import GUI.PrivateChatWindow;
import GUI.PublicChatWindow;
import GUI.UnicastMpChatMenu;
import control.PrivateChatController;
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; PrivateChatController privateChatController;
PublicChatWindow publicChatWindow;
@Override @Override
public String getName() { public String getName() {
@@ -41,30 +46,37 @@ 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); privateChatController = new PrivateChatController(pitiupi, this);
chatController = new ChatController(connectionManager, chatMenu);
guiManager.addMulticastMenu(chatMenu); publicChatWindow = new PublicChatWindow(pitiupi);
MulticastMpChatMenu multicastMpChatMenu = new MulticastMpChatMenu(publicChatWindow);
UnicastMpChatMenu unicastMpChatMenu = new UnicastMpChatMenu(privateChatController);
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); privateChatController.connectionReceived(connection);
}); });
return true; return true;
} }
@@ -72,25 +84,29 @@ 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); if (mpChatMessage == null) {
System.out.println("Invalid message received");
return true;
}
else System.out.println(mpChatMessage.getMessage());
privateChatController.receiveMessage(connection, mpChatMessage);
return true; return true;
} }
@Override @Override
public boolean onSocketClosed(TcpConnection connection) { public boolean onSocketClosed(TcpConnection connection) {
chatController.removeChatWindow(connection); privateChatController.closeChat(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) {
+9
View File
@@ -0,0 +1,9 @@
<?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$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
-1
View File
@@ -111,7 +111,6 @@ public class MenuBar extends JMenuBar {
} }
public void addPluginMenu(MenuCategory category, JMenuItem menu){ public void addPluginMenu(MenuCategory category, JMenuItem menu){
System.out.println("addPluginMenu");
categories.get(category).add(menu); categories.get(category).add(menu);
} }
} }
@@ -61,8 +61,9 @@ public class PluginManager {
private Plugin getPlugin(String pluginId) { private Plugin getPlugin(String pluginId) {
for (LoadedPlugin loadedPlugin : loadedPlugins) { for (LoadedPlugin loadedPlugin : loadedPlugins) {
if (loadedPlugin.getPlugin().getIdentifier().equals(pluginId)) if (loadedPlugin.getPlugin().getIdentifier().equals(pluginId)) {
return loadedPlugin.getPlugin(); return loadedPlugin.getPlugin();
}
} }
return null; return null;
} }
@@ -80,7 +81,7 @@ public class PluginManager {
plugin.receivePrivateConnection(connection); plugin.receivePrivateConnection(connection);
return false; return true;
} }
public void handleDisconnection(TcpConnection connection, String receiverId) { public void handleDisconnection(TcpConnection connection, String receiverId) {
@@ -117,7 +118,6 @@ public class PluginManager {
ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class, classLoader); ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class, classLoader);
for (Plugin plugin : serviceLoader) { for (Plugin plugin : serviceLoader) {
System.out.println("Loading: " + plugin.getName());
LoadedPlugin loadedPlugin = new LoadedPlugin(plugin, classLoader); LoadedPlugin loadedPlugin = new LoadedPlugin(plugin, classLoader);
plugin.createPlugin(pitiupi); plugin.createPlugin(pitiupi);
+3 -2
View File
@@ -271,8 +271,9 @@ public class SocketTCP extends Thread {
return; return;
} }
if (pluginManager.handleConnection(connection, connection.getReceiver())) if (pluginManager.handleConnection(connection, connection.getReceiver())) {
privateConnectionReceiver(connection); privateConnectionReceiver(connection);
}
else { else {
InfoMessage.sendSystemInfo( InfoMessage.sendSystemInfo(
connection, connection,
@@ -336,7 +337,7 @@ public class SocketTCP extends Thread {
* ao plugin destinatário. * ao plugin destinatário.
* *
* <p>Dados que não representam mensagens de controle são entregues * <p>Dados que não representam mensagens de controle são entregues
* diretamente ao {@link PrivateConnectionPlugin}. Mensagens de controle * diretamente ao {@link pitiupi.models.Plugin}. Mensagens de controle
* relacionadas ao encerramento ou a erros internos provocam a notificação * relacionadas ao encerramento ou a erros internos provocam a notificação
* do plugin e o fechamento da conexão.</p> * do plugin e o fechamento da conexão.</p>
* *
@@ -6,6 +6,7 @@ import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException; import java.net.SocketException;
import java.util.Arrays;
/** /**
* Representa uma conexão TCP ativa com um peer remoto. * Representa uma conexão TCP ativa com um peer remoto.
@@ -77,6 +78,7 @@ public class TcpConnection {
byte[] message = input.readNBytes(length); byte[] message = input.readNBytes(length);
if (message.length != length) { if (message.length != length) {
throw new IOException("Connection closed before receiving the complete message."); throw new IOException("Connection closed before receiving the complete message.");
} }