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.

66 lines
2.6 KiB

1 year ago
  1. package server.util;
  2. import server.Main;
  3. import server.net.ChatServer;
  4. public class ServerBuilder {
  5. private static final int DEFAULT_PORT = 5000;
  6. private static final int MAX_USERS = 5;
  7. private static final String HELP_MSG = """
  8. -h, -help: prints this help message.
  9. -p, -port: sets the port for the ChatServer
  10. """;
  11. public static ChatServer buildServerFromConsoleArgs(String[] args) {
  12. int port = DEFAULT_PORT;
  13. int max_users = MAX_USERS;
  14. int ptr = 0;
  15. while (ptr < args.length) {
  16. switch (args[ptr]) {
  17. case "-h":
  18. case "-help":
  19. if (ptr != 0 || args.length > 1) {
  20. Main.console.error("You cannot use -h or -help in combination with other commands!");
  21. return null;
  22. }
  23. Main.console.println(HELP_MSG);
  24. return null;
  25. case "-p":
  26. case "-port":
  27. try {
  28. if (++ptr == args.length) {
  29. Main.console.error("Please enter a port-number (0 to 65536)!");
  30. return null;
  31. }
  32. port = Integer.parseInt(args[ptr], 10);
  33. if (port < 0 || port >= 65536) {
  34. Main.console.error("Please enter a valid port-number (0 to 65536)!");
  35. return null;
  36. }
  37. } catch (NumberFormatException e) {
  38. Main.console.error("Please enter a valid port-number (0 to 65536)!");
  39. return null;
  40. }
  41. break;
  42. case "-m":
  43. case "-max_users":
  44. try {
  45. if (++ptr == args.length) {
  46. Main.console.error("Please enter a user count!");
  47. return null;
  48. }
  49. max_users = Integer.parseInt(args[ptr], 10);
  50. } catch (NumberFormatException e) {
  51. Main.console.error("Please enter a valid user count!");
  52. return null;
  53. }
  54. break;
  55. default:
  56. Main.console.error("Couldn't parse argument: '" + args[ptr] + "', please try '-h' or '-help' for help.");
  57. return null;
  58. }
  59. ++ptr;
  60. }
  61. return new ChatServer(port, max_users);
  62. }
  63. }