API - salmito/leda GitHub Wiki
This page contains a quick reference for the Leda library API:
This function creates a stage object reference and its passed
Parameters:
'handler' type: function(...) (required field)
This is the function that will be used as the
event handler for the created stage.
It will receive the unpacked event at every execution as its parameters.
'init' type: function() (optional field)
If defined, this function is called after the creation of a instance
of the created stage.
'bind' type: function({connectors}) (optional field)
If defined, this function is called before the execution of
the stage graph containing this stage.
It will receive the output connectors table defined on the running graph as its parameter.
'serial' or 'stateful' type: boolean (optional field)
if 'false' or 'nil' the stage wont have persistent state. Multiple
parallel instances os this stages can be created by the runtime.
else the stage will have a persistent state, but all events are
whithin this stage will be serialized.
'name' type: string (optional field)
Optional 'name' of the stage used in the tostring() metamethod
Create a connector between two stages
- key: Index for the connector on the output ports (default is the value '1')
- stage2: Target stage
- type: Connector type, can be "local" or "decoupled" (default)
Put event into the stage's event queue
- ... : Event data
Create a cluster object containing the stages passed as function parameters. Clusters can be manipulated with the following arithmetic operators:
- cluster1 + cluster2 = Union between two clusters
- cluster1 - cluster2 = Difference between two clusters
- cluster1 * cluster2 = Intersection between two clusters
This function creates a new stage graph from an array of connectors received as parameter.
Optional fields:
'start' type: stage
Define a starting stage for this graph. Used by the send method of the graph.
'name' type: string
Optional 'name' of the graph used in the tostring() metamethod
Starts the execution of a graph.
Parameter: a table with the following optional fields:
- controller: A controller reference to be used by the runtime.
- maxpar: Maximum number of parallel instances of stages in this graph.
- port: TCP port to be used in the inter-process communication mecanism.
Example:
... --graph definition
require "leda.controller.fixed_thread_pool" --> load the fixed thread pool controller
graph:run{controller=leda.controller.fixed_thread_pool.get(10)} --> get the controller with 10 fixed threads
Creates a partition on the graph. It expects a ordered list of clusters with stages belonging to the graph as a parameter. Each of stage of the graph must be inside of exactly one defined cluster for a partition to be valid. - This function raises an error in the case of an invalid cluster configuration
Map each previously defined cluster to a exclusive process. Each remote process must be started before the execution of the graph.
Returns a cluster with all stages defined on the graph.
Examples:
graph:part(graph:all()) --> Partition the graph with a single cluster with all stages of the graph
graph:map("localhost") --> Map the cluster to the local process
graph:part(graph:all()-stage1,leda.cluster(stage1)) --> Partition the graph with two clusters
graph:map("host1.localdomain.com:9999","host2.localdomain.com:9999") --> Map both clusters
Starts a new Leda process and waits for the execution of a graph with a cluster mapped to this process.
Parameter: a table with the following optional fields:
- controller: A controller reference to be used by the runtime.
- maxpar: Maximum number of parallel instances of stages in this graph.
- port: TCP port to be used in the inter-process communication mecanism.
The following functions can be called by event handlers and are only available inside active instances of its stage.
Send an event to an output port. Returns true
if the event was successfully delivered to an event queue
of a stage, or nil and an error message if the event cannot be delivered.
Causes the current running instance to sleep for a period of time (in seconds).
Terminate the execution of the current process with exit_code
.
Returns the current time.
Release the current thread for other instances that are ready to execute (equivalent of leda.sleep(0)
)