Class XTablesClient
- All Implemented Interfaces:
Subscriptions
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final byte[]
static final com.google.protobuf.ByteString
static final byte[]
static final com.google.protobuf.ByteString
static final String
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for XTablesClient.XTablesClient
(String ip) Constructor for XTablesClient with a specified IP address.XTablesClient
(String ip, int pushSocketPort, int requestSocketPort, int subscribeSocketPort) Constructor for XTablesClient with a specified IP address and ports. -
Method Summary
Modifier and TypeMethodDescriptionaddVersionProperty
(String prop) Adds a custom property to the version string.org.zeromq.ZContext
Retrieves the ZMQ context associated with this client.static XTablesClientManager
Asynchronously retrieves the default XTablesClientManager instance.getIp()
Retrieves the IP address of the XTABLES server.int
Retrieves the PUSH socket.org.zeromq.ZMQ.Socket
Retrieves the PUSH socket for registry.int
Retrieves the REQ (Request) socket port.Retrieves the socket monitor.int
Retrieves the port number used for the SUB (Subscriber) socket.org.zeromq.ZMQ.Socket
Retrieves the SUB (Subscriber) socket.Returns the version information of the XTables client.Registers a new threaded XTableContext associated with the given key.Retrieves or creates an XTableContext associated with the specified key.void
shutdown()
Gracefully shuts down the XTablesClient.void
Shuts down and removes an XTableContext associated with the given key.boolean
subscribe
(String key, Consumer<XTableProto.XTableMessage.XTableUpdate> consumer) Subscribes to a specific key and associates a consumer to process updates for that key.boolean
Subscribes to updates for all keys and associates a consumer to process updates for all keys.boolean
Subscribes a consumer-to-server log update.boolean
unsubscribe
(String key, Consumer<XTableProto.XTableMessage.XTableUpdate> consumer) Unsubscribes a specific consumer from a given key.boolean
Unsubscribes a specific consumer from all keys.boolean
Unsubscribes a consumer from server log updates.Methods inherited from class org.kobe.xbot.Utilities.Entities.QueuedRequests
publish, sendBatchedPushRequests, sendPutMessage, setHandlers, setPushHandler
Methods inherited from class org.kobe.xbot.Utilities.Entities.Requests
_getXTablesDataProto, delete, delete, getBoolean, getBooleanList, getBytes, getBytesList, getCoordinates, getDouble, getDoubleList, getFloatList, getInteger, getIntegerList, getLong, getLongList, getPing, getPose2d, getPose3d, getRawBytes, getRawJson, getServerStatistics, getString, getStringList, getTables, getTables, getUnknownBytes, putBoolean, putBooleanList, putBytes, putBytes, putBytesList, putCoordinates, putDouble, putDoubleList, putFloatList, putInteger, putIntegerList, putLong, putLongList, putPose2d, putPose3d, putString, putStringList, putTypedBytes, putUnknownBytes, reboot, reconnectRequestSocket, set, set, setServerDebug, setSockets
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.kobe.xbot.Utilities.Entities.Subscriptions
subscribe
-
Field Details
-
UUID
-
success
public static final byte[] success -
fail
public static final byte[] fail -
successByte
public static final com.google.protobuf.ByteString successByte -
failByte
public static final com.google.protobuf.ByteString failByte -
subscriptionConsumers
public final Map<String,List<Consumer<XTableProto.XTableMessage.XTableUpdate>>> subscriptionConsumers -
logConsumers
-
-
Constructor Details
-
XTablesClient
public XTablesClient()Default constructor for XTablesClient. Initializes the client without specifying an IP address. -
XTablesClient
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
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
- Specified by:
subscribe
in interfaceSubscriptions
-
subscribe
- Specified by:
subscribe
in interfaceSubscriptions
-
registerXTableContext
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
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
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
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 interfaceSubscriptions
- Parameters:
consumer
- a Consumer function to handle incoming server log messages- Returns:
- true if the subscription was successful, false otherwise
-
unsubscribeToServerLogs
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 interfaceSubscriptions
- Parameters:
consumer
- a Consumer function previously registered to handle server log messages- Returns:
- true if the consumer was removed successfully, false otherwise
-
subscribe
Subscribes to a specific key and associates a consumer to process updates for that key.- Specified by:
subscribe
in interfaceSubscriptions
- 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
Subscribes to updates for all keys and associates a consumer to process updates for all keys.- Specified by:
subscribe
in interfaceSubscriptions
- Parameters:
consumer
- The consumer function that processes updates for any key.- Returns:
- true if the subscription and consumer addition were successful, false otherwise.
-
unsubscribe
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 interfaceSubscriptions
- 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
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 interfaceSubscriptions
- 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
Returns the version information of the XTables client.- Returns:
- A string containing the version details.
-
addVersionProperty
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
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
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
Asynchronously retrieves the default XTablesClientManager instance. This allows for non-blocking initialization of the client manager.- Returns:
- A new instance of XTablesClientManager.
-