From 619695c90d4702e32857b30f6df50bda2c7c1813 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 22 Apr 2025 11:59:52 +0200 Subject: [PATCH] static participant list --- Participant.cpp | 58 +++++++++++++++++++++++++++++-- Participant.h | 10 +++++- Participants/ParticipantUDP.cpp | 60 ++++++++++++--------------------- Participants/ParticipantUDP.h | 16 ++++----- Participants/SiteServer.cpp | 10 +++--- 5 files changed, 100 insertions(+), 54 deletions(-) diff --git a/Participant.cpp b/Participant.cpp index 65a1f51..7166ad0 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -4,6 +4,8 @@ namespace RoboidControl { +std::list Participant::participants; + Participant::Participant() {} Participant::Participant(const char* ipAddress, int port) { @@ -34,12 +36,63 @@ void Participant::Update(unsigned long currentTimeMs) { } } +Participant* Participant::GetParticipant(const char* ipAddress, + unsigned int port) { + for (Participant* participant : Participant::participants) { + if (participant == nullptr) + continue; + if (strcmp(participant->ipAddress, ipAddress) == 0 && + participant->port == port) + return participant; + } + std::cout << "Could not find participant " << ipAddress << ":" << (int)port + << std::endl; + return nullptr; +} + +Participant* Participant::GetParticipant(unsigned char participantId) { + for (Participant* participant : Participant::participants) { + if (participant == nullptr) + continue; + if (participant->networkId == participantId) + return participant; + } + std::cout << "Could not find participant " << (int)participantId << std::endl; + return nullptr; +} + +Participant* Participant::AddParticipant(const char* ipAddress, unsigned int port) { + Participant* participant = new Participant(ipAddress, port); + Participant::AddParticipant(participant); + return participant; +} + +void Participant::AddParticipant(Participant* participant) { + Participant* foundParticipant = + Participant::GetParticipant(participant->networkId); + if (foundParticipant == nullptr) { +#if defined(NO_STD) + this->things[this->thingCount++] = thing; +#else + Participant::participants.push_back(participant); +#endif + std::cout << "Add participant " << participant->ipAddress << ":" + << participant->port << "[" << (int)participant->networkId + << "]\n"; + } else { + std::cout << "Did not add, existing thing " << participant->ipAddress << ":" + << participant->port << "[" << (int)participant->networkId + << "]\n"; + } +} + Thing* Participant::Get(unsigned char thingId) { for (Thing* thing : this->things) { if (thing->id == thingId) return thing; } - // std::cout << "Could not find thing " << this->ipAddress << ":" << this->port + // std::cout << "Could not find thing " << this->ipAddress << ":" << + // this->port // << "[" << (int)thingId << "]\n"; return nullptr; } @@ -64,7 +117,8 @@ void Participant::Add(Thing* thing, bool checkId) { #else this->things.push_back(thing); #endif - // std::cout << "Add thing " << this->ipAddress << ":" << this->port << "[" + // std::cout << "Add thing " << this->ipAddress << ":" << this->port << + // "[" // << (int)thing->id << "]\n"; } else { // std::cout << "Did not add, existing thing " << this->ipAddress << ":" diff --git a/Participant.h b/Participant.h index 5fdffc4..be7133e 100644 --- a/Participant.h +++ b/Participant.h @@ -18,7 +18,7 @@ class Participant { const char* ipAddress = "0.0.0.0"; /// @brief The port number for UDP communication with the participant. This is /// 0 for isolated participants. - int port = 0; + unsigned int port = 0; /// @brief The network Id to identify the participant. /// @note This field is likely to disappear in future versions @@ -40,11 +40,19 @@ class Participant { unsigned char thingCount = 0; Thing* things[MAX_THING_COUNT]; #else + /// @brief The list of known participants + static std::list participants; + /// @brief The list of things managed by this participant std::list things; #endif public: + static Participant* GetParticipant(const char* ipAddress, unsigned int port); + static Participant* GetParticipant(unsigned char participantId); + static Participant* AddParticipant(const char* ipAddress, unsigned int port); + static void AddParticipant(Participant* participant); + /// @brief Find a thing managed by this participant /// @param networkId The network ID for the thing /// @param thingId The ID of the thing diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index 4554483..da55b48 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -31,6 +31,7 @@ ParticipantUDP::ParticipantUDP(int port) { this->remoteSite = nullptr; if (this->port == 0) this->isIsolated = true; + Participant::AddParticipant(this); } ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) @@ -39,6 +40,7 @@ ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) this->isIsolated = true; else this->remoteSite = new Participant(ipAddress, port); + Participant::AddParticipant(this); } static ParticipantUDP* isolatedParticipant = nullptr; @@ -135,27 +137,6 @@ void ParticipantUDP::ReceiveUDP() { #endif } -Participant* ParticipantUDP::GetParticipant(const char* ipAddress, int port) { - for (Participant* sender : this->senders) { - if (strcmp(sender->ipAddress, ipAddress) == 0 && sender->port == port) - return sender; - } - return nullptr; -} - -Participant* ParticipantUDP::AddParticipant(const char* ipAddress, int port) { - // std::cout << "New Participant " << ipAddress << ":" << port << "\n"; - Participant* participant = new Participant(ipAddress, port); -#if defined(NO_STD) - participant->networkId = this->senderCount; - this->senders[this->senderCount++] = participant; -#else - participant->networkId = (unsigned char)this->senders.size(); - this->senders.push_back(participant); -#endif - return participant; -} - #pragma region Send void ParticipantUDP::SendThingInfo(Participant* remoteParticipant, @@ -179,14 +160,12 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant, } bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) { - // std::cout << "send msg " << (int)this->buffer[0] << " to " - // << remoteParticipant->ipAddress << std::endl; int bufferSize = msg->Serialize(this->buffer); if (bufferSize <= 0) return true; - std::cout << "send msg " << (int)this->buffer[0] << " to " - << remoteParticipant->ipAddress << std::endl; + // std::cout << "send msg " << (int)this->buffer[0] << " to " + // << remoteParticipant->ipAddress << std::endl; #if defined(_WIN32) || defined(_WIN64) Windows::ParticipantUDP* thisWindows = @@ -259,6 +238,8 @@ bool ParticipantUDP::Publish(IMessage* msg) { void ParticipantUDP::ReceiveData(unsigned char packetSize, char* senderIpAddress, unsigned int senderPort) { + std::cout << "Receive data from " << senderIpAddress << ":" << senderPort + << std::endl; Participant* sender = this->GetParticipant(senderIpAddress, senderPort); if (sender == nullptr) { sender = this->AddParticipant(senderIpAddress, senderPort); @@ -346,12 +327,12 @@ void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) { << " -> " << (int)msg->networkId << "\n"; #endif - if (this->networkId != msg->networkId) { + if (this->networkId != msg->networkId) this->networkId = msg->networkId; - // std::cout << this->things.size() << " things\n"; - for (Thing* thing : this->things) - this->SendThingInfo(sender, thing); - } + + // std::cout << this->things.size() << " things\n"; + for (Thing* thing : this->things) + this->SendThingInfo(sender, thing); } void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) { @@ -421,16 +402,19 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) { << "/" << (int)msg->thingId << "] "; #endif - Thing* thing = sender->Get(msg->thingId); - if (thing != nullptr) - thing->ProcessBinary(msg->data); + Participant* owner = Participant::GetParticipant(msg->networkId); + if (owner != nullptr) { + Thing* thing = owner->Get(msg->thingId); + if (thing != nullptr) + thing->ProcessBinary(msg->data); #if !defined(NO_STD) - else { - std::cout << " unknown thing [" << (int)msg->networkId << "/" - << (int)msg->thingId << "]"; - } - std::cout << std::endl; + else { + std::cout << " unknown thing [" << (int)msg->networkId << "/" + << (int)msg->thingId << "]"; + } + std::cout << std::endl; #endif + } } // Receive diff --git a/Participants/ParticipantUDP.h b/Participants/ParticipantUDP.h index ec93af9..f50187e 100644 --- a/Participants/ParticipantUDP.h +++ b/Participants/ParticipantUDP.h @@ -112,12 +112,12 @@ class ParticipantUDP : public Participant { unsigned int senderPort); void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant); -#if defined(NO_STD) - unsigned char senderCount = 0; - Participant* senders[MAX_SENDER_COUNT]; -#else - std::list senders; -#endif +// #if defined(NO_STD) +// unsigned char senderCount = 0; +// Participant* senders[MAX_SENDER_COUNT]; +// #else +// std::list senders; +// #endif protected: unsigned long nextPublishMe = 0; @@ -126,8 +126,8 @@ class ParticipantUDP : public Participant { void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort); - Participant* GetParticipant(const char* ipAddress, int port); - Participant* AddParticipant(const char* ipAddress, int port); + //Participant* GetParticipant(const char* ipAddress, int port); + // Participant* AddParticipant(const char* ipAddress, int port); void ReceiveUDP(); diff --git a/Participants/SiteServer.cpp b/Participants/SiteServer.cpp index 8bde494..e300371 100644 --- a/Participants/SiteServer.cpp +++ b/Participants/SiteServer.cpp @@ -16,11 +16,11 @@ SiteServer::SiteServer(int port) { this->ipAddress = "0.0.0.0"; this->port = port; -#if defined(NO_STD) - this->senders[this->senderCount++] = this; -#else - this->senders.push_back(this); -#endif +// #if defined(NO_STD) +// this->senders[this->senderCount++] = this; +// #else +// this->senders.push_back(this); +// #endif SetupUDP(port, ipAddress, 0);