Coverage Summary for Class: WelcomeServer (it.polimi.ingsw.Server)

Class Class, % Method, % Line, %
WelcomeServer 0% (0/1) 0% (0/2) 0% (0/15)


1 package it.polimi.ingsw.Server; 2  3 import it.polimi.ingsw.Logger; 4 import it.polimi.ingsw.Network.KeepAliveSocketWrapper; 5 import it.polimi.ingsw.Network.SocketWrapper; 6 import it.polimi.ingsw.Server.Messages.ServerResponses.Welcome; 7  8 import java.io.IOException; 9 import java.net.InetAddress; 10 import java.net.ServerSocket; 11 import java.net.Socket; 12  13 /** 14  * This is the main server thread. <br> 15  * Expected behaviour: a thread runs forever, accepting all connections and dispatching the 16  * sockets to the {@link LobbyServer}. 17  */ 18 public class WelcomeServer implements Runnable { 19  private final ServerSocket serverSocket; 20  21  /** 22  * Create a new Welcome server, once run the server binds to an address and listens for connections 23  * 24  * @param port the port the server will bind to 25  * @param address the address the server will bind to 26  */ 27  public WelcomeServer(int port, InetAddress address) { 28  Logger.info("Starting Welcome Server on: " + address + ":" + port); 29  try { 30  this.serverSocket = new ServerSocket(port, 1000, address); 31  } catch (IOException e) { 32  throw new RuntimeException(e.getMessage()); 33  } 34  } 35  36  /** 37  * Used when running the server in a {@link Thread}, will make the server listen for connections, dispatching a 38  * {@link LobbyServer} for each new connection. 39  */ 40  @Override 41  public void run() { 42  Logger.info("Server initialized and listening for new connections"); 43  while (true) { 44  try { 45  Socket socket = serverSocket.accept(); 46  SocketWrapper sw = new KeepAliveSocketWrapper(socket, 5000, false); 47  Logger.info("New connection from: " + sw.getInetAddress()); 48  sw.sendMessage(new Welcome()); 49  LobbyServer.spawn(sw); 50  } catch (IOException e) { 51  Logger.severe("Caught an exception while awaiting new connections:\n" + e.getMessage()); 52  return; 53  } 54  } 55  } 56 }