diff --git a/LocalParticipant.cpp b/LocalParticipant.cpp index 42e11f9..ba12dec 100644 --- a/LocalParticipant.cpp +++ b/LocalParticipant.cpp @@ -271,7 +271,7 @@ void LocalParticipant::ReceiveData(unsigned char packetSize, void LocalParticipant::ReceiveData(unsigned char bufferSize, Participant* remoteParticipant) { unsigned char msgId = this->buffer[0]; - //std::cout << "receive msg " << (int)msgId << "\n"; + // std::cout << "receive msg " << (int)msgId << "\n"; switch (msgId) { case ParticipantMsg::id: { ParticipantMsg* msg = new ParticipantMsg(this->buffer); @@ -314,8 +314,8 @@ void LocalParticipant::ReceiveData(unsigned char bufferSize, void LocalParticipant::Process(Participant* sender, ParticipantMsg* msg) {} void LocalParticipant::Process(Participant* sender, SiteMsg* msg) { - std::cout << this->name << ": process NetworkId [" << (int)this->networkId - << "/" << (int)msg->networkId << "]\n"; + std::cout << this->name << ": process Site Id " << (int)this->networkId + << "->" << (int)msg->networkId << "\n"; if (this->networkId != msg->networkId) { this->networkId = msg->networkId; // std::cout << this->things.size() << " things\n"; @@ -327,12 +327,24 @@ void LocalParticipant::Process(Participant* sender, SiteMsg* msg) { void LocalParticipant::Process(Participant* sender, InvestigateMsg* msg) {} void LocalParticipant::Process(Participant* sender, ThingMsg* msg) { - std::cout << this->name << ": process Thing [" << (int)this->networkId << "/" << (int)msg->networkId << "]\n"; + std::cout << this->name << ": process Thing [" << (int)msg->networkId << "/" + << (int)msg->thingId << "]\n"; +#if !defined(NO_STD) + auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); + //Thing* newThing; + if (thingMsgProcessor != thingMsgProcessors.end()) { // found item + //newThing = + thingMsgProcessor->second(sender, msg->networkId, msg->thingId); + } else + //newThing = + new Thing(sender, msg->networkId, msg->thingId, + (Thing::Type)msg->thingType); +#endif } void LocalParticipant::Process(Participant* sender, NameMsg* msg) { - std::cout << this->name << ": process Name [" << (int)this->networkId - << "/" << (int)msg->networkId << "]\n"; + std::cout << this->name << ": process Name [" << (int)msg->networkId << "/" + << (int)msg->thingId << "]\n"; Thing* thing = sender->Get(msg->networkId, msg->thingId); if (thing != nullptr) { @@ -351,17 +363,20 @@ void LocalParticipant::Process(Participant* sender, NameMsg* msg) { #endif thingName[nameLength] = '\0'; thing->name = thingName; - // std::cout << "thing name = " << thing->name << " length = " << nameLength + // std::cout << "thing name = " << thing->name << " length = " << + // nameLength // << "\n"; } } void LocalParticipant::Process(Participant* sender, PoseMsg* msg) { - std::cout << this->name << ": process Pose [" << (int)this->networkId << "/" << (int)msg->networkId << "]\n"; + std::cout << this->name << ": process Pose [" << (int)this->networkId << "/" + << (int)msg->networkId << "]\n"; } void LocalParticipant::Process(Participant* sender, BinaryMsg* msg) { - std::cout << this->name << ": process Binary [" << (int)this->networkId << "/" << (int)msg->networkId << "]\n"; + std::cout << this->name << ": process Binary [" << (int)this->networkId << "/" + << (int)msg->networkId << "]\n"; Thing* thing = sender->Get(msg->networkId, msg->thingId); if (thing != nullptr) thing->ProcessBinary(msg->bytes); diff --git a/LocalParticipant.h b/LocalParticipant.h index 76742a7..260f794 100644 --- a/LocalParticipant.h +++ b/LocalParticipant.h @@ -11,7 +11,9 @@ #include "Participant.h" #if !defined(NO_STD) +#include #include +// #include #endif #if defined(_WIN32) || defined(_WIN64) @@ -51,7 +53,9 @@ class LocalParticipant : public Participant { /// @brief Create a participant which will try to connect to a site. /// @param ipAddress The IP address of the site /// @param port The port used by the site - LocalParticipant(const char* ipAddress, int port = 7681, int localPort = 7681); + LocalParticipant(const char* ipAddress, + int port = 7681, + int localPort = 7681); // Note to self: one cannot specify the port used by the local participant // now!! @@ -136,6 +140,35 @@ class LocalParticipant : public Participant { virtual void Process(Participant* sender, NameMsg* msg); virtual void Process(Participant* sender, PoseMsg* msg); virtual void Process(Participant* sender, BinaryMsg* msg); + +#if !defined(NO_STD) + public: + using ThingConstructor = std::function; + + template + void Register(unsigned char thingType) { + thingMsgProcessors[thingType] = [](Participant* participant, + unsigned char networkId, + unsigned char thingId) { + return new ThingClass(participant, networkId, thingId); + }; + }; + + template + void Register2(unsigned char thingType, ThingConstructor f) { + thingMsgProcessors[thingType] = [f](Participant* participant, + unsigned char networkId, + unsigned char thingId) { + return f(participant, networkId, thingId); + }; + }; + + protected: + std::unordered_map thingMsgProcessors; + +#endif }; } // namespace RoboidControl diff --git a/SiteServer.cpp b/SiteServer.cpp index ae82d84..4531b5b 100644 --- a/SiteServer.cpp +++ b/SiteServer.cpp @@ -25,7 +25,7 @@ SiteServer::SiteServer(int port) { SetupUDP(port, ipAddress, 0); #if !defined(NO_STD) - Register((unsigned char)Thing::Type::TemperatureSensor); + //Register((unsigned char)Thing::Type::TemperatureSensor); #endif } @@ -45,19 +45,20 @@ void SiteServer::Process(Participant* sender, SiteMsg* msg) {} void SiteServer::Process(Participant* sender, ThingMsg* msg) { Thing* thing = sender->Get(msg->networkId, msg->thingId); if (thing == nullptr) { -#if defined(NO_STD) +// #if defined(NO_STD) new Thing(sender, msg->networkId, msg->thingId, (Thing::Type)msg->thingType); -#else - auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); - Thing* newThing; - if (thingMsgProcessor != thingMsgProcessors.end()) // found item - newThing = - thingMsgProcessor->second(sender, msg->networkId, msg->thingId); - else - newThing = new Thing(sender, msg->networkId, msg->thingId, - (Thing::Type)msg->thingType); -#endif +// #else +// auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); +// //Thing* newThing; +// if (thingMsgProcessor != thingMsgProcessors.end()) // found item +// //newThing = +// thingMsgProcessor->second(sender, msg->networkId, msg->thingId); +// else +// //newThing = +// new Thing(sender, msg->networkId, msg->thingId, +// (Thing::Type)msg->thingType); +// #endif } } diff --git a/SiteServer.h b/SiteServer.h index 4b1f9bb..84a0fe0 100644 --- a/SiteServer.h +++ b/SiteServer.h @@ -17,16 +17,16 @@ class SiteServer : public LocalParticipant { // virtual void Update(unsigned long currentTimeMs = 0) override; -#if !defined(NO_STD) - template - void Register(unsigned char thingType) { - thingMsgProcessors[thingType] = [](Participant* participant, - unsigned char networkId, - unsigned char thingId) { - return new ThingClass(participant, networkId, thingId); - }; - }; -#endif +// #if !defined(NO_STD) +// template +// void Register(unsigned char thingType) { +// thingMsgProcessors[thingType] = [](Participant* participant, +// unsigned char networkId, +// unsigned char thingId) { +// return new ThingClass(participant, networkId, thingId); +// }; +// }; +// #endif protected: unsigned long nextPublishMe = 0; @@ -35,12 +35,12 @@ class SiteServer : public LocalParticipant { virtual void Process(Participant* sender, SiteMsg* msg) override; virtual void Process(Participant* sender, ThingMsg* msg) override; -#if !defined(NO_STD) - using ThingConstructor = std::function; - std::unordered_map thingMsgProcessors; -#endif +// #if !defined(NO_STD) +// using ThingConstructor = std::function; +// std::unordered_map thingMsgProcessors; +// #endif }; } // namespace RoboidControl diff --git a/Things/TemperatureSensor.cpp b/Things/TemperatureSensor.cpp index 2667d73..f31044a 100644 --- a/Things/TemperatureSensor.cpp +++ b/Things/TemperatureSensor.cpp @@ -4,10 +4,6 @@ namespace RoboidControl { -// TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {} - -// TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {} - TemperatureSensor::TemperatureSensor(Participant* participant, unsigned char networkId, unsigned char thingId)