From 5367cc196196071f3536f243b0622fac8b51f2cf Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 14 Jan 2025 20:19:02 +0100 Subject: [PATCH 1/3] Trying but failing to use Register --- SiteServer.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ SiteServer.h | 1 + 2 files changed, 75 insertions(+) diff --git a/SiteServer.cpp b/SiteServer.cpp index e69de29..a1c5dee 100644 --- a/SiteServer.cpp +++ b/SiteServer.cpp @@ -0,0 +1,74 @@ +#include "SiteServer.h" + +#include "Sensors/TemperatureSensor.h" + +#include +#include +#include + +namespace Passer { +namespace Control { + +SiteServer::SiteServer(int port) { + this->name = "Site Server"; + + this->ipAddress = "0.0.0.0"; + this->port = port; + + this->participants.push_back(this); + + SetupUDP(port, ipAddress, 0); + + Register((unsigned char)Thing::Type::TemperatureSensor); +} + +void SiteServer::Update(unsigned long currentTimeMs) { + this->ReceiveUDP(); + Thing::UpdateAll(currentTimeMs); +} + +void SiteServer::Process(Participant *sender, ClientMsg *msg) { + if (msg->networkId == 0) { + std::cout << this->name << " received New Client -> " << sender->ipAddress + << " " << (int)sender->networkId << "\n"; + NetworkIdMsg *msg = new NetworkIdMsg(sender->networkId); + sender->Send(msg); + delete msg; + } +} + +void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {} + +using ThingConstructor = std::function( + unsigned char networkId, unsigned char thingId)>; +std::unordered_map thingMsgProcessors; + +template +void SiteServer::Register(unsigned char thingType) { + thingMsgProcessors[thingType] = [](unsigned char networkId, + unsigned char thingId) { + return std::make_unique(networkId, thingId); + }; +} + +// template +// void SiteServer::Register(Thing::Type thingType) +// { +// Register((unsigned char)thingType); +// } + +void SiteServer::Process(ThingMsg *msg) { + + Thing *thing = Thing::Get(msg->networkId, msg->thingId); + if (thing == nullptr) { + auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); + if (thingMsgProcessor != thingMsgProcessors.end()) // found item + thingMsgProcessor->second(msg->networkId, msg->thingId); + else + new Thing(this, msg->networkId, msg->thingId, + (Thing::Type)msg->thingType); + } +} + +} // namespace Control +} // namespace Passer diff --git a/SiteServer.h b/SiteServer.h index 95a6c29..3f38b51 100644 --- a/SiteServer.h +++ b/SiteServer.h @@ -13,6 +13,7 @@ public: virtual void Update(unsigned long currentTimeMs) override; template void Register(unsigned char thingType); + // template void Register(Thing::Type thingType); protected: unsigned long nextPublishMe = 0; From 7867c3c8cfb43185737fa9912dae5da1bc6186cb Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 20 Jan 2025 14:49:27 +0100 Subject: [PATCH 2/3] Working register --- SiteServer.cpp | 22 ---------------------- SiteServer.h | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/SiteServer.cpp b/SiteServer.cpp index a1c5dee..83149d0 100644 --- a/SiteServer.cpp +++ b/SiteServer.cpp @@ -2,10 +2,6 @@ #include "Sensors/TemperatureSensor.h" -#include -#include -#include - namespace Passer { namespace Control { @@ -39,24 +35,6 @@ void SiteServer::Process(Participant *sender, ClientMsg *msg) { void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {} -using ThingConstructor = std::function( - unsigned char networkId, unsigned char thingId)>; -std::unordered_map thingMsgProcessors; - -template -void SiteServer::Register(unsigned char thingType) { - thingMsgProcessors[thingType] = [](unsigned char networkId, - unsigned char thingId) { - return std::make_unique(networkId, thingId); - }; -} - -// template -// void SiteServer::Register(Thing::Type thingType) -// { -// Register((unsigned char)thingType); -// } - void SiteServer::Process(ThingMsg *msg) { Thing *thing = Thing::Get(msg->networkId, msg->thingId); diff --git a/SiteServer.h b/SiteServer.h index 3f38b51..507c8b9 100644 --- a/SiteServer.h +++ b/SiteServer.h @@ -2,6 +2,10 @@ #include "Participant.h" +#include +#include +#include + namespace Passer { namespace Control { @@ -12,8 +16,12 @@ public: virtual void Update(unsigned long currentTimeMs) override; - template void Register(unsigned char thingType); - // template void Register(Thing::Type thingType); + template void Register(unsigned char thingType) { + thingMsgProcessors[thingType] = [](unsigned char networkId, + unsigned char thingId) { + return std::make_unique(networkId, thingId); + }; + }; protected: unsigned long nextPublishMe = 0; @@ -21,6 +29,10 @@ protected: virtual void Process(Participant *sender, ClientMsg *msg) override; virtual void Process(Participant *sender, NetworkIdMsg *msg) override; virtual void Process(ThingMsg *msg) override; + +using ThingConstructor = std::function( + unsigned char networkId, unsigned char thingId)>; + std::unordered_map thingMsgProcessors; }; } // namespace Control From a8a09dbebf93f12e80aa0b888441610a6a1847ca Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Sun, 26 Jan 2025 20:33:12 +0100 Subject: [PATCH 3/3] thing name -> source id --- Participant.cpp | 14 ++++++++++++++ Participant.h | 1 + Thing.h | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Participant.cpp b/Participant.cpp index 25339e2..4fb6c72 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -181,6 +181,11 @@ void Participant::ReceiveData(unsigned char bufferSize, Process(msg); delete msg; } break; + case NameMsg::id: { + NameMsg *msg = new NameMsg(this->buffer); + Process(msg); + delete msg; + } break; case PoseMsg::id: { PoseMsg *msg = new PoseMsg(this->buffer); Process(msg); @@ -219,6 +224,15 @@ void Participant::Process(InvestigateMsg *msg) {} void Participant::Process(ThingMsg *msg) {} +void Participant::Process(NameMsg *msg) { + Thing *thing = Thing::Get(msg->networkId, msg->thingId); + if (thing != nullptr) { + thing->name = new char[strlen(msg->name)]; + strcpy(thing->name, msg->name); + std::cout << "thing name = " << thing->name << "\n"; + } +} + void Participant::Process(PoseMsg *msg) {} void Participant::Process(CustomMsg *msg) { diff --git a/Participant.h b/Participant.h index 9e6dc43..5e00490 100644 --- a/Participant.h +++ b/Participant.h @@ -87,6 +87,7 @@ protected: virtual void Process(Participant *sender, NetworkIdMsg *msg); virtual void Process(InvestigateMsg *msg); virtual void Process(ThingMsg *msg); + virtual void Process(NameMsg *msg); virtual void Process(PoseMsg *msg); virtual void Process(CustomMsg *msg); }; diff --git a/Thing.h b/Thing.h index 41431c2..b4b29de 100644 --- a/Thing.h +++ b/Thing.h @@ -73,7 +73,7 @@ protected: public: /// @brief The type of Thing unsigned char type = 0; - const char *name = nullptr; + char *name = nullptr; const char *modelUrl = nullptr; float modelScale = 1; // protected Sensor sensor;