Criação de clientes e servidores no ServerManager
This commit is contained in:
1 parent
ff7cc3b6ec
commit
3f3c7854b2
3 files changed
+159
-81
No files matched your search
Generated
+22
-23
@@ -5,11 +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/control/ConnectionManager.java" beforeDir="false" afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/control/ConnectionManager.java" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/pitiupi/src/pitiupi/models/Messages/ConnectionRequestMessage.java" beforeDir="false" afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/models/Messages/ConnectionRequestMessage.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$/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$/pitiupi/src/pitiupi/net/tcp/SocketTCP.java" beforeDir="false" afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/tcp/SocketTCP.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" />
|
||||||
@@ -27,27 +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": "rebase/servidores-tcp-e-udp",
|
"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",
|
||||||
"node.js.detected.package.eslint": "true",
|
"node.js.detected.package.eslint": "true",
|
||||||
"node.js.detected.package.tslint": "true",
|
"node.js.detected.package.tslint": "true",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"node.js.selected.package.tslint": "(autodetect)",
|
"node.js.selected.package.tslint": "(autodetect)",
|
||||||
"project.structure.last.edited": "Modules",
|
"project.structure.last.edited": "Modules",
|
||||||
"project.structure.proportion": "0.0",
|
"project.structure.proportion": "0.0",
|
||||||
"project.structure.side.proportion": "0.2",
|
"project.structure.side.proportion": "0.2",
|
||||||
"settings.editor.selected.configurable": "preferences.keymap"
|
"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" />
|
||||||
|
|||||||
@@ -40,13 +40,27 @@ public class ServerManager {
|
|||||||
tcpClients = new HashMap<NetworkData, TcpClient>();
|
tcpClients = new HashMap<NetworkData, TcpClient>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UdpServer getUdpServer(String serverName, String pluginId) {
|
private NetworkData createNetworkData(
|
||||||
NetworkData data = new NetworkData(
|
String serverName,
|
||||||
|
String pluginId,
|
||||||
|
NetworkData.ConnectionProtocol protocol,
|
||||||
|
NetworkData.ConnectionType type
|
||||||
|
) {
|
||||||
|
return new NetworkData(
|
||||||
UUID.randomUUID().toString(),
|
UUID.randomUUID().toString(),
|
||||||
serverName,
|
serverName,
|
||||||
connectionManager.getUserName(),
|
connectionManager.getUserName(),
|
||||||
LocalNetworkInfo.bestLocalIpv4(),
|
LocalNetworkInfo.bestLocalIpv4(),
|
||||||
pluginId,
|
pluginId,
|
||||||
|
protocol,
|
||||||
|
type
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UdpServer createUdpServer(String serverName, String pluginId) {
|
||||||
|
NetworkData data = createNetworkData(
|
||||||
|
serverName,
|
||||||
|
pluginId,
|
||||||
NetworkData.ConnectionProtocol.UDP,
|
NetworkData.ConnectionProtocol.UDP,
|
||||||
NetworkData.ConnectionType.SERVER
|
NetworkData.ConnectionType.SERVER
|
||||||
);
|
);
|
||||||
@@ -56,43 +70,70 @@ public class ServerManager {
|
|||||||
return udpServer;
|
return udpServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UdpClient getUdpClient(
|
public UdpClient createUdpClient(String serverName, String pluginId, NetworkData serverData) {
|
||||||
String serverName,
|
NetworkData clientData = createNetworkData(
|
||||||
String pluginId,
|
|
||||||
NetworkData serverData) {
|
|
||||||
|
|
||||||
NetworkData clientData = new NetworkData(
|
|
||||||
UUID.randomUUID().toString(),
|
|
||||||
serverName,
|
serverName,
|
||||||
connectionManager.getUserName(),
|
|
||||||
LocalNetworkInfo.bestLocalIpv4(),
|
|
||||||
pluginId,
|
pluginId,
|
||||||
NetworkData.ConnectionProtocol.UDP,
|
NetworkData.ConnectionProtocol.UDP,
|
||||||
NetworkData.ConnectionType.CLIENT
|
NetworkData.ConnectionType.CLIENT
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!connectToServer(serverData, clientData))
|
UdpClient newClient = null;
|
||||||
throw new ServerNotFoundException(
|
|
||||||
"Could not find server " +
|
try {
|
||||||
serverName + "in " +
|
TcpConnection connection = ConnectionFactory.createConnection(
|
||||||
serverData.ownerAdress()
|
InetAddress.getByName(serverData.ownerAdress()),
|
||||||
|
connectionManager.getPort(),
|
||||||
|
SERVER_CONNECTION_ID,
|
||||||
|
SERVER_CONNECTION_ID,
|
||||||
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
return new UdpClient(
|
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,
|
connectionManager,
|
||||||
serverData,
|
serverData,
|
||||||
clientData
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean connectToServer(
|
public TcpServer createTcpServer(String serverName, String pluginId) {
|
||||||
NetworkData clientData,
|
NetworkData data = createNetworkData(
|
||||||
NetworkData serverData
|
serverName,
|
||||||
) throws ServerNotFoundException {
|
pluginId,
|
||||||
ConnectionRequestMessage request =
|
NetworkData.ConnectionProtocol.TCP,
|
||||||
ConnectionRequestMessage.pendingRequest(
|
NetworkData.ConnectionType.SERVER
|
||||||
clientData.id(),
|
);
|
||||||
serverData
|
|
||||||
|
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 {
|
try {
|
||||||
@@ -104,10 +145,35 @@ public class ServerManager {
|
|||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
if (connection == null)
|
if (connection == null) {
|
||||||
throw new ServerNotFoundException(
|
throw new ServerNotFoundException("Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
"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.setTimeout(HANDSHAKE_TIMEOUT_MS);
|
||||||
|
|
||||||
connection.send(request.toByteArray());
|
connection.send(request.toByteArray());
|
||||||
@@ -117,16 +183,15 @@ public class ServerManager {
|
|||||||
ObjectInputStream ois = new ObjectInputStream(bis);
|
ObjectInputStream ois = new ObjectInputStream(bis);
|
||||||
|
|
||||||
if (!(ois.readObject() instanceof ConnectionRequestMessage response)) {
|
if (!(ois.readObject() instanceof ConnectionRequestMessage response)) {
|
||||||
throw new ServerNotFoundException(
|
throw new ServerNotFoundException(serverData.ownerAdress() + " responded with an invalid message");
|
||||||
serverData.ownerAdress() +
|
|
||||||
" responded with an invalid message");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connection.setTimeout(0);
|
||||||
return (response.getRequestStatus() == ConnectionRequestMessage.RequestStatus.ACCEPTED);
|
return (response.getRequestStatus() == ConnectionRequestMessage.RequestStatus.ACCEPTED);
|
||||||
|
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
Logger.getLogger(ServerManager.class.getName()).log(Level.WARNING, null, e);
|
Logger.getLogger(ServerManager.class.getName()).log(Level.WARNING, null, e);
|
||||||
throw new ServerNotFoundException(
|
throw new ServerNotFoundException("Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
"Could not estabilish connection with " + serverData.ownerAdress());
|
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, e);
|
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, e);
|
||||||
}
|
}
|
||||||
@@ -166,15 +231,12 @@ public class ServerManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectUdpClient(
|
private void connectUdpClient(ConnectionRequestMessage request, TcpConnection tcpConnection
|
||||||
ConnectionRequestMessage request,
|
|
||||||
TcpConnection tcpConnection
|
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
UdpServer udpServer = getUdpServer(request.getReceiverId());
|
UdpServer udpServer = getUdpServer(request.getReceiverId());
|
||||||
|
|
||||||
if (udpServer == null) {
|
if (udpServer == null) {
|
||||||
ConnectionRequestMessage response =
|
ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
|
||||||
ConnectionRequestMessage.rejectedRequest(request);
|
|
||||||
|
|
||||||
tcpConnection.send(response.toByteArray());
|
tcpConnection.send(response.toByteArray());
|
||||||
return;
|
return;
|
||||||
@@ -182,8 +244,7 @@ public class ServerManager {
|
|||||||
|
|
||||||
udpServer.addClient(request.getSenderData());
|
udpServer.addClient(request.getSenderData());
|
||||||
|
|
||||||
ConnectionRequestMessage response =
|
ConnectionRequestMessage response = ConnectionRequestMessage.acceptedRequest(request);
|
||||||
ConnectionRequestMessage.acceptedRequest(request);
|
|
||||||
|
|
||||||
tcpConnection.send(response.toByteArray());
|
tcpConnection.send(response.toByteArray());
|
||||||
tcpConnection.close();
|
tcpConnection.close();
|
||||||
@@ -192,24 +253,37 @@ public class ServerManager {
|
|||||||
private void connectTcpClient(
|
private void connectTcpClient(
|
||||||
ConnectionRequestMessage request,
|
ConnectionRequestMessage request,
|
||||||
TcpConnection tcpConnection
|
TcpConnection tcpConnection
|
||||||
) throws IOException {
|
) {
|
||||||
|
TcpClient newClient = null;
|
||||||
TcpServer tcpServer = getTcpServer(request.getReceiverId());
|
TcpServer tcpServer = getTcpServer(request.getReceiverId());
|
||||||
|
try {
|
||||||
if (tcpServer == null) {
|
if (tcpServer == null) {
|
||||||
ConnectionRequestMessage response =
|
ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
|
||||||
ConnectionRequestMessage.rejectedRequest(request);
|
|
||||||
|
|
||||||
tcpConnection.send(response.toByteArray());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tcpServer.addClient(request.getSenderData(), );
|
|
||||||
|
|
||||||
ConnectionRequestMessage response =
|
|
||||||
ConnectionRequestMessage.acceptedRequest(request);
|
|
||||||
|
|
||||||
tcpConnection.send(response.toByteArray());
|
tcpConnection.send(response.toByteArray());
|
||||||
tcpConnection.close();
|
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) {
|
public void receiveConnection(TcpConnection tcpConnection) {
|
||||||
@@ -224,16 +298,15 @@ public class ServerManager {
|
|||||||
Logger.getLogger(ServerManager.class.getName()).log(
|
Logger.getLogger(ServerManager.class.getName()).log(
|
||||||
Level.WARNING,
|
Level.WARNING,
|
||||||
null,
|
null,
|
||||||
"Invalid status: " + request.getRequestStatus()
|
"Invalid request status: " + request.getRequestStatus()
|
||||||
);
|
);
|
||||||
ConnectionRequestMessage response =
|
|
||||||
ConnectionRequestMessage.rejectedRequest(request);
|
ConnectionRequestMessage response = ConnectionRequestMessage.rejectedRequest(request);
|
||||||
tcpConnection.send(response.toByteArray());
|
tcpConnection.send(response.toByteArray());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.getSenderData().connectionProtocol() ==
|
if (request.getSenderData().connectionProtocol() == NetworkData.ConnectionProtocol.UDP) {
|
||||||
NetworkData.ConnectionProtocol.UDP) {
|
|
||||||
connectUdpClient(request, tcpConnection);
|
connectUdpClient(request, tcpConnection);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -248,7 +321,13 @@ public class ServerManager {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (ClassNotFoundException | IOException e) {
|
} catch (ClassNotFoundException | IOException e) {
|
||||||
|
try {
|
||||||
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, e);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BIN
Binary file not shown.
Reference in new issue
Block a user