Class Server

java.lang.Object
java.lang.Thread
it.polimi.ingsw.eriantys.server.Server
All Implemented Interfaces:
Runnable

public class Server extends Thread
This class represents the application server thread. It handles all the clients, instantiating ClientConnection objects for each of them, and exposes various methods
  • Field Details

  • Constructor Details

    • Server

      public Server(int port) throws IOException
      Constructs a Server object, starting the ServerSocket.
      Parameters:
      port - the port number on which the Server will run
      Throws:
      IOException - if the ServerSocket could not be opened
  • Method Details

    • main

      public static void main(String[] args)
      The method is called to launch the server-side application.
      Parameters:
      args - the command line arguments
    • run

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • connect

      public void connect(String username, ClientConnection connection)
      Handles a user connection by processing a Handshake communication item.
      Parameters:
      username - the username requested by the client
      connection - a reference to the client connection instance
    • reconnect

      public void reconnect(String username, int gameId, String passcode, ClientConnection connection)
      Handles a user reconnection by processing a Reconnect communication item.
      Parameters:
      username - the username which has been chosen by the client prior to its disconnection
      gameId - the identifier of the game inside which the client was playing prior to its disconnection
      passcode - the hexadecimal code which has been associated with the client by the disconnection handling infrastructure
      connection - a reference to the client connection instance
    • disconnect

      public void disconnect(ClientConnection connection)
      Handles a user disconnection, causing an update on the internal state of the class.
      Parameters:
      connection - a reference to the client connection instance
    • isConnected

      public boolean isConnected(String username)
      A method to know if a user with a specified identifier exists or not.
      Parameters:
      username - the target user
      Returns:
      true if and only if a ClientConnection exists for the target username
    • getConnection

      public ClientConnection getConnection(String username) throws NoConnectionException
      A getter for the client connection instance of a user.
      Parameters:
      username - the target user
      Returns:
      a reference to the client connection
      Throws:
      NoConnectionException - if no connection can be retrieved for the target player
    • handleMessage

      public void handleMessage(ConnectionMessage message) throws NoConnectionException
      Handles a user request by processing a ConnectionMessage communication item.
      Parameters:
      message - the target message to process
      Throws:
      NoConnectionException - if no connection can be retrieved for the target player
    • sendHelp

      public void sendHelp(HelpRequest helpRequest) throws NoConnectionException
      Handles the processing of a HelpRequest communication item.
      Parameters:
      helpRequest - the target request to process
      Throws:
      NoConnectionException - if no connection can be retrieved for the target player
    • gameOver

      public void gameOver(Game game, List<String> players)
      Processes the server-side end of the game.
      Parameters:
      game - the identifier of the game which has been ended
      players - the list of usernames who played the game