Class ClientThread

java.lang.Object
java.lang.Thread
nl.andrewl.concord_server.client.ClientThread
All Implemented Interfaces:
Runnable

public class ClientThread extends Thread
This thread is responsible for handling the connection to a single client of a server. The client thread acts as the server's representation of a client.
  • Field Details

    • socket

      private final Socket socket
    • in

      private InputStream in
    • out

      private OutputStream out
    • server

      private final ConcordServer server
    • clientId

      private UUID clientId
    • clientNickname

      private String clientNickname
    • currentChannel

      private Channel currentChannel
    • running

      private volatile boolean running
  • Constructor Details

    • ClientThread

      public ClientThread(Socket socket, ConcordServer server) throws IOException
      Constructs a new client thread.
      Parameters:
      socket - The socket to use to communicate with the client.
      server - The server to which this thread belongs.
      Throws:
      IOException - If we cannot obtain the input and output streams from the socket.
  • Method Details

    • sendToClient

      public void sendToClient(nl.andrewl.concord_core.msg.Message message)
      Sends the given message to the client. Note that this method is synchronized, such that multiple messages cannot be sent simultaneously.
      Parameters:
      message - The message to send.
    • sendToClient

      public void sendToClient(byte[] bytes)
      Sends the given bytes to the client. This is a shortcut for sendToClient(Message) which can be used to optimize message sending in certain instances.
      Parameters:
      bytes - The bytes to send.
    • shutdown

      public void shutdown()
      Shuts down this client thread, closing the underlying socket and setting running to false so that the main thread loop will exit shortly.
    • run

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

      private boolean identifyClient()
      Initial method that attempts to obtain identification information from a newly-connected client. It is the intent that we should close the socket if the client is not able to identify itself.
      Returns:
      True if we were able to obtain identification from the client, or false otherwise.
    • establishEncryption

      private boolean establishEncryption()
      Tries to establish an encrypted connection with a client. This should be the first thing which is called upon to interact with the client, because it assumes that the client is also attempting to establish a secure connection as soon as it opens its socket.
      Returns:
      True if an encrypted connection could be established, or false otherwise.
    • toData

      public nl.andrewl.concord_core.msg.types.UserData toData()
    • toString

      public String toString()
      Overrides:
      toString in class Thread