Sturk 1.0.2
Publish-subscribe C implementation.
|
Message broker. More...
#include <stdarg.h>
#include <stddef.h>
Go to the source code of this file.
Data Structures | |
struct | StLoadVt |
Vtable for message construction. More... | |
Typedefs | |
typedef char | StLoad |
Opaque data type that represents the message load. | |
typedef struct StBroker | StBroker |
The message broker. | |
typedef struct StSubscriber | StSubscriber |
The subscriber. | |
typedef struct StChannel | StChannel |
The channel for messages. | |
Functions | |
void | st_publish (StChannel *ch,...) |
Broadcast the message. | |
void | st_subscribe (StSubscriber *sber, const char *topic) |
Subscribe to a topic. | |
StBroker * | st_broker_create (const struct StLoadVt *vp) |
Create the message broker. | |
void | st_broker_destroy (StBroker *broker) |
Destroy the message broker. | |
StChannel * | st_broker_search (StBroker *broker, const char *topic) |
Find the channel that is assigned to the given topic. | |
const char * | st_channel_gettopic (const StChannel *ch) |
Get the topic for the given channel. | |
StSubscriber * | st_subscriber_create (StBroker *broker) |
Create the subscriber. | |
void | st_subscriber_destroy (StSubscriber *sber) |
Destroy the subscriber. | |
StLoad * | st_subscriber_await (StSubscriber *sber) |
Wait for the messages that are wanted by the subscriber. | |
StLoad * | st_subscriber_poll (StSubscriber *sber) |
Poll for the messages that are wanted by the subscriber. | |
void | st_subscriber_unload (StSubscriber *sber) |
Inform the broker that the message can be released for the given subscriber. | |
StChannel * | st_load_getchan (const StLoad *load) |
Get the source channel of the message. | |
Message broker.
This header file provides data types and functions that implement the publish-subscribe messaging pattern.
The message broker.
The broker holds the list of all subscribers (StSubscriber) in usage and a dictionary of channels (StChannel). All the messaging done through channels created by the same broker will also use the same API for message construction (StLoadVt).
The channel for messages.
typedef char StLoad |
Opaque data type that represents the message load.
The memory for each message has two contexts:
It is for the user to decide how to use those contexts by defining the message constructor (StLoadVt::ctor).
Direct context is a contiguous memory block allocated from fixed-size memory pool and its size is constant for all messages. The size of the block is a multiple of the size of the metadata, big enough to hold one instance of the metadata and one instance of the user defined load. The size of the load for the direct context is defined with the StLoadVt::size callback.
Array | meta + load (StLoad*) |
---|---|
0 | meta |
... | load |
n-1 | |
n |
The indirect context is optional and it is everything that is allocated by the contructor callback - StLoadVt::ctor and that is accessible through pointers placed somewhere in the direct context.
Create the message broker.
[in] | vp | The pointer to the vtable for the StLoad. |
The chosen vtable will influence the behaviour of the functions that are responsible for constructing and receiving the messages:
void st_broker_destroy | ( | StBroker * | broker | ) |
Destroy the message broker.
[in] | broker | The pointer to the broker. |
Find the channel that is assigned to the given topic.
[in,out] | broker | The message broker. |
[in] | topic | The topic. It is also used as the key for the channel's dictionary. |
This function also creates the channel if none is found.
const char * st_channel_gettopic | ( | const StChannel * | ch | ) |
Get the topic for the given channel.
[in] | ch | The channel. |
Get the source channel of the message.
[in] | load | The pointer to the message load. |
void st_publish | ( | StChannel * | ch, |
... | |||
) |
Broadcast the message.
[in,out] | ch | The channel to which the message is sent. |
[in] | ... | The list of arguments used by the StLoadVt::ctor. |
void st_subscribe | ( | StSubscriber * | sber, |
const char * | topic | ||
) |
Subscribe to a topic.
[in,out] | sber | The subscriber that expresses the interest in the topic. |
[in] | topic | The topic to which the subscription will be made. |
StLoad * st_subscriber_await | ( | StSubscriber * | sber | ) |
Wait for the messages that are wanted by the subscriber.
[in,out] | sber | The pointer to the subscriber. |
This function will receive the message immediately and return the load, if the subscriber's message queue is not empty. Otherwise, with multithreading enabled, this will block the thread that has called this function until some other thread publishes to topic that the given subscriber is interested in. With a single thread application, the blocking is not supported.
StSubscriber * st_subscriber_create | ( | StBroker * | broker | ) |
Create the subscriber.
[in,out] | broker | The message broker. |
void st_subscriber_destroy | ( | StSubscriber * | sber | ) |
Destroy the subscriber.
[in,out] | sber | The pointer to the subscriber. |
StLoad * st_subscriber_poll | ( | StSubscriber * | sber | ) |
Poll for the messages that are wanted by the subscriber.
[in,out] | sber | The pointer to the subscriber. |
This function will receive the message and return the load, if the subscriber's message queue is not empty. Otherwise, it will return NULL.
void st_subscriber_unload | ( | StSubscriber * | sber | ) |
Inform the broker that the message can be released for the given subscriber.
[in,out] | sber | The pointer to the subscriber. |
Release the last message received by the subscriber - decrement the subscribers counter for the message. When the counter hits 0, the message is returned to the memory pool.