Compare commits
2
Commits
main
...
49cf3760a0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49cf3760a0 | ||
|
|
53d7436966 |
No files matched your search
Generated
+36
-24
@@ -5,7 +5,19 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<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>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@@ -24,30 +36,30 @@
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent">{
|
||||
"keyToString": {
|
||||
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
||||
"RunOnceActivity.git.unshallow": "true",
|
||||
"RunOnceActivity.typescript.service.memoryLimit.init": "true",
|
||||
"codeWithMe.voiceChat.enabledByDefault": "false",
|
||||
"git-widget-placeholder": "main",
|
||||
"ignore.virus.scanning.warn.message": "true",
|
||||
"kotlin-language-version-configured": "true",
|
||||
"last_opened_file_path": "C:/Users/Gustavo/OneDrive/Documentos/Faculdade/2026_2/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)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"project.structure.last.edited": "Project",
|
||||
"project.structure.proportion": "0.0",
|
||||
"project.structure.side.proportion": "0.0",
|
||||
"run.code.analysis.last.selected.profile": "pProject Default",
|
||||
"settings.editor.selected.configurable": "preferences.lookFeel"
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
||||
"RunOnceActivity.git.unshallow": "true",
|
||||
"RunOnceActivity.typescript.service.memoryLimit.init": "true",
|
||||
"codeWithMe.voiceChat.enabledByDefault": "false",
|
||||
"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/Gustavo/OneDrive/Documentos/Faculdade/2026_2/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)",
|
||||
"nodejs_package_manager_path": "npm",
|
||||
"project.structure.last.edited": "Project",
|
||||
"project.structure.proportion": "0.0",
|
||||
"project.structure.side.proportion": "0.0",
|
||||
"run.code.analysis.last.selected.profile": "pProject Default",
|
||||
"settings.editor.selected.configurable": "preferences.lookFeel"
|
||||
}
|
||||
}</component>
|
||||
}]]></component>
|
||||
<component name="RecentsManager">
|
||||
<key name="CopyFile.RECENT_KEYS">
|
||||
<recent name="C:\Users\Gustavo\OneDrive\Documentos\Faculdade\2026_2\Monitoria\pitiupi\plugins\ping" />
|
||||
|
||||
BIN
Binary file not shown.
@@ -5,11 +5,9 @@ import pitiupi.application.activity.PeerActivityInbox;
|
||||
import pitiupi.application.invites.InviteInboxService;
|
||||
import pitiupi.application.peeractions.PeerActionBroker;
|
||||
import pitiupi.application.presence.PresenceService;
|
||||
import pitiupi.models.Messages.SessionInviteMessage;
|
||||
import pitiupi.models.Messages.*;
|
||||
import pitiupi.control.plugins.PluginManager;
|
||||
import pitiupi.models.Messages.GoodbyeMessage;
|
||||
import pitiupi.models.Messages.HeartbeatMessage;
|
||||
import pitiupi.models.Messages.Message;
|
||||
import pitiupi.net.servers.ServerManager;
|
||||
import pitiupi.net.tcp.SocketTCP;
|
||||
import pitiupi.net.tcp.TcpAttemptListener;
|
||||
import pitiupi.net.tcp.TcpConnectNotice;
|
||||
@@ -32,6 +30,7 @@ public class ConnectionManager {
|
||||
private final PluginManager pluginManager;
|
||||
|
||||
private final HeartbeatManager heartbeatManager;
|
||||
private final ServerManager serverManager;
|
||||
private final PresenceService presenceService;
|
||||
private final PeerActionBroker peerActionBroker;
|
||||
private final InviteInboxService inviteInboxService;
|
||||
@@ -70,6 +69,7 @@ public class ConnectionManager {
|
||||
}
|
||||
|
||||
heartbeatManager = new HeartbeatManager(this);
|
||||
serverManager = new ServerManager(this);
|
||||
peerActionBroker = new PeerActionBroker();
|
||||
inviteInboxService = new InviteInboxService(heartbeatManager);
|
||||
peerActivityInbox = new PeerActivityInbox();
|
||||
@@ -401,4 +401,8 @@ public class ConnectionManager {
|
||||
public void setUserName(String userName) {
|
||||
this.username = userName;
|
||||
}
|
||||
|
||||
public void receiveServerConnection(TcpConnection tcpConnection) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package pitiupi.models.Messages;
|
||||
|
||||
import pitiupi.net.servers.NetworkData;
|
||||
|
||||
public class ConnectionRequestMessage extends Message {
|
||||
public enum RequestStatus {
|
||||
PENDING,
|
||||
ACCEPTED,
|
||||
REJECTED
|
||||
}
|
||||
|
||||
final private String receiverId;
|
||||
final private NetworkData senderData;
|
||||
final private RequestStatus requestStatus;
|
||||
|
||||
private ConnectionRequestMessage(
|
||||
String receiverId,
|
||||
NetworkData senderData,
|
||||
RequestStatus requestStatus
|
||||
) {
|
||||
this.receiverId = receiverId;
|
||||
this.senderData = senderData;
|
||||
this.requestStatus = requestStatus;
|
||||
}
|
||||
|
||||
public static ConnectionRequestMessage pendingRequest(
|
||||
String receiverId,
|
||||
NetworkData 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 NetworkData 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 NetworkData(
|
||||
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,24 @@
|
||||
package pitiupi.net.servers;
|
||||
|
||||
public abstract class NetworkEndpoint {
|
||||
protected final NetworkData networkData;
|
||||
protected boolean isOpen;
|
||||
|
||||
NetworkEndpoint(NetworkData networkData) {
|
||||
this.networkData = networkData;
|
||||
|
||||
this.isOpen = true;
|
||||
}
|
||||
|
||||
public NetworkData getData() {
|
||||
return networkData;
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return isOpen;
|
||||
}
|
||||
|
||||
void close() {
|
||||
isOpen = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package pitiupi.net.servers;
|
||||
|
||||
import pitiupi.control.ConnectionManager;
|
||||
import pitiupi.models.Messages.ConnectionRequestMessage;
|
||||
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<NetworkData, UdpServer> udpServers;
|
||||
private final Map<NetworkData, UdpClient> udpClients;
|
||||
|
||||
private final Map<NetworkData, TcpServer> tcpServers;
|
||||
private final Map<NetworkData, TcpClient> tcpClients;
|
||||
|
||||
private static final int HANDSHAKE_TIMEOUT_MS = 1000;
|
||||
|
||||
public static final String SERVER_CONNECTION_ID = "PITIUPI.SERVER.CONNECTION.REQUEST";
|
||||
|
||||
public ServerManager(ConnectionManager connectionManager) {
|
||||
this.connectionManager = connectionManager;
|
||||
|
||||
udpServers = new HashMap<NetworkData, UdpServer>();
|
||||
udpClients = new HashMap<NetworkData, UdpClient>();
|
||||
|
||||
tcpServers = new HashMap<NetworkData, TcpServer>();
|
||||
tcpClients = new HashMap<NetworkData, TcpClient>();
|
||||
}
|
||||
|
||||
public UdpServer getUdpServer(String serverName, String pluginId) {
|
||||
NetworkData data = new NetworkData(
|
||||
UUID.randomUUID().toString(),
|
||||
serverName,
|
||||
connectionManager.getUserName(),
|
||||
LocalNetworkInfo.bestLocalIpv4(),
|
||||
pluginId,
|
||||
NetworkData.ConnectionProtocol.UDP,
|
||||
NetworkData.ConnectionType.SERVER
|
||||
);
|
||||
|
||||
UdpServer udpServer = new UdpServer(connectionManager, data);
|
||||
udpServers.put(data, udpServer);
|
||||
return udpServer;
|
||||
}
|
||||
|
||||
public UdpClient getUdpClient(
|
||||
String serverName,
|
||||
String pluginId,
|
||||
NetworkData serverData) {
|
||||
|
||||
NetworkData clientData = new NetworkData(
|
||||
UUID.randomUUID().toString(),
|
||||
serverName,
|
||||
connectionManager.getUserName(),
|
||||
LocalNetworkInfo.bestLocalIpv4(),
|
||||
pluginId,
|
||||
NetworkData.ConnectionProtocol.UDP,
|
||||
NetworkData.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(
|
||||
NetworkData clientData,
|
||||
NetworkData serverData
|
||||
) throws ServerNotFoundException {
|
||||
ConnectionRequestMessage request =
|
||||
ConnectionRequestMessage.pendingRequest(
|
||||
serverData.id(),
|
||||
serverData
|
||||
);
|
||||
|
||||
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());
|
||||
|
||||
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 (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;
|
||||
}
|
||||
|
||||
public void receiveConnection(TcpConnection tcpConnection) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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,51 @@
|
||||
package pitiupi.net.servers;
|
||||
|
||||
import pitiupi.net.tcp.TcpConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class TcpClient extends NetworkEndpoint {
|
||||
NetworkData serverData;
|
||||
|
||||
TcpConnection tcpConnection;
|
||||
|
||||
public TcpClient(NetworkData clientData, NetworkData serverData, TcpConnection tcpConnection) {
|
||||
super(clientData);
|
||||
|
||||
this.tcpConnection = tcpConnection;
|
||||
|
||||
this.serverData = serverData;
|
||||
}
|
||||
|
||||
public byte[] getBytes()
|
||||
throws IOException, ClosedConnectionException {
|
||||
|
||||
if (isOpen) return tcpConnection.receive();
|
||||
else throw new ClosedConnectionException("This connection is closed");
|
||||
}
|
||||
|
||||
public void send(byte[] bytes)
|
||||
throws IOException, ClosedConnectionException {
|
||||
|
||||
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||
|
||||
tcpConnection.send(bytes);
|
||||
}
|
||||
|
||||
public NetworkData getServerData() {
|
||||
return serverData;
|
||||
}
|
||||
|
||||
@Override
|
||||
void close() {
|
||||
try {
|
||||
super.close();
|
||||
tcpConnection.close();
|
||||
} catch (IOException e) {
|
||||
Logger.getLogger(TcpClient.class.getName()).log(Level.WARNING, null, e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package pitiupi.net.servers;
|
||||
|
||||
import pitiupi.net.tcp.TcpConnection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class TcpServer extends NetworkEndpoint{
|
||||
List<NetworkData> clientsData;
|
||||
Map<String, TcpClient> clientsNetwork;
|
||||
|
||||
private final BlockingQueue<TcpClient> buffer;
|
||||
|
||||
TcpServer(NetworkData serverData) {
|
||||
super(serverData);
|
||||
|
||||
clientsData = new ArrayList<>();
|
||||
clientsNetwork = new ConcurrentHashMap<>();
|
||||
|
||||
buffer = new LinkedBlockingQueue<>();
|
||||
}
|
||||
|
||||
void addClient(NetworkData clientData, TcpClient clientNetwork) throws ClosedConnectionException, InterruptedException {
|
||||
if (!isOpen) throw new ClosedConnectionException("This endpoint is closed");
|
||||
|
||||
clientsData.add(clientData);
|
||||
clientsNetwork.put(clientData.id(), clientNetwork);
|
||||
|
||||
buffer.put(clientNetwork);
|
||||
}
|
||||
|
||||
void removeClient(String clientId) throws ClosedConnectionException, InterruptedException {
|
||||
if (!isOpen) throw new ClosedConnectionException("This endpoint is closed");
|
||||
|
||||
for (NetworkData data: clientsData) {
|
||||
if (data.id().equals(clientId)) {
|
||||
clientsData.remove(data);
|
||||
TcpClient client = clientsNetwork.remove(data.id());
|
||||
|
||||
buffer.remove(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TcpClient getClient() throws InterruptedException {
|
||||
return buffer.take();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package pitiupi.net.servers;
|
||||
|
||||
import pitiupi.control.ConnectionManager;
|
||||
|
||||
public class UdpClient extends NetworkEndpoint {
|
||||
private final NetworkData serverData;
|
||||
|
||||
private final UdpNetworkIO udpNetworkIO;
|
||||
|
||||
public UdpClient(
|
||||
ConnectionManager connectionManager,
|
||||
NetworkData serverData,
|
||||
NetworkData clientData
|
||||
) {
|
||||
super(clientData);
|
||||
|
||||
this.udpNetworkIO = new UdpNetworkIO(connectionManager);
|
||||
|
||||
this.serverData = serverData;
|
||||
}
|
||||
|
||||
void receiveBytes(byte[] bytes)
|
||||
throws InterruptedException, ClosedConnectionException {
|
||||
|
||||
if (isOpen) udpNetworkIO.receiveBytes(bytes);
|
||||
else throw new ClosedConnectionException("This connection is closed");
|
||||
}
|
||||
|
||||
public byte[] getBytes()
|
||||
throws InterruptedException, ClosedConnectionException {
|
||||
|
||||
if (isOpen) return udpNetworkIO.getBytes();
|
||||
else throw new ClosedConnectionException("This connection is closed");
|
||||
}
|
||||
|
||||
public void send(byte[] bytes)
|
||||
throws ClosedConnectionException {
|
||||
|
||||
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||
|
||||
udpNetworkIO.send(bytes, networkData.id(), serverData.id(), serverData.ownerAdress());
|
||||
}
|
||||
|
||||
public NetworkData getServerData() {
|
||||
return serverData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
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.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class UdpNetworkIO {
|
||||
private final ConnectionManager connectionManager;
|
||||
|
||||
private final BlockingQueue<byte[]> buffer;
|
||||
private static final int MAX_BUFFER_CAPACITY = 1024;
|
||||
|
||||
public UdpNetworkIO(ConnectionManager connectionManager) {
|
||||
this.connectionManager = connectionManager;
|
||||
this.buffer = new LinkedBlockingQueue<>(MAX_BUFFER_CAPACITY);
|
||||
}
|
||||
|
||||
protected void receiveBytes(byte[] bytes) throws InterruptedException {
|
||||
buffer.put(bytes);
|
||||
}
|
||||
|
||||
public byte[] getBytes() throws InterruptedException {
|
||||
return buffer.take();
|
||||
}
|
||||
|
||||
public void send(byte[] bytes, String senderId, String receiverId, String receiverAdress) {
|
||||
UdpConnectionMessage message = new UdpConnectionMessage(
|
||||
senderId,
|
||||
receiverId,
|
||||
bytes
|
||||
);
|
||||
|
||||
try {
|
||||
InetAddress clientAdress = InetAddress.getByName(receiverAdress);
|
||||
connectionManager.sendUdpUnicast(message, clientAdress);
|
||||
}
|
||||
catch (UnknownHostException e) {
|
||||
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unknown host exception", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package pitiupi.net.servers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import pitiupi.control.ConnectionManager;
|
||||
|
||||
public class UdpServer extends NetworkEndpoint{
|
||||
private final List<NetworkData> clientConnections;
|
||||
|
||||
private final UdpNetworkIO udpNetworkIO;
|
||||
|
||||
public UdpServer(
|
||||
ConnectionManager connectionManager,
|
||||
NetworkData serverData
|
||||
) {
|
||||
super(serverData);
|
||||
|
||||
this.udpNetworkIO = new UdpNetworkIO(connectionManager);
|
||||
|
||||
this.clientConnections = new ArrayList<>();
|
||||
}
|
||||
|
||||
void addClient(NetworkData clientConnection) {
|
||||
clientConnections.add(clientConnection);
|
||||
}
|
||||
|
||||
void removeClient(NetworkData clientConnection) {
|
||||
clientConnections.remove(clientConnection);
|
||||
}
|
||||
|
||||
void receiveBytes(byte[] bytes)
|
||||
throws InterruptedException, ClosedConnectionException {
|
||||
|
||||
if (isOpen) udpNetworkIO.receiveBytes(bytes);
|
||||
else throw new ClosedConnectionException("This endpoint is closed");
|
||||
}
|
||||
|
||||
public byte[] getBytes()
|
||||
throws InterruptedException, ClosedConnectionException {
|
||||
|
||||
if (isOpen) return udpNetworkIO.getBytes();
|
||||
else throw new ClosedConnectionException("This connection is closed");
|
||||
}
|
||||
|
||||
public void sendUnicast(byte[] bytes, NetworkData clientConnection)
|
||||
throws ClosedConnectionException {
|
||||
|
||||
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||
|
||||
udpNetworkIO.send(bytes, networkData.id(), clientConnection.id(), clientConnection.ownerAdress());
|
||||
}
|
||||
|
||||
public void sendMulticast(byte[] bytes)
|
||||
throws ClosedConnectionException {
|
||||
|
||||
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||
|
||||
for (NetworkData clientConnection : clientConnections) {
|
||||
udpNetworkIO.send(bytes, networkData.id(), clientConnection.id(), clientConnection.ownerAdress());
|
||||
}
|
||||
}
|
||||
|
||||
public List<NetworkData> getClientConnections() {
|
||||
return Collections.unmodifiableList(clientConnections);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import pitiupi.control.plugins.PluginManager;
|
||||
import pitiupi.models.Messages.Message;
|
||||
import pitiupi.models.Messages.InfoMessage;
|
||||
import pitiupi.models.Messages.InfoType;
|
||||
import pitiupi.net.servers.ServerManager;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
@@ -286,6 +287,10 @@ public class SocketTCP extends Thread {
|
||||
return;
|
||||
}
|
||||
|
||||
if (connection.getReceiver().equals(ServerManager.SERVER_CONNECTION_ID)) {
|
||||
|
||||
}
|
||||
|
||||
if (pluginManager.handleConnection(connection, connection.getReceiver())) {
|
||||
privateConnectionReceiver(connection);
|
||||
}
|
||||
|
||||
@@ -71,14 +71,13 @@ public class TcpConnection {
|
||||
* @throws IOException se o tamanho da mensagem for inválido ou se a conexão
|
||||
* for encerrada antes que a mensagem seja completamente recebida
|
||||
*/
|
||||
byte[] receive() throws IOException {
|
||||
public byte[] receive() throws IOException {
|
||||
int length = input.readInt();
|
||||
|
||||
if (length < 0) throw new IOException("Invalid message length.");
|
||||
|
||||
byte[] message = input.readNBytes(length);
|
||||
|
||||
|
||||
if (message.length != length) {
|
||||
throw new IOException("Connection closed before receiving the complete message.");
|
||||
}
|
||||
@@ -103,7 +102,7 @@ public class TcpConnection {
|
||||
*
|
||||
* @throws IOException se ocorrer um erro ao fechar o socket
|
||||
*/
|
||||
void close() throws IOException {
|
||||
public void close() throws IOException {
|
||||
socket.close();
|
||||
}
|
||||
|
||||
@@ -129,7 +128,7 @@ public class TcpConnection {
|
||||
* @param timeout tempo limite, em milissegundos
|
||||
* @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);
|
||||
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