![]() |
Mads
Multi-Agent Distributed System
|
#include <stdbool.h>#include <stdlib.h>Go to the source code of this file.
Macros | |
| #define | MADS_EXPORT |
Typedefs | |
| typedef void * | agent_t |
Enumerations | |
| enum | message_type_t { mads_none = 0 , mads_json = 1 , mads_blob , mads_error } |
| enum | event_type_t { mads_marker = 0 , mads_marker_in , mads_marker_out , mads_startup , mads_shutdown , mads_message } |
Functions | |
| MADS_EXPORT const char * | mads_version () |
| Returns the MADS library version string. | |
| MADS_EXPORT const char * | mads_default_settings_uri () |
| Returns the default settings URI compiled into the library. | |
| MADS_EXPORT void | mads_free (void *ptr) |
| Releases memory allocated by the MADS C API. | |
| MADS_EXPORT agent_t | agent_create (const char *name, const char *settings_uri) |
| Creates a new agent instance. | |
| MADS_EXPORT int | agent_init (agent_t agent, bool crypto) |
| Initializes an agent before connecting it. | |
| MADS_EXPORT void | agent_destroy (agent_t agent) |
| Destroys an agent instance. | |
| MADS_EXPORT void | agent_set_id (agent_t agent, const char *id) |
| Sets the agent identifier. | |
| MADS_EXPORT const char * | agent_id (agent_t agent) |
| Returns the current agent identifier. | |
| MADS_EXPORT void | agent_install_loop_watchdog (agent_t agent) |
| Installs the internal loop watchdog for the agent. | |
| MADS_EXPORT void | agent_set_key_dir (agent_t agent, const char *key_dir) |
| Sets the directory containing authentication keys. | |
| MADS_EXPORT void | agent_set_client_key_name (agent_t agent, const char *client_key_name) |
| Sets the client key filename. | |
| MADS_EXPORT void | agent_set_server_key_name (agent_t agent, const char *server_key_name) |
| Sets the server key filename. | |
| MADS_EXPORT void | agent_set_auth_verbose (agent_t agent, bool verbose) |
| Enables or disables verbose authentication logging. | |
| MADS_EXPORT int | agent_set_client_public_key (agent_t agent, const char *key) |
| Sets the client public key. | |
| MADS_EXPORT int | agent_set_client_secret_key (agent_t agent, const char *key) |
| Sets the client secret key. | |
| MADS_EXPORT int | agent_set_server_public_key (agent_t agent, const char *key) |
| Sets the server public key. | |
| MADS_EXPORT int | agent_setup_crypto (agent_t agent, bool verbose) |
| Enables CURVE encryption and sets up authentication. | |
| MADS_EXPORT int | agent_connect (agent_t agent, int delay_ms) |
| Connects the agent to the MADS infrastructure. | |
| MADS_EXPORT int | agent_register_event (agent_t agent, event_type_t event, const char *info_json) |
| Registers an event for the connected agent. | |
| MADS_EXPORT int | agent_disconnect (agent_t agent) |
| Disconnects the agent if it is connected. | |
| MADS_EXPORT int | agent_stop (agent_t agent) |
| Requests this agent's loops to stop. | |
| MADS_EXPORT bool | agent_running (agent_t agent) |
| Whether this agent's loops should keep going. | |
| MADS_EXPORT void | mads_stop_process (void) |
| Requests a process-wide stop. | |
| MADS_EXPORT bool | mads_process_running (void) |
| Whether a process-wide stop has been requested. | |
| MADS_EXPORT void | agent_set_receive_timeout (agent_t agent, int timeout) |
| Sets the receive timeout in milliseconds. | |
| MADS_EXPORT int | agent_receive_timeout (agent_t agent) |
| Returns the current receive timeout. | |
| MADS_EXPORT const char * | agent_last_error () |
| Returns the last error message produced by the wrapper. | |
| MADS_EXPORT const char * | agent_get_settings (agent_t agent, int n) |
| Serializes the current settings to JSON. | |
| MADS_EXPORT int | agent_set_settings_timeout (agent_t agent, int to_ms) |
| Sets the timeout used when fetching settings. | |
| MADS_EXPORT void | agent_set_pub_topic (agent_t agent, const char *topic) |
| Sets the publish topic for the agent. | |
| MADS_EXPORT void | agent_set_sub_topics (agent_t agent, const char **topics, int n_topics) |
| Sets the subscribe topics for the agent. | |
| MADS_EXPORT const char * | agent_pub_topic (agent_t agent) |
| Return publishing topic for the agent. | |
| MADS_EXPORT int | agent_sub_topics (agent_t agent, char **topics, size_t *n_topics) |
| Return subscription topics for the agent. | |
| MADS_EXPORT char * | agent_topics (agent_t agent, int tab) |
| MADS_EXPORT int | agent_settings_timeout (agent_t agent) |
| Returns the current settings timeout. | |
| MADS_EXPORT int | discover_broker_settings (const char *room, char **url, size_t url_size) |
| Discovers the broker settings URI advertised in a service room. | |
| MADS_EXPORT bool | agent_setting_bool (agent_t agent, const char *key) |
| Reads a boolean setting. | |
| MADS_EXPORT int | agent_setting_int (agent_t agent, const char *key) |
| Reads an integer setting. | |
| MADS_EXPORT double | agent_setting_dbl (agent_t agent, const char *key) |
| Reads a floating-point setting. | |
| MADS_EXPORT const char * | agent_setting_str (agent_t agent, const char *key) |
| Reads a string setting. | |
| MADS_EXPORT void | agent_print_settings (agent_t agent, int tab) |
| Prints the current settings JSON to standard output. | |
| MADS_EXPORT const char * | agent_settings_uri (agent_t agent) |
| Returns the current settings URI. | |
| MADS_EXPORT int | agent_set_high_watermark (agent_t agent, int n) |
| Sets the outgoing message high watermark. | |
| MADS_EXPORT int | agent_high_watermark (agent_t agent) |
| Returns the outgoing message high watermark. | |
| MADS_EXPORT int | agent_set_wire_format (agent_t agent, int format) |
| Sets the outgoing wire format. | |
| MADS_EXPORT int | agent_wire_format (agent_t agent) |
| Returns the outgoing wire format. | |
| MADS_EXPORT int | agent_set_compression (agent_t agent, int compression) |
| Sets the outgoing compression policy. | |
| MADS_EXPORT int | agent_compression (agent_t agent) |
| Returns the outgoing compression policy. | |
| MADS_EXPORT int | agent_publish (agent_t agent, const char *message, const char *topic) |
| Publishes a JSON message on a topic. | |
| MADS_EXPORT message_type_t | agent_receive (agent_t agent, bool dont_block) |
| Receives the next incoming message. | |
| MADS_EXPORT void | agent_last_message (agent_t agent, char **topic, char **message) |
| Returns the topic and payload of the last received message. | |
| enum event_type_t |
| enum message_type_t |
| MADS_EXPORT int agent_compression | ( | agent_t | agent | ) |
Returns the outgoing compression policy.
| agent | Agent handle. |
0 = None, 1 = Snappy, 2 = Auto. | MADS_EXPORT int agent_connect | ( | agent_t | agent, |
| int | delay_ms | ||
| ) |
Connects the agent to the MADS infrastructure.
| agent | Agent handle. |
| delay_ms | Delay in milliseconds applied by the underlying connect call. |
0 on success, -1 on error.On failure, call agent_last_error() for details.
| MADS_EXPORT agent_t agent_create | ( | const char * | name, |
| const char * | settings_uri | ||
| ) |
Creates a new agent instance.
| name | Agent name. |
| settings_uri | URI or path used to load the agent settings. |
| MADS_EXPORT void agent_destroy | ( | agent_t | agent | ) |
Destroys an agent instance.
The agent is shut down before being deleted.
| agent | Agent handle. |
| MADS_EXPORT int agent_disconnect | ( | agent_t | agent | ) |
Disconnects the agent if it is connected.
| agent | Agent handle. |
0 on success, -1 on error.Returns 0 if the agent is already disconnected.
| MADS_EXPORT const char * agent_get_settings | ( | agent_t | agent, |
| int | n | ||
| ) |
Serializes the current settings to JSON.
| agent | Agent handle. |
| n | Indentation passed to JSON dump formatting. |
| MADS_EXPORT int agent_high_watermark | ( | agent_t | agent | ) |
Returns the outgoing message high watermark.
| agent | Agent handle. |
| MADS_EXPORT const char * agent_id | ( | agent_t | agent | ) |
Returns the current agent identifier.
| agent | Agent handle. |
| MADS_EXPORT int agent_init | ( | agent_t | agent, |
| bool | crypto | ||
| ) |
Initializes an agent before connecting it.
A note about cryptpographic setup: it is possible to enable cryptographic communication in two ways: by reading keys from files, or by setting them directly from strings:
agent_set_key_dir() to set the directory containing the authentication keys, agent_set_client_key_name() and agent_set_server_key_name() to set the client and server key filenames, and then call agent_init() with crypto set to true.agent_setup_crypto() to enable agent_set_client_public_key(), agent_set_client_secret_key(), and agent_set_server_public_key() to set the keys, and finally call agent_init() with crypto set to true.In the second form, it is an error to call agent_init() with crypto set to true before setting the keys, or to set the keys before calling agent_setup_crypto().
| agent | Agent handle. |
| crypto | Enables cryptographic setup when true. |
0 on success, -1 on error.On failure, call agent_last_error() for details.
| MADS_EXPORT void agent_install_loop_watchdog | ( | agent_t | agent | ) |
Installs the internal loop watchdog for the agent.
| agent | Agent handle. |
| MADS_EXPORT const char * agent_last_error | ( | ) |
Returns the last error message produced by the wrapper.
| MADS_EXPORT void agent_last_message | ( | agent_t | agent, |
| char ** | topic, | ||
| char ** | message | ||
| ) |
Returns the topic and payload of the last received message.
| agent | Agent handle. |
| topic | Output pointer receiving the topic string. |
| message | Output pointer receiving the message payload string. |
The returned pointers refer to internal storage that is overwritten by the next call to this function.
| MADS_EXPORT void agent_print_settings | ( | agent_t | agent, |
| int | tab | ||
| ) |
Prints the current settings JSON to standard output.
| agent | Agent handle. |
| tab | Indentation passed to JSON dump formatting. |
| MADS_EXPORT const char * agent_pub_topic | ( | agent_t | agent | ) |
Return publishing topic for the agent.
| agent |
| MADS_EXPORT int agent_publish | ( | agent_t | agent, |
| const char * | message, | ||
| const char * | topic | ||
| ) |
Publishes a JSON message on a topic.
| agent | Agent handle. |
| message | Null-terminated JSON string. |
| topic | Topic name. |
0 on success, -1 on error.On failure, call agent_last_error() for details.
| MADS_EXPORT message_type_t agent_receive | ( | agent_t | agent, |
| bool | dont_block | ||
| ) |
Receives the next incoming message.
| agent | Agent handle. |
| dont_block | When true, performs a non-blocking receive. |
mads_none if no message is available, or mads_error on failure. | MADS_EXPORT int agent_receive_timeout | ( | agent_t | agent | ) |
Returns the current receive timeout.
| agent | Agent handle. |
| MADS_EXPORT int agent_register_event | ( | agent_t | agent, |
| event_type_t | event, | ||
| const char * | info_json | ||
| ) |
Registers an event for the connected agent.
| agent | Agent handle. |
| event | Event type to register. |
| info_json | Optional JSON payload string. Pass NULL for no payload. |
0 on success, -1 on error.On failure, call agent_last_error() for details.
| MADS_EXPORT bool agent_running | ( | agent_t | agent | ) |
Whether this agent's loops should keep going.
False after agent_stop(), agent_disconnect(), a remote shutdown/restart command, or a process-wide stop (mads_stop_process(), SIGINT/SIGTERM). Reconnecting with agent_connect() re-arms a stop raised by agent_disconnect().
| agent | Agent handle. |
true while the agent should keep running; false otherwise (including for a NULL handle). | MADS_EXPORT void agent_set_auth_verbose | ( | agent_t | agent, |
| bool | verbose | ||
| ) |
Enables or disables verbose authentication logging.
| agent | Agent handle. |
| verbose | Set to true to enable verbose output. |
| MADS_EXPORT void agent_set_client_key_name | ( | agent_t | agent, |
| const char * | client_key_name | ||
| ) |
Sets the client key filename.
| agent | Agent handle. |
| client_key_name | Client key filename. |
| MADS_EXPORT int agent_set_client_public_key | ( | agent_t | agent, |
| const char * | key | ||
| ) |
Sets the client public key.
Note that this functiuon must be called after agent_setup_crypto() and before agent_connect()
| agent | Agent handle. |
| key | Public key string. |
0 on success, -1 on error. | MADS_EXPORT int agent_set_client_secret_key | ( | agent_t | agent, |
| const char * | key | ||
| ) |
Sets the client secret key.
Note that this functiuon must be called after agent_setup_crypto() and before agent_connect()
| agent | Agent handle. |
| key | Secret key string. |
0 on success, -1 on error. | MADS_EXPORT int agent_set_compression | ( | agent_t | agent, |
| int | compression | ||
| ) |
Sets the outgoing compression policy.
| agent | Agent handle. |
| compression | 0 = None, 1 = Snappy, 2 = Auto (default). |
0 on success, -1 on error. | MADS_EXPORT int agent_set_high_watermark | ( | agent_t | agent, |
| int | n | ||
| ) |
Sets the outgoing message high watermark.
| agent | Agent handle. |
| n | High watermark value. |
0 on success, -1 on error. | MADS_EXPORT void agent_set_id | ( | agent_t | agent, |
| const char * | id | ||
| ) |
Sets the agent identifier.
| agent | Agent handle. |
| id | Null-terminated identifier string. |
| MADS_EXPORT void agent_set_key_dir | ( | agent_t | agent, |
| const char * | key_dir | ||
| ) |
Sets the directory containing authentication keys.
| agent | Agent handle. |
| key_dir | Filesystem path to the key directory. |
| MADS_EXPORT void agent_set_pub_topic | ( | agent_t | agent, |
| const char * | topic | ||
| ) |
Sets the publish topic for the agent.
| agent | Agent handle. |
| topic | Publish topic. |
| MADS_EXPORT void agent_set_receive_timeout | ( | agent_t | agent, |
| int | timeout | ||
| ) |
Sets the receive timeout in milliseconds.
| agent | Agent handle. |
| timeout | Timeout in milliseconds. |
| MADS_EXPORT void agent_set_server_key_name | ( | agent_t | agent, |
| const char * | server_key_name | ||
| ) |
Sets the server key filename.
| agent | Agent handle. |
| server_key_name | Server key filename. |
| MADS_EXPORT int agent_set_server_public_key | ( | agent_t | agent, |
| const char * | key | ||
| ) |
Sets the server public key.
Note that this functiuon must be called after agent_setup_crypto() and before agent_connect()
| agent | Agent handle. |
| key | Public key string. |
0 on success, -1 on error. | MADS_EXPORT int agent_set_settings_timeout | ( | agent_t | agent, |
| int | to_ms | ||
| ) |
Sets the timeout used when fetching settings.
| agent | Agent handle. |
| to_ms | Timeout in milliseconds. |
0 on success, -1 on error. | MADS_EXPORT void agent_set_sub_topics | ( | agent_t | agent, |
| const char ** | topics, | ||
| int | n_topics | ||
| ) |
Sets the subscribe topics for the agent.
| agent | Agent handle. |
| topics | Array of null-terminated topic strings. |
| n_topics | Number of topics in the array. |
| MADS_EXPORT int agent_set_wire_format | ( | agent_t | agent, |
| int | format | ||
| ) |
Sets the outgoing wire format.
| agent | Agent handle. |
| format | 0 = JSON (default), 1 = MessagePack. |
0 on success, -1 on error. | MADS_EXPORT bool agent_setting_bool | ( | agent_t | agent, |
| const char * | key | ||
| ) |
Reads a boolean setting.
| agent | Agent handle. |
| key | Setting key. |
false if the key is missing. | MADS_EXPORT double agent_setting_dbl | ( | agent_t | agent, |
| const char * | key | ||
| ) |
Reads a floating-point setting.
| agent | Agent handle. |
| key | Setting key. |
0.0 if the key is missing. | MADS_EXPORT int agent_setting_int | ( | agent_t | agent, |
| const char * | key | ||
| ) |
Reads an integer setting.
| agent | Agent handle. |
| key | Setting key. |
0 if the key is missing. | MADS_EXPORT const char * agent_setting_str | ( | agent_t | agent, |
| const char * | key | ||
| ) |
Reads a string setting.
| agent | Agent handle. |
| key | Setting key. |
| MADS_EXPORT int agent_settings_timeout | ( | agent_t | agent | ) |
Returns the current settings timeout.
| agent | Agent handle. |
| MADS_EXPORT const char * agent_settings_uri | ( | agent_t | agent | ) |
Returns the current settings URI.
| agent | Agent handle. |
| MADS_EXPORT int agent_setup_crypto | ( | agent_t | agent, |
| bool | verbose | ||
| ) |
Enables CURVE encryption and sets up authentication.
Note that this function must be called before agent_init() and before setting the keys
| agent | Agent handle. |
| verbose | Set to true to enable verbose output. |
0 on success, -1 on error.On failure, call agent_last_error() for details.
| MADS_EXPORT int agent_stop | ( | agent_t | agent | ) |
Requests this agent's loops to stop.
Stops the agent's Mads::Runtime; other agents in the same process keep running unless they share the same Runtime. Use this to end a receive loop driven by agent_running().
| agent | Agent handle. |
0 on success, -1 on error (NULL handle). | MADS_EXPORT int agent_sub_topics | ( | agent_t | agent, |
| char ** | topics, | ||
| size_t * | n_topics | ||
| ) |
Return subscription topics for the agent.
The topics output parameter receives an array of null-terminated strings containing the subscription topics. Pass the address of a char* initialized to NULL to have the function allocate the array with malloc(); the caller owns it and must release it with mads_free(). The n_topics output parameter receives the number of topics in the array.
| agent | |
| topics | |
| n_topics |
| MADS_EXPORT char * agent_topics | ( | agent_t | agent, |
| int | tab | ||
| ) |
| MADS_EXPORT int agent_wire_format | ( | agent_t | agent | ) |
Returns the outgoing wire format.
| agent | Agent handle. |
0 = JSON, 1 = MessagePack. | MADS_EXPORT int discover_broker_settings | ( | const char * | room, |
| char ** | url, | ||
| size_t | url_size | ||
| ) |
Discovers the broker settings URI advertised in a service room.
If room is NULL or empty, the compiled default MADS service room is used. The discovered URI is written as tcp://ip:port.
| room | Service discovery room name, or NULL for the default room. |
| url | Pointer to the output buffer receiving the null-terminated settings URI. Pass the address of a char* initialized to NULL to have the function allocate a buffer with malloc(); the caller owns it and must release it with mads_free(). |
| url_size | Size of the provided output buffer in bytes. Ignored when the function allocates the buffer. |
0 on success, -1 on error.On failure, call agent_last_error() for details.
| MADS_EXPORT const char * mads_default_settings_uri | ( | ) |
Returns the default settings URI compiled into the library.
| MADS_EXPORT void mads_free | ( | void * | ptr | ) |
Releases memory allocated by the MADS C API.
Use this for buffers returned by functions that allocate output memory. This keeps allocation and deallocation inside the same runtime on Windows.
| ptr | Pointer returned by a MADS C API allocation, or NULL. |
| MADS_EXPORT bool mads_process_running | ( | void | ) |
Whether a process-wide stop has been requested.
true until mads_stop_process() (or a signal handler) stops the process-wide run flag. | MADS_EXPORT void mads_stop_process | ( | void | ) |
Requests a process-wide stop.
Every agent's agent_running() becomes false, as with SIGINT/SIGTERM.
| MADS_EXPORT const char * mads_version | ( | ) |
Returns the MADS library version string.