|
An **EventSniffer** is a class that can subscribe to a list of topics and when an event is received on one of those topics automatically calls a callback function.
|
|
An **EventSniffer** is a class that can subscribe to a list of topics and when an event is received on one of those topics automatically calls a callback function.
|
|
In fact in its constructor we have to provide a reference to the [EventBroker](EventBroker), the list (vector) of topics and the callback.
|
|
In fact in its constructor we have to provide a reference to the [EventBroker](EventBroker), the list (vector) of topics and the callback.
|
|
|
|
|
|
The callback type is defined as:
|
|
The callback type is defined as:
|
|
```cpp
|
|
```cpp
|
|
using OnEventReceived = function<void(uint8_t, uint8_t)>;
|
|
using OnEventReceived = function<void(uint8_t, uint8_t)>;
|
|
```
|
|
```
|
|
where the two parameters are the event ID and the topic on which that event has been received.
|
|
where the two parameters are the event ID and the topic on which that event has been received.
|
|
|
|
|
|
For each one of the specified topics, a **Sniffer** is created.
|
|
For each one of the specified topics, a **Sniffer** is created.
|
|
A Sniffer is a very simple class that extends [EventHandlerBase](EventHandler): when created it subscribes to the given topic and when an event is posted on that topic (i.e. its *postEvent(const Event& ev)* method is called) it calls the callback function (passing it the event ID and the topic).
|
|
A Sniffer is a very simple class that extends [EventHandlerBase](EventHandler): when created it subscribes to the given topic and when an event is posted on that topic (i.e. its _postEvent(const Event& ev)_ method is called) it calls the callback function (passing it the event ID and the topic).
|
|
|
|
|
|
You can find the complete implementation of the EventSniffer in `src/shared/events/utils/EventSniffer.h`.
|
|
You can find the complete implementation of the EventSniffer in `src/shared/events/utils/EventSniffer.h`.
|
|
|
|
|
|
## Example
|
|
## Example
|
|
This example shows the usage of the EventSniffer, in the simple case in which it is subscribed to a single topic.
|
|
This example shows the usage of the EventSniffer, in the simple case in which it is subscribed to a single topic.
|
|
|
|
|
|
First of all define the possible events and topics:
|
|
First of all define the possible events and topics:
|
|
```cpp
|
|
```cpp
|
|
#include <Common.h>
|
|
#include <Common.h>
|
... | | ... | |