Class BaseClient

java.lang.Object
com.example.client.app.BaseClient
All Implemented Interfaces:
Client

public class BaseClient extends Object implements Client
The BaseClient class implements the Client interface and represents a basic client working with a chat application.
  • Constructor Summary

    Constructors
    Constructor
    Description
    BaseClient(int clientPort, MessageSender messageSender, StreamIO streamIO, Listener listener)
    Constructs a BaseClient with the specified client port, message sender, stream IO, and listener.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    login(String nickname)
    Logs in with the specified nickname and client port.
    void
    Logs out and stops the client by sending an exit command and interrupting the listener.
    void
    Sends a message to the server with the client's nickname and message content.
    void
    Starts the work of the client, including setting up the listener and reading user input.
    void
    Launch the input message listener

    Methods inherited from class java.lang.Object

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

    • BaseClient

      public BaseClient(int clientPort, MessageSender messageSender, StreamIO streamIO, Listener listener)
      Constructs a BaseClient with the specified client port, message sender, stream IO, and listener.
      Parameters:
      clientPort - the port number of the client
      messageSender - the message sender for sending messages
      streamIO - the stream IO interface for input-output processing
      listener - the listener for input messages from the thread
  • Method Details

    • start

      public void start()
      Starts the work of the client, including setting up the listener and reading user input.
      Specified by:
      start in interface Client
    • login

      public boolean login(String nickname) throws IOException
      Logs in with the specified nickname and client port.
      Specified by:
      login in interface Client
      Parameters:
      nickname - the nickname of the client
      Returns:
      true if the login is successful, false otherwise
      Throws:
      IOException - if an error occurs during login
    • logoutAndStop

      public void logoutAndStop() throws IOException
      Logs out and stops the client by sending an exit command and interrupting the listener.
      Specified by:
      logoutAndStop in interface Client
      Throws:
      IOException - if an error occurs during logout
    • sendMessage

      public void sendMessage(String message) throws IOException
      Sends a message to the server with the client's nickname and message content.
      Specified by:
      sendMessage in interface Client
      Parameters:
      message - the message content to send
      Throws:
      IOException - if an error occurs during message sending
    • startListener

      public void startListener()
      Launch the input message listener
      Specified by:
      startListener in interface Client