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);
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";

View File

@ -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)

View File

@ -5,16 +5,36 @@ 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);
/// @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);
/// @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);
/// @brief Remove a participant
/// @param participant The participant to remove
void Remove(Participant* participant);
/// @brief Get all participants
/// @return All participants
const std::list<Participant*>& GetAll() const;
private:
#if defined(NO_STD)
#else

View File

@ -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)

View File

@ -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

View File

@ -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 " <<