Class TcpClient

java.lang.Object
it.polimi.ingsw.communication.tcp_client.TcpClient
Direct Known Subclasses:
SugarClient

public abstract class TcpClient extends Object
  • Field Details

    • hostname

      private final String hostname
    • port

      private final int port
    • socket

      private Socket socket
    • logStream

      private PrintStream logStream
    • logHeader

      private String logHeader
  • Constructor Details

    • TcpClient

      public TcpClient(String hostname, int port)
  • Method Details

    • run

      public final void run() throws DisconnectionException
      Throws:
      DisconnectionException
    • inputHandler

      private void inputHandler() throws DisconnectionException
      Handles all incoming input from the socket
      Throws:
      DisconnectionException
    • readLine

      private String readLine(BufferedReader in) throws IOException
      Throws:
      IOException
    • onConnect

      protected abstract void onConnect()
      Subclasses override this method to make it behave as they wish.
    • onDisconnect

      protected abstract void onDisconnect()
      Subclasses override this method to make it behave as they wish.
    • onMessage

      protected abstract void onMessage(String input)
      Subclasses override this method to make it behave as they wish.
      Parameters:
      input - a string received from the socket
    • send

      protected final void send(String message) throws IOException
      Delivers message to the server
      Parameters:
      message - message to deliver
      Throws:
      IOException - if message could not be delivered
    • setLogStream

      public final void setLogStream(PrintStream logStream)
    • setLogHeader

      public final void setLogHeader(String logHeader)
    • log

      protected void log(String message)
      Logs the message to the current logStream
      Parameters:
      message - any log message
    • disconnect

      protected void disconnect() throws DisconnectionException
      Throws:
      DisconnectionException