add disconnect
This commit is contained in:
@@ -5,6 +5,7 @@ import java.net.InetSocketAddress;
|
|||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
|
||||||
import network.protocol.packets.CreateRoomPacket;
|
import network.protocol.packets.CreateRoomPacket;
|
||||||
|
import network.protocol.packets.DisconnectPacket;
|
||||||
import network.protocol.packets.JoinRoomPacket;
|
import network.protocol.packets.JoinRoomPacket;
|
||||||
import network.protocol.packets.LeaveRoomPacket;
|
import network.protocol.packets.LeaveRoomPacket;
|
||||||
import network.protocol.packets.LoginPacket;
|
import network.protocol.packets.LoginPacket;
|
||||||
@@ -23,7 +24,7 @@ public class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
//TODO: send disconnect packet
|
this.connexion.sendPacket(new DisconnectPacket("Leaving"));
|
||||||
this.connexion.close();
|
this.connexion.close();
|
||||||
this.callback.handleDisconnect();
|
this.callback.handleDisconnect();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import network.protocol.Packet;
|
|||||||
import network.protocol.PacketVisitor;
|
import network.protocol.PacketVisitor;
|
||||||
import network.protocol.packets.ChatMessagePacket;
|
import network.protocol.packets.ChatMessagePacket;
|
||||||
import network.protocol.packets.CreateRoomPacket;
|
import network.protocol.packets.CreateRoomPacket;
|
||||||
|
import network.protocol.packets.DisconnectPacket;
|
||||||
import network.protocol.packets.JoinRoomPacket;
|
import network.protocol.packets.JoinRoomPacket;
|
||||||
import network.protocol.packets.LeaveRoomPacket;
|
import network.protocol.packets.LeaveRoomPacket;
|
||||||
import network.protocol.packets.LoginPacket;
|
import network.protocol.packets.LoginPacket;
|
||||||
@@ -68,6 +69,12 @@ public class ClientConnexion implements PacketVisitor, PacketHandler{
|
|||||||
this.callback.handleServerResponse(packet.getResponse());
|
this.callback.handleServerResponse(packet.getResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitPacket(DisconnectPacket packet) {
|
||||||
|
this.close();
|
||||||
|
this.callback.handleDisconnect();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(CreateRoomPacket packet) {
|
public void visitPacket(CreateRoomPacket packet) {
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package network.protocol;
|
|||||||
|
|
||||||
import network.protocol.packets.ChatMessagePacket;
|
import network.protocol.packets.ChatMessagePacket;
|
||||||
import network.protocol.packets.CreateRoomPacket;
|
import network.protocol.packets.CreateRoomPacket;
|
||||||
|
import network.protocol.packets.DisconnectPacket;
|
||||||
import network.protocol.packets.JoinRoomPacket;
|
import network.protocol.packets.JoinRoomPacket;
|
||||||
import network.protocol.packets.LeaveRoomPacket;
|
import network.protocol.packets.LeaveRoomPacket;
|
||||||
import network.protocol.packets.LoginPacket;
|
import network.protocol.packets.LoginPacket;
|
||||||
@@ -18,6 +19,7 @@ public interface PacketVisitor {
|
|||||||
|
|
||||||
void visitPacket(ChatMessagePacket packet);
|
void visitPacket(ChatMessagePacket packet);
|
||||||
void visitPacket(CreateRoomPacket packet);
|
void visitPacket(CreateRoomPacket packet);
|
||||||
|
void visitPacket(DisconnectPacket packet);
|
||||||
void visitPacket(JoinRoomPacket packet);
|
void visitPacket(JoinRoomPacket packet);
|
||||||
void visitPacket(LeaveRoomPacket packet);
|
void visitPacket(LeaveRoomPacket packet);
|
||||||
void visitPacket(LoginPacket 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 DatagramSocket serverSocket;
|
||||||
private final Map<InetSocketAddress, ServerConnexion> connexions;
|
private final Map<InetSocketAddress, ServerConnexion> connexions;
|
||||||
private final SocketReader reader;
|
private final SocketReader reader;
|
||||||
private final Map<String, ArrayList<ServerConnexion>> roomNames;
|
private final Map<String, ArrayList<ServerConnexion>> rooms;
|
||||||
|
|
||||||
public Server(int port) throws SocketException {
|
public Server(int port) throws SocketException {
|
||||||
this.serverSocket = new DatagramSocket(port);
|
this.serverSocket = new DatagramSocket(port);
|
||||||
this.connexions = new HashMap<>();
|
this.connexions = new HashMap<>();
|
||||||
this.reader = new SocketReader(serverSocket, this);
|
this.reader = new SocketReader(serverSocket, this);
|
||||||
this.roomNames = new HashMap<>();
|
this.rooms = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> getRoomNames() {
|
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) {
|
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)) {
|
if(entry.getValue().contains(connexion)) {
|
||||||
return entry.getKey();
|
return entry.getKey();
|
||||||
}
|
}
|
||||||
@@ -42,20 +42,20 @@ public class Server implements PacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void createRoom(String roomName, ServerConnexion connexion) throws SocketException {
|
public void createRoom(String roomName, ServerConnexion connexion) throws SocketException {
|
||||||
if(roomNames.containsKey(roomName)) {
|
if(rooms.containsKey(roomName)) {
|
||||||
throw new SocketException("Room already exists");
|
throw new SocketException("Room already exists");
|
||||||
}
|
}
|
||||||
roomNames.put(roomName, new ArrayList<>());
|
rooms.put(roomName, new ArrayList<>());
|
||||||
roomNames.get(roomName).add(connexion);
|
rooms.get(roomName).add(connexion);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void leaveRoom(ServerConnexion connexion) throws SocketException {
|
public void leaveRoom(ServerConnexion connexion) throws SocketException {
|
||||||
String roomName = getRoomName(connexion);
|
String roomName = getRoomName(connexion);
|
||||||
if(roomName != null) {
|
if(roomName != null) {
|
||||||
roomNames.get(roomName).remove(connexion);
|
rooms.get(roomName).remove(connexion);
|
||||||
// Remove the room if it is empty
|
// Remove the room if it is empty
|
||||||
if(roomNames.get(roomName).isEmpty()) {
|
if(rooms.get(roomName).isEmpty()) {
|
||||||
roomNames.remove(roomName);
|
rooms.remove(roomName);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -63,8 +63,8 @@ public class Server implements PacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void joinRoom(String roomName, ServerConnexion connexion) throws SocketException {
|
public void joinRoom(String roomName, ServerConnexion connexion) throws SocketException {
|
||||||
if(roomNames.containsKey(roomName) && !roomNames.get(roomName).contains(connexion)) {
|
if(rooms.containsKey(roomName) && !rooms.get(roomName).contains(connexion)) {
|
||||||
roomNames.get(roomName).add(connexion);
|
rooms.get(roomName).add(connexion);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new SocketException("Room does not exist");
|
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 {
|
public void sendToRoom(ServerConnexion connexion, SendChatMessagePacket packet) throws SocketException {
|
||||||
String roomName = getRoomName(connexion);
|
String roomName = getRoomName(connexion);
|
||||||
ChatMessagePacket chatPacket = new ChatMessagePacket(Instant.now(), connexion.getChatterName(), packet.getContent());
|
ChatMessagePacket chatPacket = new ChatMessagePacket(Instant.now(), connexion.getChatterName(), packet.getContent());
|
||||||
if(roomName != null && roomNames.containsKey(roomName)) {
|
if(roomName != null && rooms.containsKey(roomName)) {
|
||||||
roomNames.get(roomName).forEach(con -> con.sendPacket(chatPacket));
|
rooms.get(roomName).forEach(con -> con.sendPacket(chatPacket));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new SocketException("You are not in a room or the room does not exist");
|
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()));
|
.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
|
@Override
|
||||||
public void handlePacket(Packet packet, InetSocketAddress address) {
|
public void handlePacket(Packet packet, InetSocketAddress address) {
|
||||||
if (!connexions.containsKey(address)) {
|
if (!connexions.containsKey(address)) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import network.protocol.Packet;
|
|||||||
import network.protocol.PacketVisitor;
|
import network.protocol.PacketVisitor;
|
||||||
import network.protocol.packets.ChatMessagePacket;
|
import network.protocol.packets.ChatMessagePacket;
|
||||||
import network.protocol.packets.CreateRoomPacket;
|
import network.protocol.packets.CreateRoomPacket;
|
||||||
|
import network.protocol.packets.DisconnectPacket;
|
||||||
import network.protocol.packets.JoinRoomPacket;
|
import network.protocol.packets.JoinRoomPacket;
|
||||||
import network.protocol.packets.LeaveRoomPacket;
|
import network.protocol.packets.LeaveRoomPacket;
|
||||||
import network.protocol.packets.LoginPacket;
|
import network.protocol.packets.LoginPacket;
|
||||||
@@ -41,15 +42,10 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
this.writer.sendPacket(packet, clientAddress);
|
this.writer.sendPacket(packet, clientAddress);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
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
|
@Override
|
||||||
public void visitPacket(CreateRoomPacket packet) {
|
public void visitPacket(CreateRoomPacket packet) {
|
||||||
try {
|
try {
|
||||||
@@ -97,12 +93,6 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
sendPacket(new RoomListPacket(server.getRoomNames()));
|
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
|
@Override
|
||||||
public void visitPacket(SendChatMessagePacket packet) {
|
public void visitPacket(SendChatMessagePacket packet) {
|
||||||
try {
|
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
|
@Override
|
||||||
public void visitPacket(ServerResponsePacket packet) {
|
public void visitPacket(ServerResponsePacket packet) {
|
||||||
// I'm never supposed to receive this from the client
|
// I'm never supposed to receive this from the client
|
||||||
|
|||||||
Reference in New Issue
Block a user