From b5e8b96f65c51599378de28a35f5b5dd4312ea5e Mon Sep 17 00:00:00 2001 From: Florian Charlaix Date: Wed, 19 Jan 2022 09:31:24 +0100 Subject: [PATCH] Add documentation to client package --- src/fr/univ/lyon1/client/Client.java | 82 ++++++++++++++++++++- src/fr/univ/lyon1/client/ClientReceive.java | 11 +++ src/fr/univ/lyon1/client/ClientSend.java | 11 +++ src/fr/univ/lyon1/client/MainClient.java | 8 +- 4 files changed, 108 insertions(+), 4 deletions(-) diff --git a/src/fr/univ/lyon1/client/Client.java b/src/fr/univ/lyon1/client/Client.java index 29d9fac..791a586 100644 --- a/src/fr/univ/lyon1/client/Client.java +++ b/src/fr/univ/lyon1/client/Client.java @@ -19,10 +19,12 @@ import java.io.ObjectOutputStream; import java.net.Socket; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +/** + * The core of the client side + */ public class Client { private final int port; private final String address; @@ -34,7 +36,14 @@ public class Client { private final List channels = new ArrayList<>(); 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 { this.address = address; this.port = port; @@ -45,6 +54,11 @@ public class Client { 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 { SSLContext ctx = ChatSSL.getSSLContext(); @@ -58,6 +72,10 @@ public class Client { return connection; } + /** + * Close all connection to the server + * @throws IOException if fail to close the connection to the server + */ public void disconnectedServer() throws IOException { socket.close(); out.close(); @@ -67,20 +85,33 @@ public class Client { System.exit(0); } + /** + * Send a command to the server + * @param cmd the command + */ private void send(Command cmd) { try { out.writeObject(cmd); out.flush(); } catch (IOException e) { - System.err.println("Fail to send message !"); + System.err.println("Fail to send command !"); e.printStackTrace(); } } + /** + * Send a message in the first channel + * @param content the content of the message + */ public void sendMessage(String content) { 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 { List args = Arrays.asList(content.split(" ")); 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 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 { String[] args = content.split(" "); String channelName = args[0].replace("#", ""); @@ -107,6 +143,11 @@ public class Client { 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 { if (data instanceof Command) 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 { switch (cmd.getType()) { 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 { out.writeObject(new Command(CommandType.list, null)); out.flush(); @@ -137,25 +187,46 @@ public class Client { out.flush(); } + /** + * Receiving message from the server + * @param cmd the message command + */ protected void commandMessage(Command cmd) { System.out.println(); System.out.println(cmd.getArgs().get(0)); } + /** + * User list handler + * @param cmd the command list + */ private void commandList(Command cmd) { 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) { 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) { Channel chan = (Channel) cmd.getArgs().get(0); channels.add(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 { if (started) return; @@ -176,6 +247,11 @@ public class Client { 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 { if (in == null) in = new ObjectInputStream(socket.getInputStream()); diff --git a/src/fr/univ/lyon1/client/ClientReceive.java b/src/fr/univ/lyon1/client/ClientReceive.java index 4f5aa9d..0124fb2 100644 --- a/src/fr/univ/lyon1/client/ClientReceive.java +++ b/src/fr/univ/lyon1/client/ClientReceive.java @@ -5,16 +5,27 @@ import java.io.ObjectInputStream; import java.net.Socket; import java.net.SocketException; +/** + * Clint message receiver manager + */ public class ClientReceive implements Runnable { private final Client client; private ObjectInputStream in; 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) { this.client = client; this.socket = socket; } + /** + * The mai thread + */ @Override public void run() { try { diff --git a/src/fr/univ/lyon1/client/ClientSend.java b/src/fr/univ/lyon1/client/ClientSend.java index 8d36f80..fb8f486 100644 --- a/src/fr/univ/lyon1/client/ClientSend.java +++ b/src/fr/univ/lyon1/client/ClientSend.java @@ -6,17 +6,28 @@ import java.io.ObjectOutputStream; import java.net.Socket; import java.util.Scanner; +/** + * Clint message sender manager + */ public class ClientSend implements Runnable { private final Client client; private final ObjectOutputStream out; 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) { this.client = client; this.out = out; this.socket = socket; } + /** + * The mai thread + */ public void run() { Scanner sc = new Scanner(System.in); diff --git a/src/fr/univ/lyon1/client/MainClient.java b/src/fr/univ/lyon1/client/MainClient.java index afc4565..aa7bc43 100644 --- a/src/fr/univ/lyon1/client/MainClient.java +++ b/src/fr/univ/lyon1/client/MainClient.java @@ -1,5 +1,8 @@ package fr.univ.lyon1.client; +/** + * The main program for the client CLI + */ public class MainClient { public static void main(String[] args) { try { @@ -19,11 +22,14 @@ public class MainClient { } } + /** + * Help usage for arguments + */ private static void printUsage() { System.out.println("java client.Client
"); System.out.println("\t
: server's ip address"); System.out.println("\t: server's port"); - System.out.println("\t: user's UUID"); + System.out.println("\t: username"); System.out.println("\t: user's password"); } }