Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f3c7854b2 |
No files matched your search
Generated
+22
-23
@@ -5,11 +5,10 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<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$/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/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>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@@ -27,27 +26,27 @@
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"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"
|
||||
<component name="PropertiesComponent">{
|
||||
"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"
|
||||
}
|
||||
}]]></component>
|
||||
}</component>
|
||||
<component name="RecentsManager">
|
||||
<key name="CopyFile.RECENT_KEYS">
|
||||
<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>();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
Binary file not shown.
Reference in new issue
Block a user