ClientListener + ClientConsole
This commit is contained in:
@@ -1,118 +1,54 @@
|
||||
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.*;
|
||||
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;
|
||||
|
||||
public class Client {
|
||||
|
||||
private final ClientConnexion connexion;
|
||||
private final ClientListener 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);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Enter your pseudo:");
|
||||
String pseudo = scanner.nextLine();
|
||||
public Client(InetSocketAddress serverAddress, ClientListener callback, String pseudo) throws SocketException {
|
||||
this.connexion = new ClientConnexion(new DatagramSocket(), serverAddress, callback);
|
||||
this.callback = callback;
|
||||
login(pseudo);
|
||||
}
|
||||
|
||||
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("/listRooms")) {
|
||||
RequestRoomList();
|
||||
} else if(message.startsWith("/joinRoom")) {
|
||||
String roomName = message.substring(10).trim();
|
||||
SendJoinRoom(roomName);
|
||||
} else if(message.startsWith("/leaveRoom")) {
|
||||
SendLeaveRoom();
|
||||
} else if(message.startsWith("/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("Unknown command");
|
||||
}
|
||||
} else {
|
||||
SendChatMessage(message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
public void close() {
|
||||
//TODO: send disconnect packet
|
||||
this.connexion.close();
|
||||
this.callback.handleDisconnect();
|
||||
}
|
||||
|
||||
private void login(String pseudo) {
|
||||
try {
|
||||
this.connexion.sendPacket(new LoginPacket(pseudo));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.connexion.sendPacket(new LoginPacket(pseudo));
|
||||
}
|
||||
|
||||
public void SendChatMessage(String message) {
|
||||
try {
|
||||
this.connexion.sendPacket(new SendChatMessagePacket(message));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.connexion.sendPacket(new SendChatMessagePacket(message));
|
||||
}
|
||||
|
||||
public void SendCreateRoom(String roomName) {
|
||||
try {
|
||||
this.connexion.sendPacket(new CreateRoomPacket(roomName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.connexion.sendPacket(new CreateRoomPacket(roomName));
|
||||
}
|
||||
|
||||
public void SendJoinRoom(String roomName) {
|
||||
try {
|
||||
this.connexion.sendPacket(new JoinRoomPacket(roomName));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.connexion.sendPacket(new JoinRoomPacket(roomName));
|
||||
}
|
||||
|
||||
public void SendLeaveRoom() {
|
||||
try {
|
||||
this.connexion.sendPacket(new LeaveRoomPacket());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.connexion.sendPacket(new LeaveRoomPacket());
|
||||
}
|
||||
|
||||
public void RequestRoomList() {
|
||||
try {
|
||||
this.connexion.sendPacket(new RequestRoomListPacket());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.connexion.sendPacket(new RequestRoomListPacket());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user