add disconnect
This commit is contained in:
@@ -5,6 +5,7 @@ import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
|
||||
import network.protocol.packets.CreateRoomPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.JoinRoomPacket;
|
||||
import network.protocol.packets.LeaveRoomPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
@@ -23,7 +24,7 @@ public class Client {
|
||||
}
|
||||
|
||||
public void close() {
|
||||
//TODO: send disconnect packet
|
||||
this.connexion.sendPacket(new DisconnectPacket("Leaving"));
|
||||
this.connexion.close();
|
||||
this.callback.handleDisconnect();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.packets.ChatMessagePacket;
|
||||
import network.protocol.packets.CreateRoomPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.JoinRoomPacket;
|
||||
import network.protocol.packets.LeaveRoomPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
@@ -68,6 +69,12 @@ public class ClientConnexion implements PacketVisitor, PacketHandler{
|
||||
this.callback.handleServerResponse(packet.getResponse());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(DisconnectPacket packet) {
|
||||
this.close();
|
||||
this.callback.handleDisconnect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(CreateRoomPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
|
||||
@@ -2,6 +2,7 @@ package network.protocol;
|
||||
|
||||
import network.protocol.packets.ChatMessagePacket;
|
||||
import network.protocol.packets.CreateRoomPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.JoinRoomPacket;
|
||||
import network.protocol.packets.LeaveRoomPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
@@ -18,6 +19,7 @@ public interface PacketVisitor {
|
||||
|
||||
void visitPacket(ChatMessagePacket packet);
|
||||
void visitPacket(CreateRoomPacket packet);
|
||||
void visitPacket(DisconnectPacket packet);
|
||||
void visitPacket(JoinRoomPacket packet);
|
||||
void visitPacket(LeaveRoomPacket packet);
|
||||
void visitPacket(LoginPacket packet);
|
||||
|
||||
23
ChatApp/src/network/protocol/packets/DisconnectPacket.java
Normal file
23
ChatApp/src/network/protocol/packets/DisconnectPacket.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
|
||||
public class DisconnectPacket extends Packet {
|
||||
|
||||
private final String reason;
|
||||
|
||||
public DisconnectPacket(String reason) {
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
public String getReason() {
|
||||
return reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,21 +19,21 @@ public class Server implements PacketHandler {
|
||||
private final DatagramSocket serverSocket;
|
||||
private final Map<InetSocketAddress, ServerConnexion> connexions;
|
||||
private final SocketReader reader;
|
||||
private final Map<String, ArrayList<ServerConnexion>> roomNames;
|
||||
private final Map<String, ArrayList<ServerConnexion>> rooms;
|
||||
|
||||
public Server(int port) throws SocketException {
|
||||
this.serverSocket = new DatagramSocket(port);
|
||||
this.connexions = new HashMap<>();
|
||||
this.reader = new SocketReader(serverSocket, this);
|
||||
this.roomNames = new HashMap<>();
|
||||
this.rooms = new HashMap<>();
|
||||
}
|
||||
|
||||
public ArrayList<String> getRoomNames() {
|
||||
return roomNames.keySet().stream().collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
||||
return rooms.keySet().stream().collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
|
||||
}
|
||||
|
||||
public String getRoomName(ServerConnexion connexion) {
|
||||
for (Map.Entry<String, ArrayList<ServerConnexion>> entry : roomNames.entrySet()) {
|
||||
for (Map.Entry<String, ArrayList<ServerConnexion>> entry : rooms.entrySet()) {
|
||||
if(entry.getValue().contains(connexion)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
@@ -42,20 +42,20 @@ public class Server implements PacketHandler {
|
||||
}
|
||||
|
||||
public void createRoom(String roomName, ServerConnexion connexion) throws SocketException {
|
||||
if(roomNames.containsKey(roomName)) {
|
||||
if(rooms.containsKey(roomName)) {
|
||||
throw new SocketException("Room already exists");
|
||||
}
|
||||
roomNames.put(roomName, new ArrayList<>());
|
||||
roomNames.get(roomName).add(connexion);
|
||||
rooms.put(roomName, new ArrayList<>());
|
||||
rooms.get(roomName).add(connexion);
|
||||
}
|
||||
|
||||
public void leaveRoom(ServerConnexion connexion) throws SocketException {
|
||||
String roomName = getRoomName(connexion);
|
||||
if(roomName != null) {
|
||||
roomNames.get(roomName).remove(connexion);
|
||||
rooms.get(roomName).remove(connexion);
|
||||
// Remove the room if it is empty
|
||||
if(roomNames.get(roomName).isEmpty()) {
|
||||
roomNames.remove(roomName);
|
||||
if(rooms.get(roomName).isEmpty()) {
|
||||
rooms.remove(roomName);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -63,8 +63,8 @@ public class Server implements PacketHandler {
|
||||
}
|
||||
|
||||
public void joinRoom(String roomName, ServerConnexion connexion) throws SocketException {
|
||||
if(roomNames.containsKey(roomName) && !roomNames.get(roomName).contains(connexion)) {
|
||||
roomNames.get(roomName).add(connexion);
|
||||
if(rooms.containsKey(roomName) && !rooms.get(roomName).contains(connexion)) {
|
||||
rooms.get(roomName).add(connexion);
|
||||
return;
|
||||
}
|
||||
throw new SocketException("Room does not exist");
|
||||
@@ -73,8 +73,8 @@ public class Server implements PacketHandler {
|
||||
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));
|
||||
if(roomName != null && rooms.containsKey(roomName)) {
|
||||
rooms.get(roomName).forEach(con -> con.sendPacket(chatPacket));
|
||||
return;
|
||||
}
|
||||
throw new SocketException("You are not in a room or the room does not exist");
|
||||
@@ -89,6 +89,20 @@ public class Server implements PacketHandler {
|
||||
.anyMatch(connexion -> pseudo.equals(connexion.getChatterName()));
|
||||
}
|
||||
|
||||
void removeConnexion(ServerConnexion connexion) {
|
||||
for (var it = this.connexions.entrySet().iterator(); it.hasNext();) {
|
||||
var entry = it.next();
|
||||
if (entry.getValue() == connexion) {
|
||||
it.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (var entry : this.rooms.entrySet()) {
|
||||
if (entry.getValue().remove(connexion))
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePacket(Packet packet, InetSocketAddress address) {
|
||||
if (!connexions.containsKey(address)) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.packets.ChatMessagePacket;
|
||||
import network.protocol.packets.CreateRoomPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.JoinRoomPacket;
|
||||
import network.protocol.packets.LeaveRoomPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
@@ -41,15 +42,10 @@ public class ServerConnexion implements PacketVisitor {
|
||||
this.writer.sendPacket(packet, clientAddress);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
this.server.removeConnexion(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ChatMessagePacket packet) {
|
||||
// I'm never supposed to receive this from the client
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(CreateRoomPacket packet) {
|
||||
try {
|
||||
@@ -97,12 +93,6 @@ public class ServerConnexion implements PacketVisitor {
|
||||
sendPacket(new RoomListPacket(server.getRoomNames()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(RoomListPacket packet) {
|
||||
// I'm never supposed to receive this from the client
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(SendChatMessagePacket packet) {
|
||||
try {
|
||||
@@ -112,6 +102,28 @@ public class ServerConnexion implements PacketVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(DisconnectPacket packet) {
|
||||
this.onDisconnect();
|
||||
}
|
||||
|
||||
private void onDisconnect() {
|
||||
this.server.removeConnexion(this);
|
||||
System.out.println("[Server] Chatter " + chatterName + " disconnected !");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(RoomListPacket packet) {
|
||||
// I'm never supposed to receive this from the client
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ChatMessagePacket packet) {
|
||||
// I'm never supposed to receive this from the client
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ServerResponsePacket packet) {
|
||||
// I'm never supposed to receive this from the client
|
||||
|
||||
Reference in New Issue
Block a user