flux_event_decode(3)
SYNOPSIS
#include <flux/core.h>
int flux_event_decode (const flux_msg_t *msg,
const char **topic,
const char **s);
int flux_event_decode_raw (const flux_msg_t *msg,
const char **topic,
const void **data,
size_t *len);
int flux_event_unpack (const flux_msg_t *msg,
const char **topic,
const char *fmt,
...);
flux_msg_t *flux_event_encode (const char *topic, const char *s);
flux_msg_t *flux_event_encode_raw (const char *topic,
const void *data,
size_t len);
flux_msg_t *flux_event_pack (const char *topic,
const char *fmt,
...);
Link with -lflux-core.
DESCRIPTION
flux_event_decode()
decodes a Flux event message msg
.
topic
, if non-NULL, will be set to the message's topic string. The
storage for this string belongs to msg
and should not be freed.
s
, if non-NULL, will be set to the message's NULL-terminated string
payload. If no payload exists, it is set to NULL. The storage for this string
belongs to msg
and should not be freed.
flux_event_decode_raw()
decodes an event message with a raw payload,
setting data
and len
to the payload data and length. The storage
for the raw payload belongs to msg
and should not be freed.
flux_event_unpack()
decodes a Flux event message with a JSON payload as
above, parsing the payload using variable arguments with a format string
in the style of jansson's json_unpack()
(used internally). Decoding fails
if the message doesn't have a JSON payload.
flux_event_encode()
encodes a Flux event message with topic string
topic
and optional NULL-terminated string payload s
. The newly
constructed message that is returned must be destroyed with
flux_msg_destroy()
.
flux_event_encode_raw()
encodes a Flux event message with topic
string topic
. If data
is non-NULL, its contents will be used as
the message payload, and the payload type set to raw.
flux_event_pack()
encodes a Flux event message with a JSON payload as
above, encoding the payload using variable arguments with a format string
in the style of jansson's json_pack()
(used internally). Decoding fails
if the message doesn't have a JSON payload.
Events propagated to all subscribers. Events will not be received
without a matching subscription established using flux_event_subscribe()
.
ENCODING JSON PAYLOADS
Flux API functions that are based on Jansson's json_pack()
accept the following tokens in their format string.
The type in parenthesis denotes the resulting JSON type, and
the type in brackets (if any) denotes the C type that is expected as
the corresponding argument or arguments.
- s (string)['const char *']
Convert a null terminated UTF-8 string to a JSON string.
- s? (string)['const char *']
Like s, but if the argument is NULL, outputs a JSON null value.
- s# (string)['const char *', 'int']
Convert a UTF-8 buffer of a given length to a JSON string.
- s% (string)['const char *', 'size_t']
Like s# but the length argument is of type size_t.
- + ['const char *']
Like s, but concatenate to the previous string. Only valid after a string.
- +# ['const char *', 'int']
Like s#, but concatenate to the previous string. Only valid after a string.
- +% ['const char *', 'size_t']
Like +#, but the length argument is of type size_t.
- n (null)
Output a JSON null value. No argument is consumed.
- b (boolean)['int']
Convert a C int to JSON boolean value. Zero is converted to false and non-zero to true.
- i (integer)['int']
Convert a C int to JSON integer.
- I (integer)['int64_t']
Convert a C int64_t to JSON integer. Note: Jansson expects a json_int_t here without committing to a size, but Flux guarantees that this is a 64-bit integer.
- f (real)['double']
Convert a C double to JSON real.
- o (any value)['json_t *']
Output any given JSON value as-is. If the value is added to an array or object, the reference to the value passed to o is stolen by the container.
- O (any value)['json_t *']
Like o, but the argument's reference count is incremented. This is useful if you pack into an array or object and want to keep the reference for the JSON value consumed by O to yourself.
- o?, O? (any value)['json_t *']
Like o and O, respectively, but if the argument is NULL, output a JSON null value.
- [fmt] (array)
Build an array with contents from the inner format string. fmt may contain objects and arrays, i.e. recursive value building is supported.
- {fmt} (object)
Build an object with contents from the inner format string fmt. The first, third, etc. format specifier represent a key, and must be a string as object keys are always strings. The second, fourth, etc. format specifier represent a value. Any value may be an object or array, i.e. recursive value building is supported.
Whitespace, : (colon) and , (comma) are ignored.
These descriptions came from the Jansson 2.9 manual.
See also: Jansson API: Building Values: http://jansson.readthedocs.io/en/2.9/apiref.html#building-values
DECODING JSON PAYLOADS
Flux API functions that are based on Jansson's json_unpack()
accept the following tokens in their format string.
The type in parenthesis denotes the resulting JSON type, and
the type in brackets (if any) denotes the C type that is expected as
the corresponding argument or arguments.
- s (string)['const char *']
Convert a JSON string to a pointer to a null terminated UTF-8 string. The resulting string is extracted by using 'json_string_value()' internally, so it exists as long as there are still references to the corresponding JSON string.
- s% (string)['const char *', 'size_t']
Convert a JSON string to a pointer to a null terminated UTF-8 string and its length.
- n (null)
Expect a JSON null value. Nothing is extracted.
- b (boolean)['int']
Convert a JSON boolean value to a C int, so that true is converted to 1 and false to 0.
- i (integer)['int']
Convert a JSON integer to a C int.
- I (integer)['int64_t']
Convert a JSON integer to a C int64_t. Note: Jansson expects a json_int_t here without committing to a size, but Flux guarantees that this is a 64-bit integer.
- f (real)['double']
Convert JSON real to a C double.
- F (real)['double']
Convert JSON number (integer or real) to a C double.
- o (any value)['json_t *']
Store a JSON value, with no conversion, to a json_t pointer.
- O (any value)['json_t *']
Like o, but the JSON value's reference count is incremented.
- [fmt] (array)
Convert each item in the JSON array according to the inner format string. fmt may contain objects and arrays, i.e. recursive value extraction is supported.
- {fmt} (object)
Convert each item in the JSON object according to the inner format string fmt. The first, third, etc. format specifier represent a key, and must by s. The corresponding argument to unpack functions is read as the object key. The second, fourth, etc. format specifier represent a value and is written to the address given as the corresponding argument. Note that every other argument is read from and every other is written to. fmt may contain objects and arrays as values, i.e. recursive value extraction is supported. Any s representing a key may be suffixed with ? to make the key optional. If the key is not found, nothing is extracted.
- !
This special format specifier is used to enable the check that all object and array items are accessed, on a per-value basis. It must appear inside an array or object as the last format specifier before the closing bracket or brace.
Whitespace, : (colon) and , (comma) are ignored.
These descriptions came from the Jansson 2.9 manual.
See also: Jansson API: Parsing and Validating Values: http://jansson.readthedocs.io/en/2.9/apiref.html#parsing-and-validating-values
RETURN VALUE
Decoding functions return 0 on success. On error, -1 is returned, and
errno
is set appropriately.
Encoding functions return a message on success. On error, NULL is returned,
and errno
is set appropriately.
ERRORS
- EINVAL
The
msg
argument was NULL or there was a problem encoding.- ENOMEM
Memory was unavailable.
- EPROTO
Message decoding failed, such as due to incorrect message type, missing topic string, etc.
RESOURCES
Flux: http://flux-framework.org
Flux RFC: https://flux-framework.readthedocs.io/projects/flux-rfc
Issue Tracker: https://github.com/flux-framework/flux-core/issues