Package client

Class ConnectionHandler

java.lang.Object
client.ConnectionHandler

public class ConnectionHandler extends Object
  • Constructor Details

    • ConnectionHandler

      public ConnectionHandler(String ip_address, String port)
      Constructor for ConnectionHandler. Initializes the connection handler with the specified IP address and port for server communication.
      Parameters:
      ip_address - The IP address of the server to which the connection handler will communicate for HTTP requests.
      port - The port number on which the server is listening for HTTP requests. This, combined with the IP address, will be used to construct the base URL for all server interactions.
  • Method Details

    • Get_Chats

      public static ArrayList<String> Get_Chats(User user)
      Retrieves a list of chat names that the specified user is a member of. Makes a GET request to the server with the user's name and parses the response to return an ArrayList of chat names. If the server response is not successful, it returns an empty ArrayList.
      Parameters:
      user - The User object representing the user for whom the chat list is to be retrieved. The user's name is used in the GET request to the server to identify which chats to return.
      Returns:
      An ArrayList of strings, where each string is the name of a chat that the specified user is a member of. If the server response is not successful (i.e., status code is not 200), it returns an empty ArrayList.
    • Get_Chat

      public static Chat Get_Chat(String chat)
      Retrieves a Chat object from the server based on the provided chat name. Makes a GET request to the server and parses the response to create a Chat object. Additionally, it verifies that all images in the messages of the chat are downloaded locally by checking if they exist in the "resources" directory and downloading them if they do not exist.
      Parameters:
      chat - The name of the chat to be retrieved from the server.
      Returns:
      A Chat object containing the chat information, messages, and users. If the server response is not successful, it returns a Chat object with only the chat name and empty messages and users.
    • Connect

      public static void Connect(User user, String chat)
      Handles the connection of a user to a chat. Constructs a JSON payload containing the user and chat information, and makes a POST request to the server to notify it of the new connection. The server is expected to add the user to the specified chat and update its records accordingly.
      Parameters:
      user - The user to be connected to the chat.
      chat - The name of the chat to which the user is to be connected.
    • Disconnect

      public static void Disconnect(User user, String chat)
      Handles the disconnection of a user from a chat. Constructs a JSON payload containing the user and chat information, and makes a POST request to the server to notify it of the disconnection. The server is expected to remove the user from the specified chat and update its records accordingly.
      Parameters:
      user - The user to be disconnected from the chat.
      chat - The name of the chat from which the user is to be disconnected.
    • Send_Message

      public static void Send_Message(Message message, String chat)
      Sends a message to the server within a specific chat. Constructs a JSON payload containing the message and chat information, and makes a POST request to the server to send the message. The server is expected to handle the message and distribute it to other users in the chat.
      Parameters:
      message - The message object to be sent to the server.
      chat - The name of the chat in which the message is to be sent.
    • Get_Image

      public static void Get_Image(String hash)
      Retrieves an image from the server based on the provided hash. If the image already exists locally in the "resources" directory, it does nothing. Otherwise, it makes a GET request to the server to retrieve the image data, decodes it from Base64, and saves it to the "resources" directory with the filename as the hash.
      Parameters:
      hash - The hash string representing the image to be retrieved from the server. This hash is used to check if the image already exists locally and to request the image from the server if it does not exist.
    • Send_Image

      public static String Send_Image(String image_path)
      Sends an image to the server by reading the image file, encoding it in Base64, and making a POST request with the encoded image as the payload. The server is expected to save the image and return a hash that can be used to retrieve the image later.
      Parameters:
      image_path - The file path of the image to be sent to the server.
      Returns:
      The hash string returned by the server that can be used to retrieve the image, or null if an error occurs during the process.