Class XTablesClient

All Implemented Interfaces:
Subscriptions

public class XTablesClient extends QueuedRequests implements Subscriptions
XTablesClient - A client class for interacting with the XTABLES server.

This class is responsible for client-side functionality in the XTABLES system, including resolving the server's IP address, handling versioning information, and providing socket initialization for communication with the XTABLES server. It manages connections and provides utility methods for interacting with the server.

Author: Kobe Lei Version: 1.0 Package: XTABLES

This is part of the XTABLES project and provides client-side functionality for socket communication with the server.

  • Field Details

  • Constructor Details

    • XTablesClient

      public XTablesClient()
      Default constructor for XTablesClient. Initializes the client without specifying an IP address.
    • XTablesClient

      public XTablesClient(String ip)
      Constructor for XTablesClient with a specified IP address. Initializes the client with the given IP address.

      If the IP address is not provided (i.e., it's null), the client will attempt to automatically resolve the IP address of the XTABLES server using mDNS (Multicast DNS).

      The following default ports are used for socket communication: - Push socket port: 48800 - Request socket port: 48801 - Subscribe socket port: 48802

      Parameters:
      ip - The IP address of the XTABLES server. If null, it will resolve the IP automatically.
    • XTablesClient

      public XTablesClient(String ip, int pushSocketPort, int requestSocketPort, int subscribeSocketPort)
      Constructor for XTablesClient with a specified IP address and ports. Initializes the client with the given parameters.
      Parameters:
      ip - The IP address of the XTABLES server.
      pushSocketPort - The port for the push socket.
      requestSocketPort - The port for the request socket.
      subscribeSocketPort - The port for the subscriber socket.
  • Method Details

    • subscribe

      public CachedSubscriber subscribe(String key)
      Specified by:
      subscribe in interface Subscriptions
    • subscribe

      public CachedSubscriber subscribe(String key, int queue)
      Specified by:
      subscribe in interface Subscriptions
    • registerXTableContext

      public XTableContext registerXTableContext(String key)
      Retrieves or creates an XTableContext associated with the specified key. Each XTableContext maintains its own sockets for multi-threaded communication.
      Parameters:
      key - The unique identifier for the context.
      Returns:
      The XTableContext instance associated with the given key.
    • registerNewThreadedContext

      public XTableContext registerNewThreadedContext(String key)
      Registers a new threaded XTableContext associated with the given key. This is a wrapper around `getXTableContext` for convenience.
      Parameters:
      key - The unique identifier for the context.
      Returns:
      The newly created or retrieved XTableContext.
    • shutdownXTableContext

      public void shutdownXTableContext(String key)
      Shuts down and removes an XTableContext associated with the given key. If the context exists, it is closed and removed from the context map.
      Parameters:
      key - The unique identifier for the context to be shut down.
    • subscribeToServerLogs

      public boolean subscribeToServerLogs(Consumer<XTableProto.XTableMessage.XTableLog> consumer)
      Subscribes a consumer-to-server log update.

      This method registers a consumer to receive log updates from the server. It ensures the subscription to the appropriate log category via the subscription socket.

      Specified by:
      subscribeToServerLogs in interface Subscriptions
      Parameters:
      consumer - a Consumer function to handle incoming server log messages
      Returns:
      true if the subscription was successful, false otherwise
    • unsubscribeToServerLogs

      public boolean unsubscribeToServerLogs(Consumer<XTableProto.XTableMessage.XTableLog> consumer)
      Unsubscribes a consumer from server log updates.

      This method removes a previously registered consumer from receiving server log updates. If no more consumers are registered, it unsubscribes from the log category on the subscription socket.

      Specified by:
      unsubscribeToServerLogs in interface Subscriptions
      Parameters:
      consumer - a Consumer function previously registered to handle server log messages
      Returns:
      true if the consumer was removed successfully, false otherwise
    • subscribe

      public boolean subscribe(String key, Consumer<XTableProto.XTableMessage.XTableUpdate> consumer)
      Subscribes to a specific key and associates a consumer to process updates for that key.
      Specified by:
      subscribe in interface Subscriptions
      Parameters:
      key - The key to subscribe to.
      consumer - The consumer function that processes updates for the specified key.
      Returns:
      true if the subscription and consumer addition were successful, false otherwise.
    • subscribe

      public boolean subscribe(Consumer<XTableProto.XTableMessage.XTableUpdate> consumer)
      Subscribes to updates for all keys and associates a consumer to process updates for all keys.
      Specified by:
      subscribe in interface Subscriptions
      Parameters:
      consumer - The consumer function that processes updates for any key.
      Returns:
      true if the subscription and consumer addition were successful, false otherwise.
    • unsubscribe

      public boolean unsubscribe(String key, Consumer<XTableProto.XTableMessage.XTableUpdate> consumer)
      Unsubscribes a specific consumer from a given key. If no consumers remain for the key, it unsubscribes the key from the subscription socket.
      Specified by:
      unsubscribe in interface Subscriptions
      Parameters:
      key - The key to unsubscribe from.
      consumer - The consumer function to remove from the key's subscription.
      Returns:
      true if the consumer was successfully removed or the key was unsubscribed, false otherwise.
    • unsubscribe

      public boolean unsubscribe(Consumer<XTableProto.XTableMessage.XTableUpdate> consumer)
      Unsubscribes a specific consumer from all keys. If no consumers remain for all keys, it unsubscribes from the subscription socket for all keys.
      Specified by:
      unsubscribe in interface Subscriptions
      Parameters:
      consumer - The consumer function to remove from all key subscriptions.
      Returns:
      true if the consumer was successfully removed or unsubscribed from all keys, false otherwise.
    • shutdown

      public void shutdown()
      Gracefully shuts down the XTablesClient. - Destroys the ZMQ context if it exists and is not yet closed. - Interrupts and stops the subscription handler thread if it's active. - Logs the shutdown event for debugging and monitoring.
    • getVersion

      public String getVersion()
      Returns the version information of the XTables client.
      Returns:
      A string containing the version details.
    • addVersionProperty

      public String addVersionProperty(String prop)
      Adds a custom property to the version string.
      Parameters:
      prop - The property to be added to the version string.
      Returns:
      The updated version string with the new property.
    • getRequestSocketPort

      public int getRequestSocketPort()
      Retrieves the REQ (Request) socket port. The REQ socket is typically used for sending requests in a request-reply pattern.
      Returns:
      The ZMQ.Socket instance representing the REQ socket.
    • getPushSocketPort

      public int getPushSocketPort()
      Retrieves the PUSH socket. The PUSH socket is used for sending messages in a one-way pattern, usually to a PULL socket.
      Returns:
      The ZMQ.Socket instance representing the PUSH socket.
    • getRegsitrySocket

      public org.zeromq.ZMQ.Socket getRegsitrySocket()
      Retrieves the PUSH socket for registry. The PUSH socket is used for sending messages in a one-way pattern, usually to a PULL socket.
      Returns:
      The ZMQ.Socket instance representing the PUSH socket.
    • getSocketMonitor

      public XTablesSocketMonitor getSocketMonitor()
      Retrieves the socket monitor. The socket monitor is responsible for monitoring the state of the sockets, handling events such as connections, disconnections, and errors. It provides real-time updates about the status of each monitored socket.
      Returns:
      The XTablesSocketMonitor instance responsible for monitoring socket events.
    • getSubSocket

      public org.zeromq.ZMQ.Socket getSubSocket()
      Retrieves the SUB (Subscriber) socket. The SUB socket is used to subscribe to messages from a PUB (Publisher) socket.
      Returns:
      The ZMQ.Socket instance representing the SUB socket.
    • getSubscribeSocketPort

      public int getSubscribeSocketPort()
      Retrieves the port number used for the SUB (Subscriber) socket. This socket is used to receive updates from the XTABLES server.
      Returns:
      The port number of the subscription socket.
    • getIp

      public String getIp()
      Retrieves the IP address of the XTABLES server. This is either manually provided during initialization or resolved automatically.
      Returns:
      The IP address of the server as a string.
    • getContext

      public org.zeromq.ZContext getContext()
      Retrieves the ZMQ context associated with this client. The context manages sockets and handles networking resources.
      Returns:
      The ZContext instance used by this client.
    • getDefaultClientAsynchronously

      public static XTablesClientManager getDefaultClientAsynchronously()
      Asynchronously retrieves the default XTablesClientManager instance. This allows for non-blocking initialization of the client manager.
      Returns:
      A new instance of XTablesClientManager.