From 3f3c7854b28d996cdd89d4c2464ba3743353b5b7 Mon Sep 17 00:00:00 2001 From: GustavoHMDS Date: Wed, 11 Nov 2026 16:14:41 -0300 Subject: [PATCH] =?UTF-8?q?Cria=C3=A7=C3=A3o=20de=20clientes=20e=20servido?= =?UTF-8?q?res=20no=20ServerManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 45 ++-- .../pitiupi/net/servers/ServerManager.java | 215 ++++++++++++------ scripts.zip | Bin 6367 -> 0 bytes 3 files changed, 169 insertions(+), 91 deletions(-) delete mode 100644 scripts.zip 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 @@ + - - - + - { + "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 08c32a7adccbd0edf8e17da5ffa19b8e0b7c4009..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6367 zcma)>1x#E^--j1j+=|2E?hXsZ-K97b*Tua+ftFp|-KDrwT#CCDSfCV&w0H|G?pFA2 z?<;9;?tSx}lgyculRWd!Ony0;XS7t2kO=|5E{K+`>7Si{H)sGFfS0wWgS)pEw~rqt z00H^ehd&!VU2Fhq+N*uXt;rKd`#+vq|4705jpFQJ_5UQH0nh-Nf2oJ+FC=G6A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