... | ... | @@ -6,9 +6,9 @@ In order to manage this events exchange, the broker maintains a map that associa |
|
|
map<uint8_t, vector<EventHandlerBase*>> subscribers;
|
|
|
```
|
|
|
|
|
|
The events that are posted instead, are put in an event queue. The active object thread takes care of removing events from the event queue and to forward them to all the subscribers, according to the vectors stored in the `subscribers` map.
|
|
|
The events that are posted to the broker are put in an event queue. The active object thread takes care of removing events from the event queue and to forward them to all the subscribers, according to the vectors stored in the `subscribers` map.
|
|
|
|
|
|
### Methods
|
|
|
## Methods
|
|
|
In addition to the [singleton](Singleton) and [active object](Active-Object) methods, the EventBroker exposes:
|
|
|
- **void post(const Event& ev, uint8_t topic)**: post an event to a specific topic.
|
|
|
- **uint16_t postDelayed(const Event& ev, uint8_t topic)**: post an event to a specific topic after a given delay in milliseconds (the delay can be passed as a template, e.g. postDelayed<100>(...)).
|
... | ... | @@ -17,12 +17,14 @@ In addition to the [singleton](Singleton) and [active object](Active-Object) met |
|
|
- **void unsubscribe(EventHandlerBase\* subscriber, uint8_t topic)**: unsubscribe the given subscriber to the specified topic. If no topic is passed, the unsubscribe the subscriber from every topic it was subscribed to.
|
|
|
- **void clearDelayedEvents()**: remove all pending events.
|
|
|
|
|
|
> :warning: **WARNING: note that the current *EventBroker* implementation does not support posting events that contain a payload. See [this page](Events) to know how events are structured.**
|
|
|
You can find the complete definition of the EventBroker in `src/shared/events/EventBroker.h`.
|
|
|
|
|
|
### Example
|
|
|
> :warning: **WARNING: note that the current *EventBroker* implementation does not support posting events that contain a payload. See the [events basics](Events) page to know how events are structured.**
|
|
|
|
|
|
## Example
|
|
|
This example shows how to define an EventHandler object that subscribes to a topic and received the events that are posted on that topic.
|
|
|
|
|
|
##### MyEventHandler.h
|
|
|
#### MyEventHandler.h
|
|
|
First of all we need to include the required dependencies. We also need to specify a list of possible events and topics.
|
|
|
```cpp
|
|
|
#pragma once
|
... | ... | @@ -83,8 +85,9 @@ private: |
|
|
};
|
|
|
```
|
|
|
|
|
|
##### test-eventbroker.cpp
|
|
|
#### test-eventbroker.cpp
|
|
|
In the main we need to create an instance of the EventBroker (it is a singleton object) and start it (it is also an active object).
|
|
|
Also, we need to start the custom EventHandler.
|
|
|
Then we can post some events, both on topic `TOPIC_1` and `TOPIC_2`.
|
|
|
```cpp
|
|
|
#include <Common.h>
|
... | ... | @@ -115,7 +118,7 @@ int main() |
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
##### Output
|
|
|
#### Output
|
|
|
What we can observe is that the program output will be only something like:
|
|
|
```sh
|
|
|
0.12> Received EV_1
|
... | ... | |