established connection between Client and Serveur, setting up basic interactions
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
import client.Client;
|
import client.Client;
|
||||||
import server.Server;
|
import server.Server;
|
||||||
@@ -8,8 +9,13 @@ public class ChatApp {
|
|||||||
Server server = new Server(6665);
|
Server server = new Server(6665);
|
||||||
Client client = new Client(new InetSocketAddress("localhost", 6665));
|
Client client = new Client(new InetSocketAddress("localhost", 6665));
|
||||||
|
|
||||||
while (true) {
|
client.SendCreateRoom("Room1");
|
||||||
|
client.RequestRoomList();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
String message = scan.nextLine();
|
||||||
|
client.SendChatMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,21 @@ import java.net.DatagramSocket;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
|
||||||
import network.protocol.packets.LoginPacket;
|
import network.protocol.packets.*;
|
||||||
import network.protocol.packets.SendChatMessagePacket;
|
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
private final ClientConnexion connexion;
|
private final ClientConnexion connexion;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
Client client = new Client(new InetSocketAddress("localhost", 6665));
|
||||||
|
client.SendChatMessage("Hello");
|
||||||
|
} catch (SocketException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Client(InetSocketAddress serverAddress) throws SocketException {
|
public Client(InetSocketAddress serverAddress) throws SocketException {
|
||||||
this.connexion = new ClientConnexion(new DatagramSocket(), serverAddress);
|
this.connexion = new ClientConnexion(new DatagramSocket(), serverAddress);
|
||||||
login("Moi");
|
login("Moi");
|
||||||
@@ -32,4 +40,20 @@ public class Client {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SendCreateRoom(String roomName) {
|
||||||
|
try {
|
||||||
|
this.connexion.sendPacket(new CreateRoomPacket(roomName));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RequestRoomList() {
|
||||||
|
try {
|
||||||
|
this.connexion.sendPacket(new RequestRoomListPacket());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package client;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
import network.PacketHandler;
|
import network.PacketHandler;
|
||||||
import network.SocketReader;
|
import network.SocketReader;
|
||||||
@@ -47,7 +48,14 @@ public class ClientConnexion implements PacketVisitor, PacketHandler{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(ChatMessagePacket packet) {
|
public void visitPacket(ChatMessagePacket packet) {
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String time = packet.getTime().toString();
|
||||||
|
sb.append(time, 11, 19); // We only take the HH:MM:SS part
|
||||||
|
sb.append(" ");
|
||||||
|
sb.append(packet.getChatter());
|
||||||
|
sb.append(" : ");
|
||||||
|
sb.append(packet.getContent());
|
||||||
|
System.out.println(sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -77,8 +85,12 @@ public class ClientConnexion implements PacketVisitor, PacketHandler{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(RoomListPacket packet) {
|
public void visitPacket(RoomListPacket packet) {
|
||||||
System.out.println("Handled room list !");
|
// System.out.println("Handled room list !");
|
||||||
// throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
// throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||||
|
System.out.println("Rooms :");
|
||||||
|
for (String room : packet.getRoomNames()) {
|
||||||
|
System.out.println("\t" + room);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ public class ServerResponsePacket extends Packet {
|
|||||||
static private final long serialVersionUID = Packets.Login.ordinal();
|
static private final long serialVersionUID = Packets.Login.ordinal();
|
||||||
|
|
||||||
public static enum Response {
|
public static enum Response {
|
||||||
AuthSuccess, AuthError, RoomCreated, RoomNotCreated, RoomJoined, RoomNotJoined, NotInRoom;
|
AuthSuccess, AuthError, RoomCreated, RoomNotCreated, RoomJoined, RoomNotJoined, NotInRoom, RoomLeft, RoomNotLeft, messageSent, messageNotSent;
|
||||||
};
|
};
|
||||||
|
|
||||||
private final Response response;
|
private final Response response;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package server;
|
|||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -10,23 +11,75 @@ import java.util.Map;
|
|||||||
import network.PacketHandler;
|
import network.PacketHandler;
|
||||||
import network.SocketReader;
|
import network.SocketReader;
|
||||||
import network.protocol.Packet;
|
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 {
|
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 ArrayList<String> roomNames;
|
private final Map<String, ArrayList<ServerConnexion>> roomNames;
|
||||||
|
|
||||||
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 ArrayList<>();
|
this.roomNames = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> getRoomNames() {
|
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() {
|
public void close() {
|
||||||
@@ -34,11 +87,8 @@ public class Server implements PacketHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasChatterName(String pseudo) {
|
public boolean hasChatterName(String pseudo) {
|
||||||
for (var entry : this.connexions.entrySet()) {
|
return this.connexions.values().stream()
|
||||||
if (pseudo.equals(entry.getValue().getChatterName()))
|
.anyMatch(connexion -> pseudo.equals(connexion.getChatterName()));
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package server;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.SocketException;
|
||||||
|
|
||||||
import network.SocketWriter;
|
import network.SocketWriter;
|
||||||
import network.protocol.Packet;
|
import network.protocol.Packet;
|
||||||
@@ -45,22 +46,38 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(ChatMessagePacket packet) {
|
public void visitPacket(ChatMessagePacket packet) {
|
||||||
|
// I'm never supposed to receive this from the client
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(CreateRoomPacket packet) {
|
public void visitPacket(CreateRoomPacket packet) {
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
try {
|
||||||
|
server.createRoom(packet.getRoomName(), this);
|
||||||
|
sendPacket(new ServerResponsePacket(Response.RoomCreated));
|
||||||
|
} catch (SocketException e) {
|
||||||
|
sendPacket(new ServerResponsePacket(Response.RoomNotCreated));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(JoinRoomPacket packet) {
|
public void visitPacket(JoinRoomPacket packet) {
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
try {
|
||||||
|
server.joinRoom(packet.getRoomName(), this);
|
||||||
|
sendPacket(new ServerResponsePacket(Response.RoomJoined));
|
||||||
|
} catch (SocketException e) {
|
||||||
|
sendPacket(new ServerResponsePacket(Response.RoomNotJoined));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(LeaveRoomPacket packet) {
|
public void visitPacket(LeaveRoomPacket packet) {
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
try {
|
||||||
|
server.leaveRoom(this);
|
||||||
|
sendPacket(new ServerResponsePacket(Response.RoomLeft));
|
||||||
|
} catch (SocketException e) {
|
||||||
|
sendPacket(new ServerResponsePacket(Response.RoomNotLeft));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -77,21 +94,28 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(RequestRoomListPacket packet) {
|
public void visitPacket(RequestRoomListPacket packet) {
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
sendPacket(new RoomListPacket(server.getRoomNames()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(RoomListPacket packet) {
|
public void visitPacket(RoomListPacket packet) {
|
||||||
|
// I'm never supposed to receive this from the client
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(SendChatMessagePacket packet) {
|
public void visitPacket(SendChatMessagePacket packet) {
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
try {
|
||||||
|
server.sendToRoom(this, packet);
|
||||||
|
sendPacket(new ServerResponsePacket(Response.messageSent));
|
||||||
|
} catch (SocketException e) {
|
||||||
|
sendPacket(new ServerResponsePacket(Response.messageNotSent));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(ServerResponsePacket packet) {
|
public void visitPacket(ServerResponsePacket packet) {
|
||||||
|
// I'm never supposed to receive this from the client
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,31 @@ import java.net.DatagramPacket;
|
|||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
private final DatagramSocket socket;
|
private final DatagramSocket socket;
|
||||||
private final InetSocketAddress address;
|
private final InetSocketAddress address;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
Scanner portScanner = new Scanner(System.in);
|
||||||
|
System.out.println("Enter the server port number: ");
|
||||||
|
int port = portScanner.nextInt();
|
||||||
|
Client client = new Client(new InetSocketAddress("localhost", port));
|
||||||
|
while(true) {
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
String message = scan.nextLine();
|
||||||
|
client.sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new client
|
||||||
|
* @param address server adress (needed
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
public Client(InetSocketAddress address) throws IOException {
|
public Client(InetSocketAddress address) throws IOException {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.socket = new DatagramSocket();
|
this.socket = new DatagramSocket();
|
||||||
@@ -19,7 +38,7 @@ public class Client {
|
|||||||
private void sendGreetings() throws IOException {
|
private void sendGreetings() throws IOException {
|
||||||
int random = new Random().nextInt(1000);
|
int random = new Random().nextInt(1000);
|
||||||
String machineName = "RX" + random;
|
String machineName = "RX" + random;
|
||||||
sendMessage("hello serveur " + machineName);
|
sendMessage("hello Serveur, I am " + machineName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessage(String message) throws IOException {
|
public void sendMessage(String message) throws IOException {
|
||||||
@@ -32,7 +51,7 @@ public class Client {
|
|||||||
private void processHandshake(DatagramPacket packet) {
|
private void processHandshake(DatagramPacket packet) {
|
||||||
String message = new String(packet.getData(), 0, packet.getLength());
|
String message = new String(packet.getData(), 0, packet.getLength());
|
||||||
String machineName = message.split(" ")[1];
|
String machineName = message.split(" ")[1];
|
||||||
System.out.println("\t[Client] Serveur " + machineName + " ready: @" + packet.getAddress().getHostAddress() + ": "
|
System.out.println("↳[Client] Serveur " + machineName + " ready: @" + packet.getAddress().getHostAddress() + ": "
|
||||||
+ packet.getPort());
|
+ packet.getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ public class Server {
|
|||||||
new Thread(this::readMessages).start();
|
new Thread(this::readMessages).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return this.socket.getLocalPort();
|
||||||
|
}
|
||||||
|
|
||||||
private void sendMessage(String message, InetAddress address, int port) throws IOException {
|
private void sendMessage(String message, InetAddress address, int port) throws IOException {
|
||||||
byte[] buffer = message.getBytes();
|
byte[] buffer = message.getBytes();
|
||||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
|
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
|
||||||
|
|||||||
Reference in New Issue
Block a user