Documented everything I could and tried unifying format

This commit is contained in:
Seraina
2022-03-27 22:43:35 +02:00
parent 6cf98643d1
commit c87d894e10
12 changed files with 80 additions and 145 deletions

View File

@@ -36,9 +36,8 @@ public class Client {
}
/**
*
* Sends a message to the Server in a formatted way COMND$msg
*/
public void sendMessage() {
try {
Scanner sc = new Scanner(System.in);
@@ -56,7 +55,6 @@ public class Client {
}
/**
* Starts a thread which listens for incoming messages
*/
@@ -86,6 +84,7 @@ public class Client {
/**
* Sends a message to the server, as is.
*
* @param msg the message sent. Should already be protocol-formatted.
*/
public void sendMsgToServer(String msg) {
@@ -101,6 +100,7 @@ public class Client {
/**
* parses a received message according to the client protocol.
*
* @param msg the message to be parsed.
*/
public void parse(String msg) {
@@ -137,7 +137,8 @@ public class Client {
hostname = args[0];
}
String systemName = System.getProperty("user.name");
System.out.println("Choose a nickname (Suggestion: " + systemName + "): ");
System.out.println("Choose a nickname (Suggestion: " + systemName
+ "): "); //Suggests a name based on System username
String username = sc.next();
Socket socket;
try {

View File

@@ -6,8 +6,9 @@ public class JClientProtocolParser {
/**
* Used by the client to parse an incoming protocol message.
*
* @param msg the encoded message that needs to be parsed
* @param c this Client(required so this method can access the Client's methods)
* @param c this Client(required so this method can access the Client's methods)
*/
public static void parse(String msg, Client c) {
String header = ""; //"header" is the first 5 characters, i.e. the protocol part

View File

@@ -4,7 +4,7 @@ public class MessageFormatter {
/**
* Takes a given Message and reformats it to where the JServerProtocolParser.parse() method can
* handle it. May need to be redesigned one the games uses a GUI
* handle it (see Protocol.txt). May need to be redesigned once the games uses a GUI
*
* @param msg the Messaged to be reformatted
* @return the reformatted message in the form HEADR$msg

View File

@@ -19,6 +19,7 @@ public class ClientPinger implements Runnable {
/**
* @param socket the socket the Client is connected to which is used to end the thread if the
* connection is lost.
*
* @param out the output through which the pings are sent.
*/
public ClientPinger(BufferedWriter out, Socket socket) {

View File

@@ -19,6 +19,7 @@ public class ServerPinger implements Runnable {
/**
* @param socket the socket the ClientHandler is connected to; used to end the thread if the
* connection is lost.
*
* @param out the output through which the pings are sent.
*/
public ServerPinger(BufferedWriter out, Socket socket) {

View File

@@ -1,22 +1,23 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.server;
/**
* This class is built to contain the usernames of all players in a single string.
* This allows a duplicate check (--> ClientHandler) when a new player chooses
* a name: does the string with all the previous names contain the new player's
* desired username? If yes, he is being assigned a random name. If no, he can keep
* his desired name.
* **/
* This class is built to contain the usernames of all players in a single string. This allows a
* duplicate check (--> ClientHandler) when a new player chooses a name: does the string with all
* the previous names contain the new player's desired username? If yes, he is being assigned a
* random name. If no, he can keep his desired name.
*/
public class AllClientNames {
static StringBuilder names = new StringBuilder();
/**
* Safes a new name to the List of all Names
* @param currentName the new name to be added
* @return All names adding the new currentName
*/
public static String allNames(String currentName) {
return names.append(currentName).toString();
}
static StringBuilder names = new StringBuilder();
/**
* Safes a new name to the List of all Names
*
* @param currentName the new name to be added
* @return All names adding the new currentName
*/
public static String allNames(String currentName) {
return names.append(currentName).toString();
}
}

View File

@@ -99,7 +99,9 @@ public class ClientHandler implements Runnable {
}
/**
* Lets the client change their respective username, if the username is already taken, a similar option is chosen
* Lets the client change their respective username, if the username is already taken, a similar
* option is chosen.
*
* @param newName The desired new name to replace the old one with.
*/
public void changeUsername(String newName) {
@@ -109,11 +111,12 @@ public class ClientHandler implements Runnable {
String h = this.clientUserName; //just a friendly little helper
this.clientUserName = newName;
AllClientNames.allNames(newName);
broadcastMessage(h +" have changed their nickname to " + clientUserName);
broadcastMessage(h + " have changed their nickname to " + clientUserName);
}
/**
* Broadcasts a Message to all active clients in the form "Username: msg"
*
* @param msg the Message to be broadcasted
*/
@@ -124,9 +127,10 @@ public class ClientHandler implements Runnable {
}
//TODO: Documentation
/**
/** Sends a given message to client
*
* @param msg
* @param msg the given message
*/
public void sendMsgToClient(String msg) {
@@ -139,11 +143,21 @@ public class ClientHandler implements Runnable {
}
}
/**
* Does what it sounds like
*/
public void removeClientHandler() {
connectedClients.remove(this);
broadcastMessage("SERVER: " + clientUserName + " has left the server");
}
/**
* Does exactly what it says on the tin, closes all connections of Client to Server.
*
* @param socket the socket to be closed
* @param in the in-Stream reader to be closed
* @param out the out-Stream Write to be closed
*/
public void closeEverything(Socket socket, BufferedReader in, BufferedWriter out) {
removeClientHandler();
try {

View File

@@ -6,7 +6,8 @@ import java.util.Random;
public class NameGenerator {
/**
* Creates a random alteration of a Name by adding 4 numbers at the end of the Name that shall be alterd
* Creates a random alteration of a Name by adding 4 numbers at the end of the Name that shall be altered
*
* @param username the to be altered username
* @return username + four numbers
*/

View File

@@ -17,6 +17,9 @@ public class Server {
this.serverSocket = serverSocket;
}
/**
* Starts up a Server that opens Port 42069 either located wia IP address or localhost
*/
public void startServer() {
try {
System.out.println("Port 42069 is open on " + this.serverSocket.getInetAddress());