ServerManager WIP
This commit is contained in:
1 parent
53d7436966
commit
49cf3760a0
12 files changed
+244
-161
No files matched your search
@@ -7,6 +7,7 @@ import pitiupi.application.peeractions.PeerActionBroker;
|
|||||||
import pitiupi.application.presence.PresenceService;
|
import pitiupi.application.presence.PresenceService;
|
||||||
import pitiupi.models.Messages.*;
|
import pitiupi.models.Messages.*;
|
||||||
import pitiupi.control.plugins.PluginManager;
|
import pitiupi.control.plugins.PluginManager;
|
||||||
|
import pitiupi.net.servers.ServerManager;
|
||||||
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;
|
||||||
@@ -29,6 +30,7 @@ public class ConnectionManager {
|
|||||||
private final PluginManager pluginManager;
|
private final PluginManager pluginManager;
|
||||||
|
|
||||||
private final HeartbeatManager heartbeatManager;
|
private final HeartbeatManager heartbeatManager;
|
||||||
|
private final ServerManager serverManager;
|
||||||
private final PresenceService presenceService;
|
private final PresenceService presenceService;
|
||||||
private final PeerActionBroker peerActionBroker;
|
private final PeerActionBroker peerActionBroker;
|
||||||
private final InviteInboxService inviteInboxService;
|
private final InviteInboxService inviteInboxService;
|
||||||
@@ -67,6 +69,7 @@ public class ConnectionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
heartbeatManager = new HeartbeatManager(this);
|
heartbeatManager = new HeartbeatManager(this);
|
||||||
|
serverManager = new ServerManager(this);
|
||||||
peerActionBroker = new PeerActionBroker();
|
peerActionBroker = new PeerActionBroker();
|
||||||
inviteInboxService = new InviteInboxService(heartbeatManager);
|
inviteInboxService = new InviteInboxService(heartbeatManager);
|
||||||
peerActivityInbox = new PeerActivityInbox();
|
peerActivityInbox = new PeerActivityInbox();
|
||||||
@@ -399,9 +402,7 @@ public class ConnectionManager {
|
|||||||
this.username = userName;
|
this.username = userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveConnectionRequest(ConnectionRequestMessage message) {
|
public void receiveServerConnection(TcpConnection tcpConnection) {
|
||||||
if (message == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package pitiupi.models.Messages;
|
package pitiupi.models.Messages;
|
||||||
|
|
||||||
import pitiupi.net.servers.ConnectionData;
|
import pitiupi.net.servers.NetworkData;
|
||||||
|
|
||||||
public class ConnectionRequestMessage extends Message {
|
public class ConnectionRequestMessage extends Message {
|
||||||
public enum RequestStatus {
|
public enum RequestStatus {
|
||||||
@@ -10,12 +10,12 @@ public class ConnectionRequestMessage extends Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final private String receiverId;
|
final private String receiverId;
|
||||||
final private ConnectionData senderData;
|
final private NetworkData senderData;
|
||||||
final private RequestStatus requestStatus;
|
final private RequestStatus requestStatus;
|
||||||
|
|
||||||
private ConnectionRequestMessage(
|
private ConnectionRequestMessage(
|
||||||
String receiverId,
|
String receiverId,
|
||||||
ConnectionData senderData,
|
NetworkData senderData,
|
||||||
RequestStatus requestStatus
|
RequestStatus requestStatus
|
||||||
) {
|
) {
|
||||||
this.receiverId = receiverId;
|
this.receiverId = receiverId;
|
||||||
@@ -25,7 +25,7 @@ public class ConnectionRequestMessage extends Message {
|
|||||||
|
|
||||||
public static ConnectionRequestMessage pendingRequest(
|
public static ConnectionRequestMessage pendingRequest(
|
||||||
String receiverId,
|
String receiverId,
|
||||||
ConnectionData senderData
|
NetworkData senderData
|
||||||
) {
|
) {
|
||||||
return new ConnectionRequestMessage(receiverId, senderData, RequestStatus.PENDING);
|
return new ConnectionRequestMessage(receiverId, senderData, RequestStatus.PENDING);
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@ public class ConnectionRequestMessage extends Message {
|
|||||||
return receiverId;
|
return receiverId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectionData getSenderData() {
|
public NetworkData getSenderData() {
|
||||||
return senderData;
|
return senderData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ package pitiupi.net.servers;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
public record ConnectionData(
|
public record NetworkData(
|
||||||
String id,
|
String id,
|
||||||
String connectionName,
|
String connectionName,
|
||||||
String ownerName,
|
String ownerName,
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
-26
@@ -1,7 +1,7 @@
|
|||||||
package pitiupi.control;
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.models.Messages.ConnectionRequestMessage;
|
import pitiupi.models.Messages.ConnectionRequestMessage;
|
||||||
import pitiupi.net.servers.*;
|
|
||||||
import pitiupi.net.tcp.ConnectionFactory;
|
import pitiupi.net.tcp.ConnectionFactory;
|
||||||
import pitiupi.net.tcp.TcpConnection;
|
import pitiupi.net.tcp.TcpConnection;
|
||||||
import pitiupi.presentation.shell.LocalNetworkInfo;
|
import pitiupi.presentation.shell.LocalNetworkInfo;
|
||||||
@@ -21,33 +21,35 @@ import java.util.logging.Logger;
|
|||||||
public class ServerManager {
|
public class ServerManager {
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
|
|
||||||
private final Map<ConnectionData, UdpServer> udpServers;
|
private final Map<NetworkData, UdpServer> udpServers;
|
||||||
private final Map<ConnectionData, UdpClient> udpClients;
|
private final Map<NetworkData, UdpClient> udpClients;
|
||||||
|
|
||||||
private final Map<ConnectionData, TcpServer> tcpServers;
|
private final Map<NetworkData, TcpServer> tcpServers;
|
||||||
private final Map<ConnectionData, TcpClient> tcpClients;
|
private final Map<NetworkData, TcpClient> tcpClients;
|
||||||
|
|
||||||
private static final int HANDSHAKE_TIMEOUT_MS = 1000;
|
private static final int HANDSHAKE_TIMEOUT_MS = 1000;
|
||||||
|
|
||||||
|
public static final String SERVER_CONNECTION_ID = "PITIUPI.SERVER.CONNECTION.REQUEST";
|
||||||
|
|
||||||
public ServerManager(ConnectionManager connectionManager) {
|
public ServerManager(ConnectionManager connectionManager) {
|
||||||
this.connectionManager = connectionManager;
|
this.connectionManager = connectionManager;
|
||||||
|
|
||||||
udpServers = new HashMap<ConnectionData, UdpServer>();
|
udpServers = new HashMap<NetworkData, UdpServer>();
|
||||||
udpClients = new HashMap<ConnectionData, UdpClient>();
|
udpClients = new HashMap<NetworkData, UdpClient>();
|
||||||
|
|
||||||
tcpServers = new HashMap<ConnectionData, TcpServer>();
|
tcpServers = new HashMap<NetworkData, TcpServer>();
|
||||||
tcpClients = new HashMap<ConnectionData, TcpClient>();
|
tcpClients = new HashMap<NetworkData, TcpClient>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UdpServer getUdpServer(String serverName, String pluginId) {
|
public UdpServer getUdpServer(String serverName, String pluginId) {
|
||||||
ConnectionData data = new ConnectionData(
|
NetworkData data = new NetworkData(
|
||||||
UUID.randomUUID().toString(),
|
UUID.randomUUID().toString(),
|
||||||
serverName,
|
serverName,
|
||||||
connectionManager.getUserName(),
|
connectionManager.getUserName(),
|
||||||
LocalNetworkInfo.bestLocalIpv4(),
|
LocalNetworkInfo.bestLocalIpv4(),
|
||||||
pluginId,
|
pluginId,
|
||||||
ConnectionData.ConnectionProtocol.UDP,
|
NetworkData.ConnectionProtocol.UDP,
|
||||||
ConnectionData.ConnectionType.SERVER
|
NetworkData.ConnectionType.SERVER
|
||||||
);
|
);
|
||||||
|
|
||||||
UdpServer udpServer = new UdpServer(connectionManager, data);
|
UdpServer udpServer = new UdpServer(connectionManager, data);
|
||||||
@@ -58,16 +60,16 @@ public class ServerManager {
|
|||||||
public UdpClient getUdpClient(
|
public UdpClient getUdpClient(
|
||||||
String serverName,
|
String serverName,
|
||||||
String pluginId,
|
String pluginId,
|
||||||
ConnectionData serverData) {
|
NetworkData serverData) {
|
||||||
|
|
||||||
ConnectionData clientData = new ConnectionData(
|
NetworkData clientData = new NetworkData(
|
||||||
UUID.randomUUID().toString(),
|
UUID.randomUUID().toString(),
|
||||||
serverName,
|
serverName,
|
||||||
connectionManager.getUserName(),
|
connectionManager.getUserName(),
|
||||||
LocalNetworkInfo.bestLocalIpv4(),
|
LocalNetworkInfo.bestLocalIpv4(),
|
||||||
pluginId,
|
pluginId,
|
||||||
ConnectionData.ConnectionProtocol.UDP,
|
NetworkData.ConnectionProtocol.UDP,
|
||||||
ConnectionData.ConnectionType.CLIENT
|
NetworkData.ConnectionType.CLIENT
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!doConnectionHandshake(serverData, clientData))
|
if (!doConnectionHandshake(serverData, clientData))
|
||||||
@@ -85,8 +87,8 @@ public class ServerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean doConnectionHandshake(
|
private boolean doConnectionHandshake(
|
||||||
ConnectionData clientData,
|
NetworkData clientData,
|
||||||
ConnectionData serverData
|
NetworkData serverData
|
||||||
) throws ServerNotFoundException {
|
) throws ServerNotFoundException {
|
||||||
ConnectionRequestMessage request =
|
ConnectionRequestMessage request =
|
||||||
ConnectionRequestMessage.pendingRequest(
|
ConnectionRequestMessage.pendingRequest(
|
||||||
@@ -98,8 +100,8 @@ public class ServerManager {
|
|||||||
TcpConnection connection = ConnectionFactory.createConnection(
|
TcpConnection connection = ConnectionFactory.createConnection(
|
||||||
InetAddress.getByName(serverData.ownerAdress()),
|
InetAddress.getByName(serverData.ownerAdress()),
|
||||||
connectionManager.getPort(),
|
connectionManager.getPort(),
|
||||||
clientData.pluginId(),
|
SERVER_CONNECTION_ID,
|
||||||
serverData.pluginId(),
|
SERVER_CONNECTION_ID,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -123,11 +125,6 @@ public class ServerManager {
|
|||||||
|
|
||||||
return (response.getRequestStatus() == ConnectionRequestMessage.RequestStatus.ACCEPTED);
|
return (response.getRequestStatus() == ConnectionRequestMessage.RequestStatus.ACCEPTED);
|
||||||
}
|
}
|
||||||
catch (TimeoutException e) {
|
|
||||||
throw new ServerNotFoundException(
|
|
||||||
serverData.ownerAdress() +
|
|
||||||
" has timed out");
|
|
||||||
}
|
|
||||||
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(
|
||||||
@@ -139,4 +136,8 @@ public class ServerManager {
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void receiveConnection(TcpConnection tcpConnection) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,51 @@
|
|||||||
package pitiupi.net.servers;
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
public class TcpClient {
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,53 @@
|
|||||||
package pitiupi.net.servers;
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
public class TcpServer {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,51 +1,35 @@
|
|||||||
package pitiupi.net.servers;
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
import pitiupi.control.ConnectionManager;
|
import pitiupi.control.ConnectionManager;
|
||||||
import pitiupi.models.Messages.UdpConnectionMessage;
|
|
||||||
|
|
||||||
import java.net.InetAddress;
|
public class UdpClient extends NetworkEndpoint {
|
||||||
import java.net.UnknownHostException;
|
private final NetworkData serverData;
|
||||||
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 UdpNetworkIO udpNetworkIO;
|
||||||
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(
|
public UdpClient(
|
||||||
ConnectionManager connectionManager,
|
ConnectionManager connectionManager,
|
||||||
ConnectionData serverData,
|
NetworkData serverData,
|
||||||
ConnectionData clientData
|
NetworkData clientData
|
||||||
) {
|
) {
|
||||||
this.connectionManager = connectionManager;
|
super(clientData);
|
||||||
|
|
||||||
|
this.udpNetworkIO = new UdpNetworkIO(connectionManager);
|
||||||
|
|
||||||
this.serverData = serverData;
|
this.serverData = serverData;
|
||||||
this.clientData = clientData;
|
|
||||||
|
|
||||||
buffer = new ArrayBlockingQueue<>(MAX_BUFFER_CAPACITY);
|
|
||||||
|
|
||||||
isOpen = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void receiveBytes(byte[] bytes)
|
void receiveBytes(byte[] bytes)
|
||||||
throws InterruptedException, ClosedConnectionException {
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
if (isOpen) buffer.put(bytes);
|
if (isOpen) udpNetworkIO.receiveBytes(bytes);
|
||||||
else throw new ClosedConnectionException("This connection is closed");
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getBytes()
|
public byte[] getBytes()
|
||||||
throws InterruptedException, ClosedConnectionException {
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
if (isOpen) return buffer.take();
|
if (isOpen) return udpNetworkIO.getBytes();
|
||||||
else throw new ClosedConnectionException("This connection is closed");
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,34 +38,10 @@ public class UdpClient {
|
|||||||
|
|
||||||
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
UdpConnectionMessage message = new UdpConnectionMessage(
|
udpNetworkIO.send(bytes, networkData.id(), serverData.id(), serverData.ownerAdress());
|
||||||
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() {
|
public NetworkData getServerData() {
|
||||||
return serverData;
|
return serverData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectionData getClientData() {
|
|
||||||
return clientData;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isOpen() {
|
|
||||||
return isOpen;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void close() {
|
|
||||||
isOpen = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,83 +1,55 @@
|
|||||||
package pitiupi.net.servers;
|
package pitiupi.net.servers;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
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.control.ConnectionManager;
|
||||||
import pitiupi.models.Messages.UdpConnectionMessage;
|
|
||||||
|
|
||||||
public class UdpServer {
|
public class UdpServer extends NetworkEndpoint{
|
||||||
private final ConnectionManager connectionManager;
|
private final List<NetworkData> clientConnections;
|
||||||
|
|
||||||
private final ConnectionData serverData;
|
private final UdpNetworkIO udpNetworkIO;
|
||||||
private final List<ConnectionData> clientConnections;
|
|
||||||
|
|
||||||
private final BlockingQueue<byte[]> buffer;
|
|
||||||
private boolean isOpen;
|
|
||||||
|
|
||||||
private static final int MAX_BUFFER_CAPACITY = 1024;
|
|
||||||
|
|
||||||
public UdpServer(
|
public UdpServer(
|
||||||
ConnectionManager connectionManager,
|
ConnectionManager connectionManager,
|
||||||
ConnectionData serverData
|
NetworkData serverData
|
||||||
) {
|
) {
|
||||||
this.connectionManager = connectionManager;
|
super(serverData);
|
||||||
|
|
||||||
|
this.udpNetworkIO = new UdpNetworkIO(connectionManager);
|
||||||
|
|
||||||
this.serverData = serverData;
|
|
||||||
this.clientConnections = new ArrayList<>();
|
this.clientConnections = new ArrayList<>();
|
||||||
|
|
||||||
buffer = new ArrayBlockingQueue<>(MAX_BUFFER_CAPACITY);
|
|
||||||
|
|
||||||
isOpen = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addClient(ConnectionData clientConnection) {
|
void addClient(NetworkData clientConnection) {
|
||||||
clientConnections.add(clientConnection);
|
clientConnections.add(clientConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void removeClient(ConnectionData clientConnection) {
|
void removeClient(NetworkData clientConnection) {
|
||||||
clientConnections.remove(clientConnection);
|
clientConnections.remove(clientConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void receiveBytes(byte[] bytes)
|
void receiveBytes(byte[] bytes)
|
||||||
throws InterruptedException, ClosedConnectionException {
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
if (isOpen) buffer.put(bytes);
|
if (isOpen) udpNetworkIO.receiveBytes(bytes);
|
||||||
else throw new ClosedConnectionException("This connection is closed");
|
else throw new ClosedConnectionException("This endpoint is closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getBytes()
|
public byte[] getBytes()
|
||||||
throws InterruptedException, ClosedConnectionException {
|
throws InterruptedException, ClosedConnectionException {
|
||||||
|
|
||||||
if (isOpen) return buffer.take();
|
if (isOpen) return udpNetworkIO.getBytes();
|
||||||
else throw new ClosedConnectionException("This connection is closed");
|
else throw new ClosedConnectionException("This connection is closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendUnicast(byte[] bytes, ConnectionData clientConnection)
|
public void sendUnicast(byte[] bytes, NetworkData clientConnection)
|
||||||
throws ClosedConnectionException {
|
throws ClosedConnectionException {
|
||||||
|
|
||||||
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
UdpConnectionMessage message = new UdpConnectionMessage(
|
udpNetworkIO.send(bytes, networkData.id(), clientConnection.id(), clientConnection.ownerAdress());
|
||||||
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)
|
public void sendMulticast(byte[] bytes)
|
||||||
@@ -85,35 +57,12 @@ public class UdpServer {
|
|||||||
|
|
||||||
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
if (!isOpen) throw new ClosedConnectionException("This connection is closed");
|
||||||
|
|
||||||
for (ConnectionData clientConnection : clientConnections) {
|
for (NetworkData clientConnection : clientConnections) {
|
||||||
UdpConnectionMessage message = new UdpConnectionMessage(
|
udpNetworkIO.send(bytes, networkData.id(), clientConnection.id(), clientConnection.ownerAdress());
|
||||||
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() {
|
public List<NetworkData> getClientConnections() {
|
||||||
return serverData;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ConnectionData> getClientConnections() {
|
|
||||||
return Collections.unmodifiableList(clientConnections);
|
return Collections.unmodifiableList(clientConnections);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOpen() {
|
|
||||||
return isOpen;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void close() {
|
|
||||||
isOpen = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import pitiupi.control.plugins.PluginManager;
|
|||||||
import pitiupi.models.Messages.Message;
|
import pitiupi.models.Messages.Message;
|
||||||
import pitiupi.models.Messages.InfoMessage;
|
import pitiupi.models.Messages.InfoMessage;
|
||||||
import pitiupi.models.Messages.InfoType;
|
import pitiupi.models.Messages.InfoType;
|
||||||
|
import pitiupi.net.servers.ServerManager;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -286,6 +287,10 @@ public class SocketTCP extends Thread {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (connection.getReceiver().equals(ServerManager.SERVER_CONNECTION_ID)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (pluginManager.handleConnection(connection, connection.getReceiver())) {
|
if (pluginManager.handleConnection(connection, connection.getReceiver())) {
|
||||||
privateConnectionReceiver(connection);
|
privateConnectionReceiver(connection);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ public class TcpConnection {
|
|||||||
*
|
*
|
||||||
* @throws IOException se ocorrer um erro ao fechar o socket
|
* @throws IOException se ocorrer um erro ao fechar o socket
|
||||||
*/
|
*/
|
||||||
void close() throws IOException {
|
public void close() throws IOException {
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user