From b79a67b596ca28bcb65968c94b1d893d9bda8923 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Sat, 19 Feb 2022 12:26:19 +0100 Subject: [PATCH] ADD: assign a unique id to each connecting participant --- include/EventManager/Manager.hpp | 6 +++++- include/EventManager/Participant.hpp | 19 +++++++++++++++++++ src/EventManager/Manager.cpp | 6 ++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/EventManager/Manager.hpp b/include/EventManager/Manager.hpp index 7a7f820..0d4ecc9 100644 --- a/include/EventManager/Manager.hpp +++ b/include/EventManager/Manager.hpp @@ -75,6 +75,9 @@ /// mutex to protect participants_ std::mutex mutexParticipants_; + /// the id for the next participant connecting + std::uint32_t nextParticipantID_; + /* * all private methods */ @@ -123,7 +126,8 @@ */ Manager() : mainThread_(nullptr), isMainThreadRunning_(false), stopMainThread_(false), schedulingThread_(nullptr), - isSchedulingThreadRunning_(false), stopSchedulingThread_(false){} + isSchedulingThreadRunning_(false), stopSchedulingThread_(false), + nextParticipantID_(1){} ~Manager(); diff --git a/include/EventManager/Participant.hpp b/include/EventManager/Participant.hpp index 5846ca7..6869943 100644 --- a/include/EventManager/Participant.hpp +++ b/include/EventManager/Participant.hpp @@ -31,6 +31,8 @@ class Participant : public std::enable_shared_from_this { private: + /// a unique id for this participant, helpful for debugging + std::uint32_t id_; /// pointer to the event manager std::shared_ptr manager_; @@ -160,6 +162,23 @@ void setManager(std::shared_ptr manager) { manager_=manager;_subscribe(EVENT_TYPE_SHUTDOWN);} + /** + * @brief Method to set the unique id of the participant + * + * This method is in general only used by the EventManager::Manager! + * Only use this method if you really know what you are doing! + * + * @param id - the id to set + */ + void setID(const std::uint32_t id) { id_=id;} + + /** + * @brief Method to return the unique id of the participant + * + * @return std::uint32_t + */ + std::uint32_t getID() const { return id_;} + /** * @brief Method called by the EventManager::Manager to schedule the particpant * diff --git a/src/EventManager/Manager.cpp b/src/EventManager/Manager.cpp index 09f8557..4cfbb38 100644 --- a/src/EventManager/Manager.cpp +++ b/src/EventManager/Manager.cpp @@ -346,6 +346,12 @@ std::lock_guard guard(mutexParticipants_); particpants_.push_back(participant); participant->setManager(shared_from_this()); + + // we can set and increment here because only one participant is in this critical + // section in any moment + participant->setID(nextParticipantID_); + nextParticipantID_++; + participant->init(); }