established connection between Client and Serveur, setting up basic interactions
This commit is contained in:
@@ -3,6 +3,7 @@ package server;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -10,23 +11,75 @@ import java.util.Map;
|
||||
import network.PacketHandler;
|
||||
import network.SocketReader;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.Packets;
|
||||
import network.protocol.packets.ChatMessagePacket;
|
||||
import network.protocol.packets.SendChatMessagePacket;
|
||||
import network.protocol.packets.ServerResponsePacket;
|
||||
|
||||
public class Server implements PacketHandler {
|
||||
|
||||
private final DatagramSocket serverSocket;
|
||||
private final Map<InetSocketAddress, ServerConnexion> connexions;
|
||||
private final SocketReader reader;
|
||||
private final ArrayList<String> roomNames;
|
||||
private final Map<String, ArrayList<ServerConnexion>> 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<>();
|
||||
this.roomNames = new HashMap<>();
|
||||
}
|
||||
|
||||
public ArrayList<String> getRoomNames() {
|
||||
return roomNames;
|
||||
return roomNames.keySet().stream().collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
||||
}
|
||||
|
||||
public String getRoomName(ServerConnexion connexion) {
|
||||
for (Map.Entry<String, ArrayList<ServerConnexion>> entry : roomNames.entrySet()) {
|
||||
if(entry.getValue().contains(connexion)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void createRoom(String roomName, ServerConnexion connexion) throws SocketException {
|
||||
if(roomNames.containsKey(roomName)) {
|
||||
throw new SocketException("Room already exists");
|
||||
}
|
||||
roomNames.put(roomName, new ArrayList<>());
|
||||
roomNames.get(roomName).add(connexion);
|
||||
}
|
||||
|
||||
public void leaveRoom(ServerConnexion connexion) throws SocketException {
|
||||
String roomName = getRoomName(connexion);
|
||||
if(roomName != null) {
|
||||
roomNames.get(roomName).remove(connexion);
|
||||
// Remove the room if it is empty
|
||||
if(roomNames.get(roomName).isEmpty()) {
|
||||
roomNames.remove(roomName);
|
||||
}
|
||||
return;
|
||||
}
|
||||
throw new SocketException("Room does not exist");
|
||||
}
|
||||
|
||||
public void joinRoom(String roomName, ServerConnexion connexion) throws SocketException {
|
||||
if(roomNames.containsKey(roomName)) {
|
||||
roomNames.get(roomName).add(connexion);
|
||||
return;
|
||||
}
|
||||
throw new SocketException("Room does not exist");
|
||||
}
|
||||
|
||||
public void sendToRoom(ServerConnexion connexion, SendChatMessagePacket packet) throws SocketException {
|
||||
String roomName = getRoomName(connexion);
|
||||
ChatMessagePacket chatPacket = new ChatMessagePacket(Instant.now(), connexion.getChatterName(), packet.getContent());
|
||||
if(roomName != null && roomNames.containsKey(roomName)) {
|
||||
roomNames.get(roomName).forEach(con -> con.sendPacket(chatPacket));
|
||||
return;
|
||||
}
|
||||
throw new SocketException("You are not in a room or the room does not exist");
|
||||
}
|
||||
|
||||
public void close() {
|
||||
@@ -34,11 +87,8 @@ public class Server implements PacketHandler {
|
||||
}
|
||||
|
||||
public boolean hasChatterName(String pseudo) {
|
||||
for (var entry : this.connexions.entrySet()) {
|
||||
if (pseudo.equals(entry.getValue().getChatterName()))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return this.connexions.values().stream()
|
||||
.anyMatch(connexion -> pseudo.equals(connexion.getChatterName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user