Class RpcClient
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRpcClient(WorkerRegistry workerRegistry) Constructs a newRpcClientinstance. -
Method Summary
Modifier and TypeMethodDescriptionsendRequest(String host, int port, byte opCode, String payload) Sends a request to a specified host and port with a given operation code and payload.sendRequest(String host, int port, String payload) Sends a request to a specified host and port with a default operation code.
-
Field Details
-
workerRegistry
WorkerRegistry workerRegistry
-
-
Constructor Details
-
RpcClient
Constructs a newRpcClientinstance.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- TheWorkerRegistryto associate with this client.
-
-
Method Details
-
sendRequest
Sends a request to a specified host and port with a default operation code.This method defaults to using
TitanProtocol.OP_SUBMIT_JOBas 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, ornullif an IO error occurs indicating a potential network issue or dead worker. - See Also:
-
sendRequest
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 usingTitanProtocol.read(DataInputStream).If the server returns an error (
TitanProtocol.OP_ERROR), an error message is logged and returned. In case of anIOException, it indicates a network problem or a dead server/worker, andnullis 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, ornullif an IO error occurs indicating a potential network issue or dead worker.
-