feature/separacao-gerenciadores #1
No files matched your search
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library>
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MODULE_DIR$/../pitiupi/dist/pitiupi.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -8,7 +8,7 @@ public class SocketTestMenu extends JMenu {
|
|||||||
|
|
||||||
public SocketTestMenu(SocketTestWindow socketTestWindow) {
|
public SocketTestMenu(SocketTestWindow socketTestWindow) {
|
||||||
this.socketTestWindow = socketTestWindow;
|
this.socketTestWindow = socketTestWindow;
|
||||||
this.setText("Chat");
|
this.setText("MP-Chat");
|
||||||
JMenuItem menuItem = new JMenuItem();
|
JMenuItem menuItem = new JMenuItem();
|
||||||
menuItem.setText("Show Chat Window");
|
menuItem.setText("Show Chat Window");
|
||||||
menuItem.addActionListener(this::showTestWindow);
|
menuItem.addActionListener(this::showTestWindow);
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ public class SocketTestWindow extends JFrame {
|
|||||||
try {
|
try {
|
||||||
TestMessage msg = createMessage("TCP Unicast");
|
TestMessage msg = createMessage("TCP Unicast");
|
||||||
InetAddress ip = InetAddress.getByName(ipField.getText());
|
InetAddress ip = InetAddress.getByName(ipField.getText());
|
||||||
mainWindow.getSocketTCP().send(msg.toByteArray(), ip);
|
mainWindow.getSocketTCP().send(msg, ip);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
@@ -106,7 +106,7 @@ public class SocketTestWindow extends JFrame {
|
|||||||
try {
|
try {
|
||||||
TestMessage msg = createMessage("UDP Unicast");
|
TestMessage msg = createMessage("UDP Unicast");
|
||||||
InetAddress ip = InetAddress.getByName(ipField.getText());
|
InetAddress ip = InetAddress.getByName(ipField.getText());
|
||||||
mainWindow.getSocketUDP().sendUnicast(msg.toByteArray(), ip);
|
mainWindow.getSocketUDP().sendUnicast(msg, ip);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ public class SocketTestWindow extends JFrame {
|
|||||||
public void sendMulticast() {
|
public void sendMulticast() {
|
||||||
try {
|
try {
|
||||||
TestMessage msg = createMessage("UDP Multicast");
|
TestMessage msg = createMessage("UDP Multicast");
|
||||||
mainWindow.getSocketUDP().sendMulticast(msg.toByteArray());
|
mainWindow.getSocketUDP().sendMulticast(msg);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,14 @@ import GUI.SocketTestWindow;
|
|||||||
import net.TestMessage;
|
import net.TestMessage;
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.GUI.MainWindow;
|
||||||
import pitiupi.net.Message;
|
import pitiupi.net.Message;
|
||||||
import pitiupi.plugin.Plugin;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
import pitiupi.plugin.PrivateConnectionPlugin;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
|
|
||||||
public class SocketTest implements Plugin{
|
public class SocketTest implements PrivateConnectionPlugin{
|
||||||
SocketTestWindow socketTestWindow;
|
SocketTestWindow socketTestWindow;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -29,6 +30,11 @@ public class SocketTest implements Plugin{
|
|||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier() {
|
||||||
|
return "Socket.Test.123456";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createPlugin(MainWindow mainWindow) {
|
public void createPlugin(MainWindow mainWindow) {
|
||||||
this.socketTestWindow = new SocketTestWindow(mainWindow);
|
this.socketTestWindow = new SocketTestWindow(mainWindow);
|
||||||
@@ -41,8 +47,8 @@ public class SocketTest implements Plugin{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void receiveMessage(byte[] bytes) {
|
public void receiveMessage(byte[] message) {
|
||||||
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
ByteArrayInputStream bis = new ByteArrayInputStream(message);
|
||||||
ObjectInputStream ois;
|
ObjectInputStream ois;
|
||||||
try {
|
try {
|
||||||
ois = new ObjectInputStream(bis);
|
ois = new ObjectInputStream(bis);
|
||||||
@@ -53,4 +59,19 @@ public class SocketTest implements Plugin{
|
|||||||
System.out.println(ex.toString());
|
System.out.println(ex.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receivePrivateConnection(TcpConnection tcpConnection) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveBytes(TcpConnection connection, byte[] bytes) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSocketClosed(TcpConnection tcpConnection) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -66,7 +66,7 @@ public class HeartbeatManager {
|
|||||||
private void announce() {
|
private void announce() {
|
||||||
try {
|
try {
|
||||||
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
|
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
|
||||||
mainWindow.getSocketUDP().sendMulticast(msg.toByteArray());
|
mainWindow.getSocketUDP().sendMulticast(msg);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Logger.getLogger(HeartbeatManager.class.getName()).log(Level.WARNING, "Falha ao enviar heartbeat", ex);
|
Logger.getLogger(HeartbeatManager.class.getName()).log(Level.WARNING, "Falha ao enviar heartbeat", ex);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -316,16 +316,12 @@ public class SocketTCP extends Thread {
|
|||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
byte[] bytes = connection.receive();
|
byte[] bytes = connection.receive();
|
||||||
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
|
||||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
|
||||||
|
|
||||||
Object object = ois.readObject();
|
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(bytes);
|
||||||
if (object instanceof Message message) {
|
|
||||||
SysInfoMessage sysMessage = SysInfoMessage.parseSystemInfo(message);
|
|
||||||
|
|
||||||
if (sysMessage == null) {
|
if (sysMessage == null) {
|
||||||
for (Plugin plugin : main.getPlugins()) {
|
for (Plugin plugin : main.getPlugins()) {
|
||||||
plugin.receiveMessage(message);
|
plugin.receiveMessage(bytes);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -341,24 +337,6 @@ public class SocketTCP extends Thread {
|
|||||||
default -> {}
|
default -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
SysInfoMessage.sendSystemInfo(
|
|
||||||
connection,
|
|
||||||
SysInfoType.MESSAGE_REJECTED,
|
|
||||||
"Public messages must be instances of Message"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (ClassNotFoundException ex) {
|
|
||||||
SysInfoMessage.sendSystemInfo(
|
|
||||||
connection,
|
|
||||||
SysInfoType.INTERNAL_ERROR,
|
|
||||||
"class Message not found."
|
|
||||||
);
|
|
||||||
logger.log(Level.SEVERE, null, ex);
|
|
||||||
connectionManager.closeConnection(connection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (SocketTimeoutException ex) {
|
catch (SocketTimeoutException ex) {
|
||||||
if (connection.hasTimedOut()) {
|
if (connection.hasTimedOut()) {
|
||||||
connectionManager.closeConnection(connection, "Connection timeout");
|
connectionManager.closeConnection(connection, "Connection timeout");
|
||||||
|
|||||||
@@ -48,11 +48,4 @@ public class SysInfoMessage extends Message {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SysInfoMessage parseSystemInfo(Message message) {
|
|
||||||
if (message instanceof SysInfoMessage) {
|
|
||||||
return (SysInfoMessage) message;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -74,7 +74,7 @@ public class SocketUDP extends Thread {
|
|||||||
* @param msg mensagem serializada a ser enviada.
|
* @param msg mensagem serializada a ser enviada.
|
||||||
* @throws IOException caso ocorra um erro durante o envio.
|
* @throws IOException caso ocorra um erro durante o envio.
|
||||||
*/
|
*/
|
||||||
public void sendMulticast(byte[] msg) throws IOException {
|
public void sendMulticast(Message msg) throws IOException {
|
||||||
send(msg, this.address);
|
send(msg, this.address);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ public class SocketUDP extends Thread {
|
|||||||
* @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(byte[] msg, InetAddress destinationAddress) throws IOException {
|
public void sendUnicast(Message msg, InetAddress destinationAddress) throws IOException {
|
||||||
send(msg, destinationAddress);
|
send(msg, destinationAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,9 +96,10 @@ 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(byte[] msg, InetAddress destinationAddress) throws IOException {
|
private void send(Message msg, InetAddress destinationAddress) throws IOException {
|
||||||
DatagramPacket msgPacket;
|
DatagramPacket msgPacket;
|
||||||
msgPacket = new DatagramPacket(msg, msg.length, destinationAddress, this.main.getPort());
|
byte[] buf = msg.toByteArray();
|
||||||
|
msgPacket = new DatagramPacket(buf, buf.length, destinationAddress, this.main.getPort());
|
||||||
multicastSocket.send(msgPacket);
|
multicastSocket.send(msgPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,14 +150,9 @@ public class SocketUDP extends Thread {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Message message = tryParseMessage(msgPacket.getData());
|
|
||||||
if (message == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Plugin plugin : main.getPlugins()) {
|
for (Plugin plugin : main.getPlugins()) {
|
||||||
System.out.println(msgPacket.getAddress().toString());
|
System.out.println(msgPacket.getAddress().toString());
|
||||||
plugin.receiveMessage(message);
|
plugin.receiveMessage(msgPacket.getData());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,20 +175,6 @@ public class SocketUDP extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Message tryParseMessage(byte[] data) {
|
|
||||||
try {
|
|
||||||
ByteArrayInputStream bis = new ByteArrayInputStream(data);
|
|
||||||
ObjectInputStream ois = new ObjectInputStream(bis);
|
|
||||||
|
|
||||||
Object object = ois.readObject();
|
|
||||||
|
|
||||||
if (object instanceof Message message) return message;
|
|
||||||
return null;
|
|
||||||
} catch (IOException | ClassNotFoundException ex) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inicia a recepção de mensagens UDP.
|
* Inicia a recepção de mensagens UDP.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -47,6 +47,6 @@ public interface Plugin {
|
|||||||
*
|
*
|
||||||
* @param message mensagem serializada recebida.
|
* @param message mensagem serializada recebida.
|
||||||
*/
|
*/
|
||||||
public void receiveMessage(Message message);
|
public void receiveMessage(byte[] message);
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user