smax-clib v0.9
A C/C++ client library for SMA-X
|
SMA-X is a software implementation for SMA shared data, and is the base layer for the software reflective memory (RM) emulation, and DSM replacement. It works by communicating TCP/IP messages to a central Redis server. More...
Macros | |
#define | _POSIX_C_SOURCE 199309 |
For clock_gettime() | |
Functions | |
int | smaxAddConnectHook (void(*setupCall)(void)) |
int | smaxAddDisconnectHook (void(*cleanupCall)(void)) |
int | smaxAddSubscriber (const char *idStem, RedisSubscriberCall f) |
int | smaxConnect () |
int | smaxConnectTo (const char *server) |
int | smaxDisconnect () |
char * | smaxGetHostName () |
char ** | smaxGetKeys (const char *table, int *n) |
char * | smaxGetProgramID () |
Redis * | smaxGetRedis () |
int | smaxIsConnected () |
boolean | smaxIsPipelined () |
boolean | smaxIsVerbose () |
int | smaxKeyCount (const char *table) |
int | smaxPull (const char *table, const char *key, XType type, int count, void *value, XMeta *meta) |
int | smaxReconnect () |
int | smaxReleaseWaits () |
int | smaxRemoveConnectHook (void(*setupCall)(void)) |
int | smaxRemoveDisconnectHook (void(*cleanupCall)(void)) |
int | smaxRemoveSubscribers (RedisSubscriberCall f) |
int | smaxSetAuth (const char *username, const char *password) |
int | smaxSetDB (int idx) |
void | smaxSetHostName (const char *name) |
int | smaxSetPipelineConsumer (void(*f)(RESP *)) |
int | smaxSetPipelined (boolean isEnabled) |
int | smaxSetServer (const char *host, int port) |
int | smaxSetTcpBuf (int size) |
void | smaxSetVerbose (boolean value) |
int | smaxShare (const char *table, const char *key, const void *value, XType type, int count) |
int | smaxShareArray (const char *table, const char *key, const void *ptr, XType type, int ndim, const int *sizes) |
int | smaxShareField (const char *table, const XField *f) |
int | smaxShareStruct (const char *id, const XStructure *s) |
int | smaxSubscribe (const char *table, const char *key) |
int | smaxUnsubscribe (const char *table, const char *key) |
int | smaxWaitOnAnySubscribed (char **changedTable, char **changedKey, int timeout) |
SMA-X is a software implementation for SMA shared data, and is the base layer for the software reflective memory (RM) emulation, and DSM replacement. It works by communicating TCP/IP messages to a central Redis server.
There is also extra functionality, for configuring, performance tweaking, verbosity control, and some convenience methods (e.g. data serialization/deserialization).
int smaxAddConnectHook | ( | void(*)(void) | setupCall | ) |
Add a callback function for when SMA-X is connected. It's a wrapper to redisxAddConnectHook().
setupCall | Callback function |
References redisxAddConnectHook(), smaxGetRedis(), and X_SUCCESS.
int smaxAddDisconnectHook | ( | void(*)(void) | cleanupCall | ) |
Add a callback function for when SMA-X is disconnected. It's a wrapper to redisxAddDisconnectHook().
cleanupCall | Callback function |
References redisxAddDisconnectHook(), smaxGetRedis(), and X_SUCCESS.
int smaxAddSubscriber | ( | const char * | idStem, |
RedisSubscriberCall | f | ||
) |
Add a subcriber (callback) function to process incoming PUB/SUB messages for a given SMA-X table (or id). The function should itself check that the channel receiving notification is indeed what it expectes before acting on it, as the callback routine will be invoked for any update inside the specified table, unless the table argument refers to a specific aggregate ID of a single variable. This call only registers the callback routine for SMA-X update notifications for variables that begin with the specified stem. You will still have to subscrive to any relevant variables with smaxSubscribe() to enable delivering update notifications for the variables of your choice.
idStem | Table name or ID stem for which the supplied callback function will be invoked as long as the beginning of the PUB/SUB update channel matches the given stem. Alternatively, it can be a fully qualified SMA-X ID (of the form table:key) f a single variable. |
f | The function to call when there is an incoming PUB/SUB update to a channel starting with stem. |
References redisxAddSubscriber(), SMAX_UPDATES_ROOT, smaxGetRedis(), X_SUCCESS, and xGetAggregateID().
int smaxConnect | ( | ) |
Initializes the SMA-X sharing library in this runtime instance.
References FALSE, redisxConnect(), redisxInit(), redisxSelectDB(), redisxSetPassword(), redisxSetPort(), redisxSetTcpBuf(), redisxSetTransmitErrorHandler(), redisxSetUser(), SMAX_DEFAULT_HOSTNAME, smaxAddConnectHook(), smaxAddDisconnectHook(), smaxAddSubscriber(), smaxGetProgramID(), smaxIsConnected(), smaxLazyFlush(), smaxReleaseWaits(), smaxSetPipelineConsumer(), smaxSetResilient(), smaxTransmitErrorHandler(), TRUE, X_NO_INIT, X_SUCCESS, x_trace(), and xvprintf.
int smaxConnectTo | ( | const char * | server | ) |
Initializes the SMA-X sharing library in this runtime instance with the specified Redis server. SMA-X is initialized in resilient mode, so that we'll automatically attempt to reconnect to the Redis server if the connection is severed (once it was established). If that is not the desired behavior, you should call smaxSetResilient(FALSE)
after connecting.
server | SMA-X Redis server name or IP address, e.g. "127.0.0.1". |
References smaxConnect(), smaxSetServer(), and X_SUCCESS.
int smaxDisconnect | ( | ) |
Disables the SMA-X sharing capability, closing underlying network connections.
References redisxDisconnect(), smaxIsConnected(), x_error(), X_NO_INIT, X_SUCCESS, and xvprintf.
char * smaxGetHostName | ( | ) |
Returns the host name on which this program is running. It returns a reference to the same static variable every time. As such you should never call free() on the returned value. Note, that only the leading part of the host name is returned, so for a host that is registered as 'somenode.somedomain' only 'somenode' is returned.
References xStringCopyOf().
char ** smaxGetKeys | ( | const char * | table, |
int * | n | ||
) |
Returns a snapshot of the key names stored in a given Redis hash table, ot NULL if there was an error.
table | Host name or owner ID whose variable to count. | |
[out] | n | Pointer to which the number of keys (>=0) or an error (<0) is returned. An error returned by redisxGetKeys(), or else: |
X_NO_INIT if the SMA-X sharing was not initialized, e.g. via smaxConnect(). X_GROUP_INVALID if the table name is invalid. X_NULL if the output 'n' pointer is NULL.
References redisxGetKeys(), x_error(), x_trace_null(), and xvprintf.
char * smaxGetProgramID | ( | ) |
Returns the SMA-X program ID.
References smaxGetHostName(), and xGetAggregateID().
Redis * smaxGetRedis | ( | ) |
int smaxIsConnected | ( | ) |
Checks whether SMA-X sharing is currently open (by a preceding call to smaxConnect() call.
References redisxIsConnected().
boolean smaxIsPipelined | ( | ) |
Check if SMA-X is configured with pipeline mode enabled.
boolean smaxIsVerbose | ( | ) |
Checks id verbose reporting is enabled.
References redisxIsVerbose().
int smaxKeyCount | ( | const char * | table | ) |
Retrieve the current number of variables stored on host (or owner ID).
table | Hash table name. |
References RESP::n, redisxCheckRESP(), redisxDestroyRESP(), redisxRequest(), RESP_INT, x_error(), X_GROUP_INVALID, x_trace(), and xvprintf.
int smaxPull | ( | const char * | table, |
const char * | key, | ||
XType | type, | ||
int | count, | ||
void * | value, | ||
XMeta * | meta | ||
) |
Pull data from the specified hash table. This calls data via the interactive client to Redis.
[in] | table | Hash table name. |
[in] | key | Variable name under which the data is stored. |
[in] | type | SMA-X variable type, e.g. X_FLOAT or X_CHARS(40), of the buffer. |
[in] | count | Number of points to retrieve into the buffer. |
[out] | value | Pointer to the buffer to which the data is to be retrieved. |
[out] | meta | Pointer to metadata or NULL if no metadata is needed. |
References REDISX_INTERACTIVE_CHANNEL, X_NULL, X_STRUCT, X_SUCCESS, x_trace(), xGetAggregateID(), and xStringCopyOf().
int smaxReconnect | ( | ) |
Reconnects to the SMA-X server. It will try connecting repeatedly at regular intervals until the connection is made. If resilient mode is enabled, then locally accumulated shares will be sent to the Redis server upon reconnection. However, subscriptions are not automatically re-established. The caller is responsible for reinstate any necessary subscriptions after the reconnection or via an approproate connection hook.
or the error returned by redisxReconnect().
References redisxReconnect(), SMAX_RECONNECT_RETRY_SECONDS, x_error(), X_NO_INIT, X_SUCCESS, and xvprintf.
int smaxReleaseWaits | ( | ) |
Unblocks all smax_wait*() calls, which will return X_REL_PREMATURE, as a result.
int smaxRemoveConnectHook | ( | void(*)(void) | setupCall | ) |
Remove a post-connection callback function. It's a wrapper to redisxRemoveConnectHook().
setupCall | Callback function |
References redisxRemoveConnectHook(), smaxGetRedis(), and X_SUCCESS.
int smaxRemoveDisconnectHook | ( | void(*)(void) | cleanupCall | ) |
Remove a post-cdisconnect callback function. It's a wrapper to redisxRemiveDisconnectHook().
cleanupCall | Callback function |
References redisxRemoveDisconnectHook(), smaxGetRedis(), and X_SUCCESS.
int smaxRemoveSubscribers | ( | RedisSubscriberCall | f | ) |
Remove all instances of a subscriber callback function from the current list of functions processing PUB/SUB messages. This call only deactivates the callback routine, but does not stop the delivery of update notifications from the Redis server. You should therefore also call smaxUnsubscribe() as appropriate to stop notifications for variables that no longer have associated callbacks.
f | Function to remove |
References redisxRemoveSubscribers(), smaxGetRedis(), and X_SUCCESS.
int smaxSetAuth | ( | const char * | username, |
const char * | password | ||
) |
Sets the SMA-X database authentication parameters (if any) before connecting to the SMA-X server.
username | Redis ACL user name (if any), or NULL for no user-based authentication |
password | Redis database password (if any), or NULL if the database is not password protected |
References smaxIsConnected(), X_ALREADY_OPEN, x_error(), X_SUCCESS, and xStringCopyOf().
int smaxSetDB | ( | int | idx | ) |
Sets a non-default Redis database index to use for SMA-X before connecting to the SMA-X server.
idx | The Redis database index to use (if not the default one) |
References smaxIsConnected(), X_ALREADY_OPEN, x_error(), and X_SUCCESS.
void smaxSetHostName | ( | const char * | name | ) |
Changes the host name to the user-specified value instead of the default (leading component of the value returned by gethostname()). Subsequent calls to smaxGetHostName() will return the newly set value. An argument of NULL resets to the default.
name | the host name to use, or NULL to revert to the default (leading component of gethostname()). |
References xStringCopyOf().
int smaxSetPipelineConsumer | ( | void(*)(RESP *) | f | ) |
Change the pipeline response consumer function (from it's default or other previous consumer). It is a wrapper for redisxSetPipelineConsumer().
f | The function to process ALL pipeline responses from Redis. |
References redisxSetPipelineConsumer(), smaxGetRedis(), and X_SUCCESS.
int smaxSetPipelined | ( | boolean | isEnabled | ) |
Enable or disable pipelined write operations (enabled by default). When pipelining, share calls will return as soon as the request is sent to the Redis server, without waiting for a response. Instead, responses are consumed asynchronously by a dedicated thread, which will report errors to stderr. Pipelined writes can have a significant performance advantage over handshaking at the cost of one extra socket connection to Redis (dedicated to pipelining) and the extra thread consuming responses.
The default state of pipelined writes might vary by platform (e.g. enabled on Linux, disabled on LynxOS).
IMPORTANT: calls to smaxSetPipelined() must precede the call to smaxConnect().
isEnabled | TRUE to enable pipelined writes, FALSE to disable (default is enabled). |
References smaxIsConnected(), X_ALREADY_OPEN, x_error(), and X_SUCCESS.
int smaxSetServer | ( | const char * | host, |
int | port | ||
) |
Configures the SMA-X server before connecting.
host | The SMA-X REdis server host name or IP address. |
port | The Redis port number on the SMA-X server, or <=0 to use the default |
References REDISX_TCP_PORT, smaxIsConnected(), X_ALREADY_OPEN, x_error(), X_SUCCESS, and xStringCopyOf().
int smaxSetTcpBuf | ( | int | size | ) |
Set the size of the TCP/IP buffers (send and receive) for future client connections.
size | (bytes) requested buffer size, or <= 0 to use default value |
References smaxIsConnected(), X_ALREADY_OPEN, x_error(), and X_SUCCESS.
void smaxSetVerbose | ( | boolean | value | ) |
Enable or disable verbose reporting of all SMA-X operations (and possibly some details of them). Reporting is done on the standard output (stdout). It may be useful when debugging programs that use the SMA-X interface. Verbose reporting is DISABLED by default.
value | TRUE to enable verbose reporting, or FALSE to disable. |
References redisxSetVerbose().
int smaxShare | ( | const char * | table, |
const char * | key, | ||
const void * | value, | ||
XType | type, | ||
int | count | ||
) |
Share the data into a Redis hash table over the interactive Redis client. It's a fire-and-forget type implementation, which sends the data to Redis, without waiting for confirmation of its arrival. The choice improves the efficiency and throughput, and minimizes execution time, of the call, but it also means that a pipelined pull request in quick succession, e.g. via smaxQueue(), may return a value on the pipeline client before this call is fully executed on the interactive Redis client.
(It is generally unlikely that you will follow this share call with a pipelined pull of the same variable. It would not only create superflous network traffic for no good reason, but it also would have unpredictable results. So, don't.)
table | Hash table name in which to share entry. |
key | Variable name under which the data is stored. |
value | Pointer to the buffer whose data is to be shared. |
type | SMA-X variable type, e.g. X_FLOAT or X_CHARS(40), of the buffer. |
count | Number of 1D elements. |
References smaxShareArray(), and X_SUCCESS.
int smaxShareArray | ( | const char * | table, |
const char * | key, | ||
const void * | ptr, | ||
XType | type, | ||
int | ndim, | ||
const int * | sizes | ||
) |
Share a multidimensional array, such as an int[][][]
, or float[][]
, in a single atomic transaction.
table | Hash table in which to write entry. |
key | Variable name under which the data is stored. |
ptr | Pointer to the data buffer, such as an int[][][] or float[][] . |
type | SMA-X variable type, e.g. X_FLOAT or X_CHARS(40), of the buffer. |
ndim | Dimensionality of the data (0 <= ndim <= X_MAX_DIMS). |
sizes | An array of ints containing the sizes along each dimension. |
References XField::isSerialized, XField::name, XField::ndim, REDISX_CMDBUF_SIZE, XField::sizes, smaxShareField(), smaxValuesToString(), TRUE, XField::type, XField::value, x_error(), X_MAX_ELEMENTS, X_NULL, X_RAW, X_SIZE_INVALID, X_STRUCT, X_SUCCESS, x_trace(), and xGetElementCount().
int smaxShareField | ( | const char * | table, |
const XField * | f | ||
) |
Share a field object, which may contain any SMA-X data type.
table | Hash table in which to write entry. |
f | Pointer for XField holding the data to share. |
References XField::name, smaxShareStruct(), XField::type, XField::value, X_NO_SERVICE, X_STRUCT, X_SUCCESS, x_trace(), and xGetAggregateID().
int smaxShareStruct | ( | const char * | id, |
const XStructure * | s | ||
) |
Share a structure, and all its data including recursive sub-structures, in a single atromic transaction.
id | Structure's ID, i.e. its own aggregated hash table name. |
s | Pointer to the structure data. |
References smaxCreateField(), X_NO_SERVICE, X_STRUCT, and X_SUCCESS.
int smaxSubscribe | ( | const char * | table, |
const char * | key | ||
) |
Subscribes to a specific key(s) in specific group(s). Both the group and key names may contain Redis subscription patterns, e.g. '*' or '?', or bound characters in square-brackets, e.g. '[ab]'. The subscription only enables receiving update notifications from Redis for the specified variable or variables. After subscribing, you can either wait on the subscribed variables to change, or add callback functions to process subscribed variables changes, via smaxAddSubscriber().
table | Variable group pattern, i.e. hash-table names. (NULL is the same as '*'). |
key | Variable name pattern. (if NULL then subscribes only to the table stem). |
References redisxSubscribe(), and X_SUCCESS.
int smaxUnsubscribe | ( | const char * | table, |
const char * | key | ||
) |
Unsubscribes from a specific key(s) in specific group(s). Both the group and key names may contain Redis subscription patterns, e.g. '*' or '?', or bound characters in square-brackets, e.g. '[ab]'. Unsubscribing will only stops the delivery of update notifications for the affected varuiables, but does not deactivate the associated callbacks for these added via smaxAddSubscriber(). Therefore you should also call smaxRemovesubscribers() as appropriate to deactivate actions that can no longer get triggered by updates.
table | Variable group pattern, i.e. structure or hash-table name(s) (NULL is the same as '*'). |
key | Variable name pattern. (if NULL then unsubscribes only from the table stem). |
References redisxUnsubscribe(), and X_SUCCESS.
int smaxWaitOnAnySubscribed | ( | char ** | changedTable, |
char ** | changedKey, | ||
int | timeout | ||
) |
Waits until any variable was pushed on any host, returning both the host and variable name for the updated value. The variable must be already subscribed to with smaxSubscribe(), or else the wait will not receive update notifications.
[out] | changedTable | Pointer to the variable that points to the string buffer for the returned table name or NULL. The lease of the buffer is for the call only. |
[out] | changedKey | Pointer to the variable that points to the string buffer for the returned variable name or NULL. The lease of the buffer is for the call only. |
[in] | timeout | (s) Timeout value. 0 or negative values result in an indefinite wait. |
References RELEASEID, smaxIsConnected(), x_error(), X_GROUP_INVALID, X_INCOMPLETE, X_INTERRUPTED, X_NAME_INVALID, X_NO_SERVICE, X_SEP_LENGTH, X_SUCCESS, xdprintf, xLastSeparator(), xStringCopyOf(), and xvprintf.