static participant list

This commit is contained in:
Pascal Serrarens 2025-04-22 11:59:52 +02:00
parent 80127e0041
commit 619695c90d
5 changed files with 100 additions and 54 deletions

View File

@ -4,6 +4,8 @@
namespace RoboidControl { namespace RoboidControl {
std::list<Participant*> Participant::participants;
Participant::Participant() {} Participant::Participant() {}
Participant::Participant(const char* ipAddress, int port) { 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) { Thing* Participant::Get(unsigned char thingId) {
for (Thing* thing : this->things) { for (Thing* thing : this->things) {
if (thing->id == thingId) if (thing->id == thingId)
return thing; 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"; // << "[" << (int)thingId << "]\n";
return nullptr; return nullptr;
} }
@ -64,7 +117,8 @@ void Participant::Add(Thing* thing, bool checkId) {
#else #else
this->things.push_back(thing); this->things.push_back(thing);
#endif #endif
// std::cout << "Add thing " << this->ipAddress << ":" << this->port << "[" // std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
// "["
// << (int)thing->id << "]\n"; // << (int)thing->id << "]\n";
} else { } else {
// std::cout << "Did not add, existing thing " << this->ipAddress << ":" // std::cout << "Did not add, existing thing " << this->ipAddress << ":"

View File

@ -18,7 +18,7 @@ class Participant {
const char* ipAddress = "0.0.0.0"; const char* ipAddress = "0.0.0.0";
/// @brief The port number for UDP communication with the participant. This is /// @brief The port number for UDP communication with the participant. This is
/// 0 for isolated participants. /// 0 for isolated participants.
int port = 0; unsigned int port = 0;
/// @brief The network Id to identify the participant. /// @brief The network Id to identify the participant.
/// @note This field is likely to disappear in future versions /// @note This field is likely to disappear in future versions
@ -40,11 +40,19 @@ class Participant {
unsigned char thingCount = 0; unsigned char thingCount = 0;
Thing* things[MAX_THING_COUNT]; Thing* things[MAX_THING_COUNT];
#else #else
/// @brief The list of known participants
static std::list<Participant*> participants;
/// @brief The list of things managed by this participant /// @brief The list of things managed by this participant
std::list<Thing*> things; std::list<Thing*> things;
#endif #endif
public: 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 /// @brief Find a thing managed by this participant
/// @param networkId The network ID for the thing /// @param networkId The network ID for the thing
/// @param thingId The ID of the thing /// @param thingId The ID of the thing

View File

@ -31,6 +31,7 @@ ParticipantUDP::ParticipantUDP(int port) {
this->remoteSite = nullptr; this->remoteSite = nullptr;
if (this->port == 0) if (this->port == 0)
this->isIsolated = true; this->isIsolated = true;
Participant::AddParticipant(this);
} }
ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) 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; this->isIsolated = true;
else else
this->remoteSite = new Participant(ipAddress, port); this->remoteSite = new Participant(ipAddress, port);
Participant::AddParticipant(this);
} }
static ParticipantUDP* isolatedParticipant = nullptr; static ParticipantUDP* isolatedParticipant = nullptr;
@ -135,27 +137,6 @@ void ParticipantUDP::ReceiveUDP() {
#endif #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 #pragma region Send
void ParticipantUDP::SendThingInfo(Participant* remoteParticipant, void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
@ -179,14 +160,12 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
} }
bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) { 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); int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)
return true; return true;
std::cout << "send msg " << (int)this->buffer[0] << " to " // std::cout << "send msg " << (int)this->buffer[0] << " to "
<< remoteParticipant->ipAddress << std::endl; // << remoteParticipant->ipAddress << std::endl;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
Windows::ParticipantUDP* thisWindows = Windows::ParticipantUDP* thisWindows =
@ -259,6 +238,8 @@ bool ParticipantUDP::Publish(IMessage* msg) {
void ParticipantUDP::ReceiveData(unsigned char packetSize, void ParticipantUDP::ReceiveData(unsigned char packetSize,
char* senderIpAddress, char* senderIpAddress,
unsigned int senderPort) { unsigned int senderPort) {
std::cout << "Receive data from " << senderIpAddress << ":" << senderPort
<< std::endl;
Participant* sender = this->GetParticipant(senderIpAddress, senderPort); Participant* sender = this->GetParticipant(senderIpAddress, senderPort);
if (sender == nullptr) { if (sender == nullptr) {
sender = this->AddParticipant(senderIpAddress, senderPort); sender = this->AddParticipant(senderIpAddress, senderPort);
@ -346,12 +327,12 @@ void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) {
<< " -> " << (int)msg->networkId << "\n"; << " -> " << (int)msg->networkId << "\n";
#endif #endif
if (this->networkId != msg->networkId) { if (this->networkId != msg->networkId)
this->networkId = msg->networkId; this->networkId = msg->networkId;
// std::cout << this->things.size() << " things\n"; // std::cout << this->things.size() << " things\n";
for (Thing* thing : this->things) for (Thing* thing : this->things)
this->SendThingInfo(sender, thing); this->SendThingInfo(sender, thing);
}
} }
void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) { void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {
@ -421,7 +402,9 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
<< "/" << (int)msg->thingId << "] "; << "/" << (int)msg->thingId << "] ";
#endif #endif
Thing* thing = sender->Get(msg->thingId); Participant* owner = Participant::GetParticipant(msg->networkId);
if (owner != nullptr) {
Thing* thing = owner->Get(msg->thingId);
if (thing != nullptr) if (thing != nullptr)
thing->ProcessBinary(msg->data); thing->ProcessBinary(msg->data);
#if !defined(NO_STD) #if !defined(NO_STD)
@ -431,6 +414,7 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
} }
std::cout << std::endl; std::cout << std::endl;
#endif #endif
}
} }
// Receive // Receive

View File

@ -112,12 +112,12 @@ class ParticipantUDP : public Participant {
unsigned int senderPort); unsigned int senderPort);
void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant); void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant);
#if defined(NO_STD) // #if defined(NO_STD)
unsigned char senderCount = 0; // unsigned char senderCount = 0;
Participant* senders[MAX_SENDER_COUNT]; // Participant* senders[MAX_SENDER_COUNT];
#else // #else
std::list<Participant*> senders; // std::list<Participant*> senders;
#endif // #endif
protected: protected:
unsigned long nextPublishMe = 0; unsigned long nextPublishMe = 0;
@ -126,8 +126,8 @@ class ParticipantUDP : public Participant {
void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort); void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort);
Participant* GetParticipant(const char* ipAddress, int port); //Participant* GetParticipant(const char* ipAddress, int port);
Participant* AddParticipant(const char* ipAddress, int port); // Participant* AddParticipant(const char* ipAddress, int port);
void ReceiveUDP(); void ReceiveUDP();

View File

@ -16,11 +16,11 @@ SiteServer::SiteServer(int port) {
this->ipAddress = "0.0.0.0"; this->ipAddress = "0.0.0.0";
this->port = port; this->port = port;
#if defined(NO_STD) // #if defined(NO_STD)
this->senders[this->senderCount++] = this; // this->senders[this->senderCount++] = this;
#else // #else
this->senders.push_back(this); // this->senders.push_back(this);
#endif // #endif
SetupUDP(port, ipAddress, 0); SetupUDP(port, ipAddress, 0);