basic client/server structure

This commit is contained in:
2025-02-27 12:52:12 +01:00
parent 2917535e05
commit fed666200c
9 changed files with 381 additions and 5 deletions

View File

@@ -0,0 +1,52 @@
package server;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import network.PacketHandler;
import network.SocketReader;
import network.protocol.Packet;
public class Server implements PacketHandler {
private final DatagramSocket serverSocket;
private final Map<InetSocketAddress, ServerConnexion> connexions;
private final SocketReader reader;
private final ArrayList<String> roomNames;
public Server(int port) throws SocketException {
this.serverSocket = new DatagramSocket(port);
this.connexions = new HashMap<>();
this.reader = new SocketReader(serverSocket, this);
this.roomNames = new ArrayList<>();
}
public ArrayList<String> getRoomNames() {
return roomNames;
}
public void close() {
this.reader.stop();
}
public boolean hasChatterName(String pseudo) {
for (var entry : this.connexions.entrySet()) {
if (pseudo.equals(entry.getValue().getChatterName()))
return true;
}
return false;
}
@Override
public void handlePacket(Packet packet, InetSocketAddress address) {
if (!connexions.containsKey(address)) {
this.connexions.put(address, new ServerConnexion(this, serverSocket, address));
}
this.connexions.get(address).visit(packet);
}
}