ADD: assign a unique id to each connecting participant

This commit is contained in:
Dominik Meyer 2022-02-19 12:26:19 +01:00
parent 89f13445ba
commit b79a67b596
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
3 changed files with 30 additions and 1 deletions

View File

@ -75,6 +75,9 @@
/// mutex to protect participants_ /// mutex to protect participants_
std::mutex mutexParticipants_; std::mutex mutexParticipants_;
/// the id for the next participant connecting
std::uint32_t nextParticipantID_;
/* /*
* all private methods * all private methods
*/ */
@ -123,7 +126,8 @@
*/ */
Manager() : mainThread_(nullptr), isMainThreadRunning_(false), Manager() : mainThread_(nullptr), isMainThreadRunning_(false),
stopMainThread_(false), schedulingThread_(nullptr), stopMainThread_(false), schedulingThread_(nullptr),
isSchedulingThreadRunning_(false), stopSchedulingThread_(false){} isSchedulingThreadRunning_(false), stopSchedulingThread_(false),
nextParticipantID_(1){}
~Manager(); ~Manager();

View File

@ -31,6 +31,8 @@
class Participant : public std::enable_shared_from_this<Participant> class Participant : public std::enable_shared_from_this<Participant>
{ {
private: private:
/// a unique id for this participant, helpful for debugging
std::uint32_t id_;
/// pointer to the event manager /// pointer to the event manager
std::shared_ptr<EventManager::Manager> manager_; std::shared_ptr<EventManager::Manager> manager_;
@ -160,6 +162,23 @@
void setManager(std::shared_ptr<EventManager::Manager> manager) { manager_=manager;_subscribe(EVENT_TYPE_SHUTDOWN);} void setManager(std::shared_ptr<EventManager::Manager> 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 * @brief Method called by the EventManager::Manager to schedule the particpant
* *

View File

@ -346,6 +346,12 @@
std::lock_guard<std::mutex> guard(mutexParticipants_); std::lock_guard<std::mutex> guard(mutexParticipants_);
particpants_.push_back(participant); particpants_.push_back(participant);
participant->setManager(shared_from_this()); 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(); participant->init();
} }