Uniot Core
0.8.1
Loading...
Searching...
No Matches
EventEntity.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2023 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
29
30#pragma once
31
32#include <IterableQueue.h>
33#include <Logger.h>
34
35#include "TypeId.h"
36
37namespace uniot {
38template <class T_topic, class T_msg, class T_data>
39class EventBus;
40
54template <class T_topic, class T_msg, class T_data>
55class EventEntity : public IWithType {
56 friend class EventBus<T_topic, T_msg, T_data>;
57
58 public:
66 using DataChannelCallback = std::function<void(unsigned int, bool, T_data)>;
67
74 virtual ~EventEntity();
75
83 virtual type_id getTypeId() const override {
85 }
86
98 bool sendDataToChannel(T_topic channel, T_data data) {
99 auto sentSomewhere = false;
100 this->mEventBusQueue.forEach([&](EventBus<T_topic, T_msg, T_data> *eventBus) {
101 auto sent = eventBus->sendDataToChannel(channel, data);
102 sentSomewhere |= sent;
103 yield();
104 });
105 return sentSomewhere;
106 }
107
117 void receiveDataFromChannel(T_topic channel, DataChannelCallback callback) {
118 this->mEventBusQueue.forEach([&](EventBus<T_topic, T_msg, T_data> *eventBus) {
119 if (callback) {
120 auto empty = eventBus->isDataChannelEmpty(channel);
121 auto data = eventBus->receiveDataFromChannel(channel);
122 callback(eventBus->getId(), empty, data);
123 }
124 yield();
125 });
126 }
127
128 protected:
140 mEventBusQueue.begin();
141 while (!mEventBusQueue.isEnd()) {
142 auto connectedEventBus = mEventBusQueue.current();
143 if (connectedEventBus->getId() == eventBus->getId()) {
144 UNIOT_LOG_INFO("EventBus with id %d already connected", eventBus->getId());
145 return false;
146 }
147 mEventBusQueue.next();
148 }
149 return mEventBusQueue.pushUnique(eventBus);
150 }
151
159};
160
161} // namespace uniot
Definition IterableQueue.h:36
Definition EventBus.h:75
unsigned int getId()
Gets the unique identifier of this EventBus.
Definition EventBus.h:94
T_data receiveDataFromChannel(T_topic topic)
Receives data from a specific data channel.
Definition EventBus.cpp:76
bool sendDataToChannel(T_topic topic, T_data data)
Sends data to a specific data channel.
Definition EventBus.cpp:71
bool isDataChannelEmpty(T_topic topic)
Checks if a data channel is empty.
Definition EventBus.cpp:81
Entity that can connect to and interact with EventBus instances.
Definition EventEntity.h:55
std::function< void(unsigned int, bool, T_data)> DataChannelCallback
Callback type for handling data received from channels.
Definition EventEntity.h:66
void receiveDataFromChannel(T_topic channel, DataChannelCallback callback)
Receives data from a specific channel on all connected EventBus instances.
Definition EventEntity.h:117
bool sendDataToChannel(T_topic channel, T_data data)
Sends data to a specific channel on all connected EventBus instances.
Definition EventEntity.h:98
virtual ~EventEntity()
Destructor - disconnects from all connected EventBus instances.
Definition EventEntity.cpp:27
bool connectUnique(EventBus< T_topic, T_msg, T_data > *eventBus)
Connects to an EventBus instance if not already connected.
Definition EventEntity.h:139
virtual type_id getTypeId() const override
Returns the type ID of this class.
Definition EventEntity.h:83
IterableQueue< EventBus< T_topic, T_msg, T_data > * > mEventBusQueue
Queue of EventBus instances this entity is connected to.
Definition EventEntity.h:158
Interface for objects that expose their runtime type information.
Definition TypeId.h:52
static type_id getTypeId()
Get the unique type identifier for a given type T.
Definition TypeId.h:82
#define UNIOT_LOG_INFO(...)
Log an INFO level message Used for general information about system operation. Only compiled if UNIOT...
Definition Logger.h:268
const void * type_id
Type alias for a unique type identifier.
Definition TypeId.h:43
Contains all classes and functions related to the Uniot Core.