Server.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package server;
  2. import java.io.IOException;
  3. import java.net.ServerSocket;
  4. import java.util.ArrayList;
  5. import java.util.Optional;
  6. import common.Message;
  7. import server.CommandRegistry;
  8. import server.Channel;
  9. import util.Logger;
  10. import util.ServerProperties;
  11. public class Server {
  12. static ServerProperties prop = new ServerProperties();
  13. static int port, maxUsers;
  14. static final String version = "0.3";
  15. public static ArrayList<BanNote> banNotes = new ArrayList<BanNote>();
  16. public static ArrayList<Channel> channels;
  17. public static ClientCollection clients;
  18. static ServerSocket so;
  19. public static LocalClient OPClient;
  20. public static Logger log;
  21. public static CommandRegistry commReg;
  22. public static void main(String[] arg){
  23. System.out.println("Starting ChatServer version " + version + "...");
  24. System.out.print("Loadning properties file...");
  25. prop.loadProperties();
  26. System.out.println("Done");
  27. System.out.print("Reading numbers from properties object...");
  28. port = prop.getNumberProperty("port");
  29. maxUsers = prop.getNumberProperty("maxUsers");
  30. System.out.println("Done");
  31. System.out.print("Setting up socket...");
  32. try {
  33. so = new ServerSocket(port);
  34. } catch(IOException ex) {
  35. System.out.println("Error setting up socket. Server already running?");
  36. System.exit(0);
  37. }
  38. System.out.println("Done");
  39. clients = new ClientCollection();
  40. channels = new ArrayList<Channel>();
  41. channels.add(new Channel("Main"));
  42. System.out.print("Starting commandhandler...");
  43. commReg = new CommandRegistry();
  44. System.out.println("Done");
  45. System.out.print("Creating virtual local client...");
  46. OPClient = new LocalClient();
  47. System.out.println("Done");
  48. System.out.print("Starting logger...");
  49. try {
  50. log = new Logger();
  51. } catch (IOException e) {
  52. System.out.println();
  53. System.out.println("Unable to start logger.");
  54. e.printStackTrace();
  55. return;
  56. }
  57. System.out.println("Done");
  58. System.out.println("Server started successfully!");
  59. getClients();
  60. }
  61. static void getClients() {
  62. while(!so.isClosed()) {
  63. Client newClient = null;
  64. try {
  65. newClient = new Client(Server.so.accept());
  66. clients.add(newClient);
  67. channels.get(0).add(newClient);
  68. wideBroadcast(new Message(newClient.username + " has connected."));
  69. } catch (IllegalArgumentException ex) {
  70. } catch (ArrayIndexOutOfBoundsException ex) {
  71. newClient.send(new Message("Server full!"));
  72. newClient.disconnect(false);
  73. } catch (IOException ex) {
  74. if (so.isClosed())
  75. return;
  76. } catch (Exception ex) {
  77. System.out.println("Could not get new client!");
  78. ex.printStackTrace();
  79. }
  80. }
  81. }
  82. public static Optional<Channel> getChannelByName(String name) throws NullPointerException {
  83. return channels.stream().filter(c -> c.name.equals(name)).findFirst();
  84. }
  85. public static void wideBroadcast(Message mess) {
  86. clients.broadcast(mess);
  87. }
  88. public static String[] getUsersOnline() {
  89. return clients.listClientsArray();
  90. }
  91. public static String listClients() {
  92. return clients.listClients();
  93. }
  94. public static Optional<Client> getUserByName(String username) {
  95. return clients.getClientByName(username);
  96. }
  97. public static void cleanUp() { //Makes sure the client gets removed from all arrays
  98. clients.cleanUp();
  99. channels.forEach(c -> c.cleanUp());
  100. }
  101. public static void exit() {
  102. wideBroadcast(new Message("Shutting down server!"));
  103. for (int i = 0; i < clients.size(); i++) //Has to be done with number iteration, otherwise unsafe
  104. clients.get(i).disconnect();
  105. log.close();
  106. OPClient.disconnect();
  107. try {
  108. so.close();
  109. } catch (IOException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. }