basic client/server structure
This commit is contained in:
52
ChatApp/src/server/Server.java
Normal file
52
ChatApp/src/server/Server.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user