nexuslua
|
Within nexuslua, the send function serves as a method to dispatch an asynchronous message to an existing agent. Conceptually, this action can be seen as making an asynchronous function call in Lua. The primary purpose is to relay a message to an agent, which then executes a specific function asynchronously, and if specified, sends back a response.
Agents can be created in two ways:
Beyond the main message data (number=127), this example incorporates two optional message entries:
When a valid reply_to subtable is available (as shown above), the specified callback function is invoked asynchronously, using the function's return table as its argument. The callback function referenced in above send example could be:
It evaluates the result message from IsPrime. See addmessage for the IsPrime example function's implementation, which returns either {isPrime=true} or {isPrime=false}, based on the outcome.
To determine which number was assessed, you can refer to parameters.original_message which always encapsulates the entire processed message (i.e., the third argument of the associated send call). Hence, in this illustration, parameters.original_message.number reveals the number to which parameters.isPrime pertains.
Internally, the C++ types of the dispatched (or replied, see example) parameter values align with the pertinent Lua types used in the dispatched message. For void*, the internal type is a string. If a table contains pointers, the conversion between void* and its representation is managed by lua_pushtable and lua_totable, https://github.com/acrion/nexuslua-library/blob/main/src/lua.hpp.
The receiving function of a message has access to the original message, i. e. the message that the sender had processed, through the sub table original_message (see previous section). In spite of this, there are certain use cases where you want to define fields that the function that processes your message shall add to its reply. You can do this by adding a sub table named merge to the reply_to subtable. The merge sub table will be added to the actual replying message (potentially overwriting parts of the reply). For example
This script sends the string nexuslua to the function ReplyTest, which will prepend Hello to the string and send the resulting string to function ReceiveReply. Hence, its code line print(parameters.MyReply) will print Hello nexuslua.
When sending the message we specified a sub table reply_to/merge that contains two fields MergedEntry and MergedSubTable. Therefore, the table will contain not only MyReply, but also these two fields. So the code line printtable(parameters) will print:
In addition it contains an entry original_message with the complete message that ReplyTest had received.
The send function is more than just a message dispatcher. It establishes asynchronous communication between different agents running in separate operating system threads. This means instead of waiting for a response, you can continue with other tasks and process the response as it becomes available. This approach empowers nexuslua scripts to efficiently parallel-process demanding tasks while remaining responsive to user inputs or other events.
When executing the command line interface of nexuslua, the script may call send with a reply_to subtable specifying main as the callback agent, because the command line interface internally executed the script an agent with name "main".