Coverage Summary for Class: Main (<empty package name>)

Class Class, % Method, % Branch, % Line, %
Main 0% (0/1) 0% (0/2) 0% (0/59) 0% (0/24)


1 import it.polimi.ingsw.Client.CLI.CLI; 2 import it.polimi.ingsw.Client.GUI.GUI; 3 import it.polimi.ingsw.Logger; 4 import it.polimi.ingsw.Server.WelcomeServer; 5  6 import java.net.InetAddress; 7 import java.net.UnknownHostException; 8 import java.util.Arrays; 9  10 /** 11  * main class of the project, call the {@link #main} function to start the whole thing up 12  */ 13 public class Main { 14  private static final String HELP_MESSAGE = """ 15  Welcome to the Eriantys startup tool! 16  17  This tool must be called with at least one of the following arguments: 18  < s > will start the Server (default bound to 0.0.0.0:8080) [ max instances: 1 ] 19  < g > will start a GUI [ max instances: unlimited ] 20  < c > will start the CLI [ max instances: 1 ] 21  < h > or < -h > will print this message [ max instances: 1 ] 22  23  Additionally, one or more of the following arguments may be used: 24  < -d > will enable the logger, useful for debugging [ max instances: 1 ] 25  < -local > will force the server to bind to the loopback address of the machine [ max instances: 1 ] 26  < -port: > followed (without using spaces) by the port the server will be listening to [ max instances: 1 ] 27  28  Here are some examples of argument combinations: 29  -d -local -port80 s [starts the server binding it to 127.0.0.1:80 and enables the logger] 30  -port:420 s [starts the server binding it to 0.0.0.0:420] 31  -d g [starts the GUI enabling the logger] 32  c [starts the CLI] 33  -local s g g c [starts a local server (127.0.0.1:8080) along side 2 GUIs and 1 CLI] 34  """; 35  36  /** 37  * When fed the proper cli inputs, starts the appropriate elements of the project 38  * 39  * @param args the program's arguments 40  */ 41  public static void main(String... args) throws UnknownHostException { 42  InetAddress serverBinding = InetAddress.getByAddress(new byte[]{0, 0, 0, 0}); 43  int serverPort = 8080; 44  // if the args are coherent 45  if (args.length >= 1 && 46  Arrays.stream(args).anyMatch(arg -> arg.equals("s") || arg.equals("g") || arg.equals("c") || arg.equals("h") || arg.equals("-h")) && 47  Arrays.stream(args).filter(arg -> arg.equals("s")).count() <= 1 && 48  Arrays.stream(args).filter(arg -> arg.equals("c")).count() <= 1 && 49  Arrays.stream(args).filter(arg -> arg.equals("h") || arg.equals("-h")).count() <= 1 && 50  Arrays.stream(args).filter(arg -> arg.equals("-d")).count() <= 1 && 51  Arrays.stream(args).filter(arg -> arg.equals("-local")).count() <= 1 && 52  Arrays.stream(args).filter(arg -> arg.startsWith("-port:")).count() <= 1 53  ) { // parse arguments 54  for (String arg : args) { 55  switch (arg.trim().toLowerCase()) { 56  case "-d" -> Logger.enable(true); 57  case "-local" -> serverBinding = InetAddress.getLoopbackAddress(); 58  case String a && a.startsWith("-port:") -> serverPort = Integer.parseInt(a.substring(6)); 59  case default -> { 60  } 61  } 62  } 63  for (String arg : args) { 64  switch (arg.trim().toLowerCase()) { 65  case "h", "-h" -> System.out.println(HELP_MESSAGE); 66  case "c" -> new Thread(new CLI()).start(); 67  case "g" -> new Thread(new GUI()).start(); 68  case "s" -> new Thread(new WelcomeServer(serverPort, serverBinding)).start(); 69  case default -> { 70  } 71  } 72  } 73  } else { 74  System.out.println(HELP_MESSAGE); 75  } 76  } 77 }