ServeManager WIP
This commit is contained in:
1 parent
cd282e29e1
commit
ff7cc3b6ec
5 files changed
+142
-24
No files matched your search
Generated
+20
-2
@@ -6,7 +6,10 @@
|
|||||||
<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/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/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" />
|
||||||
</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" />
|
||||||
@@ -31,10 +34,14 @@
|
|||||||
"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",
|
||||||
|
"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.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",
|
||||||
@@ -91,4 +98,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>
|
||||||
@@ -403,6 +403,6 @@ public class ConnectionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void receiveServerConnection(TcpConnection tcpConnection) {
|
public void receiveServerConnection(TcpConnection tcpConnection) {
|
||||||
|
serverManager.receiveConnection(tcpConnection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ public class ConnectionRequestMessage extends Message {
|
|||||||
public enum RequestStatus {
|
public enum RequestStatus {
|
||||||
PENDING,
|
PENDING,
|
||||||
ACCEPTED,
|
ACCEPTED,
|
||||||
REJECTED
|
REJECTED,
|
||||||
}
|
}
|
||||||
|
|
||||||
final private String receiverId;
|
final private String receiverId;
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import java.net.UnknownHostException;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -72,7 +71,7 @@ public class ServerManager {
|
|||||||
NetworkData.ConnectionType.CLIENT
|
NetworkData.ConnectionType.CLIENT
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!doConnectionHandshake(serverData, clientData))
|
if (!connectToServer(serverData, clientData))
|
||||||
throw new ServerNotFoundException(
|
throw new ServerNotFoundException(
|
||||||
"Could not find server " +
|
"Could not find server " +
|
||||||
serverName + "in " +
|
serverName + "in " +
|
||||||
@@ -86,13 +85,13 @@ public class ServerManager {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean doConnectionHandshake(
|
private boolean connectToServer(
|
||||||
NetworkData clientData,
|
NetworkData clientData,
|
||||||
NetworkData serverData
|
NetworkData serverData
|
||||||
) throws ServerNotFoundException {
|
) throws ServerNotFoundException {
|
||||||
ConnectionRequestMessage request =
|
ConnectionRequestMessage request =
|
||||||
ConnectionRequestMessage.pendingRequest(
|
ConnectionRequestMessage.pendingRequest(
|
||||||
serverData.id(),
|
clientData.id(),
|
||||||
serverData
|
serverData
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -124,20 +123,132 @@ public class ServerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveConnection(TcpConnection tcpConnection) {
|
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
|
||||||
|
) throws IOException {
|
||||||
|
TcpServer tcpServer = getTcpServer(request.getReceiverId());
|
||||||
|
|
||||||
|
if (tcpServer == null) {
|
||||||
|
ConnectionRequestMessage response =
|
||||||
|
ConnectionRequestMessage.rejectedRequest(request);
|
||||||
|
|
||||||
|
tcpConnection.send(response.toByteArray());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tcpServer.addClient(request.getSenderData(), );
|
||||||
|
|
||||||
|
ConnectionRequestMessage response =
|
||||||
|
ConnectionRequestMessage.acceptedRequest(request);
|
||||||
|
|
||||||
|
tcpConnection.send(response.toByteArray());
|
||||||
|
tcpConnection.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user