Documented and renamed participant registry

This commit is contained in:
Pascal Serrarens 2025-05-01 14:36:09 +02:00
parent 2d77aa2461
commit 6265daea87
6 changed files with 53 additions and 33 deletions

View File

@ -72,9 +72,9 @@ void ParticipantUDP::Receive() {
senderAddress.toCharArray(sender_ipAddress, 16); senderAddress.toCharArray(sender_ipAddress, 16);
unsigned int sender_port = udp->remotePort(); unsigned int sender_port = udp->remotePort();
// Participant* remoteParticipant = this->GetParticipant(sender_ipAddress, // Participant* remoteParticipant = this->Get(sender_ipAddress,
// sender_port); if (remoteParticipant == nullptr) { // sender_port); if (remoteParticipant == nullptr) {
// remoteParticipant = this->AddParticipant(sender_ipAddress, // remoteParticipant = this->Add(sender_ipAddress,
// sender_port); // sender_port);
// // std::cout << "New sender " << sender_ipAddress << ":" << sender_port // // std::cout << "New sender " << sender_ipAddress << ":" << sender_port
// // << "\n"; // // << "\n";

View File

@ -111,7 +111,7 @@ void Participant::Remove(Thing* thing) {
#pragma region ParticipantRegistry #pragma region ParticipantRegistry
Participant* ParticipantRegistry::GetParticipant(const char* ipAddress, Participant* ParticipantRegistry::Get(const char* ipAddress,
unsigned int port) { unsigned int port) {
for (Participant* participant : ParticipantRegistry::participants) { for (Participant* participant : ParticipantRegistry::participants) {
if (participant == nullptr) if (participant == nullptr)
@ -128,7 +128,7 @@ Participant* ParticipantRegistry::GetParticipant(const char* ipAddress,
return nullptr; return nullptr;
} }
Participant* ParticipantRegistry::GetParticipant(unsigned char participantId) { Participant* ParticipantRegistry::Get(unsigned char participantId) {
for (Participant* participant : ParticipantRegistry::participants) { for (Participant* participant : ParticipantRegistry::participants) {
if (participant == nullptr) if (participant == nullptr)
continue; continue;
@ -139,16 +139,16 @@ Participant* ParticipantRegistry::GetParticipant(unsigned char participantId) {
return nullptr; return nullptr;
} }
Participant* ParticipantRegistry::AddParticipant(const char* ipAddress, Participant* ParticipantRegistry::Add(const char* ipAddress,
unsigned int port) { unsigned int port) {
Participant* participant = new Participant(ipAddress, port); Participant* participant = new Participant(ipAddress, port);
AddParticipant(participant); Add(participant);
return participant; return participant;
} }
void ParticipantRegistry::AddParticipant(Participant* participant) { void ParticipantRegistry::Add(Participant* participant) {
Participant* foundParticipant = Participant* foundParticipant =
GetParticipant(participant->ipAddress, participant->port); Get(participant->ipAddress, participant->port);
if (foundParticipant == nullptr) { if (foundParticipant == nullptr) {
#if defined(NO_STD) #if defined(NO_STD)

View File

@ -5,23 +5,43 @@ namespace RoboidControl {
constexpr int MAX_THING_COUNT = 256; constexpr int MAX_THING_COUNT = 256;
/// @brief class which manages all known participants
class ParticipantRegistry { class ParticipantRegistry {
public: public:
Participant* GetParticipant(const char* ipAddress, unsigned int port); /// @brief Retrieve a participant by its address
Participant* GetParticipant(unsigned char participantId); /// @param ipAddress The IP address of the participant
Participant* AddParticipant(const char* ipAddress, unsigned int port); /// @param port The port number of the participant
void AddParticipant(Participant* 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<Participant*>& GetAll() const; /// @brief Remove a participant
private: /// @param participant The participant to remove
#if defined(NO_STD) void Remove(Participant* participant);
#else
/// @brief The list of known participants /// @brief Get all participants
std::list<Participant*> participants; /// @return All participants
#endif const std::list<Participant*>& GetAll() const;
};
private:
#if defined(NO_STD)
#else
/// @brief The list of known participants
std::list<Participant*> participants;
#endif
};
/// @brief A participant is a device which manages things. /// @brief A participant is a device which manages things.
/// It can communicate with other participant to synchronise the state of /// 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) /// @param currentTimeMs The current time in milliseconds (optional)
virtual void Update(unsigned long currentTimeMs = 0); virtual void Update(unsigned long currentTimeMs = 0);
public: public:
static ParticipantRegistry registry; static ParticipantRegistry registry;
}; };

View File

@ -15,7 +15,7 @@ ParticipantUDP::ParticipantUDP(int port) : Participant("127.0.0.1", port) {
this->remoteSite = nullptr; this->remoteSite = nullptr;
if (this->port == 0) if (this->port == 0)
this->isIsolated = true; this->isIsolated = true;
Participant::registry.AddParticipant(this); Participant::registry.Add(this);
} }
ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) 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; this->isIsolated = true;
else else
this->remoteSite = new Participant(ipAddress, port); this->remoteSite = new Participant(ipAddress, port);
Participant::registry.AddParticipant(this); Participant::registry.Add(this);
} }
static ParticipantUDP* isolatedParticipant = nullptr; static ParticipantUDP* isolatedParticipant = nullptr;
@ -290,9 +290,9 @@ void ParticipantUDP::ReceiveData(unsigned char packetSize,
unsigned int senderPort) { unsigned int senderPort) {
// std::cout << "Receive data from " << senderIpAddress << ":" << senderPort // std::cout << "Receive data from " << senderIpAddress << ":" << senderPort
// << std::endl; // << std::endl;
Participant* sender = this->registry.GetParticipant(senderIpAddress, senderPort); Participant* sender = this->registry.Get(senderIpAddress, senderPort);
if (sender == nullptr) { if (sender == nullptr) {
sender = this->registry.AddParticipant(senderIpAddress, senderPort); sender = this->registry.Add(senderIpAddress, senderPort);
#if !defined(NO_STD) #if !defined(NO_STD)
std::cout << "New remote participant " << sender->ipAddress << ":" std::cout << "New remote participant " << sender->ipAddress << ":"
<< sender->port << std::endl; << sender->port << std::endl;
@ -445,7 +445,7 @@ void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) {
std::cout << this->name << ": process PoseMsg [" << (int)this->networkId std::cout << this->name << ": process PoseMsg [" << (int)this->networkId
<< "/" << (int)msg->networkId << "] " << (int)msg->poseType << "\n"; << "/" << (int)msg->networkId << "] " << (int)msg->poseType << "\n";
#endif #endif
Participant* owner = Participant::registry.GetParticipant(msg->networkId); Participant* owner = Participant::registry.Get(msg->networkId);
if (owner == nullptr) if (owner == nullptr)
return; return;
@ -469,7 +469,7 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
<< "/" << (int)msg->thingId << "]\n"; << "/" << (int)msg->thingId << "]\n";
#endif #endif
Participant* owner = Participant::registry.GetParticipant(msg->networkId); Participant* owner = Participant::registry.Get(msg->networkId);
if (owner != nullptr) { if (owner != nullptr) {
Thing* thing = owner->Get(msg->thingId); Thing* thing = owner->Get(msg->thingId);
if (thing != nullptr) if (thing != nullptr)

View File

@ -74,9 +74,9 @@ void Receive() {
unsigned int sender_port = ntohs(client_addr.sin_port); unsigned int sender_port = ntohs(client_addr.sin_port);
ReceiveData(packetSize, sender_ipAddress, sender_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) { // 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 // // std::cout << "New sender " << sender_ipAddress << ":" << sender_port
// // << "\n"; // // << "\n";
// // std::cout << "New remote participant " << remoteParticipant->ipAddress // // std::cout << "New remote participant " << remoteParticipant->ipAddress

View File

@ -85,9 +85,9 @@ void ParticipantUDP::Receive() {
unsigned int sender_port = ntohs(client_addr.sin_port); unsigned int sender_port = ntohs(client_addr.sin_port);
ReceiveData(packetSize, sender_ipAddress, sender_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) { // if (remoteParticipant == nullptr) {
// remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); // remoteParticipant = this->Add(sender_ipAddress, sender_port);
// // std::cout << "New sender " << sender_ipAddress << ":" // // std::cout << "New sender " << sender_ipAddress << ":"
// // << sender_port << "\n"; // // << sender_port << "\n";
// // std::cout << "New remote participant " << // // std::cout << "New remote participant " <<