Install this package:
$ jget get rotnetz
Client API Documentation
This document describes the client API for making remote procedure calls to hosts. For a detailed explanation of how responses work and the overall message flow, see the Networking Architecture document.
Overview
The client API provides methods for making remote procedure calls (RPC) to hosts. There are two main modes of operation:
- Synchronous: Blocks while waiting for a response
- Asynchronous: Sends request and receives response later via a queue
Response Handling
Understanding Responses
All responses from hosts follow the same structure:
Synchronous Response Flow
When making synchronous calls using remoteCall(), the flow is:
- Client sends request to host
- Host sends "ack" acknowledgment
- Client blocks and waits for response
- Host processes request and sends response
- Client receives response and unblocks
Example:
Asynchronous Response Flow
For asynchronous operations, you must set up a receiver thread before making async calls:
- Write programm which makes async calls using
remoteCallAsync()and gets answers viagetFromQueue() - Create the Reciever Thread
- Start both via
parallel.waitForAll() - Host processes request and sends response to the async protocol
- Receiver thread enqueues the response
- Poll the queue to get responses when ready
Example:
Timeout Management
- Set appropriate timeouts based on expected response time
- Handle timeout errors gracefully
- Consider using async calls for long-running operations
Complete Workflow Examples
Synchronous RPC Workflow
Asynchronous RPC Workflow with Multiple Requests
Broadcast and Pick One Pattern
Constructor
Client(networkMask)
Creates a new client instance.
Parameters:
networkMask(string, optional): Prefix for protocol names. Defaults to empty string.
Returns:
client(table): Client instance with the following methods:
Properties:
directProtocol(string): Protocol for direct communicationnetworkMask(string): Network mask usedreceiveProtocol(string): Protocol for receiving responsesreceiveProtocolAsync(string): Protocol for async responses
Methods
receive(protocol, timeout)
Receives messages from the network.
Parameters:
protocol(string): Protocol to listen ontimeout(number): Maximum time to wait in seconds
Returns:
state(boolean): Success/failure indicatorresult(various): Result data or error messagesid(number): Sender ID (if successful)
remoteCallAsync(methodName, params, receiverId, protocol, receiveProtocol)
Asynchronously calls a remote method.
Parameters:
methodName(string): Name of the method to callparams(table): Parameters to pass to the methodreceiverId(number): ID of the host to callprotocol(string, optional): Protocol to usereceiveProtocol(string, optional): Protocol for responses
Returns:
state(boolean): Success/failure indicatorresult(string): "ack" if acknowledged, or error message
remoteCall(protocol, receiverId, methodName, params, timeout)
Synchronously calls a remote method.
Parameters:
protocol(string): Protocol to usereceiverId(number): ID of the host to callmethodName(string): Name of the method to callparams(table): Parameters to pass to the methodtimeout(number, optional): Timeout in seconds. Defaults to 0 (infinite)
Returns:
state(boolean): Success/failure indicatorresult(various): Method result or error message
getReceiverThread(maxSize)
Gets a thread function for receiving async messages.
Parameters:
maxSize(number, optional): Maximum queue size. Defaults to 100
Returns:
receiverThread(function): Thread function that processes incoming messages
getFromQueue()
Retrieves messages from the queue.
Returns:
state(boolean): Success/failure indicatorresult(various): Message data or error messagesid(number): Sender ID
broadcast(methodName, params, protocol, timeout)
Broadcasts a message to all hosts.
Parameters:
methodName(string): Name of the method to callparams(table): Parameters to passprotocol(string, optional): Protocol to usetimeout(number): Maximum time to wait for responses
Returns:
responses(table): Array of responses from hosts- Each response contains:
message(various): Response messagesenderId(number): ID of responding host
- Each response contains:
broadcastAsync(methodName, params, protocol, receiveProtocol)
Asynchronously broadcasts a message.
Parameters:
methodName(string): Name of the method to callparams(table): Parameters to passprotocol(string, optional): Protocol to usereceiveProtocol(string, optional): Protocol for responses
pickOneAndRemoteCallAsync(protocol, methodName, params, receiveProtocol)
Selects one host and calls a method asynchronously.
Parameters:
protocol(string): Protocol to usemethodName(string): Name of the method to callparams(table): Parameters to passreceiveProtocol(string, optional): Protocol for responses
Returns:
state(boolean): Success/failure indicatorreceiverId(number): ID of the selected host (if successful)
pickOneAndRemoteCall(protocol, methodName, params, timeout)
Selects one host and calls a method synchronously.
Parameters:
protocol(string): Protocol to usemethodName(string): Name of the method to callparams(table): Parameters to passtimeout(number, optional): Timeout in seconds. Defaults to 0
Returns:
state(boolean): Success/failure indicatorresult(various): Method result or error messagereceiverId(number): ID of the selected host (if successful)
pickOneAndRemoteCallRetry(methodName, params, protocol, sleepTime)
Retries picking a host and calling a method until successful.
Parameters:
methodName(string): Name of the method to callparams(table): Parameters to passprotocol(string): Protocol to usesleepTime(number, optional): Sleep time between retries. Defaults to 120
Returns:
state(boolean): Success indicatorresult(various): Method result