Compare commits
10
Commits
7168431bcd
...
96b8faca5b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96b8faca5b | ||
|
|
cac549f1ee | ||
|
|
a86f4dbf82 | ||
|
|
23559dcc06 | ||
|
|
fa4ee046b1 | ||
|
|
0c71a85cba | ||
|
|
e7c8983775 | ||
|
|
0ec3129a22 | ||
|
|
ba2f41daf7 | ||
|
|
57059c3ef0 |
No files matched your search
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 +1 @@
|
|||||||
plugin.SocketTest
|
plugin.TestChat
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library>
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MODULE_DIR$/../pitiupi/dist/pitiupi.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package GUI;
|
||||||
|
|
||||||
|
import plugin.TestChat;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ChatMenu extends JMenu {
|
||||||
|
private final TestChat testChat;
|
||||||
|
|
||||||
|
public ChatMenu(TestChat testChat) {
|
||||||
|
this.testChat = testChat;
|
||||||
|
this.setText("MP-Chat");
|
||||||
|
|
||||||
|
JMenuItem startPrivateChat = new JMenuItem("start Private Chat");
|
||||||
|
startPrivateChat.addActionListener(_ -> showPrivateChatDialog());
|
||||||
|
this.add(startPrivateChat);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void newMenuItem(JMenuItem newMenuItem, ChatWindow chatWindow) {
|
||||||
|
newMenuItem.addActionListener(_ -> chatWindow.setVisible(true));
|
||||||
|
|
||||||
|
this.add(newMenuItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeMenuItem(JMenuItem removeMenuItem) {
|
||||||
|
this.remove(removeMenuItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showPrivateChatDialog() {
|
||||||
|
JDialog dialog = new JDialog(
|
||||||
|
SwingUtilities.getWindowAncestor(this),
|
||||||
|
"Novo chat privado",
|
||||||
|
Dialog.ModalityType.APPLICATION_MODAL
|
||||||
|
);
|
||||||
|
|
||||||
|
JTextField ipField = new JTextField();
|
||||||
|
|
||||||
|
JButton startButton = new JButton("Iniciar");
|
||||||
|
JButton cancelButton = new JButton("Cancelar");
|
||||||
|
|
||||||
|
JPanel contentPanel = new JPanel(new BorderLayout(10, 10));
|
||||||
|
contentPanel.setBorder(
|
||||||
|
BorderFactory.createEmptyBorder(10, 10, 10, 10)
|
||||||
|
);
|
||||||
|
|
||||||
|
JPanel ipPanel = new JPanel(new BorderLayout(5, 5));
|
||||||
|
ipPanel.add(new JLabel("IP:"), BorderLayout.WEST);
|
||||||
|
ipPanel.add(ipField, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
JPanel buttonsPanel = new JPanel(
|
||||||
|
new FlowLayout(FlowLayout.RIGHT)
|
||||||
|
);
|
||||||
|
|
||||||
|
buttonsPanel.add(cancelButton);
|
||||||
|
buttonsPanel.add(startButton);
|
||||||
|
|
||||||
|
JLabel errorLabel = new JLabel("Não foi possível estabelecer a conexão.");
|
||||||
|
errorLabel.setForeground(Color.RED);
|
||||||
|
errorLabel.setVisible(false);
|
||||||
|
|
||||||
|
contentPanel.add(errorLabel, BorderLayout.NORTH);
|
||||||
|
contentPanel.add(ipPanel, BorderLayout.CENTER);
|
||||||
|
contentPanel.add(buttonsPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
cancelButton.addActionListener(e -> dialog.dispose());
|
||||||
|
|
||||||
|
startButton.addActionListener(e -> {
|
||||||
|
String ip = ipField.getText();
|
||||||
|
|
||||||
|
if (testChat.openConnection(ip)) dialog.dispose();
|
||||||
|
else errorLabel.setVisible(true);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
dialog.setContentPane(contentPanel);
|
||||||
|
dialog.pack();
|
||||||
|
dialog.setLocationRelativeTo(SwingUtilities.getWindowAncestor(this));
|
||||||
|
dialog.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package GUI;
|
||||||
|
|
||||||
|
import net.TestChatMessage;
|
||||||
|
import pitiupi.GUI.MainWindow;
|
||||||
|
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 TestChatMessage createMessage(String tipoMensagem) {
|
||||||
|
try {
|
||||||
|
String nome = connectionManager.getUserName();
|
||||||
|
String ip = InetAddress.getLocalHost().getHostAddress();
|
||||||
|
String message = messageField.getText();
|
||||||
|
|
||||||
|
return new TestChatMessage(nome, ip, tipoMensagem, message);
|
||||||
|
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void receiveMessage(TestChatMessage msg) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
String mensagem = String.format(
|
||||||
|
"[%s] %s (%s): %s%n",
|
||||||
|
msg.getTipoMensagem(),
|
||||||
|
msg.getNome(),
|
||||||
|
msg.getIp(),
|
||||||
|
msg.getMessage()
|
||||||
|
);
|
||||||
|
|
||||||
|
receivedMessagesArea.append(mensagem);
|
||||||
|
|
||||||
|
receivedMessagesArea.setCaretPosition(receivedMessagesArea.getDocument().getLength());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package GUI;
|
||||||
|
|
||||||
|
import net.TestChatMessage;
|
||||||
|
import pitiupi.GUI.MainWindow;
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class PrivateChatWindow extends ChatWindow {
|
||||||
|
|
||||||
|
private final TcpConnection connection;
|
||||||
|
|
||||||
|
public PrivateChatWindow(ConnectionManager ConnectionManager, TcpConnection connection) {
|
||||||
|
this.connection = connection;
|
||||||
|
|
||||||
|
super(ConnectionManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void initButtons(JPanel bottomPanel) {
|
||||||
|
JButton tcpButton = new JButton("TCP");
|
||||||
|
|
||||||
|
tcpButton.addActionListener(e -> sendTCP());
|
||||||
|
|
||||||
|
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
|
||||||
|
|
||||||
|
buttonsPanel.add(tcpButton);
|
||||||
|
|
||||||
|
bottomPanel.add(buttonsPanel, BorderLayout.EAST);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendTCP() {
|
||||||
|
try {
|
||||||
|
TestChatMessage msg = createMessage("TCP Private");
|
||||||
|
connection.send(msg.toByteArray());
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Logger.getLogger(PrivateChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package GUI;
|
||||||
|
|
||||||
|
import net.TestChatMessage;
|
||||||
|
import pitiupi.GUI.MainWindow;
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class PublicChatWindow extends ChatWindow {
|
||||||
|
|
||||||
|
private JTextField ipField;
|
||||||
|
|
||||||
|
public PublicChatWindow(ConnectionManager connectionManager) {
|
||||||
|
super(connectionManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void initButtons(JPanel bottomPanel) {
|
||||||
|
initIpField(bottomPanel);
|
||||||
|
initCommunicationButtons(bottomPanel);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initIpField(JPanel bottomPanel) {
|
||||||
|
ipField = new JTextField();
|
||||||
|
ipField.setPreferredSize(new Dimension(130, 30));
|
||||||
|
|
||||||
|
JPanel ipPanel = new JPanel(new BorderLayout(5, 5));
|
||||||
|
|
||||||
|
ipPanel.add(new JLabel("IP destino:"), BorderLayout.WEST);
|
||||||
|
|
||||||
|
ipPanel.add(ipField, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
bottomPanel.add(ipPanel, BorderLayout.WEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initCommunicationButtons(JPanel bottomPanel) {
|
||||||
|
JButton tcpButton = new JButton("TCP");
|
||||||
|
JButton udpButton = new JButton("UDP");
|
||||||
|
JButton multicastButton = new JButton("Multicast");
|
||||||
|
|
||||||
|
tcpButton.addActionListener(e -> sendTCP());
|
||||||
|
udpButton.addActionListener(e -> sendUDP());
|
||||||
|
multicastButton.addActionListener(e -> sendMulticast());
|
||||||
|
|
||||||
|
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
|
||||||
|
|
||||||
|
buttonsPanel.add(tcpButton);
|
||||||
|
buttonsPanel.add(udpButton);
|
||||||
|
buttonsPanel.add(multicastButton);
|
||||||
|
|
||||||
|
bottomPanel.add(buttonsPanel, BorderLayout.EAST);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendTCP() {
|
||||||
|
try {
|
||||||
|
TestChatMessage msg = createMessage("TCP Unicast");
|
||||||
|
|
||||||
|
InetAddress ip = InetAddress.getByName(ipField.getText());
|
||||||
|
|
||||||
|
connectionManager.sendTcp(msg, ip);
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendUDP() {
|
||||||
|
try {
|
||||||
|
TestChatMessage msg = createMessage("UDP Unicast");
|
||||||
|
|
||||||
|
InetAddress ip = InetAddress.getByName(ipField.getText());
|
||||||
|
|
||||||
|
connectionManager.sendUdpUnicast(msg, ip);
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMulticast() {
|
||||||
|
try {
|
||||||
|
TestChatMessage msg = createMessage("UDP Multicast");
|
||||||
|
|
||||||
|
connectionManager.sendUdpMulticast(msg);
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package GUI;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
|
|
||||||
public class SocketTestMenu extends JMenu {
|
|
||||||
private final SocketTestWindow socketTestWindow;
|
|
||||||
|
|
||||||
public SocketTestMenu(SocketTestWindow socketTestWindow) {
|
|
||||||
this.socketTestWindow = socketTestWindow;
|
|
||||||
this.setText("Chat");
|
|
||||||
JMenuItem menuItem = new JMenuItem();
|
|
||||||
menuItem.setText("Show Chat Window");
|
|
||||||
menuItem.addActionListener(this::showTestWindow);
|
|
||||||
this.add(menuItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showTestWindow(ActionEvent ae) {
|
|
||||||
this.socketTestWindow.setVisible(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,151 +0,0 @@
|
|||||||
package GUI;
|
|
||||||
|
|
||||||
import net.TestMessage;
|
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class SocketTestWindow extends JFrame {
|
|
||||||
private MainWindow mainWindow;
|
|
||||||
private JTextArea receivedMessagesArea;
|
|
||||||
|
|
||||||
private JTextField ipField;
|
|
||||||
private JTextField messageField;
|
|
||||||
|
|
||||||
private JButton tcpButton;
|
|
||||||
private JButton udpButton;
|
|
||||||
private JButton multicastButton;
|
|
||||||
|
|
||||||
public SocketTestWindow(MainWindow mainWindow) {
|
|
||||||
this.mainWindow = mainWindow;
|
|
||||||
initComponents();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initComponents() {
|
|
||||||
setTitle("Socket Test");
|
|
||||||
setSize(700, 500);
|
|
||||||
setLocationRelativeTo(null);
|
|
||||||
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
|
||||||
|
|
||||||
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
|
|
||||||
|
|
||||||
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
|
||||||
|
|
||||||
initReceivedMessagesPanel(mainPanel);
|
|
||||||
initBottomPanel(mainPanel);
|
|
||||||
|
|
||||||
setContentPane(mainPanel);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initReceivedMessagesPanel(JPanel mainPanel) {
|
|
||||||
receivedMessagesArea = new JTextArea();
|
|
||||||
receivedMessagesArea.setEditable(false);
|
|
||||||
receivedMessagesArea.setLineWrap(true);
|
|
||||||
receivedMessagesArea.setWrapStyleWord(true);
|
|
||||||
|
|
||||||
JScrollPane messagesScrollPane = new JScrollPane(receivedMessagesArea);
|
|
||||||
|
|
||||||
messagesScrollPane.setBorder(BorderFactory.createTitledBorder("Mensagens recebidas"));
|
|
||||||
|
|
||||||
mainPanel.add(messagesScrollPane, BorderLayout.CENTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initBottomPanel(JPanel mainPanel) {
|
|
||||||
ipField = new JTextField();
|
|
||||||
ipField.setPreferredSize(new Dimension(130, 30));
|
|
||||||
|
|
||||||
JPanel ipPanel = new JPanel(new BorderLayout(5, 5));
|
|
||||||
ipPanel.add(new JLabel("IP destino:"), BorderLayout.WEST);
|
|
||||||
ipPanel.add(ipField, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
messageField = new JTextField();
|
|
||||||
|
|
||||||
JPanel bottomPanel = new JPanel(new BorderLayout(10, 10));
|
|
||||||
|
|
||||||
bottomPanel.add(ipPanel, BorderLayout.WEST);
|
|
||||||
bottomPanel.add(messageField, BorderLayout.CENTER);
|
|
||||||
initButtons(bottomPanel);
|
|
||||||
|
|
||||||
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initButtons(JPanel bottomPanel) {
|
|
||||||
tcpButton = new JButton("TCP");
|
|
||||||
udpButton = new JButton("UDP");
|
|
||||||
multicastButton = new JButton("Multicast");
|
|
||||||
|
|
||||||
tcpButton.addActionListener(e -> sendTCP());
|
|
||||||
udpButton.addActionListener(e -> sendUDP());
|
|
||||||
multicastButton.addActionListener(e -> sendMulticast());
|
|
||||||
|
|
||||||
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
|
|
||||||
|
|
||||||
buttonsPanel.add(tcpButton);
|
|
||||||
buttonsPanel.add(udpButton);
|
|
||||||
buttonsPanel.add(multicastButton);
|
|
||||||
|
|
||||||
bottomPanel.add(buttonsPanel, BorderLayout.EAST);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendTCP() {
|
|
||||||
try {
|
|
||||||
TestMessage msg = createMessage("TCP Unicast");
|
|
||||||
InetAddress ip = InetAddress.getByName(ipField.getText());
|
|
||||||
mainWindow.getSocketTCP().send(msg.toByteArray(), ip);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
public void sendUDP() {
|
|
||||||
try {
|
|
||||||
TestMessage msg = createMessage("UDP Unicast");
|
|
||||||
InetAddress ip = InetAddress.getByName(ipField.getText());
|
|
||||||
mainWindow.getSocketUDP().sendUnicast(msg.toByteArray(), ip);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void sendMulticast() {
|
|
||||||
try {
|
|
||||||
TestMessage msg = createMessage("UDP Multicast");
|
|
||||||
mainWindow.getSocketUDP().sendMulticast(msg.toByteArray());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private TestMessage createMessage(String tipoMensagem) {
|
|
||||||
try {
|
|
||||||
String nome = mainWindow.getUserName();
|
|
||||||
String ip = InetAddress.getLocalHost().getHostAddress();
|
|
||||||
String message = messageField.getText();
|
|
||||||
return new TestMessage(nome, ip, tipoMensagem, message);
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void receiveMessage(TestMessage msg) {
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
String mensagem = String.format(
|
|
||||||
"[%s] %s (%s): %s%n",
|
|
||||||
msg.getTipoMensagem(),
|
|
||||||
msg.getNome(),
|
|
||||||
msg.getIp(),
|
|
||||||
msg.getMessage()
|
|
||||||
);
|
|
||||||
|
|
||||||
receivedMessagesArea.append(mensagem);
|
|
||||||
|
|
||||||
receivedMessagesArea.setCaretPosition(
|
|
||||||
receivedMessagesArea.getDocument().getLength()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +1 @@
|
|||||||
plugin.SocketTest
|
plugin.TestChat
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package control;
|
||||||
|
|
||||||
|
import GUI.ChatMenu;
|
||||||
|
import GUI.ChatWindow;
|
||||||
|
import GUI.PrivateChatWindow;
|
||||||
|
import GUI.PublicChatWindow;
|
||||||
|
import net.TestChatMessage;
|
||||||
|
import pitiupi.GUI.MainWindow;
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class ChatController {
|
||||||
|
ConnectionManager connectionManager;
|
||||||
|
ChatMenu chatMenu;
|
||||||
|
|
||||||
|
PublicChatWindow publicChatWindow;
|
||||||
|
|
||||||
|
private final HashMap<TcpConnection, JMenuItem> chatMenus;
|
||||||
|
private final HashMap<TcpConnection, ChatWindow> chatWindows;
|
||||||
|
|
||||||
|
public ChatController(ConnectionManager connectionManager, ChatMenu chatMenu) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
publicChatWindow = new PublicChatWindow(connectionManager);
|
||||||
|
|
||||||
|
chatMenus = new HashMap<>();
|
||||||
|
chatWindows = new HashMap<>();
|
||||||
|
|
||||||
|
this.chatMenu = chatMenu;
|
||||||
|
|
||||||
|
this.chatMenu.newMenuItem(new JMenuItem("Show global chat"), publicChatWindow);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addChatWindow(TcpConnection tcpConnection) {
|
||||||
|
if (chatWindows.containsKey(tcpConnection)) return;
|
||||||
|
|
||||||
|
PrivateChatWindow newChat = new PrivateChatWindow(connectionManager, tcpConnection);
|
||||||
|
chatWindows.put(tcpConnection, newChat);
|
||||||
|
JMenuItem newMenuItem = new JMenuItem();
|
||||||
|
chatMenus.put(tcpConnection, newMenuItem);
|
||||||
|
newMenuItem.setText(tcpConnection.getAddress().toString());
|
||||||
|
chatMenu.newMenuItem(newMenuItem, newChat);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeChatWindow(TcpConnection tcpConnection) {
|
||||||
|
ChatWindow removedWindow = chatWindows.remove(tcpConnection);
|
||||||
|
JMenuItem removedMenu = chatMenus.remove(tcpConnection);
|
||||||
|
|
||||||
|
chatMenu.removeMenuItem(removedMenu);
|
||||||
|
removedWindow.setVisible(false);
|
||||||
|
removedWindow.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void receivePublic(TestChatMessage message) {
|
||||||
|
publicChatWindow.receiveMessage(message);
|
||||||
|
}
|
||||||
|
public void receivePrivate(TcpConnection tcpConnection, TestChatMessage message) {
|
||||||
|
chatWindows.get(tcpConnection).receiveMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-3
@@ -2,20 +2,20 @@ package net;
|
|||||||
|
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.Message;
|
||||||
|
|
||||||
public class TestMessage extends Message {
|
public class TestChatMessage extends Message {
|
||||||
private String nome;
|
private String nome;
|
||||||
private String ip;
|
private String ip;
|
||||||
private String tipoMensagem;
|
private String tipoMensagem;
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
public TestMessage(String nome, String ip, String tipoMensagem, String message) {
|
public TestChatMessage(String nome, String ip, String tipoMensagem, String message) {
|
||||||
this.nome = nome;
|
this.nome = nome;
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
this.tipoMensagem = tipoMensagem;
|
this.tipoMensagem = tipoMensagem;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TestMessage(){}
|
public TestChatMessage(){}
|
||||||
|
|
||||||
public String getNome() {
|
public String getNome() {
|
||||||
return nome;
|
return nome;
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
package plugin;
|
|
||||||
|
|
||||||
import GUI.SocketTestMenu;
|
|
||||||
import GUI.SocketTestWindow;
|
|
||||||
import net.TestMessage;
|
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
import pitiupi.net.Message;
|
|
||||||
import pitiupi.plugin.Plugin;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
|
|
||||||
public class SocketTest implements Plugin{
|
|
||||||
SocketTestWindow socketTestWindow;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return "Test Socket";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAuthor() {
|
|
||||||
return "Gustavo Henrique";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getVersion() {
|
|
||||||
return "0";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void createPlugin(MainWindow mainWindow) {
|
|
||||||
this.socketTestWindow = new SocketTestWindow(mainWindow);
|
|
||||||
mainWindow.addMenu(new SocketTestMenu(socketTestWindow));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Message getMessage() {
|
|
||||||
return new TestMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void receiveMessage(byte[] bytes) {
|
|
||||||
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
|
||||||
ObjectInputStream ois;
|
|
||||||
try {
|
|
||||||
ois = new ObjectInputStream(bis);
|
|
||||||
if (ois.readObject() instanceof TestMessage msg) {
|
|
||||||
this.socketTestWindow.receiveMessage(msg);
|
|
||||||
}
|
|
||||||
} catch (IOException | ClassNotFoundException ex) {
|
|
||||||
System.out.println(ex.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package plugin;
|
||||||
|
|
||||||
|
import GUI.ChatMenu;
|
||||||
|
import control.ChatController;
|
||||||
|
import net.TestChatMessage;
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.control.GuiManager;
|
||||||
|
import pitiupi.net.Message;
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
import pitiupi.plugin.PrivateConnectionPlugin;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
|
||||||
|
public class TestChat implements PrivateConnectionPlugin{
|
||||||
|
ChatController chatController;
|
||||||
|
ConnectionManager connectionManager;
|
||||||
|
GuiManager guiManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "MultiProtocol chat";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthor() {
|
||||||
|
return "Gustavo Henrique";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getVersion() {
|
||||||
|
return "0";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier() {
|
||||||
|
return "Multi.Protocol.Chat.Pitiupi";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
this.guiManager = guiManager;
|
||||||
|
|
||||||
|
ChatMenu chatMenu = new ChatMenu(this);
|
||||||
|
chatController = new ChatController(connectionManager, chatMenu);
|
||||||
|
guiManager.addMenu(chatMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Message getMessage() {
|
||||||
|
return new TestChatMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveMessage(byte[] message) {
|
||||||
|
TestChatMessage testChatMessage = parseMessage(message);
|
||||||
|
chatController.receivePublic(testChatMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receivePrivateConnection(TcpConnection tcpConnection) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
chatController.addChatWindow(tcpConnection);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveBytes(TcpConnection connection, byte[] bytes) {
|
||||||
|
TestChatMessage testChatMessage = parseMessage(bytes);
|
||||||
|
chatController.receivePrivate(connection, testChatMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSocketClosed(TcpConnection tcpConnection) {
|
||||||
|
chatController.removeChatWindow(tcpConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean openConnection(String ip) {
|
||||||
|
try {
|
||||||
|
connectionManager.connectPluginTcp(InetAddress.getByName(ip), getIdentifier(), getIdentifier());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.out.println("Error opening connection: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TestChatMessage parseMessage(byte[] bytes) {
|
||||||
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||||
|
ObjectInputStream ois;
|
||||||
|
try {
|
||||||
|
ois = new ObjectInputStream(bis);
|
||||||
|
if (ois.readObject() instanceof TestChatMessage msg) {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
} catch (IOException | ClassNotFoundException ex) {
|
||||||
|
System.out.println(ex.toString());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@@ -48,7 +48,6 @@ public final class Login extends JDialog {
|
|||||||
public void initComponents() {
|
public void initComponents() {
|
||||||
this.setTitle("Login");
|
this.setTitle("Login");
|
||||||
|
|
||||||
|
|
||||||
String nomeSalvo = prefs.get(PREF_NOME, System.getProperty("user.name"));
|
String nomeSalvo = prefs.get(PREF_NOME, System.getProperty("user.name"));
|
||||||
int portaSalva = prefs.getInt(PREF_PORTA, 8000);
|
int portaSalva = prefs.getInt(PREF_PORTA, 8000);
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
package pitiupi.GUI;
|
package pitiupi.GUI;
|
||||||
|
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.control.GuiManager;
|
||||||
import pitiupi.control.PluginManager;
|
import pitiupi.control.PluginManager;
|
||||||
|
|
||||||
import javax.swing.JMenu;
|
import javax.swing.JMenu;
|
||||||
@@ -28,18 +29,13 @@ import pitiupi.control.HeartbeatManager;
|
|||||||
*/
|
*/
|
||||||
public class MainWindow extends javax.swing.JFrame {
|
public class MainWindow extends javax.swing.JFrame {
|
||||||
|
|
||||||
private final MenuBar menuBar;
|
private MenuBar menuBar;
|
||||||
private javax.swing.JScrollPane jScrollPane1;
|
|
||||||
private javax.swing.JLabel status;
|
private javax.swing.JLabel status;
|
||||||
private javax.swing.JPanel statusPanel;
|
|
||||||
private javax.swing.JTabbedPane tabbedPane;
|
|
||||||
|
|
||||||
private OnlineUsersPanel onlineUsersPanel;
|
private final ConnectionManager connectionManager;
|
||||||
|
private final PluginManager pluginManager;
|
||||||
|
private final GuiManager guiManager;
|
||||||
|
|
||||||
private String userName;
|
|
||||||
|
|
||||||
private ConnectionManager connectionManager;
|
|
||||||
private PluginManager pluginManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cria a janela principal da aplicação.
|
* Cria a janela principal da aplicação.
|
||||||
@@ -48,74 +44,79 @@ public class MainWindow extends javax.swing.JFrame {
|
|||||||
* @param port porta utilizada na comunicação multicast.
|
* @param port porta utilizada na comunicação multicast.
|
||||||
*/
|
*/
|
||||||
public MainWindow(String userName, int port) {
|
public MainWindow(String userName, int port) {
|
||||||
this.userName = userName;
|
initMenu();
|
||||||
|
|
||||||
pluginManager = new PluginManager(this);
|
pluginManager = new PluginManager(this);
|
||||||
|
|
||||||
|
connectionManager = new ConnectionManager(this, userName, pluginManager, port);
|
||||||
|
guiManager = new GuiManager(this);
|
||||||
|
|
||||||
|
initUserPanel();
|
||||||
|
initStatusPanel();
|
||||||
|
|
||||||
loadPlugins();
|
loadPlugins();
|
||||||
|
|
||||||
connectionManager = new ConnectionManager(this, pluginManager, port);
|
|
||||||
|
|
||||||
menuBar = new MenuBar(this);
|
|
||||||
|
|
||||||
initComponents();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initComponents() {
|
|
||||||
this.setJMenuBar(menuBar);
|
|
||||||
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
|
||||||
this.setTitle("Share My Sheet");
|
|
||||||
|
|
||||||
jScrollPane1 = new javax.swing.JScrollPane();
|
|
||||||
tabbedPane = new javax.swing.JTabbedPane();
|
|
||||||
statusPanel = new javax.swing.JPanel();
|
|
||||||
status = new javax.swing.JLabel();
|
|
||||||
|
|
||||||
jScrollPane1.setViewportView(tabbedPane);
|
|
||||||
statusPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
|
|
||||||
updateStatus();
|
|
||||||
statusPanel.add(status);
|
|
||||||
|
|
||||||
this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
|
|
||||||
this.getContentPane().add(statusPanel, java.awt.BorderLayout.PAGE_END);
|
|
||||||
|
|
||||||
this.onlineUsersPanel = new OnlineUsersPanel();
|
|
||||||
this.connectionManager.addPeerListener(onlineUsersPanel);
|
|
||||||
this.getContentPane().add(this.onlineUsersPanel, java.awt.BorderLayout.LINE_END);
|
|
||||||
|
|
||||||
this.pack();
|
this.pack();
|
||||||
|
|
||||||
this.setSize(new java.awt.Dimension(1024, 768));
|
this.setSize(new java.awt.Dimension(1024, 768));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void initMenu() {
|
||||||
|
menuBar = new MenuBar(this);
|
||||||
|
|
||||||
|
this.setJMenuBar(menuBar);
|
||||||
|
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
this.setTitle("Share My Sheet");
|
||||||
|
|
||||||
|
javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
|
||||||
|
javax.swing.JTabbedPane tabbedPane = new javax.swing.JTabbedPane();
|
||||||
|
|
||||||
|
jScrollPane1.setViewportView(tabbedPane);
|
||||||
|
|
||||||
|
this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initUserPanel() {
|
||||||
|
OnlineUsersPanel onlineUsersPanel = new OnlineUsersPanel();
|
||||||
|
this.connectionManager.addPeerListener(onlineUsersPanel);
|
||||||
|
this.getContentPane().add(onlineUsersPanel, java.awt.BorderLayout.LINE_END);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initStatusPanel() {
|
||||||
|
javax.swing.JPanel statusPanel = new javax.swing.JPanel();
|
||||||
|
status = new javax.swing.JLabel();
|
||||||
|
|
||||||
|
statusPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
|
||||||
|
updateStatus();
|
||||||
|
statusPanel.add(status);
|
||||||
|
|
||||||
|
this.getContentPane().add(statusPanel, java.awt.BorderLayout.PAGE_END);
|
||||||
|
}
|
||||||
|
|
||||||
public void exit() {
|
public void exit() {
|
||||||
this.setVisible(false);
|
this.setVisible(false);
|
||||||
this.dispose();
|
this.dispose();
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUserName(){
|
|
||||||
return this.userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConnectionManager getConnectionManager() {
|
public ConnectionManager getConnectionManager() {
|
||||||
return connectionManager;
|
return connectionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadPlugins() {
|
public void loadPlugins() {
|
||||||
pluginManager.loadPlugins();
|
pluginManager.loadPlugins(connectionManager, guiManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMenu(JMenu menu) {
|
public void addMenu(JMenu menu) {
|
||||||
this.menuBar.addPlugin(menu);
|
this.menuBar.addPlugin(menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
public void setUserName(String userName) {
|
||||||
this.userName = userName;
|
connectionManager.setUserName(userName);
|
||||||
updateStatus();
|
updateStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateStatus() {
|
private void updateStatus() {
|
||||||
this.status.setText("Connected on port " + connectionManager + " name " + this.userName);
|
this.status.setText("Connected on port " + connectionManager + " name " + connectionManager.getUserName());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,23 +12,26 @@ import java.net.InetAddress;
|
|||||||
public class ConnectionManager {
|
public class ConnectionManager {
|
||||||
private final String pitiupiIdentifier;
|
private final String pitiupiIdentifier;
|
||||||
|
|
||||||
private PluginManager pluginManager;
|
private final PluginManager pluginManager;
|
||||||
|
|
||||||
private HeartbeatManager heartbeatManager;
|
private final HeartbeatManager heartbeatManager;
|
||||||
|
|
||||||
private SocketTCP socketTCP;
|
private SocketTCP socketTCP;
|
||||||
private SocketUDP socketUDP;
|
private SocketUDP socketUDP;
|
||||||
|
private String username;
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
public ConnectionManager(MainWindow main, PluginManager pluginManager, int port) {
|
public ConnectionManager(MainWindow main, String username, PluginManager pluginManager, int port) {
|
||||||
this.pluginManager = pluginManager;
|
this.pluginManager = pluginManager;
|
||||||
|
|
||||||
|
this.username = username;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
|
||||||
pitiupiIdentifier = "Pitiupi.Alice.2026";
|
pitiupiIdentifier = "Pitiupi.Alice.2026";
|
||||||
|
|
||||||
reconnect();
|
reconnect();
|
||||||
|
|
||||||
heartbeatManager = new HeartbeatManager(main, this);
|
heartbeatManager = new HeartbeatManager(this);
|
||||||
heartbeatManager.start();
|
heartbeatManager.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,23 +52,18 @@ public class ConnectionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean sendUdpMulticast(Message message) {
|
public void sendUdpMulticast(Message message) {
|
||||||
try {
|
try {
|
||||||
socketUDP.sendMulticast(message);
|
socketUDP.sendMulticast(message);
|
||||||
return true;
|
} catch (Exception _) {
|
||||||
} catch (Exception ex) {
|
System.err.println("Error sending UDP Multicast");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastLocal(Message message) {
|
public void broadcastLocal(byte[] message) {
|
||||||
pluginManager.handlePublicMessage(message);
|
pluginManager.handlePublicMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean sendLocal(byte[] message, String receiverId) {
|
|
||||||
return pluginManager.handlePrivateMessage(message, receiverId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addPeerListener(PeerListener peerListener) {
|
public void addPeerListener(PeerListener peerListener) {
|
||||||
heartbeatManager.addPeerListener(peerListener);
|
heartbeatManager.addPeerListener(peerListener);
|
||||||
}
|
}
|
||||||
@@ -101,4 +99,12 @@ public class ConnectionManager {
|
|||||||
this.socketUDP.start();
|
this.socketUDP.start();
|
||||||
this.socketTCP.start();
|
this.socketTCP.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return this.username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.username = userName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package pitiupi.control;
|
||||||
|
|
||||||
|
import pitiupi.GUI.MainWindow;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GuiManager {
|
||||||
|
private MainWindow mainWindow;
|
||||||
|
|
||||||
|
public GuiManager(MainWindow mainWindow) {
|
||||||
|
this.mainWindow = mainWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMenu(JMenu menu) {
|
||||||
|
mainWindow.addMenu(menu);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,8 +28,6 @@ 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 MainWindow mainWindow;
|
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
private final Map<String, PeerInfo> peers = new ConcurrentHashMap<>();
|
private final Map<String, PeerInfo> peers = new ConcurrentHashMap<>();
|
||||||
private final List<PeerListener> listeners = new CopyOnWriteArrayList<>();
|
private final List<PeerListener> listeners = new CopyOnWriteArrayList<>();
|
||||||
@@ -39,11 +37,8 @@ public class HeartbeatManager {
|
|||||||
/**
|
/**
|
||||||
* Cria um gerenciador de heartbeat associado à janela principal da aplicação.
|
* Cria um gerenciador de heartbeat associado à janela principal da aplicação.
|
||||||
*
|
*
|
||||||
* @param mainWindow janela principal utilizada para obter o nome do usuário
|
|
||||||
* e o socket de comunicação.
|
|
||||||
*/
|
*/
|
||||||
public HeartbeatManager(MainWindow mainWindow, ConnectionManager connectionManager) {
|
public HeartbeatManager(ConnectionManager connectionManager) {
|
||||||
this.mainWindow = mainWindow;
|
|
||||||
this.connectionManager = connectionManager;
|
this.connectionManager = connectionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,7 +61,7 @@ public class HeartbeatManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void announce() {
|
private void announce() {
|
||||||
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
|
HeartbeatMessage msg = new HeartbeatMessage(connectionManager.getUserName(), instanceId);
|
||||||
connectionManager.sendUdpMulticast(msg);
|
connectionManager.sendUdpMulticast(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class PluginLoader {
|
|||||||
this.mainWindow = main;
|
this.mainWindow = main;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadPlugins(List<Plugin> plugins, HashSet<String> loadedJars) {
|
public void loadPlugins(List<Plugin> plugins, HashSet<String> loadedJars, ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
File pluginDir = new File("plugins");
|
File pluginDir = new File("plugins");
|
||||||
File[] jarFiles = pluginDir.listFiles((dir, name) -> name.endsWith(".jar"));
|
File[] jarFiles = pluginDir.listFiles((dir, name) -> name.endsWith(".jar"));
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ public class PluginLoader {
|
|||||||
for (Plugin plugin : serviceLoader) {
|
for (Plugin plugin : serviceLoader) {
|
||||||
System.out.println("Loading: " + plugin.getName());
|
System.out.println("Loading: " + plugin.getName());
|
||||||
|
|
||||||
plugin.createPlugin(this.mainWindow);
|
plugin.createPlugin(connectionManager, guiManager);
|
||||||
plugins.add(plugin);
|
plugins.add(plugin);
|
||||||
loadedJars.add(file.getName());
|
loadedJars.add(file.getName());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package pitiupi.control;
|
package pitiupi.control;
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.GUI.MainWindow;
|
||||||
import pitiupi.net.Message;
|
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
import pitiupi.plugin.Plugin;
|
import pitiupi.plugin.Plugin;
|
||||||
import pitiupi.plugin.PrivateConnectionPlugin;
|
import pitiupi.plugin.PrivateConnectionPlugin;
|
||||||
@@ -11,7 +10,7 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class PluginManager {
|
public class PluginManager {
|
||||||
private PluginLoader pluginLoader;
|
private final PluginLoader pluginLoader;
|
||||||
private final List<Plugin> plugins;
|
private final List<Plugin> plugins;
|
||||||
private final HashSet<String> loadedJars;
|
private final HashSet<String> loadedJars;
|
||||||
|
|
||||||
@@ -21,11 +20,11 @@ public class PluginManager {
|
|||||||
this.loadedJars = new HashSet<>();
|
this.loadedJars = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadPlugins() {
|
public void loadPlugins(ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
pluginLoader.loadPlugins(plugins, loadedJars);
|
pluginLoader.loadPlugins(plugins, loadedJars, connectionManager, guiManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handlePublicMessage(Message msg) {
|
public void handlePublicMessage(byte[] msg) {
|
||||||
for (Plugin plugin : plugins) {
|
for (Plugin plugin : plugins) {
|
||||||
plugin.receiveMessage(msg);
|
plugin.receiveMessage(msg);
|
||||||
}
|
}
|
||||||
@@ -38,16 +37,14 @@ public class PluginManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean handlePrivateMessage(byte[] msg, String receiverId) {
|
public void handlePrivateMessage(TcpConnection connection, byte[] msg) {
|
||||||
Plugin plugin = getPlugin(receiverId);
|
Plugin plugin = getPlugin(connection.getReceiver());
|
||||||
if (plugin == null) return false;
|
if (plugin == null) return;
|
||||||
|
|
||||||
if (plugin instanceof PrivateConnectionPlugin privatePlugin) {
|
if (plugin instanceof PrivateConnectionPlugin privatePlugin) {
|
||||||
privatePlugin.receiveBytes(msg);
|
privatePlugin.receiveBytes(connection, msg);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean handleConnection(TcpConnection connection, String receiverId) {
|
public boolean handleConnection(TcpConnection connection, String receiverId) {
|
||||||
|
|||||||
@@ -102,6 +102,9 @@ public class ConnectionFactory {
|
|||||||
|
|
||||||
if (handshake == null) return null;
|
if (handshake == null) return null;
|
||||||
|
|
||||||
|
|
||||||
|
connection.send(handshake.createResponse().toByteArray());
|
||||||
|
|
||||||
connection.setSender(handshake.getSender());
|
connection.setSender(handshake.getSender());
|
||||||
connection.setReceiver(handshake.getReceiver());
|
connection.setReceiver(handshake.getReceiver());
|
||||||
connection.setPrivate(handshake.isPrivate());
|
connection.setPrivate(handshake.isPrivate());
|
||||||
@@ -148,10 +151,9 @@ public class ConnectionFactory {
|
|||||||
Object object = ois.readObject();
|
Object object = ois.readObject();
|
||||||
|
|
||||||
if (object instanceof Message message) {
|
if (object instanceof Message message) {
|
||||||
if (SysInfoMessage.parseSystemInfo(message) != null) {
|
SysInfoMessage sysInfo = SysInfoMessage.parseSystemInfo(bytes);
|
||||||
//TODO: Tratar erros
|
if (sysInfo != null) return null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (message instanceof HandshakeMessage handshake) {
|
if (message instanceof HandshakeMessage handshake) {
|
||||||
return (validateHandshake(handshake, connection)) ? handshake : null;
|
return (validateHandshake(handshake, connection)) ? handshake : null;
|
||||||
@@ -218,6 +220,7 @@ public class ConnectionFactory {
|
|||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
package pitiupi.net.tcp;
|
package pitiupi.net.tcp;
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.control.PluginManager;
|
import pitiupi.control.PluginManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.Message;
|
||||||
import pitiupi.plugin.Plugin;
|
|
||||||
import pitiupi.plugin.PrivateConnectionPlugin;
|
import pitiupi.plugin.PrivateConnectionPlugin;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -36,14 +34,15 @@ public class SocketTCP extends Thread {
|
|||||||
|
|
||||||
private final ExecutorService connectionExecutor = Executors.newCachedThreadPool();
|
private final ExecutorService connectionExecutor = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
|
private final PluginManager pluginManager;
|
||||||
|
|
||||||
private final TcpConnectionManager tcpConnectionManager;
|
private final TcpConnectionManager tcpConnectionManager;
|
||||||
|
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private InetAddress address;
|
private InetAddress address;
|
||||||
|
|
||||||
private final ConnectionManager connectionManager;
|
|
||||||
private final PluginManager pluginManager;
|
|
||||||
|
|
||||||
private volatile boolean running = true;
|
private volatile boolean running = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,7 +94,6 @@ public class SocketTCP extends Thread {
|
|||||||
System.out.println("Could not establish TCP connection.");
|
System.out.println("Could not establish TCP connection.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.send(msg.toByteArray());
|
connection.send(msg.toByteArray());
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Could not send TCP message.");
|
System.out.println("Could not send TCP message.");
|
||||||
@@ -207,7 +205,7 @@ public class SocketTCP extends Thread {
|
|||||||
saveConnection(connection);
|
saveConnection(connection);
|
||||||
|
|
||||||
connectionExecutor.submit(() -> {
|
connectionExecutor.submit(() -> {
|
||||||
startReceiver(connection, false);
|
startReceiver(connection);
|
||||||
});
|
});
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
@@ -228,7 +226,7 @@ public class SocketTCP extends Thread {
|
|||||||
if (connection == null) return;
|
if (connection == null) return;
|
||||||
|
|
||||||
saveConnection(connection);
|
saveConnection(connection);
|
||||||
startReceiver(connection, true);
|
startReceiver(connection);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,10 +257,9 @@ public class SocketTCP extends Thread {
|
|||||||
* estabelecimento da conexão.</p>
|
* estabelecimento da conexão.</p>
|
||||||
*
|
*
|
||||||
* @param connection conexão cujo fluxo de entrada será processado.
|
* @param connection conexão cujo fluxo de entrada será processado.
|
||||||
* @param notify indica se o plugin destinatário deve ser notificado sobre
|
|
||||||
* o recebimento de uma nova conexão privada.
|
|
||||||
*/
|
*/
|
||||||
private void startReceiver(TcpConnection connection, boolean notify) {
|
private void startReceiver(TcpConnection connection) {
|
||||||
|
|
||||||
if (!connection.isPrivate()) {
|
if (!connection.isPrivate()) {
|
||||||
publicConnectionReceiver(connection);
|
publicConnectionReceiver(connection);
|
||||||
return;
|
return;
|
||||||
@@ -302,45 +299,23 @@ public class SocketTCP extends Thread {
|
|||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
byte[] bytes = connection.receive();
|
byte[] bytes = connection.receive();
|
||||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
|
||||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
|
||||||
|
|
||||||
Object object = ois.readObject();
|
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(bytes);
|
||||||
if (object instanceof Message message) {
|
|
||||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
|
||||||
|
|
||||||
if (sysMessage == null) {
|
if (sysMessage == null) {
|
||||||
pluginManager.handlePublicMessage(message);
|
pluginManager.handlePublicMessage(bytes);
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
switch (sysMessage.getType()) {
|
|
||||||
case CONNECTION_CLOSED, INTERNAL_ERROR -> {
|
|
||||||
tcpConnectionManager.closeConnection(connection);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
case MESSAGE_REJECTED -> {
|
|
||||||
logger.log(Level.SEVERE, "Somehow sent something that was not an Message");
|
|
||||||
}
|
|
||||||
default -> {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
SysInfoMessage.sendSystemInfo(
|
|
||||||
connection,
|
|
||||||
SysInfoType.MESSAGE_REJECTED,
|
|
||||||
"Public messages must be instances of Message"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ex) {
|
|
||||||
SysInfoMessage.sendSystemInfo(
|
switch (sysMessage.getType()) {
|
||||||
connection,
|
case CONNECTION_CLOSED, INTERNAL_ERROR -> {
|
||||||
SysInfoType.INTERNAL_ERROR,
|
tcpConnectionManager.closeConnection(connection);
|
||||||
"class Message not found."
|
return;
|
||||||
);
|
}
|
||||||
logger.log(Level.SEVERE, null, ex);
|
case MESSAGE_REJECTED -> {
|
||||||
tcpConnectionManager.closeConnection(connection);
|
logger.log(Level.SEVERE, "Somehow sent something that was not an Message");
|
||||||
|
}
|
||||||
|
default -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SocketTimeoutException ex) {
|
catch (SocketTimeoutException ex) {
|
||||||
@@ -373,7 +348,7 @@ public class SocketTCP extends Thread {
|
|||||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
||||||
|
|
||||||
if (sysMessage == null) {
|
if (sysMessage == null) {
|
||||||
pluginManager.handlePrivateMessage(message, connection.getReceiver());
|
pluginManager.handlePrivateMessage(connection, message);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -447,7 +422,6 @@ public class SocketTCP extends Thread {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
receive();
|
receive();
|
||||||
|
|||||||
@@ -48,11 +48,4 @@ public class SysInfoMessage extends Message {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SysInfoMessage parseSystemInfo(Message message) {
|
|
||||||
if (message instanceof SysInfoMessage) {
|
|
||||||
return (SysInfoMessage) message;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -40,8 +40,8 @@ public class SocketUDP extends Thread {
|
|||||||
|
|
||||||
private InetAddress address;
|
private InetAddress address;
|
||||||
|
|
||||||
private ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
private PluginManager pluginManager;
|
private final PluginManager pluginManager;
|
||||||
|
|
||||||
private volatile boolean running = true;
|
private volatile boolean running = true;
|
||||||
public final static String INET_ADDR = "224.0.0.3";
|
public final static String INET_ADDR = "224.0.0.3";
|
||||||
@@ -152,19 +152,14 @@ public class SocketUDP extends Thread {
|
|||||||
}
|
}
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
|
|
||||||
HeartbeatMessage heartbeat = tryParseHeartbeat(msgPacket.getData());
|
HeartbeatMessage heartbeat = tryParseHeartbeat(msgPacket.getData());
|
||||||
if (heartbeat != null) {
|
if (heartbeat != null) {
|
||||||
connectionManager.receiveHeartbeat(heartbeat, msgPacket.getAddress());
|
connectionManager.receiveHeartbeat(heartbeat, msgPacket.getAddress());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Message message = tryParseMessage(msgPacket.getData());
|
pluginManager.handlePublicMessage(msgPacket.getData());
|
||||||
if (message == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println(msgPacket.getAddress().toString());
|
|
||||||
pluginManager.handlePublicMessage(message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,20 +181,6 @@ public class SocketUDP extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Message tryParseMessage(byte[] data) {
|
|
||||||
try {
|
|
||||||
ByteArrayInputStream bis = new ByteArrayInputStream(data);
|
|
||||||
ObjectInputStream ois = new ObjectInputStream(bis);
|
|
||||||
|
|
||||||
Object object = ois.readObject();
|
|
||||||
|
|
||||||
if (object instanceof Message message) return message;
|
|
||||||
return null;
|
|
||||||
} catch (IOException | ClassNotFoundException ex) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inicia a recepção de mensagens UDP.
|
* Inicia a recepção de mensagens UDP.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
package pitiupi.plugin;
|
package pitiupi.plugin;
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.GUI.MainWindow;
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.control.GuiManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.Message;
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
@@ -30,7 +32,7 @@ public interface Plugin {
|
|||||||
*
|
*
|
||||||
* @param window janela principal da aplicação.
|
* @param window janela principal da aplicação.
|
||||||
*/
|
*/
|
||||||
public void createPlugin(MainWindow window);
|
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retorna uma mensagem utilizada pelo plugin.
|
* Retorna uma mensagem utilizada pelo plugin.
|
||||||
@@ -47,6 +49,6 @@ public interface Plugin {
|
|||||||
*
|
*
|
||||||
* @param message mensagem serializada recebida.
|
* @param message mensagem serializada recebida.
|
||||||
*/
|
*/
|
||||||
public void receiveMessage(Message message);
|
public void receiveMessage(byte[] message);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,6 @@ import pitiupi.net.tcp.TcpConnection;
|
|||||||
|
|
||||||
public interface PrivateConnectionPlugin extends Plugin {
|
public interface PrivateConnectionPlugin extends Plugin {
|
||||||
public void receivePrivateConnection(TcpConnection connection);
|
public void receivePrivateConnection(TcpConnection connection);
|
||||||
public void receiveBytes(byte[] bytes);
|
public void receiveBytes(TcpConnection connection, byte[] bytes);
|
||||||
public void onSocketClosed(TcpConnection connection);
|
public void onSocketClosed(TcpConnection connection);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user