From 6265daea87513b79249de1b34f3a4e349f38f40a Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 1 May 2025 14:36:09 +0200 Subject: [PATCH] Documented and renamed participant registry --- Arduino/ArduinoParticipant.cpp | 4 +-- Participant.cpp | 12 ++++---- Participant.h | 50 +++++++++++++++++++++++---------- Participants/ParticipantUDP.cpp | 12 ++++---- Posix/PosixParticipant.cpp | 4 +-- Windows/WindowsParticipant.cpp | 4 +-- 6 files changed, 53 insertions(+), 33 deletions(-) diff --git a/Arduino/ArduinoParticipant.cpp b/Arduino/ArduinoParticipant.cpp index b283052..eff8f1b 100644 --- a/Arduino/ArduinoParticipant.cpp +++ b/Arduino/ArduinoParticipant.cpp @@ -72,9 +72,9 @@ void ParticipantUDP::Receive() { senderAddress.toCharArray(sender_ipAddress, 16); unsigned int sender_port = udp->remotePort(); - // Participant* remoteParticipant = this->GetParticipant(sender_ipAddress, + // Participant* remoteParticipant = this->Get(sender_ipAddress, // sender_port); if (remoteParticipant == nullptr) { - // remoteParticipant = this->AddParticipant(sender_ipAddress, + // remoteParticipant = this->Add(sender_ipAddress, // sender_port); // // std::cout << "New sender " << sender_ipAddress << ":" << sender_port // // << "\n"; diff --git a/Participant.cpp b/Participant.cpp index e8d33f8..0cbe739 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -111,7 +111,7 @@ void Participant::Remove(Thing* thing) { #pragma region ParticipantRegistry -Participant* ParticipantRegistry::GetParticipant(const char* ipAddress, +Participant* ParticipantRegistry::Get(const char* ipAddress, unsigned int port) { for (Participant* participant : ParticipantRegistry::participants) { if (participant == nullptr) @@ -128,7 +128,7 @@ Participant* ParticipantRegistry::GetParticipant(const char* ipAddress, return nullptr; } -Participant* ParticipantRegistry::GetParticipant(unsigned char participantId) { +Participant* ParticipantRegistry::Get(unsigned char participantId) { for (Participant* participant : ParticipantRegistry::participants) { if (participant == nullptr) continue; @@ -139,16 +139,16 @@ Participant* ParticipantRegistry::GetParticipant(unsigned char participantId) { return nullptr; } -Participant* ParticipantRegistry::AddParticipant(const char* ipAddress, +Participant* ParticipantRegistry::Add(const char* ipAddress, unsigned int port) { Participant* participant = new Participant(ipAddress, port); - AddParticipant(participant); + Add(participant); return participant; } -void ParticipantRegistry::AddParticipant(Participant* participant) { +void ParticipantRegistry::Add(Participant* participant) { Participant* foundParticipant = - GetParticipant(participant->ipAddress, participant->port); + Get(participant->ipAddress, participant->port); if (foundParticipant == nullptr) { #if defined(NO_STD) diff --git a/Participant.h b/Participant.h index 4bf393d..051589e 100644 --- a/Participant.h +++ b/Participant.h @@ -5,23 +5,43 @@ namespace RoboidControl { constexpr int MAX_THING_COUNT = 256; +/// @brief class which manages all known participants class ParticipantRegistry { - public: - Participant* GetParticipant(const char* ipAddress, unsigned int port); - Participant* GetParticipant(unsigned char participantId); - Participant* AddParticipant(const char* ipAddress, unsigned int port); - void AddParticipant(Participant* participant); + public: + /// @brief Retrieve a participant by its address + /// @param ipAddress The IP address of the participant + /// @param port The port number of the participant + /// @return The participant or a nullptr when it could not be found + Participant* Get(const char* ipAddress, unsigned int port); + /// @brief Retrieve a participant by its network ID + /// @param networkID The network ID of the participant + /// @return The participant or a nullptr when it could not be found + Participant* Get(unsigned char networkID); - void Remove(Participant* participant); + /// @brief Add a participant with the given details + /// @param ipAddress The IP address of the participant + /// @param port The port number of the participant + /// @return The added participant + Participant* Add(const char* ipAddress, unsigned int port); + /// @brief Add a participant + /// @param participant The participant to add + void Add(Participant* participant); - const std::list& GetAll() const; - private: - #if defined(NO_STD) - #else - /// @brief The list of known participants - std::list participants; - #endif - }; + /// @brief Remove a participant + /// @param participant The participant to remove + void Remove(Participant* participant); + + /// @brief Get all participants + /// @return All participants + const std::list& GetAll() const; + + private: +#if defined(NO_STD) +#else + /// @brief The list of known participants + std::list participants; +#endif +}; /// @brief A participant is a device which manages things. /// It can communicate with other participant to synchronise the state of @@ -70,7 +90,7 @@ class Participant { /// @param currentTimeMs The current time in milliseconds (optional) virtual void Update(unsigned long currentTimeMs = 0); -public: + public: static ParticipantRegistry registry; }; diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index a4d5859..01c2b86 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -15,7 +15,7 @@ ParticipantUDP::ParticipantUDP(int port) : Participant("127.0.0.1", port) { this->remoteSite = nullptr; if (this->port == 0) this->isIsolated = true; - Participant::registry.AddParticipant(this); + Participant::registry.Add(this); } ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) @@ -24,7 +24,7 @@ ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) this->isIsolated = true; else this->remoteSite = new Participant(ipAddress, port); - Participant::registry.AddParticipant(this); + Participant::registry.Add(this); } static ParticipantUDP* isolatedParticipant = nullptr; @@ -290,9 +290,9 @@ void ParticipantUDP::ReceiveData(unsigned char packetSize, unsigned int senderPort) { // std::cout << "Receive data from " << senderIpAddress << ":" << senderPort // << std::endl; - Participant* sender = this->registry.GetParticipant(senderIpAddress, senderPort); + Participant* sender = this->registry.Get(senderIpAddress, senderPort); if (sender == nullptr) { - sender = this->registry.AddParticipant(senderIpAddress, senderPort); + sender = this->registry.Add(senderIpAddress, senderPort); #if !defined(NO_STD) std::cout << "New remote participant " << sender->ipAddress << ":" << sender->port << std::endl; @@ -445,7 +445,7 @@ void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) { std::cout << this->name << ": process PoseMsg [" << (int)this->networkId << "/" << (int)msg->networkId << "] " << (int)msg->poseType << "\n"; #endif - Participant* owner = Participant::registry.GetParticipant(msg->networkId); + Participant* owner = Participant::registry.Get(msg->networkId); if (owner == nullptr) return; @@ -469,7 +469,7 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) { << "/" << (int)msg->thingId << "]\n"; #endif - Participant* owner = Participant::registry.GetParticipant(msg->networkId); + Participant* owner = Participant::registry.Get(msg->networkId); if (owner != nullptr) { Thing* thing = owner->Get(msg->thingId); if (thing != nullptr) diff --git a/Posix/PosixParticipant.cpp b/Posix/PosixParticipant.cpp index 88e18af..466035a 100644 --- a/Posix/PosixParticipant.cpp +++ b/Posix/PosixParticipant.cpp @@ -74,9 +74,9 @@ void Receive() { unsigned int sender_port = ntohs(client_addr.sin_port); ReceiveData(packetSize, sender_ipAddress, sender_port); - // RoboidControl::Participant* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port); + // RoboidControl::Participant* remoteParticipant = this->Get(sender_ipAddress, sender_port); // if (remoteParticipant == nullptr) { - // remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); + // remoteParticipant = this->Add(sender_ipAddress, sender_port); // // std::cout << "New sender " << sender_ipAddress << ":" << sender_port // // << "\n"; // // std::cout << "New remote participant " << remoteParticipant->ipAddress diff --git a/Windows/WindowsParticipant.cpp b/Windows/WindowsParticipant.cpp index d64eb1e..c6e99db 100644 --- a/Windows/WindowsParticipant.cpp +++ b/Windows/WindowsParticipant.cpp @@ -85,9 +85,9 @@ void ParticipantUDP::Receive() { unsigned int sender_port = ntohs(client_addr.sin_port); ReceiveData(packetSize, sender_ipAddress, sender_port); - // RoboidControl::ParticipantUDP* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port); + // RoboidControl::ParticipantUDP* remoteParticipant = this->Get(sender_ipAddress, sender_port); // if (remoteParticipant == nullptr) { - // remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); + // remoteParticipant = this->Add(sender_ipAddress, sender_port); // // std::cout << "New sender " << sender_ipAddress << ":" // // << sender_port << "\n"; // // std::cout << "New remote participant " <<