Class TcpClient

  • All Implemented Interfaces:
    Closeable, AutoCloseable, Runnable

    public class TcpClient
    extends Client
    implements Closeable
    Provides a TCP client for sending and receiving Agent instances over a TCP connection.

    A TCP client manages the connection lifecycle, sends asynchronous and synchronous agents, receives agents and replies, and dispatches received agents to registered API implementations. Connection behavior, socket options, serialization, security, and other client settings are configured through TcpClientParameters.

    Synchronous agents may wait for a corresponding Reply, while asynchronous agents are sent without waiting for a reply. The client also supports automatic reconnection when configured to do so.

    Author:
    Charles B. Kozoyed
    See Also:
    TcpClientParameters, TcpServer
    • Constructor Detail

      • TcpClient

        public TcpClient​(TcpClientParameters parameters)
                  throws com.kristensoft.kdc.license.runtime.LicenseException,
                         KristenCommsException
        Creates a TCP client using the specified configuration.

        The client initializes its TCP agent queue and, if automatic startup is enabled, starts the client and attempts to establish a connection. If latency metrics are enabled, the associated metric accumulators are also started.

        Parameters:
        parameters - the parameters used to configure this TCP client
        Throws:
        com.kristensoft.kdc.license.runtime.LicenseException - if the required KDC license cannot be validated
        KristenCommsException - if an error occurs while initializing the client
    • Method Detail

      • getTcpClientParameters

        public TcpClientParameters getTcpClientParameters()
        Returns the parameters used to configure this TCP client.
        Returns:
        the TCP client parameters
      • sendSync

        public Reply sendSync​(Agent agent)
                       throws IOException,
                              KristenCommsException
        Sends the specified agent synchronously and waits for its corresponding reply.

        The agent is marked as synchronous, serialized, and sent over the TCP connection. This method then blocks until a reply with the matching agent UUID is received or the configured synchronous reply timeout expires. Replies for other agents are preserved for subsequent processing.

        Latency metrics for serialization, sending, reply waiting, receiving, and deserialization are recorded when latency metrics are enabled.

        Parameters:
        agent - the agent to send
        Returns:
        the reply corresponding to the sent agent
        Throws:
        IllegalArgumentException - if agent is null
        IOException - if an I/O error occurs while sending the agent
        KristenCommsException - if the client is not connected, the reply wait is interrupted, serialization or communication fails, or the reply timeout expires
      • connect

        public final void connect()
        Connects this client to the configured remote TCP endpoint.

        If the client is already connected or a connection attempt is already in progress, this method returns without starting another connection attempt. Otherwise, a socket is created, the configured socket options are applied, the socket is bound to the configured local address, and a connection to the remote address is attempted.

        If automatic reconnection is enabled, failed connection attempts are retried using the configured reconnect delay while the client remains running and the connection attempt has not been aborted. This method blocks until a connection is established or connection attempts stop.

      • isConnected

        public boolean isConnected()
        Returns whether this client is currently connected to the remote TCP endpoint.
        Returns:
        true if the client is connected; false otherwise
      • isConnecting

        public boolean isConnecting()
        Returns whether this client is currently attempting to establish a TCP connection.
        Returns:
        true if a connection attempt is in progress; false otherwise
      • abortConnect

        public void abortConnect()
        Aborts a TCP connection attempt that is currently in progress.

        If a connection attempt is in progress, the attempt is stopped, the client is closed, and the aborted connection attempt is reported to the appropriate event handlers. If no connection attempt is in progress, this method has no effect.

      • close

        public final void close()
        Closes this TCP client and releases its connection resources.

        The input stream, output stream, and socket are closed when present. Any I/O errors encountered while closing these resources are logged rather than propagated. After the socket is closed, the client is marked as disconnected.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Specified by:
        close in class Client
      • isRunning

        public boolean isRunning()
        Determines whether this communication entity is currently running.
        Returns:
        true if this communication entity is running; false otherwise
      • flushMetrics

        public void flushMetrics()
        Flushes the accumulated latency metrics for this communication entity.

        This method may be used to report any currently accumulated metrics without waiting for the configured latency reporting interval.

      • getAgentSerializer

        public AgentSerializer getAgentSerializer()
        Returns the agent serializer used by this communication entity.

        The serializer may be used to inspect or modify serialization settings at runtime, including compression and encryption settings.

        Returns:
        the agent serializer used by this communication entity