Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f3c7854b2 | ||
|
|
ff7cc3b6ec | ||
|
|
cd282e29e1 | ||
|
|
9b84be5959 | ||
|
|
e21b11dac6 | ||
|
|
49cf3760a0 | ||
|
|
53d7436966 |
No files matched your search
Generated
+9
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
Generated
+34
-17
@@ -5,8 +5,10 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="62118a1b-608f-4144-b29c-cf46419ca676" name="Changes" comment="">
|
<list default="true" id="62118a1b-608f-4144-b29c-cf46419ca676" name="Changes" comment="">
|
||||||
|
<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$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/udp/SocketUDP.java" beforeDir="false" afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/udp/SocketUDP.java" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/ServerManager.java" beforeDir="false" afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/ServerManager.java" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/scripts.zip" beforeDir="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" />
|
||||||
@@ -24,23 +26,27 @@
|
|||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent">{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"Application.Main.executor": "Run",
|
"Application.Main.executor": "Run",
|
||||||
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
||||||
"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": "main",
|
"git-widget-placeholder": "rebase/servidores-tcp-e-udp",
|
||||||
"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",
|
"last_opened_file_path": "C:/Users/extre/OneDrive/Documentos/Faculdade/2026_02/Monitoria/Pitiupi",
|
||||||
"project.structure.last.edited": "Modules",
|
"node.js.detected.package.eslint": "true",
|
||||||
"project.structure.proportion": "0.0",
|
"node.js.detected.package.tslint": "true",
|
||||||
"project.structure.side.proportion": "0.2",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"settings.editor.selected.configurable": "preferences.keymap"
|
"node.js.selected.package.tslint": "(autodetect)",
|
||||||
|
"project.structure.last.edited": "Modules",
|
||||||
|
"project.structure.proportion": "0.0",
|
||||||
|
"project.structure.side.proportion": "0.2",
|
||||||
|
"settings.editor.selected.configurable": "preferences.keymap"
|
||||||
}
|
}
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
<recent name="C:\Users\extre\OneDrive\Documentos\Faculdade\2026_02\Monitoria\Pitiupi\pitiupi\plugins" />
|
<recent name="C:\Users\extre\OneDrive\Documentos\Faculdade\2026_02\Monitoria\Pitiupi\pitiupi\plugins" />
|
||||||
@@ -91,4 +97,15 @@
|
|||||||
</task>
|
</task>
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
|
<component name="Vcs.Log.Tabs.Properties">
|
||||||
|
<option name="TAB_STATES">
|
||||||
|
<map>
|
||||||
|
<entry key="MAIN">
|
||||||
|
<value>
|
||||||
|
<State />
|
||||||
|
</value>
|
||||||
|
</entry>
|
||||||
|
</map>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -5,11 +5,9 @@ import pitiupi.application.activity.PeerActivityInbox;
|
|||||||
import pitiupi.application.invites.InviteInboxService;
|
import pitiupi.application.invites.InviteInboxService;
|
||||||
import pitiupi.application.peeractions.PeerActionBroker;
|
import pitiupi.application.peeractions.PeerActionBroker;
|
||||||
import pitiupi.application.presence.PresenceService;
|
import pitiupi.application.presence.PresenceService;
|
||||||
import pitiupi.models.Messages.SessionInviteMessage;
|
import pitiupi.models.Messages.*;
|
||||||
import pitiupi.control.plugins.PluginManager;
|
import pitiupi.control.plugins.PluginManager;
|
||||||
import pitiupi.models.Messages.GoodbyeMessage;
|
import pitiupi.net.servers.ServerManager;
|
||||||
import pitiupi.models.Messages.HeartbeatMessage;
|
|
||||||
import pitiupi.models.Messages.Message;
|
|
||||||
import pitiupi.net.tcp.SocketTCP;
|
import pitiupi.net.tcp.SocketTCP;
|
||||||
import pitiupi.net.tcp.TcpAttemptListener;
|
import pitiupi.net.tcp.TcpAttemptListener;
|
||||||
import pitiupi.net.tcp.TcpConnectNotice;
|
import pitiupi.net.tcp.TcpConnectNotice;
|
||||||
@@ -32,6 +30,7 @@ public class ConnectionManager {
|
|||||||
private final PluginManager pluginManager;
|
private final PluginManager pluginManager;
|
||||||
|
|
||||||
private final HeartbeatManager heartbeatManager;
|
private final HeartbeatManager heartbeatManager;
|
||||||
|
private final ServerManager serverManager;
|
||||||
private final PresenceService presenceService;
|
private final PresenceService presenceService;
|
||||||
private final PeerActionBroker peerActionBroker;
|
private final PeerActionBroker peerActionBroker;
|
||||||
private final InviteInboxService inviteInboxService;
|
private final InviteInboxService inviteInboxService;
|
||||||
@@ -70,6 +69,7 @@ public class ConnectionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
heartbeatManager = new HeartbeatManager(this);
|
heartbeatManager = new HeartbeatManager(this);
|
||||||
|
serverManager = new ServerManager(this);
|
||||||
peerActionBroker = new PeerActionBroker();
|
peerActionBroker = new PeerActionBroker();
|
||||||
inviteInboxService = new InviteInboxService(heartbeatManager);
|
inviteInboxService = new InviteInboxService(heartbeatManager);
|
||||||
peerActivityInbox = new PeerActivityInbox();
|
peerActivityInbox = new PeerActivityInbox();
|
||||||
@@ -401,4 +401,8 @@ public class ConnectionManager {
|
|||||||
public void setUserName(String userName) {
|
public void setUserName(String userName) {
|
||||||
this.username = userName;
|
this.username = userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void receiveServerConnection(TcpConnection tcpConnection) {
|
||||||
|
serverManager.receiveConnection(tcpConnection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
|
import pitiupi.net.servers.NetworkData;
|
||||||
|
|
||||||
|
public class ConnectionRequestMessage extends Message {
|
||||||
|
public enum RequestStatus {
|
||||||
|
PENDING,
|
||||||
|
ACCEPTED,
|
||||||
|
REJECTED,
|
||||||
|
}
|
||||||
|
|
||||||
|
final private String receiverId;
|
||||||
|
final private NetworkData senderData;
|
||||||
|
final private RequestStatus requestStatus;
|
||||||
|
|
||||||
|
private ConnectionRequestMessage(
|
||||||
|
String receiverId,
|
||||||
|
NetworkData senderData,
|
||||||
|
RequestStatus requestStatus
|
||||||
|
) {
|
||||||
|
this.receiverId = receiverId;
|
||||||
|
this.senderData = senderData;
|
||||||
|
this.requestStatus = requestStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConnectionRequestMessage pendingRequest(
|
||||||
|
String receiverId,
|
||||||
|
NetworkData senderData
|
||||||
|
) {
|
||||||
|
return new ConnectionRequestMessage(receiverId, senderData, RequestStatus.PENDING);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConnectionRequestMessage acceptedRequest(
|
||||||
|
ConnectionRequestMessage request
|
||||||
|
) {
|
||||||
|
return new ConnectionRequestMessage(
|
||||||
|
request.receiverId,
|
||||||
|
request.senderData,
|
||||||
|
RequestStatus.ACCEPTED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConnectionRequestMessage rejectedRequest(
|
||||||
|
ConnectionRequestMessage request
|
||||||
|
) {
|
||||||
|
return new ConnectionRequestMessage(
|
||||||
|
request.receiverId,
|
||||||
|
request.senderData,
|
||||||
|
RequestStatus.ACCEPTED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getReceiverId() {
|
||||||
|
return receiverId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NetworkData getSenderData() {
|
||||||
|
return senderData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RequestStatus getRequestStatus() {
|
||||||
|
return requestStatus;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
|
public class UdpConnectionMessage extends Message {
|
||||||
|
String toConnectionId;
|
||||||
|
String fromConnectionId;
|
||||||
|
byte[] payload;
|
||||||
|
|
||||||
|
public UdpConnectionMessage(String toConnectionId, String fromConnectionId, byte[] payload) {
|
||||||
|
this.toConnectionId = toConnectionId;
|
||||||
|
this.fromConnectionId = fromConnectionId;
|
||||||
|
this.payload = payload;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
public class ClosedConnectionException extends Exception {
|
||||||
|
public ClosedConnectionException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClosedConnectionException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClosedConnectionException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public record NetworkData(
|
||||||
|
String id,
|
||||||
|
String connectionName,
|
||||||
|
String ownerName,
|
||||||
|
String ownerAdress,
|
||||||
|
String pluginId,
|
||||||
|
ConnectionProtocol connectionProtocol,
|
||||||
|
ConnectionType connectionType
|
||||||
|
) implements Serializable {
|
||||||
|
|
||||||
|
public enum ConnectionProtocol {
|
||||||
|
TCP,
|
||||||
|
UDP
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ConnectionType {
|
||||||
|
SERVER,
|
||||||
|
CLIENT
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
public abstract class NetworkEndpoint {
|
||||||
|
protected final NetworkData networkData;
|
||||||
|
protected boolean isOpen;
|
||||||
|
|
||||||
|
NetworkEndpoint(NetworkData networkData) {
|
||||||
|
this.networkData = networkData;
|
||||||
|
|
||||||
|
this.isOpen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NetworkData getData() {
|
||||||
|
return networkData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOpen() {
|
||||||
|
return isOpen;
|
||||||
|
}
|
||||||
|
|
||||||
|
void close() {
|
||||||
|
isOpen = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,333 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.models.Messages.ConnectionRequestMessage;
|
||||||
|
import pitiupi.net.tcp.ConnectionFactory;
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
import pitiupi.presentation.shell.LocalNetworkInfo;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class ServerManager {
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
|
private final Map<NetworkData, UdpServer> udpServers;
|
||||||
|
private final Map<NetworkData, UdpClient> udpClients;
|
||||||
|
|
||||||
|
private final Map<NetworkData, TcpServer> tcpServers;
|
||||||
|
private final Map<NetworkData, TcpClient> tcpClients;
|
||||||
|
|
||||||
|
private static final int HANDSHAKE_TIMEOUT_MS = 1000;
|
||||||
|
|
||||||
|
public static final String SERVER_CONNECTION_ID = "PITIUPI.SERVER.CONNECTION.REQUEST";
|
||||||
|
|
||||||
|
public ServerManager(ConnectionManager connectionManager) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
|
||||||
|
udpServers = new HashMap<NetworkData, UdpServer>();
|
||||||
|
udpClients = new HashMap<NetworkData, UdpClient>();
|
||||||
|
|
||||||
|
tcpServers = new HashMap<NetworkData, TcpServer>();
|
||||||
|
tcpClients = new HashMap<NetworkData, TcpClient>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private NetworkData createNetworkData(
|
||||||
|
String serverName,
|
||||||
|
String pluginId,
|
||||||
|
NetworkData.ConnectionProtocol protocol,
|
||||||
|
NetworkData.ConnectionType type
|
||||||
|
) {
|
||||||
|
return new NetworkData(
|
||||||
|
UUID.randomUUID().toString(),
|
||||||
|
serverName,
|
||||||
|
connectionManager.getUserName(),
|
||||||
|
LocalNetworkInfo.bestLocalIpv4(),
|
||||||
|
pluginId,
|
||||||
|
protocol,
|
||||||
|
type
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UdpServer createUdpServer(String serverName, String pluginId) {
|
||||||
|
NetworkData data = createNetworkData(
|
||||||
|
serverName,
|
||||||
|
pluginId,
|
||||||
|
NetworkData.ConnectionProtocol.UDP,
|
||||||
|
NetworkData.ConnectionType.SERVER
|
||||||
|
);
|
||||||
|
|
||||||
|
UdpServer udpServer = new UdpServer(connectionManager, data);
|
||||||
|
udpServers.put(data, udpServer);
|
||||||
|
return udpServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UdpClient createUdpClient(String serverName, String pluginId, NetworkData serverData) {
|
||||||
|
NetworkData clientData = createNetworkData(
|
||||||
|
serverName,
|
||||||
|
pluginId,
|
||||||
|
NetworkData.ConnectionProtocol.UDP,
|
||||||
|
NetworkData.ConnectionType.CLIENT
|
||||||
|
);
|
||||||
|
|
||||||
|
UdpClient newClient = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
TcpConnection connection = ConnectionFactory.createConnection(
|
||||||
|
InetAddress.getByName(serverData.ownerAdress()),
|
||||||
|
connectionManager.getPort(),
|
||||||
|
SERVER_CONNECTION_ID,
|
||||||
|
SERVER_CONNECTION_ID,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
if (connection == null) {
|
||||||
|
throw new ServerNotFoundException("Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!connectToServer(serverData, clientData, connection)) {
|
||||||
|
connection.close();
|
||||||
|
throw new ServerNotFoundException("Could not find server " + serverName + "in " + serverData.ownerAdress());
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.close();
|
||||||
|
|
||||||
|
newClient = new UdpClient(
|
||||||
|
connectionManager,
|
||||||
|
serverData,
|
||||||
|
clientData
|
||||||
|
);
|
||||||
|
|
||||||
|
udpClients.put(clientData, newClient);
|
||||||
|
return newClient;
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.WARNING, null, e);
|
||||||
|
throw new ServerNotFoundException("Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TcpServer createTcpServer(String serverName, String pluginId) {
|
||||||
|
NetworkData data = createNetworkData(
|
||||||
|
serverName,
|
||||||
|
pluginId,
|
||||||
|
NetworkData.ConnectionProtocol.TCP,
|
||||||
|
NetworkData.ConnectionType.SERVER
|
||||||
|
);
|
||||||
|
|
||||||
|
TcpServer tcpServer = new TcpServer(data);
|
||||||
|
tcpServers.put(data, tcpServer);
|
||||||
|
return tcpServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TcpClient createTcpClient(String serverName, String pluginId, NetworkData serverData) {
|
||||||
|
NetworkData clientData = createNetworkData(
|
||||||
|
serverName,
|
||||||
|
pluginId,
|
||||||
|
NetworkData.ConnectionProtocol.TCP,
|
||||||
|
NetworkData.ConnectionType.CLIENT
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
TcpConnection connection = ConnectionFactory.createConnection(
|
||||||
|
InetAddress.getByName(serverData.ownerAdress()),
|
||||||
|
connectionManager.getPort(),
|
||||||
|
SERVER_CONNECTION_ID,
|
||||||
|
SERVER_CONNECTION_ID,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
if (connection == null) {
|
||||||
|
throw new ServerNotFoundException("Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!connectToServer(serverData, clientData, connection)) {
|
||||||
|
connection.close();
|
||||||
|
throw new ServerNotFoundException("Could not find server " + serverName + "in " + serverData.ownerAdress());
|
||||||
|
}
|
||||||
|
|
||||||
|
TcpClient newClient = new TcpClient(
|
||||||
|
serverData,
|
||||||
|
clientData,
|
||||||
|
connection
|
||||||
|
);
|
||||||
|
|
||||||
|
tcpClients.put(clientData, newClient);
|
||||||
|
return newClient;
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.WARNING, null, ex);
|
||||||
|
throw new ServerNotFoundException("Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean connectToServer(NetworkData clientData, NetworkData serverData, TcpConnection connection)
|
||||||
|
throws ServerNotFoundException {
|
||||||
|
ConnectionRequestMessage request = ConnectionRequestMessage.pendingRequest(clientData.id(), serverData);
|
||||||
|
|
||||||
|
try {
|
||||||
|
connection.setTimeout(HANDSHAKE_TIMEOUT_MS);
|
||||||
|
|
||||||
|
connection.send(request.toByteArray());
|
||||||
|
byte[] bytes = connection.receive();
|
||||||
|
|
||||||
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(bis);
|
||||||
|
|
||||||
|
if (!(ois.readObject() instanceof ConnectionRequestMessage response)) {
|
||||||
|
throw new ServerNotFoundException(serverData.ownerAdress() + " responded with an invalid message");
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.setTimeout(0);
|
||||||
|
return (response.getRequestStatus() == ConnectionRequestMessage.RequestStatus.ACCEPTED);
|
||||||
|
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.WARNING, null, e);
|
||||||
|
throw new ServerNotFoundException("Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean udpServerExists(String serverId) {
|
||||||
|
for (UdpServer udpServer : udpServers.values()) {
|
||||||
|
if (udpServer.getData().id().equals(serverId))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UdpServer getUdpServer(String serverId) {
|
||||||
|
for (UdpServer udpServer : udpServers.values()) {
|
||||||
|
if (udpServer.getData().id().equals(serverId))
|
||||||
|
return udpServer;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean tcpServerExists(String serverId) {
|
||||||
|
for (TcpServer tcpServer : tcpServers.values()) {
|
||||||
|
if (tcpServer.getData().id().equals(serverId))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TcpServer getTcpServer(String serverId) {
|
||||||
|
for (TcpServer tcpServer : tcpServers.values()) {
|
||||||
|
if (tcpServer.getData().id().equals(serverId))
|
||||||
|
return tcpServer;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void connectUdpClient(ConnectionRequestMessage request, TcpConnection tcpConnection
|
||||||
|
) throws IOException {
|
||||||
|
UdpServer udpServer = getUdpServer(request.getReceiverId());
|
||||||
|
|
||||||
|
if (udpServer == null) {
|
||||||
|
ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
|
||||||
|
|
||||||
|
tcpConnection.send(response.toByteArray());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
udpServer.addClient(request.getSenderData());
|
||||||
|
|
||||||
|
ConnectionRequestMessage response = ConnectionRequestMessage.acceptedRequest(request);
|
||||||
|
|
||||||
|
tcpConnection.send(response.toByteArray());
|
||||||
|
tcpConnection.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void connectTcpClient(
|
||||||
|
ConnectionRequestMessage request,
|
||||||
|
TcpConnection tcpConnection
|
||||||
|
) {
|
||||||
|
TcpClient newClient = null;
|
||||||
|
TcpServer tcpServer = getTcpServer(request.getReceiverId());
|
||||||
|
try {
|
||||||
|
if (tcpServer == null) {
|
||||||
|
ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
|
||||||
|
|
||||||
|
tcpConnection.send(response.toByteArray());
|
||||||
|
tcpConnection.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
newClient = new TcpClient(request.getSenderData(), tcpServer.getData(), tcpConnection);
|
||||||
|
tcpServer.addClient(request.getSenderData(), newClient);
|
||||||
|
|
||||||
|
ConnectionRequestMessage response = ConnectionRequestMessage.acceptedRequest(request);
|
||||||
|
|
||||||
|
tcpConnection.send(response.toByteArray());
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
if (tcpServer != null && newClient != null) {
|
||||||
|
tcpServer.removeClient(newClient.getData().id());
|
||||||
|
}
|
||||||
|
ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
|
||||||
|
|
||||||
|
tcpConnection.send(response.toByteArray());
|
||||||
|
tcpConnection.close();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void receiveConnection(TcpConnection tcpConnection) {
|
||||||
|
try {
|
||||||
|
byte[] bytes = tcpConnection.receive();
|
||||||
|
|
||||||
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(bis);
|
||||||
|
|
||||||
|
if ((ois.readObject() instanceof ConnectionRequestMessage request)) {
|
||||||
|
if (request.getRequestStatus() != ConnectionRequestMessage.RequestStatus.PENDING) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(
|
||||||
|
Level.WARNING,
|
||||||
|
null,
|
||||||
|
"Invalid request status: " + request.getRequestStatus()
|
||||||
|
);
|
||||||
|
|
||||||
|
ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
|
||||||
|
tcpConnection.send(response.toByteArray());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getSenderData().connectionProtocol() == NetworkData.ConnectionProtocol.UDP) {
|
||||||
|
connectUdpClient(request, tcpConnection);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
connectTcpClient(request, tcpConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(
|
||||||
|
Level.SEVERE,
|
||||||
|
null,
|
||||||
|
"Did not receive an ConnectionRequestMessage"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (ClassNotFoundException | IOException e) {
|
||||||
|
try {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, e);
|
||||||
|
tcpConnection.close();
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.WARNING, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
public class ServerNotFoundException extends RuntimeException {
|
||||||
|
public ServerNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
public ServerNotFoundException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
public ServerNotFoundException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class TcpClient extends NetworkEndpoint {
|
||||||
|
NetworkData serverData;
|
||||||
|
|
||||||
|
TcpConnection tcpConnection;
|
||||||
|
|
||||||
|
public TcpClient(NetworkData clientData, NetworkData serverData, TcpConnection tcpConnection) {
|
||||||
|
super(clientData);
|
||||||
|
|
||||||
|
this.tcpConnection = tcpConnection;
|
||||||
|
|
||||||
|
this.serverData = serverData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes()
|
||||||
|
throws IOException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) return tcpConnection.receive();
|
||||||
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(byte[] bytes)
|
||||||
|
throws IOException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
|
tcpConnection.send(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NetworkData getServerData() {
|
||||||
|
return serverData;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void close() {
|
||||||
|
try {
|
||||||
|
super.close();
|
||||||
|
tcpConnection.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Logger.getLogger(TcpClient.class.getName()).log(Level.WARNING, null, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
|
public class TcpServer extends NetworkEndpoint{
|
||||||
|
List<NetworkData> clientsData;
|
||||||
|
Map<String, TcpClient> clientsNetwork;
|
||||||
|
|
||||||
|
private final BlockingQueue<TcpClient> buffer;
|
||||||
|
|
||||||
|
TcpServer(NetworkData serverData) {
|
||||||
|
super(serverData);
|
||||||
|
|
||||||
|
clientsData = new ArrayList<>();
|
||||||
|
clientsNetwork = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
buffer = new LinkedBlockingQueue<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void addClient(NetworkData clientData, TcpClient clientNetwork) throws ClosedConnectionException, InterruptedException {
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This endpoint is closed");
|
||||||
|
|
||||||
|
clientsData.add(clientData);
|
||||||
|
clientsNetwork.put(clientData.id(), clientNetwork);
|
||||||
|
|
||||||
|
buffer.put(clientNetwork);
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeClient(String clientId) throws ClosedConnectionException, InterruptedException {
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This endpoint is closed");
|
||||||
|
|
||||||
|
for (NetworkData data: clientsData) {
|
||||||
|
if (data.id().equals(clientId)) {
|
||||||
|
clientsData.remove(data);
|
||||||
|
TcpClient client = clientsNetwork.remove(data.id());
|
||||||
|
|
||||||
|
buffer.remove(client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TcpClient getClient() throws InterruptedException {
|
||||||
|
return buffer.take();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
|
||||||
|
public class UdpClient extends NetworkEndpoint {
|
||||||
|
private final NetworkData serverData;
|
||||||
|
|
||||||
|
private final UdpNetworkIO udpNetworkIO;
|
||||||
|
|
||||||
|
public UdpClient(
|
||||||
|
ConnectionManager connectionManager,
|
||||||
|
NetworkData serverData,
|
||||||
|
NetworkData clientData
|
||||||
|
) {
|
||||||
|
super(clientData);
|
||||||
|
|
||||||
|
this.udpNetworkIO = new UdpNetworkIO(connectionManager);
|
||||||
|
|
||||||
|
this.serverData = serverData;
|
||||||
|
}
|
||||||
|
|
||||||
|
void receiveBytes(byte[] bytes)
|
||||||
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) udpNetworkIO.receiveBytes(bytes);
|
||||||
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes()
|
||||||
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) return udpNetworkIO.getBytes();
|
||||||
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(byte[] bytes)
|
||||||
|
throws ClosedConnectionException {
|
||||||
|
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
|
udpNetworkIO.send(bytes, networkData.id(), serverData.id(), serverData.ownerAdress());
|
||||||
|
}
|
||||||
|
|
||||||
|
public NetworkData getServerData() {
|
||||||
|
return serverData;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.models.Messages.UdpConnectionMessage;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class UdpNetworkIO {
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
|
private final BlockingQueue<byte[]> buffer;
|
||||||
|
private static final int MAX_BUFFER_CAPACITY = 1024;
|
||||||
|
|
||||||
|
public UdpNetworkIO(ConnectionManager connectionManager) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
this.buffer = new LinkedBlockingQueue<>(MAX_BUFFER_CAPACITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void receiveBytes(byte[] bytes) throws InterruptedException {
|
||||||
|
buffer.put(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes() throws InterruptedException {
|
||||||
|
return buffer.take();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(byte[] bytes, String senderId, String receiverId, String receiverAdress) {
|
||||||
|
UdpConnectionMessage message = new UdpConnectionMessage(
|
||||||
|
senderId,
|
||||||
|
receiverId,
|
||||||
|
bytes
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
InetAddress clientAdress = InetAddress.getByName(receiverAdress);
|
||||||
|
connectionManager.sendUdpUnicast(message, clientAdress);
|
||||||
|
}
|
||||||
|
catch (UnknownHostException e) {
|
||||||
|
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unknown host exception", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
|
||||||
|
public class UdpServer extends NetworkEndpoint{
|
||||||
|
private final List<NetworkData> clientConnections;
|
||||||
|
|
||||||
|
private final UdpNetworkIO udpNetworkIO;
|
||||||
|
|
||||||
|
public UdpServer(
|
||||||
|
ConnectionManager connectionManager,
|
||||||
|
NetworkData serverData
|
||||||
|
) {
|
||||||
|
super(serverData);
|
||||||
|
|
||||||
|
this.udpNetworkIO = new UdpNetworkIO(connectionManager);
|
||||||
|
|
||||||
|
this.clientConnections = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void addClient(NetworkData clientConnection) {
|
||||||
|
clientConnections.add(clientConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeClient(NetworkData clientConnection) {
|
||||||
|
clientConnections.remove(clientConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
void receiveBytes(byte[] bytes)
|
||||||
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) udpNetworkIO.receiveBytes(bytes);
|
||||||
|
else throw new ClosedConnectionException("This endpoint is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes()
|
||||||
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) return udpNetworkIO.getBytes();
|
||||||
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendUnicast(byte[] bytes, NetworkData clientConnection)
|
||||||
|
throws ClosedConnectionException {
|
||||||
|
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
|
udpNetworkIO.send(bytes, networkData.id(), clientConnection.id(), clientConnection.ownerAdress());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMulticast(byte[] bytes)
|
||||||
|
throws ClosedConnectionException {
|
||||||
|
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
|
for (NetworkData clientConnection : clientConnections) {
|
||||||
|
udpNetworkIO.send(bytes, networkData.id(), clientConnection.id(), clientConnection.ownerAdress());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<NetworkData> getClientConnections() {
|
||||||
|
return Collections.unmodifiableList(clientConnections);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import pitiupi.control.plugins.PluginManager;
|
|||||||
import pitiupi.models.Messages.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
import pitiupi.models.Messages.InfoMessage;
|
import pitiupi.models.Messages.InfoMessage;
|
||||||
import pitiupi.models.Messages.InfoType;
|
import pitiupi.models.Messages.InfoType;
|
||||||
|
import pitiupi.net.servers.ServerManager;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -75,17 +76,6 @@ public class SocketTCP extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Envia uma mensagem por meio de uma conexão TCP pública com o peer de destino.
|
|
||||||
*
|
|
||||||
* <p>A conexão pública existente para o endereço informado é reutilizada.
|
|
||||||
* Caso não exista uma conexão válida, uma nova conexão é estabelecida antes
|
|
||||||
* do envio. Falhas durante o estabelecimento ou envio são registradas na
|
|
||||||
* saída padrão e não são propagadas ao chamador.</p>
|
|
||||||
*
|
|
||||||
* @param msg mensagem que será enviada.
|
|
||||||
* @param destinationAddress endereço IP do peer que receberá a mensagem.
|
|
||||||
*/
|
|
||||||
public boolean isAccepting() {
|
public boolean isAccepting() {
|
||||||
return serverSocket != null && serverSocket.isBound() && !serverSocket.isClosed();
|
return serverSocket != null && serverSocket.isBound() && !serverSocket.isClosed();
|
||||||
}
|
}
|
||||||
@@ -286,6 +276,10 @@ public class SocketTCP extends Thread {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (connection.getReceiver().equals(ServerManager.SERVER_CONNECTION_ID)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (pluginManager.handleConnection(connection, connection.getReceiver())) {
|
if (pluginManager.handleConnection(connection, connection.getReceiver())) {
|
||||||
privateConnectionReceiver(connection);
|
privateConnectionReceiver(connection);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,14 +71,13 @@ public class TcpConnection {
|
|||||||
* @throws IOException se o tamanho da mensagem for inválido ou se a conexão
|
* @throws IOException se o tamanho da mensagem for inválido ou se a conexão
|
||||||
* for encerrada antes que a mensagem seja completamente recebida
|
* for encerrada antes que a mensagem seja completamente recebida
|
||||||
*/
|
*/
|
||||||
byte[] receive() throws IOException {
|
public byte[] receive() throws IOException {
|
||||||
int length = input.readInt();
|
int length = input.readInt();
|
||||||
|
|
||||||
if (length < 0) throw new IOException("Invalid message length.");
|
if (length < 0) throw new IOException("Invalid message length.");
|
||||||
|
|
||||||
byte[] message = input.readNBytes(length);
|
byte[] message = input.readNBytes(length);
|
||||||
|
|
||||||
|
|
||||||
if (message.length != length) {
|
if (message.length != length) {
|
||||||
throw new IOException("Connection closed before receiving the complete message.");
|
throw new IOException("Connection closed before receiving the complete message.");
|
||||||
}
|
}
|
||||||
@@ -103,7 +102,7 @@ public class TcpConnection {
|
|||||||
*
|
*
|
||||||
* @throws IOException se ocorrer um erro ao fechar o socket
|
* @throws IOException se ocorrer um erro ao fechar o socket
|
||||||
*/
|
*/
|
||||||
void close() throws IOException {
|
public void close() throws IOException {
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +128,7 @@ public class TcpConnection {
|
|||||||
* @param timeout tempo limite, em milissegundos
|
* @param timeout tempo limite, em milissegundos
|
||||||
* @throws SocketException se ocorrer um erro ao definir o tempo limite
|
* @throws SocketException se ocorrer um erro ao definir o tempo limite
|
||||||
*/
|
*/
|
||||||
void setTimeout(int timeout) throws SocketException {
|
public void setTimeout(int timeout) throws SocketException {
|
||||||
socket.setSoTimeout(timeout);
|
socket.setSoTimeout(timeout);
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user