ServerManager WIP
This commit is contained in:
1 parent
85a1ab74d7
commit
53d7436966
15 files changed
+533
-34
No files matched your search
Generated
+36
-24
@@ -5,7 +5,19 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="184f7582-9491-4541-9ea8-677e31b83cd6" name="Changes" comment="">
|
<list default="true" id="184f7582-9491-4541-9ea8-677e31b83cd6" name="Changes" comment="">
|
||||||
<change beforePath="$PROJECT_DIR$/plugins.registry.json" beforeDir="false" afterPath="$PROJECT_DIR$/plugins.registry.json" afterDir="false" />
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/control/ServerManager.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/models/Messages/ConnectionRequestMessage.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/models/Messages/UdpConnectionMessage.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/ClosedConnectionException.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/ConnectionData.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/ServerNotFoundException.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/TcpClient.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/TcpServer.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/UdpClient.java" afterDir="false" />
|
||||||
|
<change afterPath="$PROJECT_DIR$/pitiupi/src/pitiupi/net/servers/UdpServer.java" 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$/plugins/damas/build/built-jar.properties" beforeDir="false" afterPath="$PROJECT_DIR$/plugins/damas/build/built-jar.properties" 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" />
|
||||||
@@ -24,30 +36,30 @@
|
|||||||
<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">{
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"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",
|
||||||
"RunOnceActivity.typescript.service.memoryLimit.init": "true",
|
"RunOnceActivity.typescript.service.memoryLimit.init": "true",
|
||||||
"codeWithMe.voiceChat.enabledByDefault": "false",
|
"codeWithMe.voiceChat.enabledByDefault": "false",
|
||||||
"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/Gustavo/OneDrive/Documentos/Faculdade/2026_2/Monitoria/pitiupi",
|
"last_opened_file_path": "C:/Users/Gustavo/OneDrive/Documentos/Faculdade/2026_2/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)",
|
||||||
"nodejs_package_manager_path": "npm",
|
"nodejs_package_manager_path": "npm",
|
||||||
"project.structure.last.edited": "Project",
|
"project.structure.last.edited": "Project",
|
||||||
"project.structure.proportion": "0.0",
|
"project.structure.proportion": "0.0",
|
||||||
"project.structure.side.proportion": "0.0",
|
"project.structure.side.proportion": "0.0",
|
||||||
"run.code.analysis.last.selected.profile": "pProject Default",
|
"run.code.analysis.last.selected.profile": "pProject Default",
|
||||||
"settings.editor.selected.configurable": "preferences.lookFeel"
|
"settings.editor.selected.configurable": "preferences.lookFeel"
|
||||||
}
|
}
|
||||||
}</component>
|
}]]></component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
<recent name="C:\Users\Gustavo\OneDrive\Documentos\Faculdade\2026_2\Monitoria\pitiupi\plugins\ping" />
|
<recent name="C:\Users\Gustavo\OneDrive\Documentos\Faculdade\2026_2\Monitoria\pitiupi\plugins\ping" />
|
||||||
|
|||||||
BIN
Binary file not shown.
@@ -5,11 +5,8 @@ import pitiupi.application.activity.PeerActivityInbox;
|
|||||||
import pitiupi.application.invites.InviteInboxService;
|
import pitiupi.application.invites.InviteInboxService;
|
||||||
import pitiupi.application.peeractions.PeerActionBroker;
|
import pitiupi.application.peeractions.PeerActionBroker;
|
||||||
import pitiupi.application.presence.PresenceService;
|
import pitiupi.application.presence.PresenceService;
|
||||||
import pitiupi.models.Messages.SessionInviteMessage;
|
import pitiupi.models.Messages.*;
|
||||||
import pitiupi.control.plugins.PluginManager;
|
import pitiupi.control.plugins.PluginManager;
|
||||||
import pitiupi.models.Messages.GoodbyeMessage;
|
|
||||||
import pitiupi.models.Messages.HeartbeatMessage;
|
|
||||||
import pitiupi.models.Messages.Message;
|
|
||||||
import pitiupi.net.tcp.SocketTCP;
|
import pitiupi.net.tcp.SocketTCP;
|
||||||
import pitiupi.net.tcp.TcpAttemptListener;
|
import pitiupi.net.tcp.TcpAttemptListener;
|
||||||
import pitiupi.net.tcp.TcpConnectNotice;
|
import pitiupi.net.tcp.TcpConnectNotice;
|
||||||
@@ -401,4 +398,10 @@ public class ConnectionManager {
|
|||||||
public void setUserName(String userName) {
|
public void setUserName(String userName) {
|
||||||
this.username = userName;
|
this.username = userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void receiveConnectionRequest(ConnectionRequestMessage message) {
|
||||||
|
if (message == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package pitiupi.control;
|
||||||
|
|
||||||
|
import pitiupi.models.Messages.ConnectionRequestMessage;
|
||||||
|
import pitiupi.net.servers.*;
|
||||||
|
import pitiupi.net.tcp.ConnectionFactory;
|
||||||
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
|
import pitiupi.presentation.shell.LocalNetworkInfo;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class ServerManager {
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
|
private final Map<ConnectionData, UdpServer> udpServers;
|
||||||
|
private final Map<ConnectionData, UdpClient> udpClients;
|
||||||
|
|
||||||
|
private final Map<ConnectionData, TcpServer> tcpServers;
|
||||||
|
private final Map<ConnectionData, TcpClient> tcpClients;
|
||||||
|
|
||||||
|
private static final int HANDSHAKE_TIMEOUT_MS = 1000;
|
||||||
|
|
||||||
|
public ServerManager(ConnectionManager connectionManager) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
|
||||||
|
udpServers = new HashMap<ConnectionData, UdpServer>();
|
||||||
|
udpClients = new HashMap<ConnectionData, UdpClient>();
|
||||||
|
|
||||||
|
tcpServers = new HashMap<ConnectionData, TcpServer>();
|
||||||
|
tcpClients = new HashMap<ConnectionData, TcpClient>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UdpServer getUdpServer(String serverName, String pluginId) {
|
||||||
|
ConnectionData data = new ConnectionData(
|
||||||
|
UUID.randomUUID().toString(),
|
||||||
|
serverName,
|
||||||
|
connectionManager.getUserName(),
|
||||||
|
LocalNetworkInfo.bestLocalIpv4(),
|
||||||
|
pluginId,
|
||||||
|
ConnectionData.ConnectionProtocol.UDP,
|
||||||
|
ConnectionData.ConnectionType.SERVER
|
||||||
|
);
|
||||||
|
|
||||||
|
UdpServer udpServer = new UdpServer(connectionManager, data);
|
||||||
|
udpServers.put(data, udpServer);
|
||||||
|
return udpServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UdpClient getUdpClient(
|
||||||
|
String serverName,
|
||||||
|
String pluginId,
|
||||||
|
ConnectionData serverData) {
|
||||||
|
|
||||||
|
ConnectionData clientData = new ConnectionData(
|
||||||
|
UUID.randomUUID().toString(),
|
||||||
|
serverName,
|
||||||
|
connectionManager.getUserName(),
|
||||||
|
LocalNetworkInfo.bestLocalIpv4(),
|
||||||
|
pluginId,
|
||||||
|
ConnectionData.ConnectionProtocol.UDP,
|
||||||
|
ConnectionData.ConnectionType.CLIENT
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!doConnectionHandshake(serverData, clientData))
|
||||||
|
throw new ServerNotFoundException(
|
||||||
|
"Could not find server " +
|
||||||
|
serverName + "in " +
|
||||||
|
serverData.ownerAdress()
|
||||||
|
);
|
||||||
|
|
||||||
|
return new UdpClient(
|
||||||
|
connectionManager,
|
||||||
|
serverData,
|
||||||
|
clientData
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean doConnectionHandshake(
|
||||||
|
ConnectionData clientData,
|
||||||
|
ConnectionData serverData
|
||||||
|
) throws ServerNotFoundException {
|
||||||
|
ConnectionRequestMessage request =
|
||||||
|
ConnectionRequestMessage.pendingRequest(
|
||||||
|
serverData.id(),
|
||||||
|
serverData
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
TcpConnection connection = ConnectionFactory.createConnection(
|
||||||
|
InetAddress.getByName(serverData.ownerAdress()),
|
||||||
|
connectionManager.getPort(),
|
||||||
|
clientData.pluginId(),
|
||||||
|
serverData.pluginId(),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
if (connection == null)
|
||||||
|
throw new ServerNotFoundException(
|
||||||
|
"Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
|
|
||||||
|
connection.setTimeout(HANDSHAKE_TIMEOUT_MS);
|
||||||
|
|
||||||
|
connection.send(request.toByteArray());
|
||||||
|
byte[] bytes = connection.receive();
|
||||||
|
|
||||||
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(bis);
|
||||||
|
|
||||||
|
if (!(ois.readObject() instanceof ConnectionRequestMessage response)) {
|
||||||
|
throw new ServerNotFoundException(
|
||||||
|
serverData.ownerAdress() +
|
||||||
|
" responded with an invalid message");
|
||||||
|
}
|
||||||
|
|
||||||
|
return (response.getRequestStatus() == ConnectionRequestMessage.RequestStatus.ACCEPTED);
|
||||||
|
}
|
||||||
|
catch (TimeoutException e) {
|
||||||
|
throw new ServerNotFoundException(
|
||||||
|
serverData.ownerAdress() +
|
||||||
|
" has timed out");
|
||||||
|
}
|
||||||
|
catch (UnknownHostException e) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.WARNING, null, e);
|
||||||
|
throw new ServerNotFoundException(
|
||||||
|
"Could not estabilish connection with " + serverData.ownerAdress());
|
||||||
|
}
|
||||||
|
catch (IOException | ClassNotFoundException e) {
|
||||||
|
Logger.getLogger(ServerManager.class.getName()).log(Level.SEVERE, null, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
|
import pitiupi.net.servers.ConnectionData;
|
||||||
|
|
||||||
|
public class ConnectionRequestMessage extends Message {
|
||||||
|
public enum RequestStatus {
|
||||||
|
PENDING,
|
||||||
|
ACCEPTED,
|
||||||
|
REJECTED
|
||||||
|
}
|
||||||
|
|
||||||
|
final private String receiverId;
|
||||||
|
final private ConnectionData senderData;
|
||||||
|
final private RequestStatus requestStatus;
|
||||||
|
|
||||||
|
private ConnectionRequestMessage(
|
||||||
|
String receiverId,
|
||||||
|
ConnectionData senderData,
|
||||||
|
RequestStatus requestStatus
|
||||||
|
) {
|
||||||
|
this.receiverId = receiverId;
|
||||||
|
this.senderData = senderData;
|
||||||
|
this.requestStatus = requestStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConnectionRequestMessage pendingRequest(
|
||||||
|
String receiverId,
|
||||||
|
ConnectionData senderData
|
||||||
|
) {
|
||||||
|
return new ConnectionRequestMessage(receiverId, senderData, RequestStatus.PENDING);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConnectionRequestMessage acceptedRequest(
|
||||||
|
ConnectionRequestMessage request
|
||||||
|
) {
|
||||||
|
return new ConnectionRequestMessage(
|
||||||
|
request.receiverId,
|
||||||
|
request.senderData,
|
||||||
|
RequestStatus.ACCEPTED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConnectionRequestMessage rejectedRequest(
|
||||||
|
ConnectionRequestMessage request
|
||||||
|
) {
|
||||||
|
return new ConnectionRequestMessage(
|
||||||
|
request.receiverId,
|
||||||
|
request.senderData,
|
||||||
|
RequestStatus.ACCEPTED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getReceiverId() {
|
||||||
|
return receiverId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConnectionData getSenderData() {
|
||||||
|
return senderData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RequestStatus getRequestStatus() {
|
||||||
|
return requestStatus;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
|
public class UdpConnectionMessage extends Message {
|
||||||
|
String toConnectionId;
|
||||||
|
String fromConnectionId;
|
||||||
|
byte[] payload;
|
||||||
|
|
||||||
|
public UdpConnectionMessage(String toConnectionId, String fromConnectionId, byte[] payload) {
|
||||||
|
this.toConnectionId = toConnectionId;
|
||||||
|
this.fromConnectionId = fromConnectionId;
|
||||||
|
this.payload = payload;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
public class ClosedConnectionException extends Exception {
|
||||||
|
public ClosedConnectionException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClosedConnectionException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClosedConnectionException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public record ConnectionData(
|
||||||
|
String id,
|
||||||
|
String connectionName,
|
||||||
|
String ownerName,
|
||||||
|
String ownerAdress,
|
||||||
|
String pluginId,
|
||||||
|
ConnectionProtocol connectionProtocol,
|
||||||
|
ConnectionType connectionType
|
||||||
|
) implements Serializable {
|
||||||
|
|
||||||
|
public enum ConnectionProtocol {
|
||||||
|
TCP,
|
||||||
|
UDP
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ConnectionType {
|
||||||
|
SERVER,
|
||||||
|
CLIENT
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
public class ServerNotFoundException extends RuntimeException {
|
||||||
|
public ServerNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
public ServerNotFoundException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
public ServerNotFoundException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
public class TcpClient {
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
public class TcpServer {
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.models.Messages.UdpConnectionMessage;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class UdpClient {
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
|
private final ConnectionData serverData;
|
||||||
|
private final ConnectionData clientData;
|
||||||
|
private final BlockingQueue<byte[]> buffer;
|
||||||
|
private boolean isOpen;
|
||||||
|
|
||||||
|
private static final int MAX_BUFFER_CAPACITY = 1024;
|
||||||
|
|
||||||
|
public UdpClient(
|
||||||
|
ConnectionManager connectionManager,
|
||||||
|
ConnectionData serverData,
|
||||||
|
ConnectionData clientData
|
||||||
|
) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
|
||||||
|
this.serverData = serverData;
|
||||||
|
this.clientData = clientData;
|
||||||
|
|
||||||
|
buffer = new ArrayBlockingQueue<>(MAX_BUFFER_CAPACITY);
|
||||||
|
|
||||||
|
isOpen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void receiveBytes(byte[] bytes)
|
||||||
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) buffer.put(bytes);
|
||||||
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes()
|
||||||
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) return buffer.take();
|
||||||
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(byte[] bytes)
|
||||||
|
throws ClosedConnectionException {
|
||||||
|
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
|
UdpConnectionMessage message = new UdpConnectionMessage(
|
||||||
|
serverData.id(),
|
||||||
|
clientData.id(),
|
||||||
|
bytes
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
InetAddress clientAdress = InetAddress.getByName(clientData.ownerAdress());
|
||||||
|
connectionManager.sendUdpUnicast(message, clientAdress);
|
||||||
|
}
|
||||||
|
catch (UnknownHostException e) {
|
||||||
|
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unknown host exception", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConnectionData getServerData() {
|
||||||
|
return serverData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConnectionData getClientData() {
|
||||||
|
return clientData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOpen() {
|
||||||
|
return isOpen;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void close() {
|
||||||
|
isOpen = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
|
import pitiupi.models.Messages.UdpConnectionMessage;
|
||||||
|
|
||||||
|
public class UdpServer {
|
||||||
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
|
private final ConnectionData serverData;
|
||||||
|
private final List<ConnectionData> clientConnections;
|
||||||
|
|
||||||
|
private final BlockingQueue<byte[]> buffer;
|
||||||
|
private boolean isOpen;
|
||||||
|
|
||||||
|
private static final int MAX_BUFFER_CAPACITY = 1024;
|
||||||
|
|
||||||
|
public UdpServer(
|
||||||
|
ConnectionManager connectionManager,
|
||||||
|
ConnectionData serverData
|
||||||
|
) {
|
||||||
|
this.connectionManager = connectionManager;
|
||||||
|
|
||||||
|
this.serverData = serverData;
|
||||||
|
this.clientConnections = new ArrayList<>();
|
||||||
|
|
||||||
|
buffer = new ArrayBlockingQueue<>(MAX_BUFFER_CAPACITY);
|
||||||
|
|
||||||
|
isOpen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void addClient(ConnectionData clientConnection) {
|
||||||
|
clientConnections.add(clientConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void removeClient(ConnectionData clientConnection) {
|
||||||
|
clientConnections.remove(clientConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void receiveBytes(byte[] bytes)
|
||||||
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) buffer.put(bytes);
|
||||||
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes()
|
||||||
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
|
if (isOpen) return buffer.take();
|
||||||
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendUnicast(byte[] bytes, ConnectionData clientConnection)
|
||||||
|
throws ClosedConnectionException {
|
||||||
|
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
|
UdpConnectionMessage message = new UdpConnectionMessage(
|
||||||
|
serverData.id(),
|
||||||
|
clientConnection.id(),
|
||||||
|
bytes
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
InetAddress clientAdress = InetAddress.getByName(clientConnection.ownerAdress());
|
||||||
|
connectionManager.sendUdpUnicast(message, clientAdress);
|
||||||
|
}
|
||||||
|
catch (UnknownHostException e) {
|
||||||
|
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unknown host exception", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMulticast(byte[] bytes)
|
||||||
|
throws ClosedConnectionException {
|
||||||
|
|
||||||
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
|
for (ConnectionData clientConnection : clientConnections) {
|
||||||
|
UdpConnectionMessage message = new UdpConnectionMessage(
|
||||||
|
serverData.id(),
|
||||||
|
clientConnection.id(),
|
||||||
|
bytes
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
InetAddress clientAdress = InetAddress.getByName(clientConnection.ownerAdress());
|
||||||
|
connectionManager.sendUdpUnicast(message, clientAdress);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unknown host exception", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConnectionData getServerData() {
|
||||||
|
return serverData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ConnectionData> getClientConnections() {
|
||||||
|
return Collections.unmodifiableList(clientConnections);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOpen() {
|
||||||
|
return isOpen;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void close() {
|
||||||
|
isOpen = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,14 +71,13 @@ public class TcpConnection {
|
|||||||
* @throws IOException se o tamanho da mensagem for inválido ou se a conexão
|
* @throws IOException se o tamanho da mensagem for inválido ou se a conexão
|
||||||
* for encerrada antes que a mensagem seja completamente recebida
|
* for encerrada antes que a mensagem seja completamente recebida
|
||||||
*/
|
*/
|
||||||
byte[] receive() throws IOException {
|
public byte[] receive() throws IOException {
|
||||||
int length = input.readInt();
|
int length = input.readInt();
|
||||||
|
|
||||||
if (length < 0) throw new IOException("Invalid message length.");
|
if (length < 0) throw new IOException("Invalid message length.");
|
||||||
|
|
||||||
byte[] message = input.readNBytes(length);
|
byte[] message = input.readNBytes(length);
|
||||||
|
|
||||||
|
|
||||||
if (message.length != length) {
|
if (message.length != length) {
|
||||||
throw new IOException("Connection closed before receiving the complete message.");
|
throw new IOException("Connection closed before receiving the complete message.");
|
||||||
}
|
}
|
||||||
@@ -129,7 +128,7 @@ public class TcpConnection {
|
|||||||
* @param timeout tempo limite, em milissegundos
|
* @param timeout tempo limite, em milissegundos
|
||||||
* @throws SocketException se ocorrer um erro ao definir o tempo limite
|
* @throws SocketException se ocorrer um erro ao definir o tempo limite
|
||||||
*/
|
*/
|
||||||
void setTimeout(int timeout) throws SocketException {
|
public void setTimeout(int timeout) throws SocketException {
|
||||||
socket.setSoTimeout(timeout);
|
socket.setSoTimeout(timeout);
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#Wed, 04 Nov 2026 10:51:29 -0300
|
#Sun, 08 Nov 2026 16:11:59 -0300
|
||||||
|
|
||||||
|
|
||||||
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\dama\\pitiupi\\plugins\\damas=
|
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\pitiupi\\plugins\\damas=
|
||||||
|
|
||||||
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\dama\\pitiupi\\pitiupi=
|
C\:\\Users\\Gustavo\\OneDrive\\Documentos\\Faculdade\\2026_2\\Monitoria\\pitiupi\\pitiupi=
|
||||||
Reference in new issue
Block a user