Install this package:
$ jget get rotnetz
rotnetz Host API Documentation
This document describes the host API for creating network hosts that can receive and respond to remote procedure calls. For a detailed explanation of how requests are processed and the overall message flow, see the Networking Architecture document.
Request Processing
Request Lifecycle
When a host receives a request, the following sequence occurs:
- Receiver Thread receives the request message on the main protocol
- Acknowledgment is sent back via the direct protocol (for synchronous requests)
- Message is placed in the shared queue
- Handler Thread picks up the message from the queue
- Method Lookup - the requested method is found in the registered methods
- Execution - the method is executed with provided parameters using pcall
- Response is constructed with the result or error
- Response is sent back to the client via the receive protocol
Threading Model
Hosts use a multi-threaded architecture for better performance:
-
Receiver Thread(s): Listens on the main protocol and receives incoming requests
- Puts messages into a shared queue
- Sends "ack" for synchronous requests
-
Handler Thread(s): Processes messages from the queue
- Looks up and executes the requested method
- Constructs and sends responses
This separation allows the host to continue receiving requests even while processing previous ones.
Error Handling
Hosts automatically handle errors during method execution:
Best Practices
Method Design
- Keep methods fast - handler threads are blocking during method execution
- Handle your own errors - use try/catch or pcall to prevent crashes
- Validate inputs - check parameter types and values
- Provide good descriptions - document what each method does
- Return meaningful errors - include helpful error messages
Threading Considerations
- Use appropriate thread count - more threads for high load, fewer for simple cases
- Avoid long-running operations - they block handler threads
- Consider CPU usage - complex calculations may degrade performance
- Use async patterns - if methods need to wait, consider async approaches
Performance Tips
- Monitor queue size - growing queues indicate bottlenecks
- Profile methods - identify slow methods that need optimization
- Batch operations - combine multiple operations when possible
- Use caching - cache frequent results to reduce computation
Constructor
Host(protocol, networkMask, logger)
Creates a new host instance.
Parameters:
protocol(string): Base protocol namenetworkMask(string, optional): Prefix for protocol names. Defaults to empty stringlogger(table, optional): Logger instance. Defaults to default logger from lumberjack
Returns:
host(table): Host instance with the following methods and properties
Properties:
protocol(string): Full protocol name with network masknetworkMask(string): Network mask useddirectProtocol(string): Protocol for direct communicationmethods(table): Table of registered methods
Methods
getMethods()
Returns a table of available methods with descriptions.
Returns:
methods(table): Key-value pairs where keys are method names and values are descriptions
addMethod(name, method, description)
Adds a method to the host.
Parameters:
name(string): Name of the methodmethod(function): Function to execute when method is calleddescription(string, optional): Description of the method. Defaults to "the developer implementing this method deemed his work not worth describing"
start(n)
Starts the host with multiple handler threads.
Parameters:
n(number, optional): Number of handler threads. Defaults to 4
Note: This method runs indefinitely and will block the current thread.
startSingle()
Starts the host with a single handler thread.
Note: This method runs indefinitely and will block the current thread.
Internal Methods
_handle(senderId, message)
Handles an incoming message by executing the requested method.
Parameters:
senderId(number): ID of the sendermessage(table): Message containing methodName and parameters
_sendResponseMessage(senderId, message, protocol)
Sends a response message to the client.
Parameters:
senderId(number): ID of the recipientmessage(table): Message to sendprotocol(string, optional): Protocol to use
_massHandler(queue)
Mass handler that processes messages from the queue.
Parameters:
queue(ParellelQueue): ParallelQueue instance for message processing
_messageReceive()
Receives messages from the network.
Returns:
senderId(number): ID of the sendermessage(table): Received message
_massReceive(queue)
Mass receiver that puts messages into the queue.
Parameters:
queue(ParallelQueue): ParallelQueue instance for storing messages
_massReceiveSingle()
Single thread receiver that handles messages immediately.
Built-in Methods
The host automatically registers the following methods:
ping
Returns true. Used for host discovery.
listMethods
Returns a table of available methods with their descriptions.
shellRun(params)
Executes shell commands. Parameters should be a table with command arguments.
Parameters:
params(table): Command arguments