Class ClientConnection

java.lang.Object
it.polimi.ingsw.eriantys.server.ClientConnection

public class ClientConnection extends Object
This class represents a single client-server connection. It exposes methods which allow to read from and write to the connection's I/O streams.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ClientConnection(Server server, Socket socketToClient)
    Constructs a new instance of ClientConnection with the specified parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    A getter for the game which this refers to.
    boolean
    A getter to know if the client is inside a game lobby.
    void
    Sends a Ping message to the client approximately every 2.5 seconds in order to ensure that the connection is working, disconnecting the client if an I/O error occurs.
    void
    Continuously checks for new messages being sent by the client through the connection socket's input stream and handles them according to the game phase, disconnecting the client if an I/O error occurs or if the client is unresponsive to ping messages.
    void
    setGame(Game game)
    Sets the connection's game reference to the specified Game.
    void
    setJoinedLobby(boolean joinedLobby)
    Sets the joinedLobby member variable to the specified value.
    void
    setRunning(boolean running)
    Sets the running member variable to the specified value.
    void
    write(Message message)
    Writes the specified Message to the connection socket's output stream, disconnecting the client if an I/O error occurs.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ClientConnection

      public ClientConnection(Server server, Socket socketToClient) throws IOException
      Constructs a new instance of ClientConnection with the specified parameters.
      Parameters:
      server - the game server
      socketToClient - the socket between the server and this connection's client
      Throws:
      IOException - if an error occurs when retrieving the input or output stream
  • Method Details

    • setRunning

      public void setRunning(boolean running)
      Sets the running member variable to the specified value.
      Parameters:
      running - the desired value
    • getGame

      public Game getGame()
      A getter for the game which this refers to.
      Returns:
      the game which the connection refers to (if there is one), or null otherwise.
    • setGame

      public void setGame(Game game)
      Sets the connection's game reference to the specified Game.
      Parameters:
      game - the connection's new Game reference
    • hasJoinedLobby

      public boolean hasJoinedLobby()
      A getter to know if the client is inside a game lobby.
      Returns:
      true if and only if the client associated with this has joined a game lobby
    • setJoinedLobby

      public void setJoinedLobby(boolean joinedLobby)
      Sets the joinedLobby member variable to the specified value.
      Parameters:
      joinedLobby - the desired value
    • read

      public void read()
      Continuously checks for new messages being sent by the client through the connection socket's input stream and handles them according to the game phase, disconnecting the client if an I/O error occurs or if the client is unresponsive to ping messages.
    • write

      public void write(Message message)
      Writes the specified Message to the connection socket's output stream, disconnecting the client if an I/O error occurs.
      Parameters:
      message - the message to be written and sent to the client
    • ping

      public void ping()
      Sends a Ping message to the client approximately every 2.5 seconds in order to ensure that the connection is working, disconnecting the client if an I/O error occurs.