Improved new thing processing

This commit is contained in:
Pascal Serrarens 2025-06-26 16:33:33 +02:00
parent 0ad9d3e9ec
commit 1add0647e1
14 changed files with 80 additions and 30 deletions

View File

@ -88,7 +88,7 @@ void ParticipantUDP::Receive() {
#endif #endif
} }
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { bool ParticipantUDP::SendTo(Participant* remoteParticipant, int bufferSize) {
#if defined(ARDUINO) && defined(HAS_WIFI) #if defined(ARDUINO) && defined(HAS_WIFI)
// std::cout << "Sending to:\n " << remoteParticipant->ipAddress << ":" // std::cout << "Sending to:\n " << remoteParticipant->ipAddress << ":"
// << remoteParticipant->port << "\n"; // << remoteParticipant->port << "\n";

View File

@ -9,7 +9,7 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
public: public:
void Setup(); void Setup();
void Receive(); void Receive();
bool Send(Participant* remoteParticipant, int bufferSize); bool SendTo(Participant* remoteParticipant, int bufferSize);
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
protected: protected:

View File

@ -130,7 +130,7 @@ void ParticipantUDP::Receive() {
#endif // IDF_VER #endif // IDF_VER
} }
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { bool ParticipantUDP::SendTo(Participant* remoteParticipant, int bufferSize) {
#if defined(IDF_VER) #if defined(IDF_VER)
// std::cout << "Sending to " << remoteParticipant->ipAddress << ":" // std::cout << "Sending to " << remoteParticipant->ipAddress << ":"
// << remoteParticipant->port << "\n"; // << remoteParticipant->port << "\n";

View File

@ -13,7 +13,7 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
public: public:
void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive(); void Receive();
bool Send(Participant* remoteParticipant, int bufferSize); bool SendTo(Participant* remoteParticipant, int bufferSize);
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
protected: protected:

View File

@ -49,10 +49,10 @@ Participant::~Participant() {
delete[] this->ipAddress; delete[] this->ipAddress;
} }
void Participant::Update() { void Participant::Update(bool recurse) {
for (Thing* thing : this->things) { for (Thing* thing : this->things) {
if (thing != nullptr) if (thing != nullptr)
thing->Update(true); thing->Update();
} }
} }
@ -67,26 +67,26 @@ bool Participant::Send(IMessage* msg) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
Windows::ParticipantUDP* thisWindows = Windows::ParticipantUDP* thisWindows =
static_cast<Windows::ParticipantUDP*>(this); static_cast<Windows::ParticipantUDP*>(this);
return thisWindows->Send(this, bufferSize); return thisWindows->SendTo(this, bufferSize);
#elif defined(__unix__) || defined(__APPLE__) #elif defined(__unix__) || defined(__APPLE__)
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this); Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this);
return thisPosix->Send(this, bufferSize); return thisPosix->SendTo(this, bufferSize);
#elif defined(ARDUINO) #elif defined(ARDUINO)
Arduino::ParticipantUDP* thisArduino = Arduino::ParticipantUDP* thisArduino =
static_cast<Arduino::ParticipantUDP*>(this); static_cast<Arduino::ParticipantUDP*>(this);
return thisArduino->Send(this, bufferSize); return thisArduino->SendTo(this, bufferSize);
#elif defined(IDF_VER) #elif defined(IDF_VER)
EspIdf::ParticipantUDP* thisEspIdf = EspIdf::ParticipantUDP* thisEspIdf =
static_cast<EspIdf::ParticipantUDP*>(this); static_cast<EspIdf::ParticipantUDP*>(this);
return thisEspIdf->Send(remoteParticipant, bufferSize); return thisEspIdf->SendTo(this, bufferSize);
#else #else
return false; return false;
#endif #endif
} }
Thing* Participant::Get(unsigned char thingId) { Thing* Participant::Get(unsigned char networkId, unsigned char thingId) {
for (Thing* thing : this->things) { for (Thing* thing : this->things) {
if (thing->id == thingId) if (thing->owner->networkId == networkId && 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
@ -115,7 +115,7 @@ void Participant::Add(Thing* thing, bool checkId) {
// std::cout << "Add thing with generated ID " << this->ipAddress << ":" // std::cout << "Add thing with generated ID " << this->ipAddress << ":"
// << this->port << "[" << (int)thing->id << "]\n"; // << this->port << "[" << (int)thing->id << "]\n";
} else { } else {
Thing* foundThing = Get(thing->id); Thing* foundThing = Get(thing->owner->networkId, thing->id);
if (foundThing == nullptr) { if (foundThing == nullptr) {
#if defined(NO_STD) #if defined(NO_STD)
this->things[this->thingCount++] = thing; this->things[this->thingCount++] = thing;

View File

@ -112,9 +112,10 @@ class Participant {
std::list<Thing*> things; std::list<Thing*> things;
#endif #endif
/// @brief Find a thing managed by this participant /// @brief Find a thing managed by this participant
/// @param networkId The network ID of the thing
/// @param thingId The ID of the thing /// @param thingId The ID of the thing
/// @return The thing if found, nullptr when no thing has been found /// @return The thing if found, nullptr when no thing has been found
Thing* Get(unsigned char thingId); Thing* Get(unsigned char networkId, unsigned char thingId);
/// @brief Add a new thing for this participant. /// @brief Add a new thing for this participant.
/// @param thing The thing to add /// @param thing The thing to add
/// @param checkId If true, the thing.id is regenerated if it is zero /// @param checkId If true, the thing.id is regenerated if it is zero
@ -129,7 +130,7 @@ class Participant {
public: public:
/// @brief Update all things for this participant /// @brief Update all things for this participant
virtual void Update(); virtual void Update(bool recurse = true);
#pragma endregion Update #pragma endregion Update

View File

@ -8,6 +8,10 @@
#include "Posix/PosixParticipant.h" #include "Posix/PosixParticipant.h"
#include "Windows/WindowsParticipant.h" #include "Windows/WindowsParticipant.h"
#include "Things/DistanceSensor.h"
#include "Things/TouchSensor.h"
#include "Things/DifferentialDrive.h"
#include <string.h> #include <string.h>
namespace RoboidControl { namespace RoboidControl {
@ -83,7 +87,7 @@ void ParticipantUDP::SetupUDP(int localPort,
// 1. receive external messages // 1. receive external messages
// 2. update the state // 2. update the state
// 3. send out the updated messages // 3. send out the updated messages
void ParticipantUDP::Update() { void ParticipantUDP::Update(bool recurse) {
unsigned long currentTimeMs = Thing::GetTimeMs(); unsigned long currentTimeMs = Thing::GetTimeMs();
if (this->isIsolated == false) { if (this->isIsolated == false) {
@ -182,7 +186,7 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
bool ParticipantUDP::Send(IMessage* msg) { bool ParticipantUDP::Send(IMessage* msg) {
if (this->remoteSite != nullptr) if (this->remoteSite != nullptr)
return this->remoteSite->Send(msg); return this->remoteSite->Send(msg);
return true; return true;
} }
@ -379,6 +383,43 @@ void ParticipantUDP::Process(Participant* sender, ThingMsg* msg) {
<< "/" << (int)msg->thingId << "] " << (int)msg->thingType << " " << "/" << (int)msg->thingId << "] " << (int)msg->thingType << " "
<< (int)msg->parentId << "\n"; << (int)msg->parentId << "\n";
#endif #endif
Participant* owner = Participant::registry.Get(msg->networkId);
if (owner == nullptr) {
owner = new Participant();
owner->networkId = msg->networkId;
Participant::registry.Add(owner);
}
Thing* thing = owner->Get(msg->networkId, msg->thingId);
if (thing == nullptr) {
bool isRemote = (sender->networkId != owner->networkId);
thing = ProcessNewThing(owner, msg, isRemote);
thing->id = msg->thingId;
thing->type = msg->thingType;
thing->isRemote = isRemote;
}
if (msg->parentId != 0) {
thing->SetParent(owner->Get(msg->networkId, msg->parentId));
if (thing->GetParent() == nullptr)
std::cout << "Could not find parent" << std::endl;
} else
thing->SetParent(nullptr);
}
Thing* ParticipantUDP::ProcessNewThing(Participant* owner,
ThingMsg* msg,
bool isRemote) {
switch (msg->thingType) {
case Thing::Type::DistanceSensor:
return new DistanceSensor(owner->root);
case Thing::Type::TouchSensor:
return new TouchSensor(owner->root);
case Thing::Type::DifferentialDrive:
return new DifferentialDrive(owner->root);
default:
return new Thing(owner->root);
}
} }
void ParticipantUDP::Process(Participant* sender, NameMsg* msg) { void ParticipantUDP::Process(Participant* sender, NameMsg* msg) {
@ -387,7 +428,7 @@ void ParticipantUDP::Process(Participant* sender, NameMsg* msg) {
<< (int)msg->thingId << "] "; << (int)msg->thingId << "] ";
#endif #endif
Thing* thing = sender->Get(msg->thingId); Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr) { if (thing != nullptr) {
int nameLength = msg->nameLength; int nameLength = msg->nameLength;
int stringLen = nameLength + 1; int stringLen = nameLength + 1;
@ -429,7 +470,7 @@ void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) {
if (owner == nullptr) if (owner == nullptr)
return; return;
Thing* thing = owner->Get(msg->thingId); Thing* thing = owner->Get(msg->networkId, msg->thingId);
if (thing == nullptr) if (thing == nullptr)
return; return;
@ -451,7 +492,7 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
Participant* owner = Participant::registry.Get(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->networkId, msg->thingId);
if (thing != nullptr) if (thing != nullptr)
thing->ProcessBinary(msg->data); thing->ProcessBinary(msg->data);
#if !defined(NO_STD) #if !defined(NO_STD)
@ -478,7 +519,7 @@ void ParticipantUDP::Process(Participant* sender, DestroyMsg* msg) {
<< (int)msg->thingId << "]\n"; << (int)msg->thingId << "]\n";
#endif #endif
Thing* thing = sender->Get(msg->thingId); Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr) if (thing != nullptr)
this->Remove(thing); this->Remove(thing);
} }

View File

@ -96,7 +96,7 @@ public:
#pragma region Update #pragma region Update
public: public:
virtual void Update() override; virtual void Update(bool recurse = true) override;
protected: protected:
unsigned long nextPublishMe = 0; unsigned long nextPublishMe = 0;
@ -133,7 +133,10 @@ protected:
virtual void Process(Participant* sender, ParticipantMsg* msg); virtual void Process(Participant* sender, ParticipantMsg* msg);
virtual void Process(Participant* sender, NetworkIdMsg* msg); virtual void Process(Participant* sender, NetworkIdMsg* msg);
virtual void Process(Participant* sender, InvestigateMsg* msg); virtual void Process(Participant* sender, InvestigateMsg* msg);
virtual void Process(Participant* sender, ThingMsg* msg); virtual void Process(Participant* sender, ThingMsg* msg);
virtual Thing* ProcessNewThing(Participant* sender, ThingMsg* msg, bool isRemote);
virtual void Process(Participant* sender, NameMsg* msg); virtual void Process(Participant* sender, NameMsg* msg);
virtual void Process(Participant* sender, ModelUrlMsg* msg); virtual void Process(Participant* sender, ModelUrlMsg* msg);
virtual void Process(Participant* sender, PoseMsg* msg); virtual void Process(Participant* sender, PoseMsg* msg);

View File

@ -70,16 +70,16 @@ void SiteServer::Process(Participant* sender, ParticipantMsg* msg) {
void SiteServer::Process(Participant* sender, NetworkIdMsg* msg) {} void SiteServer::Process(Participant* sender, NetworkIdMsg* msg) {}
void SiteServer::Process(Participant* sender, ThingMsg* msg) { void SiteServer::Process(Participant* sender, ThingMsg* msg) {
Thing* thing = sender->Get(msg->thingId); Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing == nullptr) if (thing == nullptr) {
// new Thing(sender, (Thing::Type)msg->thingType, msg->thingId); // new Thing(sender, (Thing::Type)msg->thingType, msg->thingId);
// Thing::Reconstruct(sender, msg->thingType, msg->thingId); // Thing::Reconstruct(sender, msg->thingType, msg->thingId);
//thing = new Thing(msg->thingType, sender->root); //thing = new Thing(msg->thingType, sender->root);
; }
thing->id = msg->thingId; thing->id = msg->thingId;
if (msg->parentId != 0) { if (msg->parentId != 0) {
thing->SetParent(Get(msg->parentId)); thing->SetParent(Get(msg->networkId, msg->parentId));
if (thing->IsRoot()) if (thing->IsRoot())
// if (thing->GetParent() != nullptr) // if (thing->GetParent() != nullptr)
#if defined(NO_STD) #if defined(NO_STD)

View File

@ -90,7 +90,7 @@ void ParticipantUDP::Receive() {
#endif #endif
} }
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { bool ParticipantUDP::SendTo(Participant* remoteParticipant, int bufferSize) {
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
// std::cout << "Send to " << remoteParticipant->ipAddress << ":" << ntohs(remoteParticipant->port) // std::cout << "Send to " << remoteParticipant->ipAddress << ":" << ntohs(remoteParticipant->port)
// << "\n"; // << "\n";

View File

@ -9,7 +9,7 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
public: public:
void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive(); void Receive();
bool Send(Participant* remoteParticipant, int bufferSize); bool SendTo(Participant* remoteParticipant, int bufferSize);
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
protected: protected:

View File

@ -88,6 +88,11 @@ class Thing {
/// This can be either a Thing::Type of a byte value for custom types /// This can be either a Thing::Type of a byte value for custom types
unsigned char type = Type::Undetermined; unsigned char type = Type::Undetermined;
/// @brief Is this a remote thing?
/// A remote thing is owned by other participant
/// and is not simulated by the local participant
bool isRemote = false;
/// @brief The participant owning this thing /// @brief The participant owning this thing
Participant* owner = nullptr; Participant* owner = nullptr;

View File

@ -102,7 +102,7 @@ void ParticipantUDP::Receive() {
#endif // _WIN32 || _WIN64 #endif // _WIN32 || _WIN64
} }
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { bool ParticipantUDP::SendTo(Participant* remoteParticipant, int bufferSize) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
char ip_str[INET_ADDRSTRLEN]; char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN);

View File

@ -9,7 +9,7 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
public: public:
void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive(); void Receive();
bool Send(Participant* remoteParticipant, int bufferSize); bool SendTo(Participant* remoteParticipant, int bufferSize);
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
protected: protected: