Compare commits
26
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f7652201c | ||
|
|
cb8e9c9bd1 | ||
|
|
c2bf3f5a9d | ||
|
|
350a2ad301 | ||
|
|
1d9faa93f8 | ||
|
|
e5eb4f3f9c | ||
|
|
581a924afc | ||
|
|
7a699cd280 | ||
|
|
88b75ad75f | ||
|
|
d551324e8b | ||
|
|
9b45d515ba | ||
|
|
2e50dacee7 | ||
|
|
a628490fcb | ||
|
|
395363e4bd | ||
|
|
12497594e4 | ||
|
|
6a597092fe | ||
|
|
ffd474e7ff | ||
|
|
4d39648ee7 | ||
|
|
095af14463 | ||
|
|
d0083e8069 | ||
|
|
dda324228f | ||
|
|
840183ee9f | ||
|
|
eb3ce132cd | ||
|
|
d1dc427185 | ||
|
|
7c9e61ce0f | ||
|
|
6538dac8af |
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
#Tue, 29 Sep 2026 11:11:06 -0300
|
#Fri, 02 Oct 2026 20:11:21 -0300
|
||||||
|
|
||||||
|
|
||||||
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\pitiupi\\pitiupi.chat=
|
C\:\\Users\\extre\\OneDrive\\Documentos\\Faculdade\\2026_02\\Monitoria\\Pitiupi\\pitiupi.chat=
|
||||||
|
|
||||||
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\pitiupi\\pitiupi=
|
C\:\\Users\\extre\\OneDrive\\Documentos\\Faculdade\\2026_02\\Monitoria\\Pitiupi\\pitiupi=
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
@@ -4,28 +4,29 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.chat.GUI;
|
package pitiupi.chat.GUI;
|
||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import pitiupi.models.Menus.MulticastMenus.MulticastMenu;
|
||||||
import javax.swing.JMenu;
|
import pitiupi.models.Menus.MulticastMenus.MulticastSubMenu;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
*/
|
*/
|
||||||
public class ChatMenu extends JMenu {
|
public class ChatMenu extends MulticastSubMenu {
|
||||||
|
ChatMulticastMenu chatMulticastMenu;
|
||||||
private final ChatWindow chatWindow;
|
|
||||||
private javax.swing.JMenuItem menuItem;
|
|
||||||
|
|
||||||
public ChatMenu(ChatWindow chatWindow) {
|
public ChatMenu(ChatWindow chatWindow) {
|
||||||
this.chatWindow = chatWindow;
|
this.chatMulticastMenu = new ChatMulticastMenu(chatWindow);
|
||||||
this.setText("Chat");
|
|
||||||
menuItem = new javax.swing.JMenuItem();
|
|
||||||
menuItem.setText("Show Chat Window");
|
|
||||||
menuItem.addActionListener(this::chatMenuActionPerformed);
|
|
||||||
this.add(menuItem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void chatMenuActionPerformed(ActionEvent ae) {
|
@Override
|
||||||
this.chatWindow.setVisible(true);
|
public String getText() {
|
||||||
|
return "Chat";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<MulticastMenu> getMenus() {
|
||||||
|
return List.of(chatMulticastMenu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package pitiupi.chat.GUI;
|
||||||
|
|
||||||
|
import pitiupi.models.Menus.MulticastMenus.MulticastActionMenu;;
|
||||||
|
|
||||||
|
public class ChatMulticastMenu extends MulticastActionMenu {
|
||||||
|
ChatWindow chatWindow;
|
||||||
|
|
||||||
|
public ChatMulticastMenu(ChatWindow chatWindow) {
|
||||||
|
this.chatWindow = chatWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getText() {
|
||||||
|
return "Show Chat Window";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick() {
|
||||||
|
chatWindow.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,30 +5,28 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.chat.GUI;
|
package pitiupi.chat.GUI;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
import pitiupi.chat.net.ChatMessage;
|
import pitiupi.chat.net.ChatMessage;
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
*/
|
*/
|
||||||
public class ChatWindow extends javax.swing.JDialog {
|
public class ChatWindow extends JDialog {
|
||||||
|
|
||||||
private javax.swing.JButton jButton1;
|
private JButton jButton1;
|
||||||
private javax.swing.JEditorPane jEditorPane1;
|
private JEditorPane jEditorPane1;
|
||||||
private javax.swing.JPanel jPanel1;
|
private JPanel jPanel1;
|
||||||
private javax.swing.JPanel jPanel2;
|
private JPanel jPanel2;
|
||||||
private javax.swing.JScrollPane jScrollPane1;
|
private JScrollPane jScrollPane1;
|
||||||
private javax.swing.JScrollPane jScrollPane2;
|
private JScrollPane jScrollPane2;
|
||||||
private javax.swing.JSplitPane jSplitPane1;
|
private JSplitPane jSplitPane1;
|
||||||
private javax.swing.JTextArea jTextArea1;
|
private JTextArea jTextArea1;
|
||||||
|
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
@@ -44,23 +42,23 @@ public class ChatWindow extends javax.swing.JDialog {
|
|||||||
*/
|
*/
|
||||||
private void initComponents() {
|
private void initComponents() {
|
||||||
|
|
||||||
this.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
|
this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
|
||||||
this.setTitle("Chat Window");
|
this.setTitle("Chat Window");
|
||||||
this.setBounds(new java.awt.Rectangle(0, 0, 800, 600));
|
this.setBounds(new java.awt.Rectangle(0, 0, 800, 600));
|
||||||
this.getContentPane().setLayout(new java.awt.BorderLayout(5, 5));
|
this.getContentPane().setLayout(new java.awt.BorderLayout(5, 5));
|
||||||
this.setPreferredSize(new java.awt.Dimension(800, 600));
|
this.setPreferredSize(new java.awt.Dimension(800, 600));
|
||||||
|
|
||||||
jSplitPane1 = new javax.swing.JSplitPane();
|
jSplitPane1 = new JSplitPane();
|
||||||
jScrollPane1 = new javax.swing.JScrollPane();
|
jScrollPane1 = new JScrollPane();
|
||||||
jEditorPane1 = new javax.swing.JEditorPane();
|
jEditorPane1 = new JEditorPane();
|
||||||
jPanel1 = new javax.swing.JPanel();
|
jPanel1 = new JPanel();
|
||||||
jScrollPane2 = new javax.swing.JScrollPane();
|
jScrollPane2 = new JScrollPane();
|
||||||
jTextArea1 = new javax.swing.JTextArea();
|
jTextArea1 = new JTextArea();
|
||||||
jPanel2 = new javax.swing.JPanel();
|
jPanel2 = new JPanel();
|
||||||
jButton1 = new javax.swing.JButton();
|
jButton1 = new JButton();
|
||||||
|
|
||||||
jSplitPane1.setDividerLocation(500);
|
jSplitPane1.setDividerLocation(500);
|
||||||
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
|
jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
|
||||||
jEditorPane1.setContentType("text/html");
|
jEditorPane1.setContentType("text/html");
|
||||||
jPanel1.setLayout(new java.awt.BorderLayout());
|
jPanel1.setLayout(new java.awt.BorderLayout());
|
||||||
jTextArea1.setColumns(20);
|
jTextArea1.setColumns(20);
|
||||||
|
|||||||
@@ -5,14 +5,14 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.chat.net;
|
package pitiupi.chat.net;
|
||||||
|
|
||||||
import pitiupi.net.Message;
|
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
*/
|
*/
|
||||||
public class ChatMessage extends Message{
|
public class ChatMessage extends Message {
|
||||||
|
|
||||||
private String text;
|
private String text;
|
||||||
private String name;
|
private String name;
|
||||||
|
|||||||
@@ -4,46 +4,43 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.chat.plugin;
|
package pitiupi.chat.plugin;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.util.ArrayList;
|
||||||
import java.io.IOException;
|
import java.util.Collection;
|
||||||
import java.io.ObjectInputStream;
|
import java.util.List;
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
import pitiupi.chat.GUI.ChatMenu;
|
import pitiupi.chat.GUI.ChatMenu;
|
||||||
import pitiupi.chat.GUI.ChatWindow;
|
import pitiupi.chat.GUI.ChatWindow;
|
||||||
import pitiupi.chat.net.ChatMessage;
|
import pitiupi.chat.net.ChatMessage;
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.control.GuiManager;
|
import pitiupi.control.GuiManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Menus.MenuCategory;
|
||||||
import pitiupi.plugin.Plugin;
|
import pitiupi.models.Messages.Message;
|
||||||
|
import pitiupi.models.Plugin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
*/
|
*/
|
||||||
public class ChatPlugin implements Plugin {
|
public class ChatPlugin extends Plugin {
|
||||||
|
|
||||||
private ChatWindow chatWindow;
|
private ChatWindow chatWindow;
|
||||||
|
|
||||||
public ChatPlugin() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager) {
|
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
this.chatWindow = new ChatWindow(connectionManager);
|
this.chatWindow = new ChatWindow(connectionManager);
|
||||||
guiManager.addMenu(new ChatMenu(chatWindow));
|
guiManager.createMenu(MenuCategory.Social, new ChatMenu(chatWindow));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void receiveMessage(byte[] message) {
|
public List<Class<? extends Message>> getMessages() {
|
||||||
ByteArrayInputStream bis = new ByteArrayInputStream(message);
|
return List.of(ChatMessage.class);
|
||||||
ObjectInputStream ois;
|
}
|
||||||
try {
|
|
||||||
ois = new ObjectInputStream(bis);
|
|
||||||
if (ois.readObject() instanceof ChatMessage msg) {
|
@Override
|
||||||
this.chatWindow.receiveChatMessage(msg);
|
public void receiveMessage(Message message) {
|
||||||
}
|
if (message instanceof ChatMessage msg) {
|
||||||
} catch (IOException | ClassNotFoundException ex) {
|
this.chatWindow.receiveChatMessage(msg);
|
||||||
System.out.println(ex.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,8 +64,5 @@ public class ChatPlugin implements Plugin {
|
|||||||
return "Pitiupi.Chat.Example";
|
return "Pitiupi.Chat.Example";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Message getMessage() {
|
|
||||||
return new ChatMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Generated
+5
@@ -0,0 +1,5 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
Generated
+5
@@ -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>
|
||||||
Generated
+8
@@ -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>
|
||||||
Generated
+6
@@ -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.
File renamed without changes.
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.
@@ -1,81 +0,0 @@
|
|||||||
package GUI;
|
|
||||||
|
|
||||||
import plugin.MultiProtocolChat;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class ChatMenu extends JMenu {
|
|
||||||
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() {
|
|
||||||
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 (multiProtocolChat.openConnection(ip)) dialog.dispose();
|
|
||||||
else errorLabel.setVisible(true);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
dialog.setContentPane(contentPanel);
|
|
||||||
dialog.pack();
|
|
||||||
dialog.setLocationRelativeTo(SwingUtilities.getWindowAncestor(this));
|
|
||||||
dialog.setVisible(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package GUI;
|
||||||
|
|
||||||
|
import pitiupi.models.Menus.MulticastMenus.MulticastActionMenu;
|
||||||
|
|
||||||
|
public class MulticastMpChatMenu extends MulticastActionMenu {
|
||||||
|
PublicChatWindow publicChatWindow;
|
||||||
|
|
||||||
|
public MulticastMpChatMenu(PublicChatWindow publicChatWindow) {
|
||||||
|
this.publicChatWindow = publicChatWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick() {
|
||||||
|
publicChatWindow.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getText() {
|
||||||
|
return "MP-Chat";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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";
|
||||||
|
}
|
||||||
|
}
|
||||||
File renamed without changes.
@@ -6,18 +6,21 @@ import GUI.PrivateChatWindow;
|
|||||||
import GUI.PublicChatWindow;
|
import GUI.PublicChatWindow;
|
||||||
import net.MpChatMessage;
|
import net.MpChatMessage;
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.control.PeerListener;
|
||||||
|
import pitiupi.models.User;
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.net.InetAddress;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ChatController {
|
public class ChatController implements PeerListener {
|
||||||
ConnectionManager connectionManager;
|
ConnectionManager connectionManager;
|
||||||
ChatMenu chatMenu;
|
ChatMenu chatMenu;
|
||||||
|
|
||||||
PublicChatWindow publicChatWindow;
|
PublicChatWindow publicChatWindow;
|
||||||
|
|
||||||
private final HashMap<TcpConnection, JMenuItem> chatMenus;
|
|
||||||
private final HashMap<TcpConnection, ChatWindow> chatWindows;
|
private final HashMap<TcpConnection, ChatWindow> chatWindows;
|
||||||
|
|
||||||
public ChatController(ConnectionManager connectionManager, ChatMenu chatMenu) {
|
public ChatController(ConnectionManager connectionManager, ChatMenu chatMenu) {
|
||||||
@@ -58,4 +61,13 @@ public class ChatController {
|
|||||||
public void receivePrivate(TcpConnection tcpConnection, MpChatMessage message) {
|
public void receivePrivate(TcpConnection tcpConnection, MpChatMessage message) {
|
||||||
chatWindows.get(tcpConnection).receiveMessage(message);
|
chatWindows.get(tcpConnection).receiveMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void openChatWindow(InetAddress address) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPeersChanged(List<User> list) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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,6 +1,6 @@
|
|||||||
package net;
|
package net;
|
||||||
|
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
|
|
||||||
public class MpChatMessage extends Message {
|
public class MpChatMessage extends Message {
|
||||||
private String nome;
|
private String nome;
|
||||||
|
|||||||
@@ -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.net.Message;
|
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
import pitiupi.plugin.PrivateConnectionPlugin;
|
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 implements PrivateConnectionPlugin{
|
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,54 +46,67 @@ public class MultiProtocolChat implements PrivateConnectionPlugin{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@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.addMenu(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 void receivePrivateConnection(TcpConnection tcpConnection) {
|
public boolean receivePrivateConnection(TcpConnection connection) {
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
chatController.addChatWindow(tcpConnection);
|
privateChatController.connectionReceived(connection);
|
||||||
});
|
});
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSocketClosed(TcpConnection tcpConnection) {
|
public boolean onSocketClosed(TcpConnection connection) {
|
||||||
chatController.removeChatWindow(tcpConnection);
|
privateChatController.closeChat(connection);
|
||||||
|
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) {
|
||||||
|
|||||||
Generated
+1
-3
@@ -2,9 +2,7 @@
|
|||||||
<module type="JAVA_MODULE" version="4">
|
<module type="JAVA_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
|
||||||
</content>
|
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
File renamed without changes.
Generated
-1
@@ -1,4 +1,3 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_26_PREVIEW" project-jdk-name="openjdk-26" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_26_PREVIEW" project-jdk-name="openjdk-26" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
|||||||
@@ -468,10 +468,10 @@ MeusPlugins
|
|||||||
|
|
|
|
||||||
+---services
|
+---services
|
||||||
|
|
|
|
||||||
+---pitiupi.plugin.Plugin
|
+---pitiupi.models.Plugin
|
||||||
```
|
```
|
||||||
|
|
||||||
O arquivo `pitiupi.plugin.Plugin` deve conter o nome completo das classes que
|
O arquivo `pitiupi.models.Plugin` deve conter o nome completo das classes que
|
||||||
implementam a interface Plugin:
|
implementam a interface Plugin:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ package pitiupi.GUI;
|
|||||||
|
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.control.GuiManager;
|
import pitiupi.control.GuiManager;
|
||||||
import pitiupi.control.PluginManager;
|
import pitiupi.control.plugins.PluginManager;
|
||||||
|
|
||||||
import javax.swing.JMenu;
|
import javax.swing.*;
|
||||||
|
|
||||||
import pitiupi.control.HeartbeatManager;
|
import java.awt.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Janela principal da aplicação.
|
* Janela principal da aplicação.
|
||||||
@@ -27,16 +27,16 @@ import pitiupi.control.HeartbeatManager;
|
|||||||
* @author tony
|
* @author tony
|
||||||
* @author Gustavo
|
* @author Gustavo
|
||||||
*/
|
*/
|
||||||
public class MainWindow extends javax.swing.JFrame {
|
public class MainWindow extends JFrame {
|
||||||
|
|
||||||
private MenuBar menuBar;
|
private MenuBar menuBar;
|
||||||
private javax.swing.JLabel status;
|
private JLabel status;
|
||||||
|
OnlineUsersPanel onlineUsersPanel;
|
||||||
|
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
private final PluginManager pluginManager;
|
private final PluginManager pluginManager;
|
||||||
private final GuiManager guiManager;
|
private final GuiManager guiManager;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cria a janela principal da aplicação.
|
* Cria a janela principal da aplicação.
|
||||||
*
|
*
|
||||||
@@ -46,14 +46,14 @@ public class MainWindow extends javax.swing.JFrame {
|
|||||||
public MainWindow(String userName, int port) {
|
public MainWindow(String userName, int port) {
|
||||||
initMenu();
|
initMenu();
|
||||||
|
|
||||||
pluginManager = new PluginManager(this);
|
pluginManager = new PluginManager();
|
||||||
|
|
||||||
connectionManager = new ConnectionManager(this, userName, pluginManager, port);
|
connectionManager = new ConnectionManager(this, userName, pluginManager, port);
|
||||||
guiManager = new GuiManager(this);
|
|
||||||
|
|
||||||
initUserPanel();
|
initUserPanel();
|
||||||
initStatusPanel();
|
initStatusPanel();
|
||||||
|
|
||||||
|
guiManager = new GuiManager(this, menuBar, onlineUsersPanel);
|
||||||
|
|
||||||
loadPlugins();
|
loadPlugins();
|
||||||
|
|
||||||
this.pack();
|
this.pack();
|
||||||
@@ -66,7 +66,7 @@ public class MainWindow extends javax.swing.JFrame {
|
|||||||
|
|
||||||
this.setJMenuBar(menuBar);
|
this.setJMenuBar(menuBar);
|
||||||
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
||||||
this.setTitle("Share My Sheet");
|
this.setTitle("Pitiupi");
|
||||||
|
|
||||||
javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
|
javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
|
||||||
javax.swing.JTabbedPane tabbedPane = new javax.swing.JTabbedPane();
|
javax.swing.JTabbedPane tabbedPane = new javax.swing.JTabbedPane();
|
||||||
@@ -77,20 +77,20 @@ public class MainWindow extends javax.swing.JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initUserPanel() {
|
private void initUserPanel() {
|
||||||
OnlineUsersPanel onlineUsersPanel = new OnlineUsersPanel();
|
onlineUsersPanel = new OnlineUsersPanel();
|
||||||
this.connectionManager.addPeerListener(onlineUsersPanel);
|
this.connectionManager.addPeerListener(onlineUsersPanel);
|
||||||
this.getContentPane().add(onlineUsersPanel, java.awt.BorderLayout.LINE_END);
|
this.getContentPane().add(onlineUsersPanel, java.awt.BorderLayout.LINE_END);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initStatusPanel() {
|
private void initStatusPanel() {
|
||||||
javax.swing.JPanel statusPanel = new javax.swing.JPanel();
|
JPanel statusPanel = new JPanel();
|
||||||
status = new javax.swing.JLabel();
|
status = new JLabel();
|
||||||
|
|
||||||
statusPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
|
statusPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
||||||
updateStatus();
|
updateStatus();
|
||||||
statusPanel.add(status);
|
statusPanel.add(status);
|
||||||
|
|
||||||
this.getContentPane().add(statusPanel, java.awt.BorderLayout.PAGE_END);
|
this.getContentPane().add(statusPanel, BorderLayout.PAGE_END);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void exit() {
|
public void exit() {
|
||||||
@@ -107,9 +107,6 @@ public class MainWindow extends javax.swing.JFrame {
|
|||||||
pluginManager.loadPlugins(connectionManager, guiManager);
|
pluginManager.loadPlugins(connectionManager, guiManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMenu(JMenu menu) {
|
|
||||||
this.menuBar.addPlugin(menu);
|
|
||||||
}
|
|
||||||
public void setUserName(String userName) {
|
public void setUserName(String userName) {
|
||||||
connectionManager.setUserName(userName);
|
connectionManager.setUserName(userName);
|
||||||
updateStatus();
|
updateStatus();
|
||||||
|
|||||||
@@ -5,8 +5,14 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.GUI;
|
package pitiupi.GUI;
|
||||||
|
|
||||||
import javax.swing.JMenu;
|
import pitiupi.models.Menus.MenuCategory;
|
||||||
import javax.swing.JMenuBar;
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.InputEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Barra de menus principal da aplicação.
|
* Barra de menus principal da aplicação.
|
||||||
@@ -21,12 +27,14 @@ import javax.swing.JMenuBar;
|
|||||||
*/
|
*/
|
||||||
public class MenuBar extends JMenuBar {
|
public class MenuBar extends JMenuBar {
|
||||||
|
|
||||||
private javax.swing.JMenuItem setupMenu;
|
private JMenuItem preferencesMenu;
|
||||||
private javax.swing.JMenuItem aboutMenu;
|
private JMenuItem aboutMenu;
|
||||||
private javax.swing.JMenu fileMenu;
|
private JMenu fileMenu;
|
||||||
private javax.swing.JMenu helpMenu;
|
private JMenu helpMenu;
|
||||||
private javax.swing.JMenu pluginMenu;
|
private JMenu pluginMenu;
|
||||||
private javax.swing.JMenuItem exitMenu;
|
private Map<MenuCategory, JMenu> categories;
|
||||||
|
|
||||||
|
private JMenuItem exitMenu;
|
||||||
private final MainWindow mainWindow;
|
private final MainWindow mainWindow;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,42 +48,54 @@ public class MenuBar extends JMenuBar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initComponents() {
|
private void initComponents() {
|
||||||
fileMenu = new javax.swing.JMenu();
|
initFileMenu();
|
||||||
exitMenu = new javax.swing.JMenuItem();
|
initPluginMenu();
|
||||||
helpMenu = new javax.swing.JMenu();
|
initHelpMenu();
|
||||||
pluginMenu = new javax.swing.JMenu();
|
}
|
||||||
setupMenu = new javax.swing.JMenuItem();
|
|
||||||
aboutMenu = new javax.swing.JMenuItem();
|
|
||||||
|
|
||||||
fileMenu.setText("File");
|
private void initFileMenu() {
|
||||||
exitMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
|
fileMenu = new JMenu("File");
|
||||||
exitMenu.setText("Exit");
|
exitMenu = new JMenuItem("Exit");
|
||||||
|
|
||||||
|
exitMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_DOWN_MASK));
|
||||||
exitMenu.addActionListener(this::exitMenuActionPerformed);
|
exitMenu.addActionListener(this::exitMenuActionPerformed);
|
||||||
fileMenu.add(exitMenu);
|
fileMenu.add(exitMenu);
|
||||||
this.add(fileMenu);
|
this.add(fileMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initPluginMenu() {
|
||||||
|
pluginMenu = new JMenu("Plugin");
|
||||||
|
categories = new HashMap<>();
|
||||||
|
|
||||||
|
for (MenuCategory category : MenuCategory.values()) {
|
||||||
|
JMenu newCategoryMenu = new JMenu(category.toString());
|
||||||
|
|
||||||
|
pluginMenu.add(newCategoryMenu);
|
||||||
|
categories.put(category, newCategoryMenu);
|
||||||
|
}
|
||||||
|
|
||||||
pluginMenu.setText("Plugin");
|
|
||||||
this.add(pluginMenu);
|
this.add(pluginMenu);
|
||||||
|
}
|
||||||
|
|
||||||
helpMenu.setText("Help");
|
private void initHelpMenu() {
|
||||||
|
helpMenu = new JMenu("Help");
|
||||||
|
preferencesMenu = new JMenuItem("Preferences");
|
||||||
|
aboutMenu = new JMenuItem("About");
|
||||||
|
|
||||||
setupMenu.setText("Preferences");
|
preferencesMenu.addActionListener(this::setupMenuActionPerformed);
|
||||||
setupMenu.addActionListener(this::setupMenuActionPerformed);
|
helpMenu.add(preferencesMenu);
|
||||||
helpMenu.add(setupMenu);
|
|
||||||
|
|
||||||
aboutMenu.setText("About");
|
|
||||||
aboutMenu.addActionListener(this::aboutMenuActionPerformed);
|
aboutMenu.addActionListener(this::aboutMenuActionPerformed);
|
||||||
helpMenu.add(aboutMenu);
|
helpMenu.add(aboutMenu);
|
||||||
|
|
||||||
this.add(helpMenu);
|
this.add(helpMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void exitMenuActionPerformed(java.awt.event.ActionEvent evt) {
|
private void exitMenuActionPerformed(ActionEvent evt) {
|
||||||
mainWindow.exit();
|
mainWindow.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupMenuActionPerformed(ActionEvent evt) {
|
||||||
private void setupMenuActionPerformed(java.awt.event.ActionEvent evt) {
|
|
||||||
Login login = new Login();
|
Login login = new Login();
|
||||||
login.setVisible(true);
|
login.setVisible(true);
|
||||||
|
|
||||||
@@ -85,18 +105,12 @@ public class MenuBar extends JMenuBar {
|
|||||||
login.dispose();
|
login.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void aboutMenuActionPerformed(java.awt.event.ActionEvent evt) {
|
private void aboutMenuActionPerformed(ActionEvent evt) {
|
||||||
About about = new About();
|
About about = new About();
|
||||||
about.setVisible(true);
|
about.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void addPluginMenu(MenuCategory category, JMenuItem menu){
|
||||||
* Adiciona um menu fornecido por um plugin à seção de plugins da barra
|
categories.get(category).add(menu);
|
||||||
* de menus.
|
|
||||||
*
|
|
||||||
* @param menu menu criado pelo plugin.
|
|
||||||
*/
|
|
||||||
public void addPlugin(JMenu menu){
|
|
||||||
this.pluginMenu.add(menu);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,14 +2,16 @@ package pitiupi.GUI;
|
|||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.swing.BorderFactory;
|
import java.util.Map;
|
||||||
import javax.swing.DefaultListModel;
|
import java.util.function.Consumer;
|
||||||
import javax.swing.JList;
|
import javax.swing.*;
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.JScrollPane;
|
import pitiupi.models.Menus.MenuCategory;
|
||||||
import javax.swing.SwingUtilities;
|
import pitiupi.models.User;
|
||||||
import pitiupi.control.PeerInfo;
|
|
||||||
import pitiupi.control.PeerListener;
|
import pitiupi.control.PeerListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,10 +22,16 @@ import pitiupi.control.PeerListener;
|
|||||||
*/
|
*/
|
||||||
public class OnlineUsersPanel extends JPanel implements PeerListener {
|
public class OnlineUsersPanel extends JPanel implements PeerListener {
|
||||||
|
|
||||||
private final DefaultListModel<String> listModel;
|
private final DefaultListModel<User> listModel;
|
||||||
private final JList<String> userList;
|
private final JList<User> userList;
|
||||||
|
JPopupMenu menu;
|
||||||
|
private Map<MenuCategory, JMenu> categories;
|
||||||
|
|
||||||
public OnlineUsersPanel() {
|
public OnlineUsersPanel() {
|
||||||
|
menu = new JPopupMenu();
|
||||||
|
categories = new HashMap<>();
|
||||||
|
initMenu();
|
||||||
|
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
setBorder(BorderFactory.createTitledBorder("Online"));
|
setBorder(BorderFactory.createTitledBorder("Online"));
|
||||||
setPreferredSize(new Dimension(160, 0));
|
setPreferredSize(new Dimension(160, 0));
|
||||||
@@ -31,6 +39,48 @@ public class OnlineUsersPanel extends JPanel implements PeerListener {
|
|||||||
listModel = new DefaultListModel<>();
|
listModel = new DefaultListModel<>();
|
||||||
userList = new JList<>(listModel);
|
userList = new JList<>(listModel);
|
||||||
add(new JScrollPane(userList), BorderLayout.CENTER);
|
add(new JScrollPane(userList), BorderLayout.CENTER);
|
||||||
|
|
||||||
|
setupUserListActions();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initMenu() {
|
||||||
|
for (MenuCategory category : MenuCategory.values()) {
|
||||||
|
JMenu newCategory = new JMenu(category.toString());
|
||||||
|
|
||||||
|
menu.add(newCategory);
|
||||||
|
categories.put(category, newCategory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupUserListActions() {
|
||||||
|
userList.addMouseListener(new MouseAdapter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
int index = userList.locationToIndex(e.getPoint());
|
||||||
|
|
||||||
|
if (index == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
userList.setSelectedIndex(index);
|
||||||
|
menu.show(userList, e.getX(), e.getY());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initUnicastMenu(JMenuItem menuItem, Consumer<User> action) {
|
||||||
|
menuItem.addActionListener(e -> {
|
||||||
|
User user = userList.getSelectedValue();
|
||||||
|
|
||||||
|
if (user != null) {
|
||||||
|
action.accept(user);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addUnicastMenu(MenuCategory category, JMenuItem menuItem) {
|
||||||
|
categories.get(category).add(menuItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,11 +89,11 @@ public class OnlineUsersPanel extends JPanel implements PeerListener {
|
|||||||
* @param peers lista atualizada de usuários ativos na rede.
|
* @param peers lista atualizada de usuários ativos na rede.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onPeersChanged(List<PeerInfo> peers) {
|
public void onPeersChanged(List<User> peers) {
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
listModel.clear();
|
listModel.clear();
|
||||||
for (PeerInfo peer : peers) {
|
for (User peer : peers) {
|
||||||
listModel.addElement(peer.toString());
|
listModel.addElement(peer);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package pitiupi.control;
|
package pitiupi.control;
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.GUI.MainWindow;
|
||||||
import pitiupi.net.HeartbeatMessage;
|
import pitiupi.control.plugins.PluginManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.HeartbeatMessage;
|
||||||
|
import pitiupi.models.Messages.Message;
|
||||||
import pitiupi.net.tcp.SocketTCP;
|
import pitiupi.net.tcp.SocketTCP;
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
import pitiupi.net.udp.SocketUDP;
|
import pitiupi.net.udp.SocketUDP;
|
||||||
@@ -63,7 +64,7 @@ public class ConnectionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastLocal(byte[] message) {
|
public void broadcastLocal(byte[] message) {
|
||||||
pluginManager.handlePublicMessage(message);
|
pluginManager.handleMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPeerListener(PeerListener peerListener) {
|
public void addPeerListener(PeerListener peerListener) {
|
||||||
|
|||||||
@@ -1,17 +1,95 @@
|
|||||||
package pitiupi.control;
|
package pitiupi.control;
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.GUI.MainWindow;
|
||||||
|
import pitiupi.GUI.MenuBar;
|
||||||
|
import pitiupi.GUI.OnlineUsersPanel;
|
||||||
|
import pitiupi.models.Menus.*;
|
||||||
|
import pitiupi.models.Menus.MulticastMenus.MulticastActionMenu;
|
||||||
|
import pitiupi.models.Menus.MulticastMenus.MulticastMenu;
|
||||||
|
import pitiupi.models.Menus.MulticastMenus.MulticastSubMenu;
|
||||||
|
import pitiupi.models.Menus.UnicastMenus.UnicastActionMenu;
|
||||||
|
import pitiupi.models.Menus.UnicastMenus.UnicastMenu;
|
||||||
|
import pitiupi.models.Menus.UnicastMenus.UnicastSubMenu;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class GuiManager {
|
public class GuiManager {
|
||||||
private MainWindow mainWindow;
|
private MainWindow mainWindow;
|
||||||
|
private MenuBar menuBar;
|
||||||
|
private OnlineUsersPanel onlineUsersPanel;
|
||||||
|
|
||||||
public GuiManager(MainWindow mainWindow) {
|
private Logger logger;
|
||||||
|
|
||||||
|
public GuiManager(MainWindow mainWindow, MenuBar menuBar, OnlineUsersPanel onlineUsersPanel) {
|
||||||
this.mainWindow = mainWindow;
|
this.mainWindow = mainWindow;
|
||||||
|
this.menuBar = menuBar;
|
||||||
|
this.onlineUsersPanel = onlineUsersPanel;
|
||||||
|
this.logger = Logger.getLogger(GuiManager.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMenu(JMenu menu) {
|
public void createMenu(MenuCategory category, Menu menu) {
|
||||||
mainWindow.addMenu(menu);
|
System.out.println(
|
||||||
|
menu.getClass().getSuperclass().getSuperclass()
|
||||||
|
);
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
MulticastMenu.class
|
||||||
|
);
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
menu.getClass().getSuperclass().getSuperclass()
|
||||||
|
== MulticastMenu.class
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
JMenuItem newMenu = buildMenu(menu);
|
||||||
|
if (menu instanceof MulticastMenu) {
|
||||||
|
menuBar.addPluginMenu(category, newMenu);
|
||||||
|
}
|
||||||
|
if (menu instanceof UnicastMenu) {
|
||||||
|
onlineUsersPanel.addUnicastMenu(category, newMenu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException e) {
|
||||||
|
logger.log(Level.WARNING, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JMenuItem buildMenu(Menu menu) {
|
||||||
|
if (menu instanceof MulticastSubMenu multicastSubMenu) {
|
||||||
|
JMenu newMenu = new JMenu(menu.getText());
|
||||||
|
|
||||||
|
for (Menu child : multicastSubMenu.getMenus()) {
|
||||||
|
newMenu.add(buildMenu(child));
|
||||||
|
}
|
||||||
|
|
||||||
|
return newMenu;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (menu instanceof UnicastSubMenu unicastSubMenu) {
|
||||||
|
JMenu newMenu = new JMenu(menu.getText());
|
||||||
|
|
||||||
|
for (Menu child : unicastSubMenu.getMenus()) {
|
||||||
|
newMenu.add(buildMenu(child));
|
||||||
|
}
|
||||||
|
|
||||||
|
return newMenu;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (menu instanceof MulticastActionMenu multicastActionMenu) {
|
||||||
|
JMenuItem newMenu = new JMenuItem(menu.getText());
|
||||||
|
newMenu.addActionListener(e -> multicastActionMenu.onClick());
|
||||||
|
return newMenu;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (menu instanceof UnicastActionMenu unicastActionMenu) {
|
||||||
|
JMenuItem newMenu = new JMenuItem(menu.getText());
|
||||||
|
|
||||||
|
onlineUsersPanel.initUnicastMenu(newMenu, unicastActionMenu::onClick);
|
||||||
|
return newMenu;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Invalid menu type");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package pitiupi.control;
|
package pitiupi.control;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -10,10 +9,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import pitiupi.models.User;
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.models.Messages.HeartbeatMessage;
|
||||||
import pitiupi.net.HeartbeatMessage;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
/**
|
/**
|
||||||
* Gerencia o mecanismo de heartbeat da aplicação.
|
* Gerencia o mecanismo de heartbeat da aplicação.
|
||||||
@@ -29,7 +27,7 @@ public class HeartbeatManager {
|
|||||||
private static final long HEARTBEAT_INTERVAL_MS = 5000;
|
private static final long HEARTBEAT_INTERVAL_MS = 5000;
|
||||||
private static final long PEER_TIMEOUT_MS = 15000; // ~3 batimentos perdidos
|
private static final long PEER_TIMEOUT_MS = 15000; // ~3 batimentos perdidos
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
private final Map<String, PeerInfo> peers = new ConcurrentHashMap<>();
|
private final Map<String, User> peers = new ConcurrentHashMap<>();
|
||||||
private final List<PeerListener> listeners = new CopyOnWriteArrayList<>();
|
private final List<PeerListener> listeners = new CopyOnWriteArrayList<>();
|
||||||
private ScheduledExecutorService scheduler;
|
private ScheduledExecutorService scheduler;
|
||||||
private final String instanceId = UUID.randomUUID().toString();
|
private final String instanceId = UUID.randomUUID().toString();
|
||||||
@@ -87,7 +85,7 @@ public class HeartbeatManager {
|
|||||||
String key = msg.getInstanceId();
|
String key = msg.getInstanceId();
|
||||||
boolean isNew = !peers.containsKey(key);
|
boolean isNew = !peers.containsKey(key);
|
||||||
|
|
||||||
PeerInfo peer = peers.computeIfAbsent(key, k -> new PeerInfo(msg.getUserName(), from, msg.getTimestamp()));
|
User peer = peers.computeIfAbsent(key, k -> new User(msg.getUserName(), from, msg.getTimestamp()));
|
||||||
peer.touch(System.currentTimeMillis());
|
peer.touch(System.currentTimeMillis());
|
||||||
|
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
@@ -104,7 +102,7 @@ public class HeartbeatManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void notifyListeners() {
|
private void notifyListeners() {
|
||||||
List<PeerInfo> snapshot = new ArrayList<>(peers.values());
|
List<User> snapshot = new ArrayList<>(peers.values());
|
||||||
for (PeerListener l : listeners) {
|
for (PeerListener l : listeners) {
|
||||||
l.onPeersChanged(snapshot);
|
l.onPeersChanged(snapshot);
|
||||||
}
|
}
|
||||||
@@ -137,7 +135,7 @@ public class HeartbeatManager {
|
|||||||
*
|
*
|
||||||
* @return lista contendo os peers atualmente conhecidos.
|
* @return lista contendo os peers atualmente conhecidos.
|
||||||
*/
|
*/
|
||||||
public List<PeerInfo> getPeers() {
|
public List<User> getPeers() {
|
||||||
return new ArrayList<>(peers.values());
|
return new ArrayList<>(peers.values());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package pitiupi.control;
|
package pitiupi.control;
|
||||||
|
|
||||||
|
import pitiupi.models.User;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,5 +19,5 @@ public interface PeerListener {
|
|||||||
*
|
*
|
||||||
* @param peers lista atualizada de usuários ativos.
|
* @param peers lista atualizada de usuários ativos.
|
||||||
*/
|
*/
|
||||||
void onPeersChanged(List<PeerInfo> peers);
|
void onPeersChanged(List<User> peers);
|
||||||
}
|
}
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
||||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
|
||||||
*/
|
|
||||||
package pitiupi.control;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLClassLoader;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
import pitiupi.Main;
|
|
||||||
import pitiupi.plugin.Plugin;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Responsável por localizar, carregar e registrar os plugins
|
|
||||||
* presentes no diretório {@code plugins}.
|
|
||||||
*
|
|
||||||
* <p>Os plugins são descobertos utilizando {@link java.util.ServiceLoader}
|
|
||||||
* e devem estar empacotados como arquivos JAR contendo a configuração
|
|
||||||
* {@code META-INF/services} correspondente.</p>
|
|
||||||
* @author flavio
|
|
||||||
*/
|
|
||||||
public class PluginLoader {
|
|
||||||
|
|
||||||
private final pitiupi.GUI.MainWindow mainWindow;
|
|
||||||
|
|
||||||
public PluginLoader(MainWindow main) {
|
|
||||||
this.mainWindow = main;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadPlugins(List<Plugin> plugins, HashSet<String> loadedJars, ConnectionManager connectionManager, GuiManager guiManager) {
|
|
||||||
File pluginDir = new File("plugins");
|
|
||||||
File[] jarFiles = pluginDir.listFiles((dir, name) -> name.endsWith(".jar"));
|
|
||||||
|
|
||||||
if (jarFiles == null || jarFiles.length == 0) {
|
|
||||||
System.out.println("No plugins found in the " + pluginDir.getAbsolutePath() + " directory.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (File file : jarFiles) {
|
|
||||||
|
|
||||||
if (loadedJars.contains(file.getName())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Discovered plugin package: " + file.getName());
|
|
||||||
|
|
||||||
try {
|
|
||||||
URLClassLoader classLoader = new URLClassLoader(
|
|
||||||
new URL[]{file.toURI().toURL()},
|
|
||||||
Main.class.getClassLoader()
|
|
||||||
);
|
|
||||||
|
|
||||||
ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class, classLoader);
|
|
||||||
|
|
||||||
for (Plugin plugin : serviceLoader) {
|
|
||||||
System.out.println("Loading: " + plugin.getName());
|
|
||||||
|
|
||||||
plugin.createPlugin(connectionManager, guiManager);
|
|
||||||
plugins.add(plugin);
|
|
||||||
loadedJars.add(file.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("Failed to load plugin " + file.getName());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
package pitiupi.control;
|
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
|
||||||
import pitiupi.plugin.Plugin;
|
|
||||||
import pitiupi.plugin.PrivateConnectionPlugin;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class PluginManager {
|
|
||||||
private final PluginLoader pluginLoader;
|
|
||||||
private final List<Plugin> plugins;
|
|
||||||
private final HashSet<String> loadedJars;
|
|
||||||
|
|
||||||
public PluginManager(MainWindow main) {
|
|
||||||
this.pluginLoader = new PluginLoader(main);
|
|
||||||
this.plugins = new ArrayList<>();
|
|
||||||
this.loadedJars = new HashSet<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadPlugins(ConnectionManager connectionManager, GuiManager guiManager) {
|
|
||||||
pluginLoader.loadPlugins(plugins, loadedJars, connectionManager, guiManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handlePublicMessage(byte[] msg) {
|
|
||||||
for (Plugin plugin : plugins) {
|
|
||||||
plugin.receiveMessage(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Plugin getPlugin(String pluginId) {
|
|
||||||
for (Plugin plugin : plugins) {
|
|
||||||
if (plugin.getIdentifier().equals(pluginId)) return plugin;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handlePrivateMessage(TcpConnection connection, byte[] msg) {
|
|
||||||
Plugin plugin = getPlugin(connection.getReceiver());
|
|
||||||
if (plugin == null) return;
|
|
||||||
|
|
||||||
if (plugin instanceof PrivateConnectionPlugin privatePlugin) {
|
|
||||||
privatePlugin.receiveBytes(connection, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean handleConnection(TcpConnection connection, String receiverId) {
|
|
||||||
Plugin plugin = getPlugin(receiverId);
|
|
||||||
if (plugin == null) return false;
|
|
||||||
|
|
||||||
if (plugin instanceof PrivateConnectionPlugin privatePlugin) {
|
|
||||||
privatePlugin.receivePrivateConnection(connection);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleDisconnection(TcpConnection connection, String receiverId) {
|
|
||||||
Plugin plugin = getPlugin(receiverId);
|
|
||||||
if (plugin instanceof PrivateConnectionPlugin privatePlugin) {
|
|
||||||
privatePlugin.onSocketClosed(connection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package pitiupi.control.plugins;
|
||||||
|
|
||||||
|
import pitiupi.models.Plugin;
|
||||||
|
|
||||||
|
class LoadedPlugin {
|
||||||
|
|
||||||
|
private final Plugin plugin;
|
||||||
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
|
public LoadedPlugin(Plugin plugin, ClassLoader classLoader) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.classLoader = classLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Plugin getPlugin() {
|
||||||
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClassLoader getClassLoader() {
|
||||||
|
return classLoader;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,154 @@
|
|||||||
|
package pitiupi.control.plugins;
|
||||||
|
|
||||||
|
import pitiupi.Main;
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.control.GuiManager;
|
||||||
|
import pitiupi.models.Messages.Message;
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
import pitiupi.models.Plugin;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ServiceLoader;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class PluginManager {
|
||||||
|
private final List<LoadedPlugin> loadedPlugins;
|
||||||
|
private final HashSet<String> loadedJars;
|
||||||
|
|
||||||
|
private final Logger logger;
|
||||||
|
|
||||||
|
public PluginManager() {
|
||||||
|
this.loadedPlugins = new ArrayList<>();
|
||||||
|
this.loadedJars = new HashSet<>();
|
||||||
|
|
||||||
|
logger = Logger.getLogger(getClass().getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handleMessage(byte[] msg) {
|
||||||
|
Message message = null;
|
||||||
|
|
||||||
|
for (LoadedPlugin loadedPlugin : loadedPlugins) {
|
||||||
|
message = deserialize(msg, loadedPlugin.getClassLoader());
|
||||||
|
if (message != null) break;
|
||||||
|
}
|
||||||
|
if (message == null) return;
|
||||||
|
|
||||||
|
for (LoadedPlugin loadedPlugin : loadedPlugins) {
|
||||||
|
Plugin plugin = loadedPlugin.getPlugin();
|
||||||
|
|
||||||
|
if (plugin.receiveAllTypes() && message.isPublic()) {
|
||||||
|
plugin.receiveMessage(message);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Class<? extends Message> messageType : plugin.getMessages()) {
|
||||||
|
if (messageType.isInstance(message)) {
|
||||||
|
plugin.receiveMessage(message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Plugin getPlugin(String pluginId) {
|
||||||
|
for (LoadedPlugin loadedPlugin : loadedPlugins) {
|
||||||
|
if (loadedPlugin.getPlugin().getIdentifier().equals(pluginId)) {
|
||||||
|
return loadedPlugin.getPlugin();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handleMessageFromConnection(TcpConnection connection, byte[] msg) {
|
||||||
|
Plugin plugin = getPlugin(connection.getReceiver());
|
||||||
|
if (plugin == null) return;
|
||||||
|
|
||||||
|
plugin.receiveBytes(connection, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean handleConnection(TcpConnection connection, String receiverId) {
|
||||||
|
Plugin plugin = getPlugin(receiverId);
|
||||||
|
if (plugin == null) return false;
|
||||||
|
|
||||||
|
plugin.receivePrivateConnection(connection);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handleDisconnection(TcpConnection connection, String receiverId) {
|
||||||
|
Plugin plugin = getPlugin(receiverId);
|
||||||
|
if (plugin == null) return;
|
||||||
|
|
||||||
|
plugin.onSocketClosed(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadPlugins(ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
|
File pluginDir = new File("plugins");
|
||||||
|
File[] jarFiles = pluginDir.listFiles((dir, name) -> name.endsWith(".jar"));
|
||||||
|
|
||||||
|
if (jarFiles == null || jarFiles.length == 0) {
|
||||||
|
System.out.println("No plugins found in the " + pluginDir.getAbsolutePath() + " directory.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (File file : jarFiles) {
|
||||||
|
|
||||||
|
if (loadedJars.contains(file.getName())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Discovered plugin package: " + file.getName());
|
||||||
|
|
||||||
|
try {
|
||||||
|
URLClassLoader classLoader = new URLClassLoader(
|
||||||
|
new URL[]{file.toURI().toURL()},
|
||||||
|
Main.class.getClassLoader()
|
||||||
|
);
|
||||||
|
|
||||||
|
ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class, classLoader);
|
||||||
|
|
||||||
|
for (Plugin plugin : serviceLoader) {
|
||||||
|
LoadedPlugin loadedPlugin = new LoadedPlugin(plugin, classLoader);
|
||||||
|
|
||||||
|
plugin.createPlugin(connectionManager, guiManager);
|
||||||
|
loadedPlugins.add(loadedPlugin);
|
||||||
|
loadedJars.add(file.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.log(Level.WARNING, "Failed to load plugin " + file.getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Message deserialize(byte[] data, ClassLoader classLoader) {
|
||||||
|
try {
|
||||||
|
ByteArrayInputStream input = new ByteArrayInputStream(data);
|
||||||
|
|
||||||
|
PluginObjectInputStream objectInput = new PluginObjectInputStream(input, classLoader);
|
||||||
|
|
||||||
|
Object object = objectInput.readObject();
|
||||||
|
|
||||||
|
if (!(object instanceof Message)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (Message) object;
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
logger.log(Level.SEVERE, "Failed to load plugin package", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package pitiupi.control.plugins;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectStreamClass;
|
||||||
|
|
||||||
|
public class PluginObjectInputStream extends ObjectInputStream {
|
||||||
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
|
public PluginObjectInputStream(InputStream input, ClassLoader classLoader) throws IOException {
|
||||||
|
super(input);
|
||||||
|
this.classLoader = classLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<?> resolveClass(ObjectStreamClass description) throws ClassNotFoundException {
|
||||||
|
return Class.forName(description.getName(), false, classLoader);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package pitiupi.models.Menus;
|
||||||
|
|
||||||
|
public abstract class Menu {
|
||||||
|
public abstract String getText();
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package pitiupi.models.Menus;
|
||||||
|
|
||||||
|
public enum MenuCategory {
|
||||||
|
Jogos,
|
||||||
|
Social,
|
||||||
|
Streaming
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package pitiupi.models.Menus.MulticastMenus;
|
||||||
|
|
||||||
|
public abstract class MulticastActionMenu extends MulticastMenu{
|
||||||
|
public abstract void onClick();
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package pitiupi.models.Menus.MulticastMenus;
|
||||||
|
|
||||||
|
import pitiupi.models.Menus.Menu;
|
||||||
|
|
||||||
|
public abstract class MulticastMenu extends Menu {
|
||||||
|
MulticastMenu() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package pitiupi.models.Menus.MulticastMenus;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class MulticastSubMenu extends MulticastMenu{
|
||||||
|
public abstract List<MulticastMenu> getMenus();
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package pitiupi.models.Menus.UnicastMenus;
|
||||||
|
|
||||||
|
import pitiupi.models.User;
|
||||||
|
|
||||||
|
public abstract class UnicastActionMenu extends UnicastMenu{
|
||||||
|
public abstract void onClick(User user);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package pitiupi.models.Menus.UnicastMenus;
|
||||||
|
|
||||||
|
import pitiupi.models.Menus.Menu;
|
||||||
|
import pitiupi.models.User;
|
||||||
|
|
||||||
|
public abstract class UnicastMenu extends Menu {
|
||||||
|
UnicastMenu() {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package pitiupi.models.Menus.UnicastMenus;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class UnicastSubMenu extends UnicastMenu{
|
||||||
|
public abstract List<UnicastMenu> getMenus();
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package pitiupi.net;
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mensagem utilizada para sinalizar a presença de um usuário na rede.
|
* Mensagem utilizada para sinalizar a presença de um usuário na rede.
|
||||||
+13
-14
@@ -1,6 +1,6 @@
|
|||||||
package pitiupi.net.tcp;
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -8,12 +8,11 @@ import java.io.ObjectInputStream;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class SysInfoMessage extends Message {
|
public class InfoMessage extends Message {
|
||||||
|
private final InfoType type;
|
||||||
private final SysInfoType type;
|
|
||||||
private final String message;
|
private final String message;
|
||||||
|
|
||||||
public SysInfoMessage(String message, SysInfoType type) {
|
public InfoMessage(String message, InfoType type) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
@@ -22,29 +21,29 @@ public class SysInfoMessage extends Message {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SysInfoType getType() {
|
public InfoType getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendSystemInfo(TcpConnection connection, SysInfoType type, String description) {
|
public static void sendSystemInfo(TcpConnection connection, InfoType type, String description) {
|
||||||
try {
|
try {
|
||||||
SysInfoMessage error = new SysInfoMessage(description, type);
|
InfoMessage error = new InfoMessage(description, type);
|
||||||
connection.send(error.toByteArray());
|
connection.send(error.toByteArray());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Logger.getLogger(SysInfoMessage.class.getName()).log(Level.WARNING, null, ex);
|
Logger.getLogger(InfoMessage.class.getName()).log(Level.WARNING, null, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SysInfoMessage parseSystemInfo(byte[] bytes) {
|
public static InfoMessage parseSystemInfo(byte[] bytes) {
|
||||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
||||||
|
|
||||||
Object object = ois.readObject();
|
Object object = ois.readObject();
|
||||||
if (object instanceof SysInfoMessage) {
|
if (object instanceof InfoMessage) {
|
||||||
return (SysInfoMessage) object;
|
return (InfoMessage) object;
|
||||||
}
|
}
|
||||||
} catch (IOException | ClassNotFoundException ex) {
|
} catch (IOException | ClassNotFoundException ex) {
|
||||||
Logger.getLogger(SysInfoMessage.class.getName()).log(Level.WARNING, null, ex);
|
Logger.getLogger(InfoMessage.class.getName()).log(Level.WARNING, null, ex);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
package pitiupi.net.tcp;
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
public enum SysInfoType {
|
public enum InfoType {
|
||||||
CONNECTION_REJECTED,
|
CONNECTION_REJECTED,
|
||||||
RECEIVER_NOT_FOUND,
|
RECEIVER_NOT_FOUND,
|
||||||
MESSAGE_REJECTED,
|
MESSAGE_REJECTED,
|
||||||
+14
-1
@@ -3,7 +3,7 @@
|
|||||||
* To change this template file, choose Tools | Templates
|
* To change this template file, choose Tools | Templates
|
||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
package pitiupi.net;
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -24,6 +24,19 @@ import java.io.Serializable;
|
|||||||
* @author flavio
|
* @author flavio
|
||||||
*/
|
*/
|
||||||
public abstract class Message implements Serializable{
|
public abstract class Message implements Serializable{
|
||||||
|
private boolean isPublic;
|
||||||
|
|
||||||
|
protected Message(){
|
||||||
|
isPublic = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Message(boolean isPublic){
|
||||||
|
this.isPublic = isPublic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPublic(){
|
||||||
|
return isPublic;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serializa a mensagem para um vetor de bytes.
|
* Serializa a mensagem para um vetor de bytes.
|
||||||
@@ -2,14 +2,15 @@
|
|||||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
|
||||||
*/
|
*/
|
||||||
package pitiupi.plugin;
|
package pitiupi.models;
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.control.GuiManager;
|
import pitiupi.control.GuiManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface base para plugins da aplicação.
|
* Interface base para plugins da aplicação.
|
||||||
*
|
*
|
||||||
@@ -18,28 +19,28 @@ import pitiupi.net.tcp.TcpConnection;
|
|||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
*/
|
*/
|
||||||
public interface Plugin {
|
public abstract class Plugin {
|
||||||
|
public abstract String getName();
|
||||||
public String getName();
|
public abstract String getAuthor();
|
||||||
public String getAuthor();
|
public abstract String getVersion();
|
||||||
public String getVersion();
|
public abstract String getIdentifier();
|
||||||
public String getIdentifier();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inicializa o plugin.
|
* Inicializa o plugin.
|
||||||
*
|
*
|
||||||
* <p>Este método é chamado pela aplicação após o carregamento do plugin.</p>
|
* <p>Este método é chamado pela aplicação após o carregamento do plugin.</p>
|
||||||
*
|
*
|
||||||
* @param window janela principal da aplicação.
|
* @param connectionManager interface com o sistema de conexões da aplicação.
|
||||||
|
* @param guiManager interface com a janela principal da aplicação
|
||||||
*/
|
*/
|
||||||
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager);
|
public abstract void createPlugin(ConnectionManager connectionManager, GuiManager guiManager);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retorna uma mensagem utilizada pelo plugin.
|
* Retorna uma mensagem utilizada pelo plugin.
|
||||||
*
|
*
|
||||||
* @return mensagem criada pelo plugin.
|
* @return mensagem criada pelo plugin.
|
||||||
*/
|
*/
|
||||||
public Message getMessage();
|
public abstract List<Class<? extends Message>> getMessages();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recebe uma mensagem enviada pela aplicação.
|
* Recebe uma mensagem enviada pela aplicação.
|
||||||
@@ -49,6 +50,16 @@ public interface Plugin {
|
|||||||
*
|
*
|
||||||
* @param message mensagem serializada recebida.
|
* @param message mensagem serializada recebida.
|
||||||
*/
|
*/
|
||||||
public void receiveMessage(byte[] message);
|
public abstract void receiveMessage(Message message);
|
||||||
|
|
||||||
|
public boolean receiveAllTypes() {return false;}
|
||||||
|
public boolean receivePrivateConnection(TcpConnection connection) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public boolean receiveBytes(TcpConnection connection, byte[] bytes) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public boolean onSocketClosed(TcpConnection connection) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package pitiupi.control;
|
package pitiupi.models;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
/**
|
/**
|
||||||
@@ -10,13 +10,13 @@ import java.net.InetAddress;
|
|||||||
*
|
*
|
||||||
* @author tony
|
* @author tony
|
||||||
*/
|
*/
|
||||||
public class PeerInfo {
|
public class User {
|
||||||
|
|
||||||
private final String userName;
|
private final String userName;
|
||||||
private final InetAddress address;
|
private final InetAddress address;
|
||||||
private volatile long lastSeen;
|
private volatile long lastSeen;
|
||||||
|
|
||||||
public PeerInfo(String userName, InetAddress address, long lastSeen) {
|
public User(String userName, InetAddress address, long lastSeen) {
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.lastSeen = lastSeen;
|
this.lastSeen = lastSeen;
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package pitiupi.net.tcp;
|
package pitiupi.net.tcp;
|
||||||
|
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
|
import pitiupi.models.Messages.InfoMessage;
|
||||||
|
import pitiupi.models.Messages.InfoType;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -152,7 +154,7 @@ public class ConnectionFactory {
|
|||||||
Object object = ois.readObject();
|
Object object = ois.readObject();
|
||||||
|
|
||||||
if (object instanceof Message message) {
|
if (object instanceof Message message) {
|
||||||
SysInfoMessage sysInfo = SysInfoMessage.parseSystemInfo(bytes);
|
InfoMessage sysInfo = InfoMessage.parseSystemInfo(bytes);
|
||||||
if (sysInfo != null) return null;
|
if (sysInfo != null) return null;
|
||||||
|
|
||||||
|
|
||||||
@@ -161,25 +163,25 @@ public class ConnectionFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SysInfoMessage.sendSystemInfo(
|
InfoMessage.sendSystemInfo(
|
||||||
connection,
|
connection,
|
||||||
SysInfoType.CONNECTION_REJECTED,
|
InfoType.CONNECTION_REJECTED,
|
||||||
"The message was not from an allowed type."
|
"The message was not from an allowed type."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (SocketTimeoutException ex) {
|
catch (SocketTimeoutException ex) {
|
||||||
SysInfoMessage.sendSystemInfo(
|
InfoMessage.sendSystemInfo(
|
||||||
connection,
|
connection,
|
||||||
SysInfoType.CONNECTION_REJECTED,
|
InfoType.CONNECTION_REJECTED,
|
||||||
"No handshake was received within the allowed time."
|
"No handshake was received within the allowed time."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException | IOException ex) {
|
catch (ClassNotFoundException | IOException ex) {
|
||||||
Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
|
||||||
SysInfoMessage.sendSystemInfo(
|
InfoMessage.sendSystemInfo(
|
||||||
connection,
|
connection,
|
||||||
SysInfoType.INTERNAL_ERROR,
|
InfoType.INTERNAL_ERROR,
|
||||||
"The server could not process the handshake."
|
"The server could not process the handshake."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -206,17 +208,17 @@ public class ConnectionFactory {
|
|||||||
String receiver = handshake.getReceiver();
|
String receiver = handshake.getReceiver();
|
||||||
|
|
||||||
if (sender == null || sender.isBlank()) {
|
if (sender == null || sender.isBlank()) {
|
||||||
SysInfoMessage.sendSystemInfo(
|
InfoMessage.sendSystemInfo(
|
||||||
connection,
|
connection,
|
||||||
SysInfoType.CONNECTION_REJECTED,
|
InfoType.CONNECTION_REJECTED,
|
||||||
"Invalid Sender: " + sender + "."
|
"Invalid Sender: " + sender + "."
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (receiver == null || receiver.isBlank()) {
|
if (receiver == null || receiver.isBlank()) {
|
||||||
SysInfoMessage.sendSystemInfo(
|
InfoMessage.sendSystemInfo(
|
||||||
connection,
|
connection,
|
||||||
SysInfoType.CONNECTION_REJECTED,
|
InfoType.CONNECTION_REJECTED,
|
||||||
"Invalid Receiver: " + receiver + "."
|
"Invalid Receiver: " + receiver + "."
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package pitiupi.net.tcp;
|
package pitiupi.net.tcp;
|
||||||
|
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
|
|
||||||
class HandshakeMessage extends Message {
|
class HandshakeMessage extends Message {
|
||||||
private final boolean isPrivate;
|
private final boolean isPrivate;
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package pitiupi.net.tcp;
|
package pitiupi.net.tcp;
|
||||||
|
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.control.PluginManager;
|
import pitiupi.control.plugins.PluginManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
import pitiupi.plugin.PrivateConnectionPlugin;
|
import pitiupi.models.Messages.InfoMessage;
|
||||||
|
import pitiupi.models.Messages.InfoType;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -270,12 +271,13 @@ 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 {
|
||||||
SysInfoMessage.sendSystemInfo(
|
InfoMessage.sendSystemInfo(
|
||||||
connection,
|
connection,
|
||||||
SysInfoType.RECEIVER_NOT_FOUND,
|
InfoType.RECEIVER_NOT_FOUND,
|
||||||
"This peer does not have the plugin: " + connection.getReceiver()
|
"This peer does not have the plugin: " + connection.getReceiver()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -300,10 +302,10 @@ public class SocketTCP extends Thread {
|
|||||||
try {
|
try {
|
||||||
byte[] bytes = connection.receive();
|
byte[] bytes = connection.receive();
|
||||||
|
|
||||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(bytes);
|
InfoMessage sysMessage = InfoMessage.parseSystemInfo(bytes);
|
||||||
|
|
||||||
if (sysMessage == null) {
|
if (sysMessage == null) {
|
||||||
pluginManager.handlePublicMessage(bytes);
|
pluginManager.handleMessage(bytes);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,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>
|
||||||
*
|
*
|
||||||
@@ -345,10 +347,10 @@ public class SocketTCP extends Thread {
|
|||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
byte[] message = connection.receive();
|
byte[] message = connection.receive();
|
||||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
InfoMessage sysMessage = InfoMessage.parseSystemInfo(message);
|
||||||
|
|
||||||
if (sysMessage == null) {
|
if (sysMessage == null) {
|
||||||
pluginManager.handlePrivateMessage(connection, message);
|
pluginManager.handleMessageFromConnection(connection, message);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,7 +392,7 @@ public class SocketTCP extends Thread {
|
|||||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(message);
|
try (ByteArrayInputStream bis = new ByteArrayInputStream(message);
|
||||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
||||||
|
|
||||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
InfoMessage sysMessage = InfoMessage.parseSystemInfo(message);
|
||||||
if (sysMessage != null) continue;
|
if (sysMessage != null) continue;
|
||||||
|
|
||||||
//TODO: Ainda não existem mensagens trocadas entre o sistema.
|
//TODO: Ainda não existem mensagens trocadas entre o sistema.
|
||||||
|
|||||||
@@ -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.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package pitiupi.net.tcp;
|
package pitiupi.net.tcp;
|
||||||
|
|
||||||
import pitiupi.control.PeerInfo;
|
import pitiupi.models.Messages.InfoType;
|
||||||
|
import pitiupi.models.User;
|
||||||
import pitiupi.control.PeerListener;
|
import pitiupi.control.PeerListener;
|
||||||
|
import pitiupi.models.Messages.InfoMessage;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@@ -99,7 +101,7 @@ class TcpConnectionManager implements PeerListener {
|
|||||||
* @param reason motivo do encerramento que será informado ao peer.
|
* @param reason motivo do encerramento que será informado ao peer.
|
||||||
*/
|
*/
|
||||||
public void closeConnection(TcpConnection connection, String reason) {
|
public void closeConnection(TcpConnection connection, String reason) {
|
||||||
SysInfoMessage.sendSystemInfo(connection, SysInfoType.CONNECTION_CLOSED, reason);
|
InfoMessage.sendSystemInfo(connection, InfoType.CONNECTION_CLOSED, reason);
|
||||||
closeConnection(connection);
|
closeConnection(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,7 +155,7 @@ class TcpConnectionManager implements PeerListener {
|
|||||||
public void closeByAdress(InetAddress address, String reason) {
|
public void closeByAdress(InetAddress address, String reason) {
|
||||||
List<TcpConnection> connections = privateConnections.get(address);
|
List<TcpConnection> connections = privateConnections.get(address);
|
||||||
for (TcpConnection connection : connections) {
|
for (TcpConnection connection : connections) {
|
||||||
SysInfoMessage.sendSystemInfo(connection, SysInfoType.CONNECTION_CLOSED, reason);
|
InfoMessage.sendSystemInfo(connection, InfoType.CONNECTION_CLOSED, reason);
|
||||||
}
|
}
|
||||||
closeByAdress(address);
|
closeByAdress(address);
|
||||||
}
|
}
|
||||||
@@ -205,7 +207,7 @@ class TcpConnectionManager implements PeerListener {
|
|||||||
public void closeAllConnections(String reason) {
|
public void closeAllConnections(String reason) {
|
||||||
for (List<TcpConnection> connections : privateConnections.values()) {
|
for (List<TcpConnection> connections : privateConnections.values()) {
|
||||||
for (TcpConnection connection : connections) {
|
for (TcpConnection connection : connections) {
|
||||||
SysInfoMessage.sendSystemInfo(connection, SysInfoType.CONNECTION_CLOSED, reason);
|
InfoMessage.sendSystemInfo(connection, InfoType.CONNECTION_CLOSED, reason);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closeAllConnections();
|
closeAllConnections();
|
||||||
@@ -245,8 +247,8 @@ class TcpConnectionManager implements PeerListener {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPeersChanged(List<PeerInfo> peers) {
|
public void onPeersChanged(List<User> peers) {
|
||||||
Set<InetAddress> activePeers = peers.stream().map(PeerInfo::getAddress).collect(Collectors.toSet());
|
Set<InetAddress> activePeers = peers.stream().map(User::getAddress).collect(Collectors.toSet());
|
||||||
|
|
||||||
for (InetAddress address : publicConnections.keySet()) {
|
for (InetAddress address : publicConnections.keySet()) {
|
||||||
if (!activePeers.contains(address)) {
|
if (!activePeers.contains(address)) {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.net.udp;
|
package pitiupi.net.udp;
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInput;
|
import java.io.ObjectInput;
|
||||||
@@ -15,10 +14,9 @@ import java.util.logging.Level;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.control.PluginManager;
|
import pitiupi.control.plugins.PluginManager;
|
||||||
import pitiupi.net.HeartbeatMessage;
|
import pitiupi.models.Messages.HeartbeatMessage;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
import pitiupi.plugin.Plugin;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gerencia a comunicação UDP multicast e unicast da aplicação.
|
* Gerencia a comunicação UDP multicast e unicast da aplicação.
|
||||||
@@ -159,7 +157,7 @@ public class SocketUDP extends Thread {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginManager.handlePublicMessage(msgPacket.getData());
|
pluginManager.handleMessage(msgPacket.getData());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
package pitiupi.plugin;
|
|
||||||
|
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
|
||||||
|
|
||||||
public interface PrivateConnectionPlugin extends Plugin {
|
|
||||||
public void receivePrivateConnection(TcpConnection connection);
|
|
||||||
public void receiveBytes(TcpConnection connection, byte[] bytes);
|
|
||||||
public void onSocketClosed(TcpConnection connection);
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user