Uniot Core
0.8.1
Loading...
Searching...
No Matches
EventBus.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
27
39
40#pragma once
41
42#include <Bytes.h>
43#include <ClearQueue.h>
44#include <DataChannels.h>
45#include <IExecutor.h>
46
48
49namespace uniot {
50template <class T_topic, class T_msg, class T_data>
51class EventEntity;
52
53template <class T_topic, class T_msg, class T_data>
54class EventEmitter;
55
56template <class T_topic, class T_msg, class T_data>
57class EventListener;
58
74template <class T_topic, class T_msg, class T_data>
75class EventBus : public uniot::IExecutor {
76 friend class EventEntity<T_topic, T_msg, T_data>;
77
78 public:
83 EventBus(unsigned int id) : mId(id) {}
84
88 virtual ~EventBus();
89
94 unsigned int getId() { return mId; }
95
104
111
120
127
138 bool openDataChannel(T_topic topic, size_t limit);
139
147 bool closeDataChannel(T_topic topic);
148
157 bool sendDataToChannel(T_topic topic, T_data data);
158
165 T_data receiveDataFromChannel(T_topic topic);
166
174 bool isDataChannelEmpty(T_topic topic);
175
184 void emitEvent(T_topic topic, T_msg msg);
185
194 virtual void execute(short _) override;
195
196 private:
197 // TODO: need real set; std::set is broken into esp xtensa sdk
198 ClearQueue<EventEntity<T_topic, T_msg, T_data> *> mEntities; // Stores registered entities (emitters and listeners)
199 ClearQueue<Pair<T_topic, T_msg>> mEvents; // Queue of pending events to be processed
200
201 DataChannels<T_topic, T_data> mDataChannels; // Storage for data channels
202
203 unsigned int mId; // Unique identifier for this event bus
204};
205
213} // namespace uniot
Defines the interface for components that can connect to an EventBus.
Definition ClearQueue.h:38
Definition DataChannels.h:43
Definition EventBus.h:75
EventBus(unsigned int id)
Constructs an EventBus with a unique identifier.
Definition EventBus.h:83
virtual void execute(short _) override
Processes all queued events.
Definition EventBus.cpp:91
bool registerEntity(EventEntity< T_topic, T_msg, T_data > *entity)
Registers an entity (emitter or listener) with this EventBus.
Definition EventBus.cpp:44
void unregisterKit(IEventBusConnectionKit< T_topic, T_msg, T_data > &connection)
Unregisters a connection kit from this EventBus.
Definition EventBus.cpp:39
void registerKit(IEventBusConnectionKit< T_topic, T_msg, T_data > &connection)
Registers a connection kit with this EventBus.
Definition EventBus.cpp:34
unsigned int getId()
Gets the unique identifier of this EventBus.
Definition EventBus.h:94
void unregisterEntity(EventEntity< T_topic, T_msg, T_data > *entity)
Unregisters an entity from this EventBus.
Definition EventBus.cpp:53
bool closeDataChannel(T_topic topic)
Closes a previously opened data channel.
Definition EventBus.cpp:66
virtual ~EventBus()
Destroys the EventBus and cleans up registered entities.
Definition EventBus.cpp:29
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
void emitEvent(T_topic topic, T_msg msg)
Emits an event to all registered listeners.
Definition EventBus.cpp:86
bool isDataChannelEmpty(T_topic topic)
Checks if a data channel is empty.
Definition EventBus.cpp:81
bool openDataChannel(T_topic topic, size_t limit)
Opens a data channel for a specific topic with a size limit.
Definition EventBus.cpp:61
Definition EventEmitter.h:41
Entity that can connect to and interact with EventBus instances.
Definition EventEntity.h:55
Definition EventListener.h:42
Definition IEventBusConnectionKit.h:52
Interface for executing tasks in the scheduler system.
Definition IExecutor.h:31
EventBus< unsigned int, int, Bytes > CoreEventBus
Standard EventBus configuration used throughout the core system.
Definition EventBus.h:211
Contains all classes and functions related to the Uniot Core.