chatprogramm was sonst
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.6 KiB

package server.util;
import server.Main;
import server.net.ChatServer;
public class ServerBuilder {
private static final int DEFAULT_PORT = 5000;
private static final int MAX_USERS = 5;
private static final String HELP_MSG = """
-h, -help: prints this help message.
-p, -port: sets the port for the ChatServer
""";
public static ChatServer buildServerFromConsoleArgs(String[] args) {
int port = DEFAULT_PORT;
int max_users = MAX_USERS;
int ptr = 0;
while (ptr < args.length) {
switch (args[ptr]) {
case "-h":
case "-help":
if (ptr != 0 || args.length > 1) {
Main.console.error("You cannot use -h or -help in combination with other commands!");
return null;
}
Main.console.println(HELP_MSG);
return null;
case "-p":
case "-port":
try {
if (++ptr == args.length) {
Main.console.error("Please enter a port-number (0 to 65536)!");
return null;
}
port = Integer.parseInt(args[ptr], 10);
if (port < 0 || port >= 65536) {
Main.console.error("Please enter a valid port-number (0 to 65536)!");
return null;
}
} catch (NumberFormatException e) {
Main.console.error("Please enter a valid port-number (0 to 65536)!");
return null;
}
break;
case "-m":
case "-max_users":
try {
if (++ptr == args.length) {
Main.console.error("Please enter a user count!");
return null;
}
max_users = Integer.parseInt(args[ptr], 10);
} catch (NumberFormatException e) {
Main.console.error("Please enter a valid user count!");
return null;
}
break;
default:
Main.console.error("Couldn't parse argument: '" + args[ptr] + "', please try '-h' or '-help' for help.");
return null;
}
++ptr;
}
return new ChatServer(port, max_users);
}
}