Package titan.network

Class RpcClient

java.lang.Object
titan.network.RpcClient

public class RpcClient extends Object
RpcClient provides functionality for making Remote Procedure Calls (RPC) to a server. It handles the underlying network communication, including connecting to a host and port, sending requests using the TitanProtocol, and receiving responses.

This client is designed to interact with servers that implement the TitanProtocol for data exchange, supporting various operation codes and payloads.

  • Field Details

  • Constructor Details

    • RpcClient

      public RpcClient(WorkerRegistry workerRegistry)
      Constructs a new RpcClient instance.

      This constructor initializes the client with a WorkerRegistry, which might be used for internal client-side worker management or lookup, though its direct usage is not explicitly shown in the provided methods.

      Parameters:
      workerRegistry - The WorkerRegistry to associate with this client.
  • Method Details

    • sendRequest

      public String sendRequest(String host, int port, String payload)
      Sends a request to a specified host and port with a default operation code.

      This method defaults to using TitanProtocol.OP_SUBMIT_JOB as the operation code. It is a convenience method for common operations like submitting jobs where the specific operation code might be implicit or less critical than the payload itself.

      Parameters:
      host - The hostname or IP address of the server to connect to.
      port - The port number on the server to connect to.
      payload - The string payload to send as part of the request.
      Returns:
      The response payload from the server as a String, or an error message prefixed with "ERROR:" if the server returns an error, or null if an IO error occurs indicating a potential network issue or dead worker.
      See Also:
    • sendRequest

      public String sendRequest(String host, int port, byte opCode, String payload)
      Sends a request to a specified host and port with a given operation code and payload.

      This is the primary method for sending RPC requests. It establishes a socket connection, sets a read timeout, sends the request using TitanProtocol.send(DataOutputStream, byte, String), and then waits for and reads the server's response using TitanProtocol.read(DataInputStream).

      If the server returns an error (TitanProtocol.OP_ERROR), an error message is logged and returned. In case of an IOException, it indicates a network problem or a dead server/worker, and null is returned. Other exceptions result in a client-side error message.

      Parameters:
      host - The hostname or IP address of the server to connect to.
      port - The port number on the server to connect to.
      opCode - The operation code (e.g., TitanProtocol.OP_SUBMIT_JOB) indicating the type of request.
      payload - The string payload to send as part of the request.
      Returns:
      The response payload from the server as a String, or an error message prefixed with "ERROR:" if the server returns an error, or null if an IO error occurs indicating a potential network issue or dead worker.