Compare commits
1 Commits
07ad2ba05e
...
handshake
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6d3bca136a |
@@ -30,3 +30,10 @@ You will also be able to create a new room.
|
||||
- /joinRoom *roomName*
|
||||
- /leaveRoom
|
||||
- /help
|
||||
|
||||
> [!NOTE]
|
||||
> There are some aliases for the commands:
|
||||
> - /create
|
||||
> - /list
|
||||
> - /join
|
||||
> - /leave
|
||||
@@ -1,21 +1,24 @@
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Scanner;
|
||||
|
||||
import client.ClientConsole;
|
||||
import client.Client;
|
||||
import server.Server;
|
||||
|
||||
public class ChatApp {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Server server = new Server(6665);
|
||||
ClientConsole client = new ClientConsole(new InetSocketAddress("localhost", 6665));
|
||||
int port = 6665;
|
||||
Server server = new Server(port);
|
||||
Client client = new Client(new InetSocketAddress("localhost", port));
|
||||
|
||||
client.getClientInterface().SendCreateRoom("101");
|
||||
client.SendCreateRoom("101");
|
||||
|
||||
client.joinThread();
|
||||
|
||||
System.out.println("Stopping server ...");
|
||||
|
||||
server.close();
|
||||
|
||||
System.out.println("Done !");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (true) {
|
||||
String message = scanner.nextLine();
|
||||
System.out.print("\033[1A");
|
||||
System.out.print("\r\033[2K");
|
||||
System.out.flush();
|
||||
client.visitMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,54 +1,142 @@
|
||||
package client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
import network.protocol.packets.CreateRoomPacket;
|
||||
import network.protocol.packets.JoinRoomPacket;
|
||||
import network.protocol.packets.LeaveRoomPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.RequestRoomListPacket;
|
||||
import network.protocol.packets.SendChatMessagePacket;
|
||||
import network.protocol.ANSIColor;
|
||||
import network.protocol.packets.*;
|
||||
|
||||
public class Client {
|
||||
|
||||
private final ClientConnexion connexion;
|
||||
private final ClientListener callback;
|
||||
|
||||
public Client(InetSocketAddress serverAddress, ClientListener callback, String pseudo) throws SocketException {
|
||||
this.connexion = new ClientConnexion(new DatagramSocket(), serverAddress, callback);
|
||||
this.callback = callback;
|
||||
public static void main(String[] args) {
|
||||
String host = "localhost";
|
||||
int port = 6665;
|
||||
try {
|
||||
Client client = new Client(new InetSocketAddress(host, port));
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while(true) {
|
||||
String message = scanner.nextLine();
|
||||
client.visitMessage(message);
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Client(InetSocketAddress serverAddress) throws SocketException {
|
||||
this.connexion = new ClientConnexion(new DatagramSocket(), serverAddress);
|
||||
int tries = 0;
|
||||
try {
|
||||
connexion.sendPacket(new HandshakePacket());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while(!connexion.connected) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
if(tries++ > 5) {
|
||||
System.out.println("Server is not responding");
|
||||
System.exit(1);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Enter your pseudo:");
|
||||
String pseudo = scanner.nextLine();
|
||||
login(pseudo);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
//TODO: send disconnect packet
|
||||
this.connexion.close();
|
||||
this.callback.handleDisconnect();
|
||||
public void visitMessage(String message){
|
||||
try {
|
||||
if(message.startsWith("/")){
|
||||
if(message.startsWith("/createRoom ")) {
|
||||
String roomName = message.substring(12).trim();
|
||||
SendCreateRoom(roomName);
|
||||
} else if(message.startsWith("/create ")){
|
||||
String roomName = message.substring(8).trim();
|
||||
SendCreateRoom(roomName);
|
||||
} else if(message.equals("/listRooms") || message.equals("/list")) {
|
||||
RequestRoomList();
|
||||
} else if(message.startsWith("/joinRoom ")) {
|
||||
String roomName = message.substring(10).trim();
|
||||
SendJoinRoom(roomName);
|
||||
} else if(message.startsWith("/join ")){
|
||||
String roomName = message.substring(6).trim();
|
||||
SendJoinRoom(roomName);
|
||||
} else if(message.equals("/leaveRoom") || message.equals("/leave")) {
|
||||
SendLeaveRoom();
|
||||
} else if(message.equals("/help")) {
|
||||
System.out.println("Available commands:");
|
||||
System.out.println("\t/createRoom <roomName>");
|
||||
System.out.println("\t/listRooms");
|
||||
System.out.println("\t/joinRoom <roomName>");
|
||||
System.out.println("\t/leaveRoom");
|
||||
System.out.println("\t/help");
|
||||
}
|
||||
else {
|
||||
System.out.println(ANSIColor.formatString("&rUnknown command&n"));
|
||||
}
|
||||
} else {
|
||||
SendChatMessage(message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void login(String pseudo) {
|
||||
try {
|
||||
this.connexion.sendPacket(new LoginPacket(pseudo));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendChatMessage(String message) {
|
||||
try {
|
||||
this.connexion.sendPacket(new SendChatMessagePacket(message));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendCreateRoom(String roomName) {
|
||||
try {
|
||||
this.connexion.sendPacket(new CreateRoomPacket(roomName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendJoinRoom(String roomName) {
|
||||
try {
|
||||
this.connexion.sendPacket(new JoinRoomPacket(roomName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendLeaveRoom() {
|
||||
try {
|
||||
this.connexion.sendPacket(new LeaveRoomPacket());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void RequestRoomList() {
|
||||
try {
|
||||
this.connexion.sendPacket(new RequestRoomListPacket());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,44 +7,30 @@ import java.net.InetSocketAddress;
|
||||
import network.PacketHandler;
|
||||
import network.SocketReader;
|
||||
import network.SocketWriter;
|
||||
import network.protocol.ANSIColor;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.packets.ChatMessagePacket;
|
||||
import network.protocol.packets.CreateRoomPacket;
|
||||
import network.protocol.packets.JoinRoomPacket;
|
||||
import network.protocol.packets.LeaveRoomPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.RequestRoomListPacket;
|
||||
import network.protocol.packets.RoomListPacket;
|
||||
import network.protocol.packets.SendChatMessagePacket;
|
||||
import network.protocol.packets.ServerResponsePacket;
|
||||
import network.protocol.packets.*;
|
||||
|
||||
public class ClientConnexion implements PacketVisitor, PacketHandler{
|
||||
|
||||
private final InetSocketAddress serverAddress;
|
||||
private final SocketWriter writer;
|
||||
private final SocketReader reader;
|
||||
private final ClientListener callback;
|
||||
protected boolean connected = false;
|
||||
|
||||
public ClientConnexion(DatagramSocket socket, InetSocketAddress serverAddress, ClientListener callback) {
|
||||
public ClientConnexion(DatagramSocket socket, InetSocketAddress serverAddress) {
|
||||
this.serverAddress = serverAddress;
|
||||
this.writer = new SocketWriter(socket);
|
||||
this.reader = new SocketReader(socket, this);
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.reader.stop();
|
||||
}
|
||||
|
||||
public void sendPacket(Packet packet) {
|
||||
try {
|
||||
public void sendPacket(Packet packet) throws IOException {
|
||||
this.writer.sendPacket(packet, serverAddress);
|
||||
} catch (IOException e) {
|
||||
this.close();
|
||||
this.callback.handleDisconnect();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,17 +41,16 @@ public class ClientConnexion implements PacketVisitor, PacketHandler{
|
||||
|
||||
@Override
|
||||
public void visitPacket(ChatMessagePacket packet) {
|
||||
this.callback.handleChatMessage(packet.getTime(), packet.getChatter(), packet.getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(RoomListPacket packet) {
|
||||
this.callback.handleRoomList(packet.getRoomNames());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ServerResponsePacket packet) {
|
||||
this.callback.handleServerResponse(packet.getResponse());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String time = packet.getTime().toString();
|
||||
sb.append("&y[")
|
||||
.append(time, 11, 19) // We only take the HH:MM:SS part
|
||||
.append("]&n")
|
||||
.append(" ")
|
||||
.append(packet.getChatter())
|
||||
.append(" : ")
|
||||
.append(packet.getContent()).append("&n"); // make the color back to normal at the end of every message
|
||||
System.out.println(ANSIColor.formatString(sb.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -93,9 +78,29 @@ public class ClientConnexion implements PacketVisitor, PacketHandler{
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(RoomListPacket packet) {
|
||||
System.out.println("Rooms :");
|
||||
for (String room : packet.getRoomNames()) {
|
||||
System.out.println("\t" + room);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(SendChatMessagePacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ServerResponsePacket packet) {
|
||||
if(packet.getResponse() == ServerResponsePacket.Response.MessageSent || packet.getResponse() == ServerResponsePacket.Response.MessageNotSent) {
|
||||
return;
|
||||
}
|
||||
System.out.println(packet.getResponse());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(HandshakePacket packet) {
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
package client;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import network.protocol.ANSIColor;
|
||||
import network.protocol.packets.ServerResponsePacket;
|
||||
import network.protocol.packets.ServerResponsePacket.Response;
|
||||
|
||||
public class ClientConsole implements ClientListener {
|
||||
|
||||
private Client client;
|
||||
private final Thread inputThread;
|
||||
private final Scanner scanner;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new ClientConsole(new InetSocketAddress("localhost", 6665));
|
||||
}
|
||||
|
||||
public ClientConsole(InetSocketAddress address) {
|
||||
this.inputThread = new Thread(this::inputLoop);
|
||||
this.scanner = new Scanner(System.in);
|
||||
String pseudo = inputPseudo();
|
||||
try {
|
||||
this.client = new Client(address, this, pseudo);
|
||||
this.inputThread.start();
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private String inputPseudo() {
|
||||
System.out.println("Enter your pseudo:");
|
||||
String pseudo = this.scanner.nextLine();
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
public Client getClientInterface() {
|
||||
return this.client;
|
||||
}
|
||||
|
||||
private void inputLoop() {
|
||||
while (!Thread.interrupted()) {
|
||||
String message = scanner.nextLine();
|
||||
visitMessage(message);
|
||||
}
|
||||
this.scanner.close();
|
||||
}
|
||||
|
||||
public void joinThread() throws InterruptedException {
|
||||
this.inputThread.join();
|
||||
}
|
||||
|
||||
private void visitMessage(String message) {
|
||||
try {
|
||||
if (message.startsWith("/")) {
|
||||
if (message.startsWith("/createRoom")) {
|
||||
String roomName = message.substring(12).trim();
|
||||
this.client.SendCreateRoom(roomName);
|
||||
} else if (message.startsWith("/listRooms")) {
|
||||
this.client.RequestRoomList();
|
||||
} else if (message.startsWith("/joinRoom")) {
|
||||
String roomName = message.substring(10).trim();
|
||||
this.client.SendJoinRoom(roomName);
|
||||
} else if (message.startsWith("/leaveRoom")) {
|
||||
this.client.SendLeaveRoom();
|
||||
} else if (message.startsWith("/bye")) {
|
||||
this.client.close();
|
||||
} else if (message.startsWith("/help")) {
|
||||
System.out.println("Available commands:");
|
||||
System.out.println("\t/bye");
|
||||
System.out.println("\t/createRoom <roomName>");
|
||||
System.out.println("\t/listRooms");
|
||||
System.out.println("\t/joinRoom <roomName>");
|
||||
System.out.println("\t/leaveRoom");
|
||||
System.out.println("\t/help");
|
||||
} else {
|
||||
System.out.println("Unknown command");
|
||||
}
|
||||
} else {
|
||||
this.client.SendChatMessage(message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleDisconnect() {
|
||||
System.out.println("Disconnected !");
|
||||
this.inputThread.interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleChatMessage(Instant time, String chatter, String content) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String strTime = time.toString();
|
||||
sb.append("&y[");
|
||||
sb.append(strTime, 11, 19); // We only take the HH:MM:SS part
|
||||
sb.append("]&n");
|
||||
sb.append(" ");
|
||||
sb.append(chatter);
|
||||
sb.append(" : ");
|
||||
sb.append(content).append("&n"); // make the color back to normal at the end of every message
|
||||
System.out.println(ANSIColor.formatString(sb.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRoomList(List<String> roomNames) {
|
||||
System.out.println("Rooms :");
|
||||
for (String room : roomNames) {
|
||||
System.out.println("\t" + room);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleServerResponse(Response response) {
|
||||
if (response == ServerResponsePacket.Response.MessageSent
|
||||
|| response == ServerResponsePacket.Response.MessageNotSent) {
|
||||
return;
|
||||
}
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package client;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import network.protocol.packets.ServerResponsePacket;
|
||||
|
||||
public interface ClientListener {
|
||||
|
||||
void handleDisconnect();
|
||||
void handleChatMessage(Instant time, String chatter, String content);
|
||||
void handleRoomList(List<String> roomNames);
|
||||
void handleServerResponse(ServerResponsePacket.Response response);
|
||||
|
||||
}
|
||||
@@ -24,7 +24,6 @@ public class SocketReader {
|
||||
|
||||
public void stop() {
|
||||
this.readThread.interrupt();
|
||||
socket.close();
|
||||
}
|
||||
|
||||
private void readLoop() {
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
package network.protocol;
|
||||
|
||||
import network.protocol.packets.ChatMessagePacket;
|
||||
import network.protocol.packets.CreateRoomPacket;
|
||||
import network.protocol.packets.JoinRoomPacket;
|
||||
import network.protocol.packets.LeaveRoomPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.RequestRoomListPacket;
|
||||
import network.protocol.packets.RoomListPacket;
|
||||
import network.protocol.packets.SendChatMessagePacket;
|
||||
import network.protocol.packets.ServerResponsePacket;
|
||||
import network.protocol.packets.*;
|
||||
|
||||
public interface PacketVisitor {
|
||||
|
||||
@@ -25,5 +17,5 @@ public interface PacketVisitor {
|
||||
void visitPacket(RoomListPacket packet);
|
||||
void visitPacket(SendChatMessagePacket packet);
|
||||
void visitPacket(ServerResponsePacket packet);
|
||||
|
||||
void visitPacket(HandshakePacket packet);
|
||||
}
|
||||
|
||||
14
ChatApp/src/network/protocol/packets/HandshakePacket.java
Normal file
14
ChatApp/src/network/protocol/packets/HandshakePacket.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
|
||||
public class HandshakePacket extends Packet {
|
||||
public HandshakePacket() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
|
||||
public class RoomListPacket extends Packet {
|
||||
|
||||
private final List<String> roomNames;
|
||||
private final ArrayList<String> roomNames;
|
||||
|
||||
public RoomListPacket(List<String> roomNames) {
|
||||
public RoomListPacket(ArrayList<String> roomNames) {
|
||||
this.roomNames = roomNames;
|
||||
}
|
||||
|
||||
public List<String> getRoomNames() {
|
||||
public ArrayList<String> getRoomNames() {
|
||||
return roomNames;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,15 +8,7 @@ import java.net.SocketException;
|
||||
import network.SocketWriter;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.packets.ChatMessagePacket;
|
||||
import network.protocol.packets.CreateRoomPacket;
|
||||
import network.protocol.packets.JoinRoomPacket;
|
||||
import network.protocol.packets.LeaveRoomPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.RequestRoomListPacket;
|
||||
import network.protocol.packets.RoomListPacket;
|
||||
import network.protocol.packets.SendChatMessagePacket;
|
||||
import network.protocol.packets.ServerResponsePacket;
|
||||
import network.protocol.packets.*;
|
||||
import network.protocol.packets.ServerResponsePacket.Response;
|
||||
|
||||
public class ServerConnexion implements PacketVisitor {
|
||||
@@ -118,4 +110,10 @@ public class ServerConnexion implements PacketVisitor {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(HandshakePacket packet) {
|
||||
System.out.println("[Server] Handshake received from " + clientAddress);
|
||||
sendPacket(new HandshakePacket());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user