Add documentation to client package
This commit is contained in:
parent
f3813d23e2
commit
b5e8b96f65
4 changed files with 108 additions and 4 deletions
|
@ -19,10 +19,12 @@ import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The core of the client side
|
||||||
|
*/
|
||||||
public class Client {
|
public class Client {
|
||||||
private final int port;
|
private final int port;
|
||||||
private final String address;
|
private final String address;
|
||||||
|
@ -34,7 +36,14 @@ public class Client {
|
||||||
private final List<Channel> channels = new ArrayList<>();
|
private final List<Channel> channels = new ArrayList<>();
|
||||||
protected boolean started = false;
|
protected boolean started = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A client need a server and login
|
||||||
|
* @param address the server address
|
||||||
|
* @param port the server port
|
||||||
|
* @param username thr username
|
||||||
|
* @param password the password
|
||||||
|
* @throws IOException When the initial communication with the server fail
|
||||||
|
*/
|
||||||
public Client(String address, int port, String username, String password) throws IOException {
|
public Client(String address, int port, String username, String password) throws IOException {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
@ -45,6 +54,11 @@ public class Client {
|
||||||
getIn();
|
getIn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise the SSL WebSocket connection with the server
|
||||||
|
* @return the socket
|
||||||
|
* @throws IOException when unable to connect with the server
|
||||||
|
*/
|
||||||
private Socket initSSL() throws IOException {
|
private Socket initSSL() throws IOException {
|
||||||
SSLContext ctx = ChatSSL.getSSLContext();
|
SSLContext ctx = ChatSSL.getSSLContext();
|
||||||
|
|
||||||
|
@ -58,6 +72,10 @@ public class Client {
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close all connection to the server
|
||||||
|
* @throws IOException if fail to close the connection to the server
|
||||||
|
*/
|
||||||
public void disconnectedServer() throws IOException {
|
public void disconnectedServer() throws IOException {
|
||||||
socket.close();
|
socket.close();
|
||||||
out.close();
|
out.close();
|
||||||
|
@ -67,20 +85,33 @@ public class Client {
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a command to the server
|
||||||
|
* @param cmd the command
|
||||||
|
*/
|
||||||
private void send(Command cmd) {
|
private void send(Command cmd) {
|
||||||
try {
|
try {
|
||||||
out.writeObject(cmd);
|
out.writeObject(cmd);
|
||||||
out.flush();
|
out.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Fail to send message !");
|
System.err.println("Fail to send command !");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message in the first channel
|
||||||
|
* @param content the content of the message
|
||||||
|
*/
|
||||||
public void sendMessage(String content) {
|
public void sendMessage(String content) {
|
||||||
send(new Command(CommandType.message, List.of(new Message(content, channels.get(0)))));
|
send(new Command(CommandType.message, List.of(new Message(content, channels.get(0)))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a command
|
||||||
|
* @param content the command name an args (/commandName arg1 arh2 arg3)
|
||||||
|
* @throws UnknownCommand if the command can't be found
|
||||||
|
*/
|
||||||
public void sendCommand(String content) throws UnknownCommand {
|
public void sendCommand(String content) throws UnknownCommand {
|
||||||
List<String> args = Arrays.asList(content.split(" "));
|
List<String> args = Arrays.asList(content.split(" "));
|
||||||
String commandName = args.get(0).replace("/", "");
|
String commandName = args.get(0).replace("/", "");
|
||||||
|
@ -95,6 +126,11 @@ public class Client {
|
||||||
send(new Command(commandType, new ArrayList<>(args.subList(1, args.size()))));
|
send(new Command(commandType, new ArrayList<>(args.subList(1, args.size()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message with a specific channel
|
||||||
|
* @param content the channel name and the message content (#chanemName content of the message)
|
||||||
|
* @throws NotInChannel if the client isn't in the channel
|
||||||
|
*/
|
||||||
public void sendMessageChannel(String content) throws NotInChannel {
|
public void sendMessageChannel(String content) throws NotInChannel {
|
||||||
String[] args = content.split(" ");
|
String[] args = content.split(" ");
|
||||||
String channelName = args[0].replace("#", "");
|
String channelName = args[0].replace("#", "");
|
||||||
|
@ -107,6 +143,11 @@ public class Client {
|
||||||
send(new Command(CommandType.message, List.of(new Message(content, channel))));
|
send(new Command(CommandType.message, List.of(new Message(content, channel))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manage income data from the server
|
||||||
|
* @param data the data
|
||||||
|
* @throws IOException if a connection error occur with the server
|
||||||
|
*/
|
||||||
public void action(Object data) throws IOException {
|
public void action(Object data) throws IOException {
|
||||||
if (data instanceof Command)
|
if (data instanceof Command)
|
||||||
command((Command) data);
|
command((Command) data);
|
||||||
|
@ -118,6 +159,11 @@ public class Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command manager
|
||||||
|
* @param cmd the command
|
||||||
|
* @throws IOException if a connection error occur with the server
|
||||||
|
*/
|
||||||
private void command(Command cmd) throws IOException {
|
private void command(Command cmd) throws IOException {
|
||||||
switch (cmd.getType()) {
|
switch (cmd.getType()) {
|
||||||
case login -> commandLogin();
|
case login -> commandLogin();
|
||||||
|
@ -128,6 +174,10 @@ public class Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* After login handler
|
||||||
|
* @throws IOException if a connection error occur with the server
|
||||||
|
*/
|
||||||
private void commandLogin() throws IOException {
|
private void commandLogin() throws IOException {
|
||||||
out.writeObject(new Command(CommandType.list, null));
|
out.writeObject(new Command(CommandType.list, null));
|
||||||
out.flush();
|
out.flush();
|
||||||
|
@ -137,25 +187,46 @@ public class Client {
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receiving message from the server
|
||||||
|
* @param cmd the message command
|
||||||
|
*/
|
||||||
protected void commandMessage(Command cmd) {
|
protected void commandMessage(Command cmd) {
|
||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println(cmd.getArgs().get(0));
|
System.out.println(cmd.getArgs().get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User list handler
|
||||||
|
* @param cmd the command list
|
||||||
|
*/
|
||||||
private void commandList(Command cmd) {
|
private void commandList(Command cmd) {
|
||||||
System.out.println("Users: "+cmd.getArgs().stream().map(Object::toString).collect(Collectors.joining(", ")));
|
System.out.println("Users: "+cmd.getArgs().stream().map(Object::toString).collect(Collectors.joining(", ")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Channel list handler
|
||||||
|
* @param cmd the command channel list
|
||||||
|
*/
|
||||||
private void commandListChannels(Command cmd) {
|
private void commandListChannels(Command cmd) {
|
||||||
System.out.println("Channels: "+cmd.getArgs().stream().map(Object::toString).collect(Collectors.joining(", ")));
|
System.out.println("Channels: "+cmd.getArgs().stream().map(Object::toString).collect(Collectors.joining(", ")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The channel join handler
|
||||||
|
* @param cmd the command join
|
||||||
|
*/
|
||||||
private void commandJoin(Command cmd) {
|
private void commandJoin(Command cmd) {
|
||||||
Channel chan = (Channel) cmd.getArgs().get(0);
|
Channel chan = (Channel) cmd.getArgs().get(0);
|
||||||
channels.add(chan);
|
channels.add(chan);
|
||||||
System.out.println("You join "+chan);
|
System.out.println("You join "+chan);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main thread function, creating sub thread for user input and output CLI management
|
||||||
|
* @throws InterruptedException If the client force exit
|
||||||
|
* @throws IOException if a connection error occur with the server
|
||||||
|
*/
|
||||||
public void run() throws InterruptedException, IOException {
|
public void run() throws InterruptedException, IOException {
|
||||||
if (started)
|
if (started)
|
||||||
return;
|
return;
|
||||||
|
@ -176,6 +247,11 @@ public class Client {
|
||||||
clientReceiveThread.interrupt();
|
clientReceiveThread.interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the in stream of the WebSocket
|
||||||
|
* @return the in stream
|
||||||
|
* @throws IOException if a connection error occur with the server
|
||||||
|
*/
|
||||||
public ObjectInputStream getIn() throws IOException {
|
public ObjectInputStream getIn() throws IOException {
|
||||||
if (in == null)
|
if (in == null)
|
||||||
in = new ObjectInputStream(socket.getInputStream());
|
in = new ObjectInputStream(socket.getInputStream());
|
||||||
|
|
|
@ -5,16 +5,27 @@ import java.io.ObjectInputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clint message receiver manager
|
||||||
|
*/
|
||||||
public class ClientReceive implements Runnable {
|
public class ClientReceive implements Runnable {
|
||||||
private final Client client;
|
private final Client client;
|
||||||
private ObjectInputStream in;
|
private ObjectInputStream in;
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a receiver manager from a client and a socket
|
||||||
|
* @param client the client
|
||||||
|
* @param socket the client socket to the server
|
||||||
|
*/
|
||||||
public ClientReceive(Client client, Socket socket) {
|
public ClientReceive(Client client, Socket socket) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The mai thread
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -6,17 +6,28 @@ import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clint message sender manager
|
||||||
|
*/
|
||||||
public class ClientSend implements Runnable {
|
public class ClientSend implements Runnable {
|
||||||
private final Client client;
|
private final Client client;
|
||||||
private final ObjectOutputStream out;
|
private final ObjectOutputStream out;
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a sender manager from a client and a socket
|
||||||
|
* @param client the client
|
||||||
|
* @param socket the client socket to the server
|
||||||
|
*/
|
||||||
public ClientSend(Client client, ObjectOutputStream out, Socket socket) {
|
public ClientSend(Client client, ObjectOutputStream out, Socket socket) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The mai thread
|
||||||
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package fr.univ.lyon1.client;
|
package fr.univ.lyon1.client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main program for the client CLI
|
||||||
|
*/
|
||||||
public class MainClient {
|
public class MainClient {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
|
@ -19,11 +22,14 @@ public class MainClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Help usage for arguments
|
||||||
|
*/
|
||||||
private static void printUsage() {
|
private static void printUsage() {
|
||||||
System.out.println("java client.Client <address> <port>");
|
System.out.println("java client.Client <address> <port>");
|
||||||
System.out.println("\t<address>: server's ip address");
|
System.out.println("\t<address>: server's ip address");
|
||||||
System.out.println("\t<port>: server's port");
|
System.out.println("\t<port>: server's port");
|
||||||
System.out.println("\t<UUID>: user's UUID");
|
System.out.println("\t<username>: username");
|
||||||
System.out.println("\t<Password>: user's password");
|
System.out.println("\t<Password>: user's password");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue