项目作者: jocelynmass

项目描述 :
Event publisher/subscriber library
高级语言: C
项目地址: git://github.com/jocelynmass/event_bus.git
创建时间: 2019-05-23T02:17:25Z
项目社区:https://github.com/jocelynmass/event_bus

开源协议:Other

下载


Event Bus GitHub version License: MIT

Event publisher/subscriber library allowing to reduce code dependencies. Events can be executed from the event bus thread or from a dedicated thread, depending of the subscriber settings.

Init

  • Define event, an event is a uint32_t
  1. enum event_bus_id
  2. {
  3. EB_EVT1 = 0,
  4. EB_EVT2,
  5. };
  • Init Event Bus
  1. void main(void)
  2. {
  3. eb_t ebus;
  4. struct app_ctx app;
  5. eb_init(&ebus, &app);
  6. ...
  7. }

Direct API

This API allows to directly notify subscribers from the event bus context. Subscribers will be notified sequentially, meaning timely critical calls can’t be ensured as one subscriber can prevent the others to be executed.

Direct API Diagram

Usage:

  • Create a subscriber
  1. static int32_t custom_evt1_sub(void *app_ctx, uint32_t event_id, void *data, uint32 len, void *arg)
  2. {
  3. printf("received evt id = %d\n", event_id);
  4. return 0;
  5. }
  6. void foo_sub(void)
  7. {
  8. eb_sub_direct(&ebus, "custom_evt1", EB_EVT1, NULL, custom_evt1_sub);
  9. }
  • Create a publisher
  1. void foo_pub(void)
  2. {
  3. eb_pub(&ebus, EB_EVT1, NULL, 0, EVENT_BUS_LOW_PRIO);
  4. }

Indirect API

This API allows to indirectly notify subscribers. Event Bus will create a thread from which the subscribers will be called. In a case a subscriber would consume too much CPU, the remaining subscribers would be defered to a new thread. This would ensure subscribers to be executed in a maximum known latency (EB_MAX_SUB_LATENCY_MS * number of subscribers). eb_pub API now takes a priority flag. It can either be low or high priority. Events published as high priority will take over any other events already queued to event bus.

Direct API Diagram

Usage:

  • Create a subscriber
  1. static int32_t custom_evt1_sub(void *app_ctx, uint32_t event_id, void *data, uint32 len, void *arg)
  2. {
  3. printf("received evt id = %d\n", event_id);
  4. return 0;
  5. }
  6. void foo_sub(void)
  7. {
  8. eb_sub_indirect(&ebus, "custom_evt1", EB_EVT1, NULL, custom_evt1_sub);
  9. }
  • Create a publisher
  1. void foo_pub(void)
  2. {
  3. eb_pub(&ebus, EB_EVT1, NULL, 0, EVENT_BUS_LOW_PRIO);
  4. }

Passing data to subscribers

eb_pub can take data to be sent to subscribers. Keep in mind that data passed to the publisher is dynamically allocated and freed by event bus.

Usage:

  • Create a subscriber
  1. static int32_t custom_evt1_sub(void *app_ctx, uint32_t event_id, void *data, uint32 len, void *arg)
  2. {
  3. printf("received evt id = %d - len = %d - data = %s\n", event_id, len, char *data);
  4. return 0;
  5. }
  6. void foo(void)
  7. {
  8. eb_sub_indirect(&ebus, "custom_evt1", EB_EVT1, NULL, custom_evt1_sub);
  9. }
  • Create a publisher

```c
void foo(void)
{
eb_pub(&ebus, EB_EVT1, “hello world”, strlen(hello world) + 1, EVENT_BUS_LOW_PRIO);
}