Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67813e4881 | ||
|
|
4cc07a3754 | ||
|
|
fa696194aa | ||
|
|
9fb4952f32 | ||
|
|
96b8faca5b | ||
|
|
cac549f1ee | ||
|
|
a86f4dbf82 | ||
|
|
7168431bcd | ||
|
|
0b7c67fef6 | ||
|
|
15ea8ac207 |
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
#Mon, 17 Aug 2026 14:26:26 -0300
|
#Tue, 29 Sep 2026 11:11:06 -0300
|
||||||
|
|
||||||
|
|
||||||
C\:\\Users\\extre\\OneDrive\\Documentos\\Faculdade\\2026_02\\Monitoria\\Pitiupi\\pitiupi.chat=
|
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\pitiupi\\pitiupi.chat=
|
||||||
|
|
||||||
C\:\\Users\\extre\\OneDrive\\Documentos\\Faculdade\\2026_02\\Monitoria\\Pitiupi\\pitiupi=
|
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\pitiupi\\pitiupi=
|
||||||
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$" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library>
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MODULE_DIR$/../../pitiupi/dist/pitiupi.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -12,6 +12,7 @@ import java.util.logging.Level;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import pitiupi.chat.net.ChatMessage;
|
import pitiupi.chat.net.ChatMessage;
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.GUI.MainWindow;
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.Message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,10 +30,10 @@ public class ChatWindow extends javax.swing.JDialog {
|
|||||||
private javax.swing.JSplitPane jSplitPane1;
|
private javax.swing.JSplitPane jSplitPane1;
|
||||||
private javax.swing.JTextArea jTextArea1;
|
private javax.swing.JTextArea jTextArea1;
|
||||||
|
|
||||||
private final MainWindow mainWindow;
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
public ChatWindow(MainWindow mainWindow) {
|
public ChatWindow(ConnectionManager connectionManager) {
|
||||||
this.mainWindow = mainWindow;
|
this.connectionManager = connectionManager;
|
||||||
initComponents();
|
initComponents();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,17 +95,13 @@ public class ChatWindow extends javax.swing.JDialog {
|
|||||||
if (jTextArea1.getText().trim().isEmpty()) {
|
if (jTextArea1.getText().trim().isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ChatMessage message = new ChatMessage(mainWindow.getUserName(), jTextArea1.getText());
|
ChatMessage message = new ChatMessage(connectionManager.getUserName(), jTextArea1.getText());
|
||||||
this.sendChatMessage(message);
|
this.sendChatMessage(message);
|
||||||
jTextArea1.setText("");
|
jTextArea1.setText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendChatMessage(Message message) {
|
public void sendChatMessage(Message message) {
|
||||||
try {
|
connectionManager.sendUdpMulticast(message);
|
||||||
mainWindow.getSocketUDP().sendMulticast(message.toByteArray());
|
|
||||||
} catch (IOException ex) {
|
|
||||||
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveChatMessage(ChatMessage message) {
|
public void receiveChatMessage(ChatMessage message) {
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ 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.GuiManager;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.Message;
|
||||||
import pitiupi.plugin.Plugin;
|
import pitiupi.plugin.Plugin;
|
||||||
|
|
||||||
@@ -21,16 +23,14 @@ import pitiupi.plugin.Plugin;
|
|||||||
public class ChatPlugin implements Plugin {
|
public class ChatPlugin implements Plugin {
|
||||||
|
|
||||||
private ChatWindow chatWindow;
|
private ChatWindow chatWindow;
|
||||||
private MainWindow mainWindow;
|
|
||||||
|
|
||||||
public ChatPlugin() {
|
public ChatPlugin() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createPlugin(MainWindow mainWindow) {
|
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
this.mainWindow = mainWindow;
|
this.chatWindow = new ChatWindow(connectionManager);
|
||||||
this.chatWindow = new ChatWindow(mainWindow);
|
guiManager.addMenu(new ChatMenu(chatWindow));
|
||||||
this.mainWindow.addMenu(new ChatMenu(chatWindow));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -42,9 +42,7 @@ public class ChatPlugin implements Plugin {
|
|||||||
if (ois.readObject() instanceof ChatMessage msg) {
|
if (ois.readObject() instanceof ChatMessage msg) {
|
||||||
this.chatWindow.receiveChatMessage(msg);
|
this.chatWindow.receiveChatMessage(msg);
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException | ClassNotFoundException ex) {
|
||||||
System.out.println(ex.toString());
|
|
||||||
} catch (ClassNotFoundException ex) {
|
|
||||||
System.out.println(ex.toString());
|
System.out.println(ex.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,6 +62,11 @@ public class ChatPlugin implements Plugin {
|
|||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier() {
|
||||||
|
return "Pitiupi.Chat.Example";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message getMessage() {
|
public Message getMessage() {
|
||||||
return new ChatMessage();
|
return new ChatMessage();
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
JAR = ../pitiupi/dist/pitiupi.jar
|
||||||
|
OUT_DIR = out
|
||||||
|
META-INF_DIR = src/META-INF
|
||||||
|
DIST = dist/mpChat.jar
|
||||||
|
|
||||||
|
SOURCES = $(shell find -name "*.java")
|
||||||
|
|
||||||
|
all:
|
||||||
|
mkdir -p "$(OUT_DIR)"
|
||||||
|
mkdir -p "$(dir $(DIST))"
|
||||||
|
javac -cp "$(JAR)" -d "$(OUT_DIR)" $(SOURCES)
|
||||||
|
cp -r "$(META-INF_DIR)" "$(OUT_DIR)"
|
||||||
|
jar cf "$(DIST)" -C "$(OUT_DIR)" .
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf "$(OUT_DIR)"/*
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1 @@
|
|||||||
|
plugin.MultiProtocolChat
|
||||||
BIN
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
Binary file not shown.
File renamed without changes.
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>
|
||||||
@@ -1,27 +1,24 @@
|
|||||||
package GUI;
|
package GUI;
|
||||||
|
|
||||||
import plugin.TestChat;
|
import plugin.MultiProtocolChat;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ChatMenu extends JMenu {
|
public class ChatMenu extends JMenu {
|
||||||
private final TestChat testChat;
|
private final MultiProtocolChat multiProtocolChat;
|
||||||
|
|
||||||
public ChatMenu(TestChat testChat) {
|
public ChatMenu(MultiProtocolChat multiProtocolChat) {
|
||||||
this.testChat = testChat;
|
this.multiProtocolChat = multiProtocolChat;
|
||||||
this.setText("MP-Chat");
|
this.setText("MP-Chat");
|
||||||
|
|
||||||
JMenuItem startPrivateChat = new JMenuItem("start Private Chat");
|
JMenuItem startPrivateChat = new JMenuItem("start Private Chat");
|
||||||
startPrivateChat.addActionListener(_ -> showPrivateChatDialog());
|
startPrivateChat.addActionListener(e -> showPrivateChatDialog());
|
||||||
this.add(startPrivateChat);
|
this.add(startPrivateChat);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void newMenuItem(JMenuItem newMenuItem, ChatWindow chatWindow) {
|
public void newMenuItem(JMenuItem newMenuItem, ChatWindow chatWindow) {
|
||||||
newMenuItem.addActionListener(_ -> chatWindow.setVisible(true));
|
newMenuItem.addActionListener(e -> chatWindow.setVisible(true));
|
||||||
|
|
||||||
this.add(newMenuItem);
|
this.add(newMenuItem);
|
||||||
}
|
}
|
||||||
@@ -71,7 +68,7 @@ public class ChatMenu extends JMenu {
|
|||||||
startButton.addActionListener(e -> {
|
startButton.addActionListener(e -> {
|
||||||
String ip = ipField.getText();
|
String ip = ipField.getText();
|
||||||
|
|
||||||
if (testChat.openConnection(ip)) dialog.dispose();
|
if (multiProtocolChat.openConnection(ip)) dialog.dispose();
|
||||||
else errorLabel.setVisible(true);
|
else errorLabel.setVisible(true);
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package GUI;
|
package GUI;
|
||||||
|
|
||||||
import net.TestChatMessage;
|
import net.MpChatMessage;
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.control.ConnectionManager;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@@ -10,13 +10,13 @@ import java.net.UnknownHostException;
|
|||||||
|
|
||||||
public abstract class ChatWindow extends JFrame {
|
public abstract class ChatWindow extends JFrame {
|
||||||
|
|
||||||
protected final MainWindow mainWindow;
|
protected final ConnectionManager connectionManager;
|
||||||
|
|
||||||
private JTextArea receivedMessagesArea;
|
private JTextArea receivedMessagesArea;
|
||||||
protected JTextField messageField;
|
protected JTextField messageField;
|
||||||
|
|
||||||
public ChatWindow(MainWindow mainWindow) {
|
public ChatWindow(ConnectionManager connectionManager) {
|
||||||
this.mainWindow = mainWindow;
|
this.connectionManager = connectionManager;
|
||||||
initComponents();
|
initComponents();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,20 +63,20 @@ public abstract class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
protected abstract void initButtons(JPanel bottomPanel);
|
protected abstract void initButtons(JPanel bottomPanel);
|
||||||
|
|
||||||
protected TestChatMessage createMessage(String tipoMensagem) {
|
protected MpChatMessage createMessage(String tipoMensagem) {
|
||||||
try {
|
try {
|
||||||
String nome = mainWindow.getUserName();
|
String nome = connectionManager.getUserName();
|
||||||
String ip = InetAddress.getLocalHost().getHostAddress();
|
String ip = InetAddress.getLocalHost().getHostAddress();
|
||||||
String message = messageField.getText();
|
String message = messageField.getText();
|
||||||
|
|
||||||
return new TestChatMessage(nome, ip, tipoMensagem, message);
|
return new MpChatMessage(nome, ip, tipoMensagem, message);
|
||||||
|
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveMessage(TestChatMessage msg) {
|
public void receiveMessage(MpChatMessage msg) {
|
||||||
SwingUtilities.invokeLater(() -> {
|
SwingUtilities.invokeLater(() -> {
|
||||||
String mensagem = String.format(
|
String mensagem = String.format(
|
||||||
"[%s] %s (%s): %s%n",
|
"[%s] %s (%s): %s%n",
|
||||||
+6
-9
@@ -1,7 +1,7 @@
|
|||||||
package GUI;
|
package GUI;
|
||||||
|
|
||||||
import net.TestChatMessage;
|
import net.MpChatMessage;
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
@@ -13,10 +13,9 @@ public class PrivateChatWindow extends ChatWindow {
|
|||||||
|
|
||||||
private final TcpConnection connection;
|
private final TcpConnection connection;
|
||||||
|
|
||||||
public PrivateChatWindow(MainWindow mainWindow, TcpConnection connection) {
|
public PrivateChatWindow(ConnectionManager ConnectionManager, TcpConnection connection) {
|
||||||
|
super(ConnectionManager);
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
|
|
||||||
super(mainWindow);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -34,13 +33,11 @@ public class PrivateChatWindow extends ChatWindow {
|
|||||||
|
|
||||||
private void sendTCP() {
|
private void sendTCP() {
|
||||||
try {
|
try {
|
||||||
TestChatMessage msg = createMessage("TCP Private");
|
MpChatMessage msg = createMessage("TCP Private");
|
||||||
connection.send(msg.toByteArray());
|
connection.send(msg.toByteArray());
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.getLogger(
|
Logger.getLogger(PrivateChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
PrivateChatWindow.class.getName()
|
|
||||||
).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+10
-10
@@ -1,7 +1,7 @@
|
|||||||
package GUI;
|
package GUI;
|
||||||
|
|
||||||
import net.TestChatMessage;
|
import net.MpChatMessage;
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.control.ConnectionManager;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@@ -13,8 +13,8 @@ public class PublicChatWindow extends ChatWindow {
|
|||||||
|
|
||||||
private JTextField ipField;
|
private JTextField ipField;
|
||||||
|
|
||||||
public PublicChatWindow(MainWindow mainWindow) {
|
public PublicChatWindow(ConnectionManager connectionManager) {
|
||||||
super(mainWindow);
|
super(connectionManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -56,11 +56,11 @@ public class PublicChatWindow extends ChatWindow {
|
|||||||
|
|
||||||
private void sendTCP() {
|
private void sendTCP() {
|
||||||
try {
|
try {
|
||||||
TestChatMessage msg = createMessage("TCP Unicast");
|
MpChatMessage msg = createMessage("TCP Unicast");
|
||||||
|
|
||||||
InetAddress ip = InetAddress.getByName(ipField.getText());
|
InetAddress ip = InetAddress.getByName(ipField.getText());
|
||||||
|
|
||||||
mainWindow.getSocketTCP().send(msg, ip);
|
connectionManager.sendTcp(msg, ip);
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
@@ -69,11 +69,11 @@ public class PublicChatWindow extends ChatWindow {
|
|||||||
|
|
||||||
private void sendUDP() {
|
private void sendUDP() {
|
||||||
try {
|
try {
|
||||||
TestChatMessage msg = createMessage("UDP Unicast");
|
MpChatMessage msg = createMessage("UDP Unicast");
|
||||||
|
|
||||||
InetAddress ip = InetAddress.getByName(ipField.getText());
|
InetAddress ip = InetAddress.getByName(ipField.getText());
|
||||||
|
|
||||||
mainWindow.getSocketUDP().sendUnicast(msg, ip);
|
connectionManager.sendUdpUnicast(msg, ip);
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(PublicChatWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
@@ -82,9 +82,9 @@ public class PublicChatWindow extends ChatWindow {
|
|||||||
|
|
||||||
private void sendMulticast() {
|
private void sendMulticast() {
|
||||||
try {
|
try {
|
||||||
TestChatMessage msg = createMessage("UDP Multicast");
|
MpChatMessage msg = createMessage("UDP Multicast");
|
||||||
|
|
||||||
mainWindow.getSocketUDP().sendMulticast(msg);
|
connectionManager.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 @@
|
|||||||
|
plugin.MultiProtocolChat
|
||||||
+9
-9
@@ -4,15 +4,15 @@ import GUI.ChatMenu;
|
|||||||
import GUI.ChatWindow;
|
import GUI.ChatWindow;
|
||||||
import GUI.PrivateChatWindow;
|
import GUI.PrivateChatWindow;
|
||||||
import GUI.PublicChatWindow;
|
import GUI.PublicChatWindow;
|
||||||
import net.TestChatMessage;
|
import net.MpChatMessage;
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class ChatController {
|
public class ChatController {
|
||||||
MainWindow mainWindow;
|
ConnectionManager connectionManager;
|
||||||
ChatMenu chatMenu;
|
ChatMenu chatMenu;
|
||||||
|
|
||||||
PublicChatWindow publicChatWindow;
|
PublicChatWindow publicChatWindow;
|
||||||
@@ -20,9 +20,9 @@ public class ChatController {
|
|||||||
private final HashMap<TcpConnection, JMenuItem> chatMenus;
|
private final HashMap<TcpConnection, JMenuItem> chatMenus;
|
||||||
private final HashMap<TcpConnection, ChatWindow> chatWindows;
|
private final HashMap<TcpConnection, ChatWindow> chatWindows;
|
||||||
|
|
||||||
public ChatController(MainWindow mainWindow, ChatMenu chatMenu) {
|
public ChatController(ConnectionManager connectionManager, ChatMenu chatMenu) {
|
||||||
this.mainWindow = mainWindow;
|
this.connectionManager = connectionManager;
|
||||||
publicChatWindow = new PublicChatWindow(mainWindow);
|
publicChatWindow = new PublicChatWindow(connectionManager);
|
||||||
|
|
||||||
chatMenus = new HashMap<>();
|
chatMenus = new HashMap<>();
|
||||||
chatWindows = new HashMap<>();
|
chatWindows = new HashMap<>();
|
||||||
@@ -35,7 +35,7 @@ public class ChatController {
|
|||||||
public void addChatWindow(TcpConnection tcpConnection) {
|
public void addChatWindow(TcpConnection tcpConnection) {
|
||||||
if (chatWindows.containsKey(tcpConnection)) return;
|
if (chatWindows.containsKey(tcpConnection)) return;
|
||||||
|
|
||||||
PrivateChatWindow newChat = new PrivateChatWindow(mainWindow, tcpConnection);
|
PrivateChatWindow newChat = new PrivateChatWindow(connectionManager, tcpConnection);
|
||||||
chatWindows.put(tcpConnection, newChat);
|
chatWindows.put(tcpConnection, newChat);
|
||||||
JMenuItem newMenuItem = new JMenuItem();
|
JMenuItem newMenuItem = new JMenuItem();
|
||||||
chatMenus.put(tcpConnection, newMenuItem);
|
chatMenus.put(tcpConnection, newMenuItem);
|
||||||
@@ -52,10 +52,10 @@ public class ChatController {
|
|||||||
removedWindow.dispose();
|
removedWindow.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receivePublic(TestChatMessage message) {
|
public void receivePublic(MpChatMessage message) {
|
||||||
publicChatWindow.receiveMessage(message);
|
publicChatWindow.receiveMessage(message);
|
||||||
}
|
}
|
||||||
public void receivePrivate(TcpConnection tcpConnection, TestChatMessage message) {
|
public void receivePrivate(TcpConnection tcpConnection, MpChatMessage message) {
|
||||||
chatWindows.get(tcpConnection).receiveMessage(message);
|
chatWindows.get(tcpConnection).receiveMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-3
@@ -2,20 +2,20 @@ package net;
|
|||||||
|
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.Message;
|
||||||
|
|
||||||
public class TestChatMessage extends Message {
|
public class MpChatMessage 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 TestChatMessage(String nome, String ip, String tipoMensagem, String message) {
|
public MpChatMessage(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 TestChatMessage(){}
|
public MpChatMessage(){}
|
||||||
|
|
||||||
public String getNome() {
|
public String getNome() {
|
||||||
return nome;
|
return nome;
|
||||||
+19
-16
@@ -2,8 +2,9 @@ package plugin;
|
|||||||
|
|
||||||
import GUI.ChatMenu;
|
import GUI.ChatMenu;
|
||||||
import control.ChatController;
|
import control.ChatController;
|
||||||
import net.TestChatMessage;
|
import net.MpChatMessage;
|
||||||
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;
|
||||||
import pitiupi.plugin.PrivateConnectionPlugin;
|
import pitiupi.plugin.PrivateConnectionPlugin;
|
||||||
@@ -14,9 +15,10 @@ import java.io.IOException;
|
|||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
|
||||||
public class TestChat implements PrivateConnectionPlugin{
|
public class MultiProtocolChat implements PrivateConnectionPlugin{
|
||||||
ChatController chatController;
|
ChatController chatController;
|
||||||
MainWindow mainWindow;
|
ConnectionManager connectionManager;
|
||||||
|
GuiManager guiManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -39,23 +41,24 @@ public class TestChat implements PrivateConnectionPlugin{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createPlugin(MainWindow mainWindow) {
|
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
this.mainWindow = mainWindow;
|
this.connectionManager = connectionManager;
|
||||||
|
this.guiManager = guiManager;
|
||||||
|
|
||||||
ChatMenu chatMenu = new ChatMenu(this);
|
ChatMenu chatMenu = new ChatMenu(this);
|
||||||
chatController = new ChatController(mainWindow, chatMenu);
|
chatController = new ChatController(connectionManager, chatMenu);
|
||||||
mainWindow.addMenu(chatMenu);
|
guiManager.addMenu(chatMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message getMessage() {
|
public Message getMessage() {
|
||||||
return new TestChatMessage();
|
return new MpChatMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void receiveMessage(byte[] message) {
|
public void receiveMessage(byte[] message) {
|
||||||
TestChatMessage testChatMessage = parseMessage(message);
|
MpChatMessage mpChatMessage = parseMessage(message);
|
||||||
chatController.receivePublic(testChatMessage);
|
chatController.receivePublic(mpChatMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -68,8 +71,8 @@ public class TestChat implements PrivateConnectionPlugin{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void receiveBytes(TcpConnection connection, byte[] bytes) {
|
public void receiveBytes(TcpConnection connection, byte[] bytes) {
|
||||||
TestChatMessage testChatMessage = parseMessage(bytes);
|
MpChatMessage mpChatMessage = parseMessage(bytes);
|
||||||
chatController.receivePrivate(connection, testChatMessage);
|
chatController.receivePrivate(connection, mpChatMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -79,7 +82,7 @@ public class TestChat implements PrivateConnectionPlugin{
|
|||||||
|
|
||||||
public boolean openConnection(String ip) {
|
public boolean openConnection(String ip) {
|
||||||
try {
|
try {
|
||||||
mainWindow.getSocketTCP().createPrivateConnection(InetAddress.getByName(ip), getIdentifier(), getIdentifier());
|
connectionManager.connectPluginTcp(InetAddress.getByName(ip), getIdentifier(), getIdentifier());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
@@ -88,12 +91,12 @@ public class TestChat implements PrivateConnectionPlugin{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TestChatMessage parseMessage(byte[] bytes) {
|
private MpChatMessage parseMessage(byte[] bytes) {
|
||||||
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||||
ObjectInputStream ois;
|
ObjectInputStream ois;
|
||||||
try {
|
try {
|
||||||
ois = new ObjectInputStream(bis);
|
ois = new ObjectInputStream(bis);
|
||||||
if (ois.readObject() instanceof TestChatMessage msg) {
|
if (ois.readObject() instanceof MpChatMessage msg) {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
} catch (IOException | ClassNotFoundException ex) {
|
} catch (IOException | ClassNotFoundException ex) {
|
||||||
Generated
+23
-31
@@ -5,24 +5,10 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="083e72ce-fa9b-4ddf-a0ef-24105aa3eabb" name="Changes" comment="">
|
<list default="true" id="083e72ce-fa9b-4ddf-a0ef-24105aa3eabb" name="Changes" comment="">
|
||||||
<change beforePath="$PROJECT_DIR$/../.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../.idea/misc.xml" afterDir="false" />
|
<change afterPath="$PROJECT_DIR$/../pitiupi.testSocket/Makefile" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/../.idea/vcs.xml" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../.idea/workspace.xml" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi.chat/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi.chat/.idea/misc.xml" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi.chat/build/built-jar.properties" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi.chat/build/built-jar.properties" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi.chat/build/classes/pitiupi/chat/GUI/ChatWindow.class" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi.chat/build/classes/pitiupi/chat/GUI/ChatWindow.class" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi.chat/dist/pitiupi.chat.jar" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi.chat/pitiupi.chat.iml" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi.chat/pitiupi.chat.iml" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi.chat/src/pitiupi/chat/GUI/ChatWindow.java" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi.chat/src/pitiupi/chat/GUI/ChatWindow.java" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/build/built-jar.properties" beforeDir="false" afterPath="$PROJECT_DIR$/build/built-jar.properties" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/../pitiupi/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi/.idea/misc.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/build/classes/pitiupi/screenCast/GUI/ScreenCastWindow.class" beforeDir="false" afterPath="$PROJECT_DIR$/build/classes/pitiupi/screenCast/GUI/ScreenCastWindow.class" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/../pitiupi/src/pitiupi/net/tcp/ConnectionFactory.java" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi/src/pitiupi/net/tcp/ConnectionFactory.java" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/dist/pitiupi.screenCast.jar" beforeDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/src/pitiupi/screenCast/GUI/ScreenCastWindow.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/pitiupi/screenCast/GUI/ScreenCastWindow.java" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi/plugins/pitiupi.chat.jar" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi/plugins/pitiupi.chat.jar" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi/plugins/pitiupi.screenCast.jar" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi/plugins/pitiupi.screenCast.jar" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi/src/pitiupi/GUI/MainWindow.java" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi/src/pitiupi/GUI/MainWindow.java" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/../pitiupi/src/pitiupi/control/HeartbeatManager.java" beforeDir="false" afterPath="$PROJECT_DIR$/../pitiupi/src/pitiupi/control/HeartbeatManager.java" afterDir="false" />
|
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@@ -46,10 +32,12 @@
|
|||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
||||||
"RunOnceActivity.git.unshallow": "true",
|
"RunOnceActivity.git.unshallow": "true",
|
||||||
"git-widget-placeholder": "feature/unicast",
|
"RunOnceActivity.typescript.service.memoryLimit.init": "true",
|
||||||
|
"codeWithMe.voiceChat.enabledByDefault": "false",
|
||||||
|
"git-widget-placeholder": "feature/separacao-gerenciadores",
|
||||||
"ignore.virus.scanning.warn.message": "true",
|
"ignore.virus.scanning.warn.message": "true",
|
||||||
"kotlin-language-version-configured": "true",
|
"kotlin-language-version-configured": "true",
|
||||||
"last_opened_file_path": "C:/Users/extre/OneDrive/Documentos/Faculdade/2026_02/Monitoria/Pitiupi/pitiupi.screenCast",
|
"last_opened_file_path": "C:/Users/Gustavo/OneDrive/Documentos/Faculdade/2026_2/Monitoria/pitiupi/pitiupi.screenCast",
|
||||||
"project.structure.last.edited": "Modules",
|
"project.structure.last.edited": "Modules",
|
||||||
"project.structure.proportion": "0.15",
|
"project.structure.proportion": "0.15",
|
||||||
"project.structure.side.proportion": "0.2",
|
"project.structure.side.proportion": "0.2",
|
||||||
@@ -57,17 +45,6 @@
|
|||||||
}
|
}
|
||||||
}]]></component>
|
}]]></component>
|
||||||
<component name="RunManager">
|
<component name="RunManager">
|
||||||
<configuration default="true" type="JetRunConfigurationType">
|
|
||||||
<module name="pitiupi.screenCast" />
|
|
||||||
<method v="2">
|
|
||||||
<option name="Make" enabled="true" />
|
|
||||||
</method>
|
|
||||||
</configuration>
|
|
||||||
<configuration default="true" type="KotlinStandaloneScriptRunConfigurationType">
|
|
||||||
<module name="pitiupi.screenCast" />
|
|
||||||
<option name="filePath" />
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
<configuration default="true" type="PythonConfigurationType" factoryName="Python">
|
<configuration default="true" type="PythonConfigurationType" factoryName="Python">
|
||||||
<module name="pitiupi.screenCast" />
|
<module name="pitiupi.screenCast" />
|
||||||
<option name="ENV_FILES" value="" />
|
<option name="ENV_FILES" value="" />
|
||||||
@@ -102,11 +79,23 @@
|
|||||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||||
<method v="2" />
|
<method v="2" />
|
||||||
</configuration>
|
</configuration>
|
||||||
|
<configuration default="true" type="JetRunConfigurationType">
|
||||||
|
<module name="pitiupi.screenCast" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
<configuration default="true" type="KotlinStandaloneScriptRunConfigurationType">
|
||||||
|
<module name="pitiupi.screenCast" />
|
||||||
|
<option name="filePath" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
</component>
|
</component>
|
||||||
<component name="SharedIndexes">
|
<component name="SharedIndexes">
|
||||||
<attachedChunks>
|
<attachedChunks>
|
||||||
<set>
|
<set>
|
||||||
<option value="bundled-jdk-9823dce3aa75-bf35d07a577b-intellij.indexing.shared.core-IU-252.28539.54" />
|
<option value="bundled-jdk-30f59d01ecdd-37e91769500f-intellij.indexing.shared.core-IU-261.26222.65" />
|
||||||
|
<option value="bundled-js-predefined-d6986cc7102b-31caf2ab9e3c-JavaScript-IU-261.26222.65" />
|
||||||
</set>
|
</set>
|
||||||
</attachedChunks>
|
</attachedChunks>
|
||||||
</component>
|
</component>
|
||||||
@@ -120,4 +109,7 @@
|
|||||||
</task>
|
</task>
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
<option name="version" value="3" />
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#Mon, 17 Aug 2026 14:35:40 -0300
|
#Tue, 29 Sep 2026 11:04:58 -0300
|
||||||
|
|
||||||
|
|
||||||
C\:\\Users\\extre\\OneDrive\\Documentos\\Faculdade\\2026_02\\Monitoria\\Pitiupi\\pitiupi.screenCast=
|
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\pitiupi\\pitiupi.screenCast=
|
||||||
|
|
||||||
C\:\\Users\\extre\\OneDrive\\Documentos\\Faculdade\\2026_02\\Monitoria\\Pitiupi\\pitiupi=
|
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\pitiupi\\pitiupi=
|
||||||
Binary file not shown.
Binary file not shown.
BIN
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$" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library>
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MODULE_DIR$/../../pitiupi/dist/pitiupi.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -5,6 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.screenCast.GUI;
|
package pitiupi.screenCast.GUI;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.control.GuiManager;
|
||||||
import pitiupi.screenCast.control.AlarmClock;
|
import pitiupi.screenCast.control.AlarmClock;
|
||||||
import pitiupi.screenCast.control.ImageTransformation;
|
import pitiupi.screenCast.control.ImageTransformation;
|
||||||
import pitiupi.screenCast.control.ScreenShot;
|
import pitiupi.screenCast.control.ScreenShot;
|
||||||
@@ -38,10 +40,13 @@ public class ScreenCastWindow extends javax.swing.JFrame {
|
|||||||
public final static int TIME = 1000;
|
public final static int TIME = 1000;
|
||||||
|
|
||||||
private float compressRatio;
|
private float compressRatio;
|
||||||
private final MainWindow mainWindow;
|
private final ConnectionManager connectionManager;
|
||||||
|
private final GuiManager guiManager;
|
||||||
|
|
||||||
|
public ScreenCastWindow(ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
this.guiManager = guiManager;
|
||||||
|
|
||||||
public ScreenCastWindow(MainWindow mainWindow) {
|
|
||||||
this.mainWindow = mainWindow;
|
|
||||||
this.compressRatio = 0.5f;
|
this.compressRatio = 0.5f;
|
||||||
this.initComponents();
|
this.initComponents();
|
||||||
}
|
}
|
||||||
@@ -91,19 +96,12 @@ public class ScreenCastWindow extends javax.swing.JFrame {
|
|||||||
BufferedImage image = ScreenShot.takeAShot();
|
BufferedImage image = ScreenShot.takeAShot();
|
||||||
BufferedImage rescaled = ImageTransformation.resize(image, 1024, 768);
|
BufferedImage rescaled = ImageTransformation.resize(image, 1024, 768);
|
||||||
byte[] byteImage = ImageTransformation.compress(rescaled, compressRatio);
|
byte[] byteImage = ImageTransformation.compress(rescaled, compressRatio);
|
||||||
ScreenCastMessage message = new ScreenCastMessage(this.mainWindow.getUserName(), byteImage);
|
ScreenCastMessage message = new ScreenCastMessage(this.connectionManager.getUserName(), byteImage);
|
||||||
msg = message.toByteArray();
|
|
||||||
|
if (!connectionManager.sendUdpMulticast(message)) this.compressRatio -= 0.1f;
|
||||||
} catch (AWTException | IOException ex) {
|
} catch (AWTException | IOException ex) {
|
||||||
Logger.getLogger(AlarmClock.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(AlarmClock.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
if (msg != null) {
|
|
||||||
try {
|
|
||||||
this.mainWindow.getSocketUDP().sendMulticast(msg);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
System.out.println(ex.toString());
|
|
||||||
this.compressRatio -= 0.1f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveScreenCastMessage(ScreenCastMessage message) {
|
public void receiveScreenCastMessage(ScreenCastMessage message) {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
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.plugin.Plugin;
|
import pitiupi.plugin.Plugin;
|
||||||
import pitiupi.screenCast.GUI.ScreenCastMenu;
|
import pitiupi.screenCast.GUI.ScreenCastMenu;
|
||||||
@@ -21,7 +23,6 @@ import pitiupi.screenCast.net.ScreenCastMessage;
|
|||||||
public class ScreenCastPlugin implements Plugin {
|
public class ScreenCastPlugin implements Plugin {
|
||||||
|
|
||||||
private ScreenCastWindow screenCastWindow;
|
private ScreenCastWindow screenCastWindow;
|
||||||
private MainWindow mainWindow;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -39,10 +40,14 @@ public class ScreenCastPlugin implements Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createPlugin(MainWindow mainWindow) {
|
public String getIdentifier() {
|
||||||
this.mainWindow = mainWindow;
|
return "Pitiupi.ScreenCast.Example";
|
||||||
this.screenCastWindow = new ScreenCastWindow(mainWindow);
|
}
|
||||||
this.mainWindow.addMenu(new ScreenCastMenu(screenCastWindow));
|
|
||||||
|
@Override
|
||||||
|
public void createPlugin(ConnectionManager connectionManager, GuiManager guiManager) {
|
||||||
|
this.screenCastWindow = new ScreenCastWindow(connectionManager, guiManager);
|
||||||
|
guiManager.addMenu(new ScreenCastMenu(screenCastWindow));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -54,9 +59,7 @@ public class ScreenCastPlugin implements Plugin {
|
|||||||
if (ois.readObject() instanceof ScreenCastMessage msg) {
|
if (ois.readObject() instanceof ScreenCastMessage msg) {
|
||||||
this.screenCastWindow.receiveScreenCastMessage(msg);
|
this.screenCastWindow.receiveScreenCastMessage(msg);
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException | ClassNotFoundException ex) {
|
||||||
System.out.println(ex.toString());
|
|
||||||
} catch (ClassNotFoundException ex) {
|
|
||||||
System.out.println(ex.toString());
|
System.out.println(ex.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +0,0 @@
|
|||||||
plugin.TestChat
|
|
||||||
Binary file not shown.
@@ -7,14 +7,5 @@
|
|||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<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>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
@@ -1 +0,0 @@
|
|||||||
plugin.TestChat
|
|
||||||
Generated
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" 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" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -46,8 +46,8 @@ javac.modulepath=
|
|||||||
javac.processormodulepath=
|
javac.processormodulepath=
|
||||||
javac.processorpath=\
|
javac.processorpath=\
|
||||||
${javac.classpath}
|
${javac.classpath}
|
||||||
javac.source=25
|
javac.source=17
|
||||||
javac.target=25
|
javac.target=17
|
||||||
javac.test.classpath=\
|
javac.test.classpath=\
|
||||||
${javac.classpath}:\
|
${javac.classpath}:\
|
||||||
${build.classes.dir}
|
${build.classes.dir}
|
||||||
|
|||||||
@@ -5,13 +5,12 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.GUI;
|
package pitiupi.GUI;
|
||||||
|
|
||||||
import pitiupi.net.tcp.SocketTCP;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.net.udp.SocketUDP;
|
import pitiupi.control.GuiManager;
|
||||||
import java.util.ArrayList;
|
import pitiupi.control.PluginManager;
|
||||||
import java.util.List;
|
|
||||||
import javax.swing.JMenu;
|
import javax.swing.JMenu;
|
||||||
import pitiupi.control.PluginLoader;
|
|
||||||
import pitiupi.plugin.Plugin;
|
|
||||||
import pitiupi.control.HeartbeatManager;
|
import pitiupi.control.HeartbeatManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,27 +29,14 @@ 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 HeartbeatManager heartbeatManager;
|
private final ConnectionManager connectionManager;
|
||||||
private OnlineUsersPanel onlineUsersPanel;
|
private final PluginManager pluginManager;
|
||||||
|
private final GuiManager guiManager;
|
||||||
|
|
||||||
|
|
||||||
private SocketUDP socketUDP;
|
|
||||||
private SocketTCP socketTCP;
|
|
||||||
|
|
||||||
private String userName;
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
private String pitiupiIdentifier;
|
|
||||||
|
|
||||||
private PluginLoader pluginManager;
|
|
||||||
private final List<Plugin> plugins;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cria a janela principal da aplicação.
|
* Cria a janela principal da aplicação.
|
||||||
*
|
*
|
||||||
@@ -58,62 +44,53 @@ 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();
|
||||||
this.port = port;
|
|
||||||
this.pitiupiIdentifier = "Pitiupi.Alice.2026";
|
pluginManager = new PluginManager(this);
|
||||||
this.menuBar = new MenuBar(this);
|
|
||||||
this.plugins = new ArrayList<>();
|
connectionManager = new ConnectionManager(this, userName, pluginManager, port);
|
||||||
this.initComponents();
|
guiManager = new GuiManager(this);
|
||||||
}
|
|
||||||
|
initUserPanel();
|
||||||
|
initStatusPanel();
|
||||||
|
|
||||||
|
loadPlugins();
|
||||||
|
|
||||||
|
this.pack();
|
||||||
|
|
||||||
private void initComponents() {
|
|
||||||
initGUI();
|
|
||||||
initSocket();
|
|
||||||
initHeartbeat();
|
|
||||||
initPlugins();
|
|
||||||
this.setSize(new java.awt.Dimension(1024, 768));
|
this.setSize(new java.awt.Dimension(1024, 768));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initGUI() {
|
private void initMenu() {
|
||||||
|
menuBar = new MenuBar(this);
|
||||||
|
|
||||||
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("Share My Sheet");
|
||||||
|
|
||||||
jScrollPane1 = new javax.swing.JScrollPane();
|
javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
|
||||||
tabbedPane = new javax.swing.JTabbedPane();
|
javax.swing.JTabbedPane tabbedPane = new javax.swing.JTabbedPane();
|
||||||
statusPanel = new javax.swing.JPanel();
|
|
||||||
status = new javax.swing.JLabel();
|
|
||||||
|
|
||||||
jScrollPane1.setViewportView(tabbedPane);
|
jScrollPane1.setViewportView(tabbedPane);
|
||||||
statusPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
|
|
||||||
status.setText("Connected on port " + this.port + " name " + this.userName);
|
|
||||||
statusPanel.add(status);
|
|
||||||
|
|
||||||
this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
|
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);
|
this.getContentPane().add(statusPanel, java.awt.BorderLayout.PAGE_END);
|
||||||
|
|
||||||
this.pack();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initSocket() {
|
|
||||||
this.socketUDP = new SocketUDP(this);
|
|
||||||
this.socketTCP = new SocketTCP(this);
|
|
||||||
|
|
||||||
this.socketUDP.start();
|
|
||||||
this.socketTCP.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initHeartbeat() {
|
|
||||||
this.heartbeatManager = new HeartbeatManager(this);
|
|
||||||
this.onlineUsersPanel = new OnlineUsersPanel();
|
|
||||||
this.heartbeatManager.addPeerListener(this.onlineUsersPanel);
|
|
||||||
this.getContentPane().add(this.onlineUsersPanel, java.awt.BorderLayout.LINE_END);
|
|
||||||
this.heartbeatManager.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initPlugins() {
|
|
||||||
this.pluginManager = new PluginLoader(this);
|
|
||||||
this.pluginManager.loadPlugins();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void exit() {
|
public void exit() {
|
||||||
@@ -122,65 +99,24 @@ public class MainWindow extends javax.swing.JFrame {
|
|||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HeartbeatManager getHeartbeatManager() {
|
public ConnectionManager getConnectionManager() {
|
||||||
return heartbeatManager;
|
return connectionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUserName(){
|
public void loadPlugins() {
|
||||||
return this.userName;
|
pluginManager.loadPlugins(connectionManager, guiManager);
|
||||||
}
|
|
||||||
|
|
||||||
public String getIdentifier(){
|
|
||||||
return this.pitiupiIdentifier;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMenu(JMenu menu) {
|
public void addMenu(JMenu menu) {
|
||||||
this.menuBar.addPlugin(menu);
|
this.menuBar.addPlugin(menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPort() {
|
|
||||||
return this.port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SocketUDP getSocketUDP() {
|
|
||||||
return this.socketUDP;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SocketTCP getSocketTCP() {
|
|
||||||
return this.socketTCP;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Plugin> getPlugins() {
|
|
||||||
return plugins;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
public void setUserName(String userName) {
|
||||||
this.userName = userName;
|
connectionManager.setUserName(userName);
|
||||||
updateStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPort(int port) {
|
|
||||||
this.port = port;
|
|
||||||
reconnect();
|
|
||||||
updateStatus();
|
updateStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateStatus() {
|
private void updateStatus() {
|
||||||
this.status.setText("Connected on port " + this.port + " name " + this.userName);
|
this.status.setText("Connected on port " + connectionManager.getPort() + " name " + connectionManager.getUserName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reconnect() {
|
|
||||||
if (this.socketUDP != null) {
|
|
||||||
this.socketUDP.close();
|
|
||||||
}
|
|
||||||
if (this.socketTCP != null) {
|
|
||||||
this.socketTCP.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.socketUDP = new SocketUDP(this);
|
|
||||||
this.socketTCP = new SocketTCP(this);
|
|
||||||
|
|
||||||
this.socketUDP.start();
|
|
||||||
this.socketTCP.start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -80,7 +80,7 @@ public class MenuBar extends JMenuBar {
|
|||||||
login.setVisible(true);
|
login.setVisible(true);
|
||||||
|
|
||||||
mainWindow.setUserName(login.getName());
|
mainWindow.setUserName(login.getName());
|
||||||
mainWindow.setPort(login.getPort());
|
mainWindow.getConnectionManager().setPort(login.getPort());
|
||||||
|
|
||||||
login.dispose();
|
login.dispose();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package pitiupi.control;
|
||||||
|
|
||||||
|
import pitiupi.GUI.MainWindow;
|
||||||
|
import pitiupi.net.HeartbeatMessage;
|
||||||
|
import pitiupi.net.Message;
|
||||||
|
import pitiupi.net.tcp.SocketTCP;
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
import pitiupi.net.udp.SocketUDP;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
|
||||||
|
public class ConnectionManager {
|
||||||
|
private final String pitiupiIdentifier;
|
||||||
|
|
||||||
|
private final PluginManager pluginManager;
|
||||||
|
|
||||||
|
private final HeartbeatManager heartbeatManager;
|
||||||
|
|
||||||
|
private SocketTCP socketTCP;
|
||||||
|
private SocketUDP socketUDP;
|
||||||
|
private String username;
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
public ConnectionManager(MainWindow main, String username, PluginManager pluginManager, int port) {
|
||||||
|
this.pluginManager = pluginManager;
|
||||||
|
|
||||||
|
this.username = username;
|
||||||
|
this.port = port;
|
||||||
|
|
||||||
|
pitiupiIdentifier = "Pitiupi.Alice.2026";
|
||||||
|
|
||||||
|
reconnect();
|
||||||
|
|
||||||
|
heartbeatManager = new HeartbeatManager(this);
|
||||||
|
heartbeatManager.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TcpConnection connectPluginTcp(InetAddress destinationAdress, String senderId, String receiverId) {
|
||||||
|
return socketTCP.createPrivateConnection(destinationAdress, senderId, receiverId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean sendTcp(Message message, InetAddress destinationAddress) {
|
||||||
|
return socketTCP.send(message, destinationAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean sendUdpUnicast(Message message, InetAddress destinationAddress) {
|
||||||
|
try {
|
||||||
|
socketUDP.sendUnicast(message, destinationAddress);
|
||||||
|
return true;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean sendUdpMulticast(Message message) {
|
||||||
|
try {
|
||||||
|
socketUDP.sendMulticast(message);
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error sending UDP Multicast");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void broadcastLocal(byte[] message) {
|
||||||
|
pluginManager.handlePublicMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPeerListener(PeerListener peerListener) {
|
||||||
|
heartbeatManager.addPeerListener(peerListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void receiveHeartbeat(HeartbeatMessage message, InetAddress destinationAddress) {
|
||||||
|
heartbeatManager.receiveHeartbeat(message, destinationAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
this.port = port;
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIdentifier(){
|
||||||
|
return this.pitiupiIdentifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reconnect() {
|
||||||
|
if (this.socketUDP != null) {
|
||||||
|
this.socketUDP.close();
|
||||||
|
}
|
||||||
|
if (this.socketTCP != null) {
|
||||||
|
this.socketTCP.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.socketUDP = new SocketUDP(this, pluginManager);
|
||||||
|
this.socketTCP = new SocketTCP(this, pluginManager);
|
||||||
|
|
||||||
|
this.socketUDP.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,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 MainWindow mainWindow;
|
|
||||||
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<>();
|
||||||
private ScheduledExecutorService scheduler;
|
private ScheduledExecutorService scheduler;
|
||||||
@@ -38,11 +37,9 @@ 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) {
|
public HeartbeatManager(ConnectionManager connectionManager) {
|
||||||
this.mainWindow = mainWindow;
|
this.connectionManager = connectionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,12 +61,8 @@ public class HeartbeatManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void announce() {
|
private void announce() {
|
||||||
try {
|
HeartbeatMessage msg = new HeartbeatMessage(connectionManager.getUserName(), instanceId);
|
||||||
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
|
connectionManager.sendUdpMulticast(msg);
|
||||||
mainWindow.getSocketUDP().sendMulticast(msg);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
Logger.getLogger(HeartbeatManager.class.getName()).log(Level.WARNING, "Falha ao enviar heartbeat", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chamado pelo Socket quando um pacote é identificado como heartbeat
|
// Chamado pelo Socket quando um pacote é identificado como heartbeat
|
||||||
|
|||||||
@@ -4,14 +4,12 @@
|
|||||||
*/
|
*/
|
||||||
package pitiupi.control;
|
package pitiupi.control;
|
||||||
|
|
||||||
import java.awt.List;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.ServiceLoader;
|
|
||||||
import java.util.Vector;
|
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.GUI.MainWindow;
|
||||||
import pitiupi.Main;
|
import pitiupi.Main;
|
||||||
import pitiupi.plugin.Plugin;
|
import pitiupi.plugin.Plugin;
|
||||||
@@ -33,7 +31,7 @@ public class PluginLoader {
|
|||||||
this.mainWindow = main;
|
this.mainWindow = main;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadPlugins() {
|
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"));
|
||||||
|
|
||||||
@@ -42,33 +40,34 @@ public class PluginLoader {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
// 2. Convert jar file paths into URLs
|
|
||||||
ArrayList urls = new ArrayList();
|
|
||||||
for (File file : jarFiles) {
|
for (File file : jarFiles) {
|
||||||
urls.add(file.toURI().toURL());
|
|
||||||
System.out.println("Discovered plugin package: " + file.getName());
|
if (loadedJars.contains(file.getName())) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Create a class loader isolated from the application's default loader
|
System.out.println("Discovered plugin package: " + file.getName());
|
||||||
URLClassLoader pluginClassLoader = new URLClassLoader(
|
|
||||||
(URL[])urls.toArray(new URL[0]),
|
try {
|
||||||
|
URLClassLoader classLoader = new URLClassLoader(
|
||||||
|
new URL[]{file.toURI().toURL()},
|
||||||
Main.class.getClassLoader()
|
Main.class.getClassLoader()
|
||||||
);
|
);
|
||||||
System.out.println(pluginClassLoader);
|
|
||||||
|
|
||||||
// 4. Locate and instantiate all classes implementing the Plugin interface
|
ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class, classLoader);
|
||||||
ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class, pluginClassLoader);
|
|
||||||
|
|
||||||
System.out.println("\n--- Executing Discovered Plugins ---" + serviceLoader.findFirst());
|
|
||||||
for (Plugin plugin : serviceLoader) {
|
for (Plugin plugin : serviceLoader) {
|
||||||
System.out.println("Loading: " + plugin.getName());
|
System.out.println("Loading: " + plugin.getName());
|
||||||
plugin.createPlugin(this.mainWindow);
|
|
||||||
this.mainWindow.getPlugins().add(plugin);
|
plugin.createPlugin(connectionManager, guiManager);
|
||||||
|
plugins.add(plugin);
|
||||||
|
loadedJars.add(file.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (MalformedURLException e) {
|
} catch (Exception e) {
|
||||||
System.out.println(e.toString());
|
System.out.println("Failed to load plugin " + file.getName());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ import java.util.logging.Logger;
|
|||||||
* a utilizar o timeout padrão definido por {@link #STANDARD_TIMEOUT}.</p>
|
* a utilizar o timeout padrão definido por {@link #STANDARD_TIMEOUT}.</p>
|
||||||
*/
|
*/
|
||||||
public class ConnectionFactory {
|
public class ConnectionFactory {
|
||||||
private static final int HANDSHAKE_TIMEOUT = 5000;
|
private static final int HANDSHAKE_TIMEOUT = 1000;
|
||||||
private static final int STANDARD_TIMEOUT = 300_000;
|
private static final int STANDARD_TIMEOUT = 300_000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,7 +64,9 @@ public class ConnectionFactory {
|
|||||||
connection.send(handshake.toByteArray());
|
connection.send(handshake.toByteArray());
|
||||||
HandshakeMessage handshakeResponse = validateConnection(connection);
|
HandshakeMessage handshakeResponse = validateConnection(connection);
|
||||||
|
|
||||||
if (handshakeResponse == null) return null;
|
if (handshakeResponse == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
connection.setTimeout(STANDARD_TIMEOUT);
|
connection.setTimeout(STANDARD_TIMEOUT);
|
||||||
|
|
||||||
@@ -102,7 +104,6 @@ public class ConnectionFactory {
|
|||||||
|
|
||||||
if (handshake == null) return null;
|
if (handshake == null) return null;
|
||||||
|
|
||||||
|
|
||||||
connection.send(handshake.createResponse().toByteArray());
|
connection.send(handshake.createResponse().toByteArray());
|
||||||
|
|
||||||
connection.setSender(handshake.getSender());
|
connection.setSender(handshake.getSender());
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
package pitiupi.net.tcp;
|
package pitiupi.net.tcp;
|
||||||
|
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.control.ConnectionManager;
|
||||||
|
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 javax.swing.*;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -37,11 +36,13 @@ public class SocketTCP extends Thread {
|
|||||||
|
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
|
private final PluginManager pluginManager;
|
||||||
|
|
||||||
|
private final TcpConnectionManager tcpConnectionManager;
|
||||||
|
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private InetAddress address;
|
private InetAddress address;
|
||||||
|
|
||||||
private final MainWindow main;
|
|
||||||
|
|
||||||
private volatile boolean running = true;
|
private volatile boolean running = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,15 +52,18 @@ public class SocketTCP extends Thread {
|
|||||||
* com os peers são estabelecidas sob demanda durante o envio ou aceitas
|
* com os peers são estabelecidas sob demanda durante o envio ou aceitas
|
||||||
* quando iniciadas por outros peers.</p>
|
* quando iniciadas por outros peers.</p>
|
||||||
*
|
*
|
||||||
* @param main janela principal da aplicação, usada para obter a porta,
|
* @param connectionManager interface de conexão da aplicação, usada para obter a porta,
|
||||||
* o identificador local e os plugins disponíveis.
|
* o identificador local e os plugins disponíveis.
|
||||||
*/
|
*/
|
||||||
public SocketTCP(MainWindow main) {
|
public SocketTCP(ConnectionManager connectionManager, PluginManager pluginManager) {
|
||||||
this.main = main;
|
this.connectionManager = connectionManager;
|
||||||
|
this.pluginManager = pluginManager;
|
||||||
|
|
||||||
logger = Logger.getLogger(getClass().getName());
|
logger = Logger.getLogger(getClass().getName());
|
||||||
connectionManager = new ConnectionManager();
|
tcpConnectionManager = new TcpConnectionManager();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
serverSocket = new ServerSocket(main.getPort());
|
serverSocket = new ServerSocket(connectionManager.getPort());
|
||||||
address = InetAddress.getByName(InetAddress.getLocalHost().getHostAddress());
|
address = InetAddress.getByName(InetAddress.getLocalHost().getHostAddress());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
System.out.println("There is no socket connection. Sorry.");
|
System.out.println("There is no socket connection. Sorry.");
|
||||||
@@ -78,25 +82,32 @@ public class SocketTCP extends Thread {
|
|||||||
* @param msg mensagem que será enviada.
|
* @param msg mensagem que será enviada.
|
||||||
* @param destinationAddress endereço IP do peer que receberá a mensagem.
|
* @param destinationAddress endereço IP do peer que receberá a mensagem.
|
||||||
*/
|
*/
|
||||||
public void send(Message msg, InetAddress destinationAddress) {
|
public boolean send(Message msg, InetAddress destinationAddress) {
|
||||||
try {
|
try {
|
||||||
TcpConnection connection = getPublicConnection(destinationAddress, main.getIdentifier(), main.getIdentifier());
|
TcpConnection connection = getPublicConnection(
|
||||||
|
destinationAddress,
|
||||||
|
connectionManager.getIdentifier(),
|
||||||
|
connectionManager.getIdentifier()
|
||||||
|
);
|
||||||
|
|
||||||
if (connection == null) {
|
if (connection == null) {
|
||||||
System.out.println("Could not establish TCP connection.");
|
System.out.println("Could not establish TCP connection.");
|
||||||
return;
|
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.");
|
||||||
System.out.println(ex.toString());
|
System.out.println(ex.toString());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Estabelece uma conexão TCP privada com um plugin específico do peer de destino.
|
* Estabelece uma conexão TCP privada com um plugin específico do peer de destino.
|
||||||
*
|
*
|
||||||
* <p>A conexão é criada como privada e passa a ser gerenciada pelo
|
* <p>A conexão é criada como privada e passa a ser gerenciada pelo
|
||||||
* {@link ConnectionManager}. O método também inicia a tarefa responsável
|
* {@link TcpConnectionManager}. O método também inicia a tarefa responsável
|
||||||
* por receber os dados dessa conexão.</p>
|
* por receber os dados dessa conexão.</p>
|
||||||
*
|
*
|
||||||
* @param destinationAddress endereço IP do peer de destino.
|
* @param destinationAddress endereço IP do peer de destino.
|
||||||
@@ -115,11 +126,11 @@ public class SocketTCP extends Thread {
|
|||||||
* Obtém uma conexão TCP pública existente com o peer de destino ou estabelece
|
* Obtém uma conexão TCP pública existente com o peer de destino ou estabelece
|
||||||
* uma nova caso não exista uma conexão válida.
|
* uma nova caso não exista uma conexão válida.
|
||||||
*
|
*
|
||||||
* <p>As conexões públicas são gerenciadas pelo {@link ConnectionManager}.
|
* <p>As conexões públicas são gerenciadas pelo {@link TcpConnectionManager}.
|
||||||
* O método primeiro verifica se já existe uma conexão pública registrada
|
* O método primeiro verifica se já existe uma conexão pública registrada
|
||||||
* para o endereço de destino e, caso ela exista e não esteja fechada,
|
* para o endereço de destino e, caso ela exista e não esteja fechada,
|
||||||
* reutiliza-a. Caso contrário, uma nova conexão pública é estabelecida,
|
* reutiliza-a. Caso contrário, uma nova conexão pública é estabelecida,
|
||||||
* registrada no {@link ConnectionManager} e disponibilizada para uso.</p>
|
* registrada no {@link TcpConnectionManager} e disponibilizada para uso.</p>
|
||||||
*
|
*
|
||||||
* <p>O acesso ao método é sincronizado para evitar que múltiplas threads
|
* <p>O acesso ao método é sincronizado para evitar que múltiplas threads
|
||||||
* estabeleçam simultaneamente conexões públicas para o mesmo peer.</p>
|
* estabeleçam simultaneamente conexões públicas para o mesmo peer.</p>
|
||||||
@@ -133,7 +144,7 @@ public class SocketTCP extends Thread {
|
|||||||
private synchronized TcpConnection getPublicConnection(InetAddress destinationAddress, String senderId, String receiverId) {
|
private synchronized TcpConnection getPublicConnection(InetAddress destinationAddress, String senderId, String receiverId) {
|
||||||
TcpConnection connection;
|
TcpConnection connection;
|
||||||
|
|
||||||
connection = connectionManager.getPublicConnection(destinationAddress);
|
connection = tcpConnectionManager.getPublicConnection(destinationAddress);
|
||||||
if (connection != null && !connection.isClosed()) return connection;
|
if (connection != null && !connection.isClosed()) return connection;
|
||||||
|
|
||||||
connection = establishOutgoingConnection(destinationAddress, senderId, receiverId, false);
|
connection = establishOutgoingConnection(destinationAddress, senderId, receiverId, false);
|
||||||
@@ -146,7 +157,7 @@ public class SocketTCP extends Thread {
|
|||||||
*
|
*
|
||||||
* <p>Cada socket aceito é encaminhado para processamento assíncrono,
|
* <p>Cada socket aceito é encaminhado para processamento assíncrono,
|
||||||
* interpretado como uma {@link TcpConnection}, registrado pelo
|
* interpretado como uma {@link TcpConnection}, registrado pelo
|
||||||
* {@link ConnectionManager} e associado ao receptor adequado.</p>
|
* {@link TcpConnectionManager} e associado ao receptor adequado.</p>
|
||||||
*/
|
*/
|
||||||
private void receive() {
|
private void receive() {
|
||||||
while (running) {
|
while (running) {
|
||||||
@@ -188,7 +199,7 @@ public class SocketTCP extends Thread {
|
|||||||
throw new IllegalArgumentException("Connections to the local host are forbidden.");
|
throw new IllegalArgumentException("Connections to the local host are forbidden.");
|
||||||
}
|
}
|
||||||
|
|
||||||
TcpConnection connection = ConnectionFactory.createConnection(destinationAddress, main.getPort(), sender, receiver, isPrivate);
|
TcpConnection connection = ConnectionFactory.createConnection(destinationAddress, connectionManager.getPort(), sender, receiver, isPrivate);
|
||||||
if (connection == null) return null;
|
if (connection == null) return null;
|
||||||
|
|
||||||
saveConnection(connection);
|
saveConnection(connection);
|
||||||
@@ -229,10 +240,10 @@ public class SocketTCP extends Thread {
|
|||||||
*/
|
*/
|
||||||
private void saveConnection(TcpConnection connection) {
|
private void saveConnection(TcpConnection connection) {
|
||||||
if (connection.isPrivate()) {
|
if (connection.isPrivate()) {
|
||||||
connectionManager.addPrivateConnection(connection);
|
tcpConnectionManager.addPrivateConnection(connection);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
connectionManager.addPublicConnection(connection);
|
tcpConnectionManager.addPublicConnection(connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,47 +265,21 @@ public class SocketTCP extends Thread {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connection.getReceiver().equals(main.getIdentifier())) {
|
if (connection.getReceiver().equals(connectionManager.getIdentifier())) {
|
||||||
systemConnectionReceiver(connection);
|
systemConnectionReceiver(connection);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Plugin receiver = findConnectionReceiver(connection);
|
if (pluginManager.handleConnection(connection, connection.getReceiver()))
|
||||||
if (receiver instanceof PrivateConnectionPlugin plugin) {
|
privateConnectionReceiver(connection);
|
||||||
plugin.receivePrivateConnection(connection);
|
else {
|
||||||
|
|
||||||
privateConnectionReceiver(connection, plugin);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Localiza o plugin responsável por receber uma conexão privada.
|
|
||||||
*
|
|
||||||
* <p>A busca é feita pelo identificador do destinatário informado na
|
|
||||||
* própria conexão. Caso nenhum plugin correspondente seja encontrado,
|
|
||||||
* uma mensagem de erro é enviada ao peer remoto.</p>
|
|
||||||
*
|
|
||||||
* @param connection conexão privada cujo destinatário será localizado.
|
|
||||||
* @return o plugin com o identificador correspondente ao destinatário,
|
|
||||||
* ou {@code null} se nenhum plugin for encontrado.
|
|
||||||
*/
|
|
||||||
private Plugin findConnectionReceiver(TcpConnection connection) {
|
|
||||||
Plugin receiver = null;
|
|
||||||
for(Plugin plugin : main.getPlugins()) {
|
|
||||||
if (plugin.getIdentifier().equals(connection.getReceiver())) {
|
|
||||||
receiver = plugin;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (receiver == null) {
|
|
||||||
SysInfoMessage.sendSystemInfo(
|
SysInfoMessage.sendSystemInfo(
|
||||||
connection,
|
connection,
|
||||||
SysInfoType.RECEIVER_NOT_FOUND,
|
SysInfoType.RECEIVER_NOT_FOUND,
|
||||||
"This peer does not have the plugin: " + connection.getReceiver()
|
"This peer does not have the plugin: " + connection.getReceiver()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return receiver;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -306,7 +291,7 @@ public class SocketTCP extends Thread {
|
|||||||
* condição de erro.</p>
|
* condição de erro.</p>
|
||||||
*
|
*
|
||||||
* <p>O método também trata timeouts e erros de I/O, removendo a conexão
|
* <p>O método também trata timeouts e erros de I/O, removendo a conexão
|
||||||
* do {@link ConnectionManager} quando ela deixa de ser utilizável.</p>
|
* do {@link TcpConnectionManager} quando ela deixa de ser utilizável.</p>
|
||||||
*
|
*
|
||||||
* @param connection conexão pública da qual as mensagens serão recebidas.
|
* @param connection conexão pública da qual as mensagens serão recebidas.
|
||||||
*/
|
*/
|
||||||
@@ -318,15 +303,13 @@ public class SocketTCP extends Thread {
|
|||||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(bytes);
|
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(bytes);
|
||||||
|
|
||||||
if (sysMessage == null) {
|
if (sysMessage == null) {
|
||||||
for (Plugin plugin : main.getPlugins()) {
|
pluginManager.handlePublicMessage(bytes);
|
||||||
plugin.receiveMessage(bytes);
|
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (sysMessage.getType()) {
|
switch (sysMessage.getType()) {
|
||||||
case CONNECTION_CLOSED, INTERNAL_ERROR -> {
|
case CONNECTION_CLOSED, INTERNAL_ERROR -> {
|
||||||
connectionManager.closeConnection(connection);
|
tcpConnectionManager.closeConnection(connection);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case MESSAGE_REJECTED -> {
|
case MESSAGE_REJECTED -> {
|
||||||
@@ -337,12 +320,12 @@ public class SocketTCP extends Thread {
|
|||||||
}
|
}
|
||||||
catch (SocketTimeoutException ex) {
|
catch (SocketTimeoutException ex) {
|
||||||
if (connection.hasTimedOut()) {
|
if (connection.hasTimedOut()) {
|
||||||
connectionManager.closeConnection(connection, "Connection timeout");
|
tcpConnectionManager.closeConnection(connection, "Connection timeout");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
connectionManager.closeConnection(connection);
|
tcpConnectionManager.closeConnection(connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -357,24 +340,22 @@ public class SocketTCP extends Thread {
|
|||||||
* do plugin e o fechamento da conexão.</p>
|
* do plugin e o fechamento da conexão.</p>
|
||||||
*
|
*
|
||||||
* @param connection conexão privada da qual os dados serão recebidos.
|
* @param connection conexão privada da qual os dados serão recebidos.
|
||||||
* @param plugin plugin responsável pelo processamento dos dados recebidos.
|
|
||||||
*/
|
*/
|
||||||
private void privateConnectionReceiver(TcpConnection connection, Plugin plugin) {
|
private void privateConnectionReceiver(TcpConnection connection) {
|
||||||
if (plugin instanceof PrivateConnectionPlugin privatePlugin) {
|
|
||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
byte[] message = connection.receive();
|
byte[] message = connection.receive();
|
||||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
||||||
|
|
||||||
if (sysMessage == null) {
|
if (sysMessage == null) {
|
||||||
privatePlugin.receiveBytes(connection, message);
|
pluginManager.handlePrivateMessage(connection, message);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (sysMessage.getType()) {
|
switch (sysMessage.getType()) {
|
||||||
case CONNECTION_CLOSED, INTERNAL_ERROR -> {
|
case CONNECTION_CLOSED, INTERNAL_ERROR -> {
|
||||||
privatePlugin.onSocketClosed(connection);
|
pluginManager.handleDisconnection(connection, connection.getReceiver());
|
||||||
connectionManager.closeConnection(connection);
|
tcpConnectionManager.closeConnection(connection);
|
||||||
}
|
}
|
||||||
default -> {
|
default -> {
|
||||||
continue;
|
continue;
|
||||||
@@ -383,13 +364,12 @@ public class SocketTCP extends Thread {
|
|||||||
}
|
}
|
||||||
catch (SocketTimeoutException ex) {
|
catch (SocketTimeoutException ex) {
|
||||||
if (connection.hasTimedOut()) {
|
if (connection.hasTimedOut()) {
|
||||||
connectionManager.closeConnection(connection, "Connection timeout");
|
tcpConnectionManager.closeConnection(connection, "Connection timeout");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
connectionManager.closeConnection(connection);
|
tcpConnectionManager.closeConnection(connection);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -421,7 +401,7 @@ public class SocketTCP extends Thread {
|
|||||||
if (connection.hasTimedOut()) return;
|
if (connection.hasTimedOut()) return;
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
connectionManager.closeConnection(connection);
|
tcpConnectionManager.closeConnection(connection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -430,19 +410,18 @@ public class SocketTCP extends Thread {
|
|||||||
* Encerra uma conexão TCP e registra o motivo informado.
|
* Encerra uma conexão TCP e registra o motivo informado.
|
||||||
*
|
*
|
||||||
* <p>Quando nenhuma descrição é fornecida, é utilizado um motivo padrão.
|
* <p>Quando nenhuma descrição é fornecida, é utilizado um motivo padrão.
|
||||||
* O fechamento efetivo da conexão é delegado ao {@link ConnectionManager}.</p>
|
* O fechamento efetivo da conexão é delegado ao {@link TcpConnectionManager}.</p>
|
||||||
*
|
*
|
||||||
* @param connection conexão que será encerrada.
|
* @param connection conexão que será encerrada.
|
||||||
* @param description motivo do encerramento; pode ser {@code null}.
|
* @param description motivo do encerramento; pode ser {@code null}.
|
||||||
*/
|
*/
|
||||||
public void disconnect(TcpConnection connection, String description) {
|
public void disconnect(TcpConnection connection, String description) {
|
||||||
connectionManager.closeConnection(
|
tcpConnectionManager.closeConnection(
|
||||||
connection,
|
connection,
|
||||||
Objects.requireNonNullElse(description, "No reason provided")
|
Objects.requireNonNullElse(description, "No reason provided")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
receive();
|
receive();
|
||||||
@@ -469,7 +448,7 @@ public class SocketTCP extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
connectionManager.closeAllConnections("The peer must have closed the application or changed ports");
|
tcpConnectionManager.closeAllConnections("The peer must have closed the application or changed ports");
|
||||||
|
|
||||||
connectionExecutor.shutdownNow();
|
connectionExecutor.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -27,7 +27,7 @@ import java.util.stream.Collectors;
|
|||||||
* peers ativos para encerrar conexões associadas a peers que deixaram de
|
* peers ativos para encerrar conexões associadas a peers que deixaram de
|
||||||
* estar disponíveis.</p>
|
* estar disponíveis.</p>
|
||||||
*/
|
*/
|
||||||
class ConnectionManager implements PeerListener {
|
class TcpConnectionManager implements PeerListener {
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final Map<InetAddress, TcpConnection> publicConnections;
|
private final Map<InetAddress, TcpConnection> publicConnections;
|
||||||
private final Map<InetAddress, List<TcpConnection>> privateConnections;
|
private final Map<InetAddress, List<TcpConnection>> privateConnections;
|
||||||
@@ -35,7 +35,7 @@ class ConnectionManager implements PeerListener {
|
|||||||
/**
|
/**
|
||||||
* Cria um gerenciador de conexões TCP vazio.
|
* Cria um gerenciador de conexões TCP vazio.
|
||||||
*/
|
*/
|
||||||
public ConnectionManager() {
|
public TcpConnectionManager() {
|
||||||
publicConnections = new ConcurrentHashMap<>();
|
publicConnections = new ConcurrentHashMap<>();
|
||||||
privateConnections = new ConcurrentHashMap<>();
|
privateConnections = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ class ConnectionManager implements PeerListener {
|
|||||||
*/
|
*/
|
||||||
public void addPrivateConnection(TcpConnection connection) {
|
public void addPrivateConnection(TcpConnection connection) {
|
||||||
List<TcpConnection> connections =
|
List<TcpConnection> connections =
|
||||||
privateConnections.computeIfAbsent(connection.getAddress(), _ -> new ArrayList<>());
|
privateConnections.computeIfAbsent(connection.getAddress(), e -> new ArrayList<>());
|
||||||
|
|
||||||
synchronized (connections) {
|
synchronized (connections) {
|
||||||
connections.add(connection);
|
connections.add(connection);
|
||||||
@@ -14,6 +14,8 @@ import java.net.*;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.control.PluginManager;
|
||||||
import pitiupi.net.HeartbeatMessage;
|
import pitiupi.net.HeartbeatMessage;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.Message;
|
||||||
import pitiupi.plugin.Plugin;
|
import pitiupi.plugin.Plugin;
|
||||||
@@ -37,7 +39,10 @@ public class SocketUDP extends Thread {
|
|||||||
private MulticastSocket multicastSocket;
|
private MulticastSocket multicastSocket;
|
||||||
|
|
||||||
private InetAddress address;
|
private InetAddress address;
|
||||||
private MainWindow main;
|
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
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";
|
||||||
|
|
||||||
@@ -47,22 +52,26 @@ public class SocketUDP extends Thread {
|
|||||||
* <p>O socket é associado à porta da aplicação e ingressa no grupo
|
* <p>O socket é associado à porta da aplicação e ingressa no grupo
|
||||||
* multicast configurado.</p>
|
* multicast configurado.</p>
|
||||||
*
|
*
|
||||||
* @param main janela principal da aplicação.
|
* @param connectionManager interface que gerencia toda conexão da aplicação.
|
||||||
|
* @param pluginManager interface que gerencia os plugins da conexão.
|
||||||
*/
|
*/
|
||||||
public SocketUDP(MainWindow main) {
|
public SocketUDP(ConnectionManager connectionManager, PluginManager pluginManager) {
|
||||||
this.main = main;
|
this.connectionManager = connectionManager;
|
||||||
|
this.pluginManager = pluginManager;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.address = InetAddress.getByName(SocketUDP.INET_ADDR);
|
this.address = InetAddress.getByName(SocketUDP.INET_ADDR);
|
||||||
} catch (UnknownHostException ex) {
|
|
||||||
System.getLogger(SocketUDP.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex);
|
multicastSocket = new MulticastSocket(connectionManager.getPort());
|
||||||
}
|
|
||||||
try {
|
|
||||||
multicastSocket = new MulticastSocket(this.main.getPort());
|
|
||||||
multicastSocket.setSendBufferSize(256000);
|
multicastSocket.setSendBufferSize(256000);
|
||||||
multicastSocket.setReceiveBufferSize(256000);
|
multicastSocket.setReceiveBufferSize(256000);
|
||||||
multicastSocket.setReuseAddress(true);
|
multicastSocket.setReuseAddress(true);
|
||||||
multicastSocket.joinGroup(address);
|
multicastSocket.joinGroup(address);
|
||||||
} catch (IOException ex) {
|
}
|
||||||
|
catch (UnknownHostException ex) {
|
||||||
|
System.getLogger(SocketUDP.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex);
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
System.out.println("There is no socket connection. Sorry.");
|
System.out.println("There is no socket connection. Sorry.");
|
||||||
System.out.println(ex);
|
System.out.println(ex);
|
||||||
}
|
}
|
||||||
@@ -71,22 +80,22 @@ public class SocketUDP extends Thread {
|
|||||||
/**
|
/**
|
||||||
* Envia uma mensagem para o grupo multicast da aplicação.
|
* Envia uma mensagem para o grupo multicast da aplicação.
|
||||||
*
|
*
|
||||||
* @param msg mensagem serializada a ser enviada.
|
* @param msg mensagem a ser enviada.
|
||||||
* @throws IOException caso ocorra um erro durante o envio.
|
* @throws IOException caso ocorra um erro durante o envio.
|
||||||
*/
|
*/
|
||||||
public void sendMulticast(Message msg) throws IOException {
|
public void sendMulticast(Message msg) throws IOException {
|
||||||
send(msg, address);
|
send(msg.toByteArray(), this.address);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Envia uma mensagem diretamente para um peer utilizando UDP.
|
* Envia uma mensagem diretamente para um peer utilizando UDP.
|
||||||
*
|
*
|
||||||
* @param msg mensagem serializada a ser enviada.
|
* @param msg mensagem a ser enviada.
|
||||||
* @param destinationAddress endereço IP do peer destinatário.
|
* @param destinationAddress endereço IP do peer destinatário.
|
||||||
* @throws IOException caso ocorra um erro durante o envio.
|
* @throws IOException caso ocorra um erro durante o envio.
|
||||||
*/
|
*/
|
||||||
public void sendUnicast(Message msg, InetAddress destinationAddress) throws IOException {
|
public void sendUnicast(Message msg, InetAddress destinationAddress) throws IOException {
|
||||||
send(msg, destinationAddress);
|
send(msg.toByteArray(), destinationAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,10 +105,9 @@ public class SocketUDP extends Thread {
|
|||||||
* @param destinationAddress endereço de destino.
|
* @param destinationAddress endereço de destino.
|
||||||
* @throws IOException caso ocorra um erro durante o envio.
|
* @throws IOException caso ocorra um erro durante o envio.
|
||||||
*/
|
*/
|
||||||
private void send(Message msg, InetAddress destinationAddress) throws IOException {
|
private void send(byte[] msg, InetAddress destinationAddress) throws IOException {
|
||||||
DatagramPacket msgPacket;
|
DatagramPacket msgPacket;
|
||||||
byte[] buf = msg.toByteArray();
|
msgPacket = new DatagramPacket(msg, msg.length, destinationAddress, connectionManager.getPort());
|
||||||
msgPacket = new DatagramPacket(buf, buf.length, destinationAddress, this.main.getPort());
|
|
||||||
multicastSocket.send(msgPacket);
|
multicastSocket.send(msgPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,14 +155,11 @@ public class SocketUDP extends Thread {
|
|||||||
|
|
||||||
HeartbeatMessage heartbeat = tryParseHeartbeat(msgPacket.getData());
|
HeartbeatMessage heartbeat = tryParseHeartbeat(msgPacket.getData());
|
||||||
if (heartbeat != null) {
|
if (heartbeat != null) {
|
||||||
main.getHeartbeatManager().receiveHeartbeat(heartbeat, msgPacket.getAddress());
|
connectionManager.receiveHeartbeat(heartbeat, msgPacket.getAddress());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Plugin plugin : main.getPlugins()) {
|
pluginManager.handlePublicMessage(msgPacket.getData());
|
||||||
System.out.println(msgPacket.getAddress().toString());
|
|
||||||
plugin.receiveMessage(msgPacket.getData());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
Reference in new issue
Block a user