diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 9c35bdd..cac8eea 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,11 +5,10 @@
+
-
-
-
+
@@ -27,27 +26,27 @@
- {
+ "keyToString": {
+ "Application.Main.executor": "Run",
+ "ModuleVcsDetector.initialDetectionPerformed": "true",
+ "RunOnceActivity.ShowReadmeOnStart": "true",
+ "RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
+ "RunOnceActivity.git.unshallow": "true",
+ "git-widget-placeholder": "rebase/servidores-tcp-e-udp",
+ "ignore.virus.scanning.warn.message": "true",
+ "kotlin-language-version-configured": "true",
+ "last_opened_file_path": "C:/Users/extre/OneDrive/Documentos/Faculdade/2026_02/Monitoria/Pitiupi",
+ "node.js.detected.package.eslint": "true",
+ "node.js.detected.package.tslint": "true",
+ "node.js.selected.package.eslint": "(autodetect)",
+ "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"
}
-}]]>
+}
diff --git a/pitiupi/src/pitiupi/net/servers/ServerManager.java b/pitiupi/src/pitiupi/net/servers/ServerManager.java
index 092a32c..ea47f4a 100644
--- a/pitiupi/src/pitiupi/net/servers/ServerManager.java
+++ b/pitiupi/src/pitiupi/net/servers/ServerManager.java
@@ -40,13 +40,27 @@ public class ServerManager {
tcpClients = new HashMap();
}
- public UdpServer getUdpServer(String serverName, String pluginId) {
- NetworkData data = new NetworkData(
+ 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
);
@@ -56,44 +70,15 @@ public class ServerManager {
return udpServer;
}
- public UdpClient getUdpClient(
- String serverName,
- String pluginId,
- NetworkData serverData) {
-
- NetworkData clientData = new NetworkData(
- UUID.randomUUID().toString(),
+ public UdpClient createUdpClient(String serverName, String pluginId, NetworkData serverData) {
+ NetworkData clientData = createNetworkData(
serverName,
- connectionManager.getUserName(),
- LocalNetworkInfo.bestLocalIpv4(),
pluginId,
NetworkData.ConnectionProtocol.UDP,
NetworkData.ConnectionType.CLIENT
);
- if (!connectToServer(serverData, clientData))
- throw new ServerNotFoundException(
- "Could not find server " +
- serverName + "in " +
- serverData.ownerAdress()
- );
-
- return new UdpClient(
- connectionManager,
- serverData,
- clientData
- );
- }
-
- private boolean connectToServer(
- NetworkData clientData,
- NetworkData serverData
- ) throws ServerNotFoundException {
- ConnectionRequestMessage request =
- ConnectionRequestMessage.pendingRequest(
- clientData.id(),
- serverData
- );
+ UdpClient newClient = null;
try {
TcpConnection connection = ConnectionFactory.createConnection(
@@ -104,10 +89,91 @@ public class ServerManager {
true
);
- if (connection == null)
- throw new ServerNotFoundException(
- "Could not estabilish connection with " + serverData.ownerAdress());
+ 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());
@@ -117,16 +183,15 @@ public class ServerManager {
ObjectInputStream ois = new ObjectInputStream(bis);
if (!(ois.readObject() instanceof ConnectionRequestMessage response)) {
- throw new ServerNotFoundException(
- serverData.ownerAdress() +
- " responded with an invalid message");
+ 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());
+ throw new ServerNotFoundException("Could not estabilish connection with " + serverData.ownerAdress());
} catch (IOException | ClassNotFoundException e) {
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, e);
}
@@ -166,15 +231,12 @@ public class ServerManager {
return null;
}
- private void connectUdpClient(
- ConnectionRequestMessage request,
- TcpConnection tcpConnection
+ private void connectUdpClient(ConnectionRequestMessage request, TcpConnection tcpConnection
) throws IOException {
UdpServer udpServer = getUdpServer(request.getReceiverId());
if (udpServer == null) {
- ConnectionRequestMessage response =
- ConnectionRequestMessage.rejectedRequest(request);
+ ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
tcpConnection.send(response.toByteArray());
return;
@@ -182,8 +244,7 @@ public class ServerManager {
udpServer.addClient(request.getSenderData());
- ConnectionRequestMessage response =
- ConnectionRequestMessage.acceptedRequest(request);
+ ConnectionRequestMessage response = ConnectionRequestMessage.acceptedRequest(request);
tcpConnection.send(response.toByteArray());
tcpConnection.close();
@@ -192,24 +253,37 @@ public class ServerManager {
private void connectTcpClient(
ConnectionRequestMessage request,
TcpConnection tcpConnection
- ) throws IOException {
+ ) {
+ TcpClient newClient = null;
TcpServer tcpServer = getTcpServer(request.getReceiverId());
+ try {
+ if (tcpServer == null) {
+ ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
- 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());
- return;
+ } 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);
+ }
}
-
- tcpServer.addClient(request.getSenderData(), );
-
- ConnectionRequestMessage response =
- ConnectionRequestMessage.acceptedRequest(request);
-
- tcpConnection.send(response.toByteArray());
- tcpConnection.close();
}
public void receiveConnection(TcpConnection tcpConnection) {
@@ -224,16 +298,15 @@ public class ServerManager {
Logger.getLogger(ServerManager.class.getName()).log(
Level.WARNING,
null,
- "Invalid status: " + request.getRequestStatus()
+ "Invalid request status: " + request.getRequestStatus()
);
- ConnectionRequestMessage response =
- ConnectionRequestMessage.rejectedRequest(request);
+
+ ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
tcpConnection.send(response.toByteArray());
return;
}
- if (request.getSenderData().connectionProtocol() ==
- NetworkData.ConnectionProtocol.UDP) {
+ if (request.getSenderData().connectionProtocol() == NetworkData.ConnectionProtocol.UDP) {
connectUdpClient(request, tcpConnection);
}
else {
@@ -248,7 +321,13 @@ public class ServerManager {
);
}
} catch (ClassNotFoundException | IOException e) {
- Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, 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);
+ }
}
}
}
\ No newline at end of file
diff --git a/scripts.zip b/scripts.zip
deleted file mode 100644
index 08c32a7..0000000
Binary files a/scripts.zip and /dev/null differ