diff --git a/Participant.cpp b/Participant.cpp index ba3c46e..d219ecc 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -19,34 +19,9 @@ void Participant::ReplaceLocalParticipant(Participant& newParticipant) { Participant::Participant() { Thing::CreateRoot(this); - //this->Add(this->root); } -/* -Participant::Participant(const char* ipAddress, int port) { - Thing::CreateRoot(this); - //this->Add(this->root); - - // make a copy of the ip address string - int addressLength = (int)strlen(ipAddress); - int stringLength = addressLength + 1; - char* addressString = new char[stringLength]; -#if defined(_WIN32) || defined(_WIN64) - strncpy_s(addressString, stringLength, ipAddress, - addressLength); // Leave space for null terminator -#else - strncpy(addressString, ipAddress, addressLength); -#endif - addressString[addressLength] = '\0'; - - this->ipAddress = addressString; - this->port = port; -} -*/ -Participant::~Participant() { - // registry.Remove(this); - // delete[] this->ipAddress; -} +Participant::~Participant() {} void Participant::Update(bool recurse) { for (Thing* thing : this->things) { @@ -56,48 +31,20 @@ void Participant::Update(bool recurse) { } bool Participant::Send(IMessage* msg) { - std::cout << "sending message " << (static_cast(this->buffer[0]) & 0xff) - << " to base Participant without communcation support " << std::endl; + std::cout << "sending message " << (static_cast(this->buffer[0]) & 0xff) + << " to base Participant without communcation support " + << std::endl; return true; } -/* -bool Participant::Send(IMessage* msg) { - int bufferSize = msg->Serialize(this->buffer); - if (bufferSize <= 0) - return true; - - // std::cout << "send msg " << (static_cast(this->buffer[0]) & 0xff) - // << " to " << this->ipAddress << std::endl; - -#if defined(_WIN32) || defined(_WIN64) - Windows::ParticipantUDP* thisWindows = - static_cast(this); - return thisWindows->SendTo(this, bufferSize); -#elif defined(__unix__) || defined(__APPLE__) - Posix::ParticipantUDP* thisPosix = static_cast(this); - return thisPosix->SendTo(this, bufferSize); -#elif defined(ARDUINO) - Arduino::ParticipantUDP* thisArduino = - static_cast(this); - return thisArduino->SendTo(this, bufferSize); -#elif defined(IDF_VER) - EspIdf::ParticipantUDP* thisEspIdf = - static_cast(this); - return thisEspIdf->SendTo(this, bufferSize); -#else - return false; -#endif -} -*/ Thing* Participant::Get(unsigned char networkId, unsigned char thingId) { for (Thing* thing : this->things) { if (thing->owner->networkId == networkId && thing->id == thingId) return thing; } - std::cout << "Could not find thing " //<< this->ipAddress << ":" << this->port - << "[" << (int)thingId << "]\n"; + std::cout << "Could not find thing " << "[" << (int)networkId << ": " + << (int)thingId << "]\n"; return nullptr; } @@ -119,9 +66,8 @@ void Participant::Add(Thing* thing, bool checkId) { thing->id = highestIx + 1; this->things.push_back(thing); #endif - std::cout << "Add thing with generated ID " - //<< this->ipAddress << ":" << this->port - << "[" << (int)thing->id << "]\n"; + std::cout << "Add thing with generated ID " + << "[" << (int)networkId << ": " << (int)thing->id << "]\n"; } else { Thing* foundThing = Get(thing->owner->networkId, thing->id); if (foundThing == nullptr) { @@ -130,13 +76,11 @@ void Participant::Add(Thing* thing, bool checkId) { #else this->things.push_back(thing); #endif - std::cout << "Add thing " << //this->ipAddress << ":" << this->port << - "[" - << (int)thing->id << "]\n"; + std::cout << "Add thing [" << (int)networkId << ": " << (int)thing->id + << "]\n"; } else { - std::cout << "Did not add, existing thing " - //<< this->ipAddress << ":" << this->port - << "[" << (int)thing->id << "]\n"; + std::cout << "Did not add, existing thing " + << "[" << (int)networkId << ": " << (int)thing->id << "]\n"; } } } @@ -164,88 +108,4 @@ void Participant::Remove(Thing* thing) { #pragma endregion -// #pragma region ParticipantRegistry - -// /* -// Participant* ParticipantRegistry::Get(const char* ipAddress, -// unsigned int port) { -// #if !defined(NO_STD) -// for (Participant* participant : ParticipantRegistry::participants) { -// if (participant == nullptr) -// continue; -// if (strcmp(participant->ipAddress, ipAddress) == 0 && -// participant->port == port) { -// // std::cout << "found participant " << participant->ipAddress << ":" -// // << (int)participant->port << std::endl; -// return participant; -// } -// } -// std::cout << "Could not find participant " << ipAddress << ":" << (int)port -// << std::endl; -// #endif -// return nullptr; -// } -// */ -// Participant* ParticipantRegistry::Get(unsigned char participantId) { -// #if !defined(NO_STD) -// for (Participant* participant : ParticipantRegistry::participants) { -// if (participant == nullptr) -// continue; -// if (participant->networkId == participantId) -// return participant; -// } -// std::cout << "Could not find participant " << (int)participantId << std::endl; -// #endif -// return nullptr; -// } - -// // Participant* ParticipantRegistry::Add(const char* ipAddress, -// // unsigned int port) { -// // Participant* participant = new Participant(ipAddress, port); -// // Add(participant); -// // return participant; -// // } - -// void ParticipantRegistry::Add(Participant* participant) { -// Participant* foundParticipant = -// Get(participant->networkId); -// //Get(participant->ipAddress, participant->port); - -// if (foundParticipant == nullptr) { -// #if defined(NO_STD) -// // this->things[this->thingCount++] = thing; -// #else -// ParticipantRegistry::participants.push_back(participant); -// #endif -// // std::cout << "Add participant " << participant->ipAddress << ":" -// // << participant->port << "[" << (int)participant->networkId -// // << "]\n"; -// // std::cout << "participants " << -// // ParticipantRegistry::participants.size() -// // << "\n"; -// // } else { -// // std::cout << "Did not add, existing participant " << -// // participant->ipAddress -// // << ":" << participant->port << "[" << -// // (int)participant->networkId -// // << "]\n"; -// } -// } - -// void ParticipantRegistry::Remove(Participant* participant) { -// // participants.remove(participant); -// } - -// #if defined(NO_STD) -// Participant** ParticipantRegistry::GetAll() const { -// return ParticipantRegistry::participants; -// } -// #else -// const std::list& ParticipantRegistry::GetAll() const { -// return ParticipantRegistry::participants; -// } -// #endif - -// #pragma endregion ParticipantRegistry - } // namespace RoboidControl diff --git a/Participant.h b/Participant.h index d3797dc..84c88d5 100644 --- a/Participant.h +++ b/Participant.h @@ -7,55 +7,6 @@ namespace RoboidControl { constexpr int MAX_THING_COUNT = 256; -/* -/// @brief class which manages all known participants -class ParticipantRegistry { - public: - /// @brief Retrieve a participant by its address - /// @param ipAddress The IP address of the participant - /// @param port The port number of the participant - /// @return The participant or a nullptr when it could not be found - //Participant* Get(const char* ipAddress, unsigned int port); - /// @brief Retrieve a participant by its network ID - /// @param networkID The network ID of the participant - /// @return The participant or a nullptr when it could not be found - Participant* Get(unsigned char networkID); - - /// @brief Add a participant with the given details - /// @param ipAddress The IP address of the participant - /// @param port The port number of the participant - /// @return The added participant - //Participant* Add(const char* ipAddress, unsigned int port); - - /// @brief Add a participant - /// @param participant The participant to add - void Add(Participant* participant); - - /// @brief Remove a participant - /// @param participant The participant to remove - void Remove(Participant* participant); - - private: -#if defined(NO_STD) - public: - Participant** GetAll() const; - int count = 0; - - private: - Participant** participants; -#else - public: - /// @brief Get all participants - /// @return All participants - const std::list& GetAll() const; - - private: - /// @brief The list of known participants - std::list participants; -#endif -}; -*/ - /// @brief A participant is a device which manages things. /// It can communicate with other participant to synchronise the state of /// things. This class is used to register the things the participant is @@ -137,12 +88,6 @@ class Participant { #pragma endregion Send -// #pragma region Participant Registry - -// public: -// static ParticipantRegistry registry; - -// #pragma endregion Participant Registry }; } // namespace RoboidControl diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index 0f6581d..68d41e7 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -16,90 +16,6 @@ namespace RoboidControl { -#pragma region ParticipantRegistry - -ParticipantRegistry ParticipantUDPGeneric::registry; - -ParticipantSocket* ParticipantRegistry::Get(const char* ipAddress, - unsigned int port) { -#if !defined(NO_STD) - for (ParticipantSocket* participant : ParticipantRegistry::participants) { - if (participant == nullptr) - continue; - if (strcmp(participant->ipAddress, ipAddress) == 0 && - participant->port == port) { - // std::cout << "found participant " << participant->ipAddress << ":" - // << (int)participant->port << std::endl; - return participant; - } - } - std::cout << "Could not find participant " << ipAddress << ":" << (int)port - << std::endl; -#endif - return nullptr; -} - -ParticipantSocket* ParticipantRegistry::Get(unsigned char participantId) { -#if !defined(NO_STD) - for (ParticipantSocket* participant : ParticipantRegistry::participants) { - if (participant == nullptr) - continue; - if (participant->networkId == participantId) - return participant; - } - std::cout << "Could not find participant " << (int)participantId << std::endl; -#endif - return nullptr; -} - -ParticipantSocket* ParticipantRegistry::Add(const char* ipAddress, - unsigned int port) { - ParticipantSocket* participant = new ParticipantSocket(ipAddress, port); - Add(participant); - return participant; -} - -void ParticipantRegistry::Add(ParticipantSocket* participant) { - Participant* foundParticipant = Get(participant->networkId); - // Get(participant->ipAddress, participant->port); - - if (foundParticipant == nullptr) { -#if defined(NO_STD) - // this->things[this->thingCount++] = thing; -#else - ParticipantRegistry::participants.push_back(participant); -#endif - // std::cout << "Add participant " << participant->ipAddress << ":" - // << participant->port << "[" << (int)participant->networkId - // << "]\n"; - // std::cout << "participants " << - // ParticipantRegistry::participants.size() - // << "\n"; - // } else { - // std::cout << "Did not add, existing participant " << - // participant->ipAddress - // << ":" << participant->port << "[" << - // (int)participant->networkId - // << "]\n"; - } -} - -void ParticipantRegistry::Remove(ParticipantSocket* participant) { - // participants.remove(participant); -} - -#if defined(NO_STD) -RemoteParticipantUDP** ParticipantRegistry::GetAll() const { - return ParticipantRegistry::participants; -} -#else -const std::list& ParticipantRegistry::GetAll() const { - return ParticipantRegistry::participants; -} -#endif - -#pragma endregion ParticipantRegistry - bool ParticipantSocket::Send(IMessage* msg) { // No message is actually sent, because this class has no networking // implementation @@ -179,7 +95,7 @@ void ParticipantUDPGeneric::Update(bool recurse) { this->nextPublishMe = currentTimeMs + this->publishInterval; } - //this->ReceiveUDP(); + this->ReceiveUDP(); } UpdateMyThings(); @@ -191,7 +107,7 @@ void ParticipantUDPGeneric::UpdateMyThings() { if (thing == nullptr) // || thing->GetParent() != nullptr) continue; - // Why don't we do recursive? + // Why don't we do recursive? // Because when a thing creates a thing in the update, // that new thing is not sent out (because of hierarchyChanged) // before it is updated itself: it is immediatedly updated! @@ -294,8 +210,7 @@ void ParticipantUDPGeneric::ReceiveData(unsigned char packetSize, unsigned int senderPort) { // std::cout << "Receive data from " << senderIpAddress << ":" << senderPort // << std::endl; - ParticipantSocket* sender = - this->registry.Get(senderIpAddress, senderPort); + ParticipantSocket* sender = this->registry.Get(senderIpAddress, senderPort); if (sender == nullptr) { sender = this->registry.Add(senderIpAddress, senderPort); #if !defined(NO_STD) @@ -413,8 +328,7 @@ void ParticipantUDPGeneric::Process(ParticipantSocket* sender, #endif } -void ParticipantUDPGeneric::Process(ParticipantSocket* sender, - ThingMsg* msg) { +void ParticipantUDPGeneric::Process(ParticipantSocket* sender, ThingMsg* msg) { #if defined(DEBUG) std::cout << this->name << ": process ThingMsg [" << (int)msg->networkId << "/" << (int)msg->thingId << "] " << (int)msg->thingType << " " @@ -459,8 +373,7 @@ Thing* ParticipantUDPGeneric::ProcessNewThing(ParticipantSocket* owner, } } -void ParticipantUDPGeneric::Process(ParticipantSocket* sender, - NameMsg* msg) { +void ParticipantUDPGeneric::Process(ParticipantSocket* sender, NameMsg* msg) { #if defined(DEBUG) std::cout << this->name << ": process NameMsg [" << (int)msg->networkId << "/" << (int)msg->thingId << "] "; @@ -500,8 +413,7 @@ void ParticipantUDPGeneric::Process(ParticipantSocket* sender, #endif } -void ParticipantUDPGeneric::Process(ParticipantSocket* sender, - PoseMsg* msg) { +void ParticipantUDPGeneric::Process(ParticipantSocket* sender, PoseMsg* msg) { #if !defined(DEBUG) && !defined(NO_STD) std::cout << this->name << ": process PoseMsg [" << (int)this->networkId << "/" << (int)msg->networkId << "] " << (int)msg->poseType << "\n"; @@ -524,8 +436,7 @@ void ParticipantUDPGeneric::Process(ParticipantSocket* sender, thing->SetAngularVelocity(msg->angularVelocity); } -void ParticipantUDPGeneric::Process(ParticipantSocket* sender, - BinaryMsg* msg) { +void ParticipantUDPGeneric::Process(ParticipantSocket* sender, BinaryMsg* msg) { #if defined(DEBUG) std::cout << this->name << ": process BinaryMsg [" << (int)msg->networkId << "/" << (int)msg->thingId << "]\n"; @@ -547,8 +458,7 @@ void ParticipantUDPGeneric::Process(ParticipantSocket* sender, } } -void ParticipantUDPGeneric::Process(ParticipantSocket* sender, - TextMsg* msg) { +void ParticipantUDPGeneric::Process(ParticipantSocket* sender, TextMsg* msg) { #if defined(DEBUG) std::cout << this->name << ": process TextMsg " << (int)msg->textLength << " " << (int)msg->text << "\n"; @@ -570,4 +480,87 @@ void ParticipantUDPGeneric::Process(ParticipantSocket* sender, // Receive #pragma endregion +#pragma region Registry + +ParticipantUDP::Registry ParticipantUDPGeneric::registry; + +ParticipantSocket* ParticipantUDP::Registry::Get(const char* ipAddress, unsigned int port) { +#if !defined(NO_STD) + for (ParticipantSocket* participant : Registry::participants) { + if (participant == nullptr) + continue; + if (strcmp(participant->ipAddress, ipAddress) == 0 && + participant->port == port) { + // std::cout << "found participant " << participant->ipAddress << ":" + // << (int)participant->port << std::endl; + return participant; + } + } + std::cout << "Could not find participant " << ipAddress << ":" << (int)port + << std::endl; +#endif + return nullptr; +} + +ParticipantSocket* ParticipantUDP::Registry::Get(unsigned char participantId) { +#if !defined(NO_STD) + for (ParticipantSocket* participant : Registry::participants) { + if (participant == nullptr) + continue; + if (participant->networkId == participantId) + return participant; + } + std::cout << "Could not find participant " << (int)participantId << std::endl; +#endif + return nullptr; +} + +ParticipantSocket* ParticipantUDP::Registry::Add(const char* ipAddress, unsigned int port) { + ParticipantSocket* participant = new ParticipantSocket(ipAddress, port); + Add(participant); + return participant; +} + +void ParticipantUDP::Registry::Add(ParticipantSocket* participant) { + Participant* foundParticipant = Get(participant->networkId); + // Get(participant->ipAddress, participant->port); + + if (foundParticipant == nullptr) { +#if defined(NO_STD) + // this->things[this->thingCount++] = thing; +#else + Registry::participants.push_back(participant); +#endif + // std::cout << "Add participant " << participant->ipAddress << ":" + // << participant->port << "[" << (int)participant->networkId + // << "]\n"; + // std::cout << "participants " << + // Registry::participants.size() + // << "\n"; + // } else { + // std::cout << "Did not add, existing participant " << + // participant->ipAddress + // << ":" << participant->port << "[" << + // (int)participant->networkId + // << "]\n"; + } +} + +void ParticipantUDP::Registry::Remove(ParticipantSocket* participant) { + // participants.remove(participant); +} + +#if defined(NO_STD) +RemoteParticipantUDP** Registry::GetAll() const { + return Registry::participants; +} +#else +const std::list& ParticipantUDP::Registry::GetAll() const { + return Registry::participants; +} +#endif + +// Registry +#pragma endregion Registry + } // namespace RoboidControl diff --git a/Participants/ParticipantUDP.h b/Participants/ParticipantUDP.h index ffca6ac..6ad8956 100644 --- a/Participants/ParticipantUDP.h +++ b/Participants/ParticipantUDP.h @@ -32,55 +32,6 @@ namespace RoboidControl { constexpr int MAX_SENDER_COUNT = 256; - - -/// @brief class which manages all known participants -class ParticipantRegistry { - public: - /// @brief Retrieve a participant by its address - /// @param ipAddress The IP address of the participant - /// @param port The port number of the participant - /// @return The participant or a nullptr when it could not be found - ParticipantSocket* Get(const char* ipAddress, unsigned int port); - /// @brief Retrieve a participant by its network ID - /// @param networkID The network ID of the participant - /// @return The participant or a nullptr when it could not be found - ParticipantSocket* Get(unsigned char networkID); - - /// @brief Add a participant with the given details - /// @param ipAddress The IP address of the participant - /// @param port The port number of the participant - /// @return The added participant - ParticipantSocket* Add(const char* ipAddress, unsigned int port); - - /// @brief Add a participant - /// @param participant The participant to add - void Add(ParticipantSocket* participant); - - /// @brief Remove a participant - /// @param participant The participant to remove - void Remove(ParticipantSocket* participant); - - private: -#if defined(NO_STD) - public: - ParticipantSocket** GetAll() const; - int count = 0; - - private: - ParticipantSocket** participants; -#else - public: - /// @brief Get all participants - /// @return All participants - const std::list& GetAll() const; - - private: - /// @brief The list of known participants - std::list participants; -#endif -}; - /// @brief A participant using UDP communication /// A local participant is the local device which can communicate with /// other participants It manages all local things and communcation with other @@ -174,9 +125,12 @@ class ParticipantUDPGeneric : public ParticipantSocket { void ReceiveData(unsigned char bufferSize, char* senderIpAddress, unsigned int senderPort); - void ReceiveData(unsigned char bufferSize, ParticipantSocket* remoteParticipant); + void ReceiveData(unsigned char bufferSize, + ParticipantSocket* remoteParticipant); - virtual void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort) = 0; + virtual void SetupUDP(int localPort, + const char* remoteIpAddress, + int remotePort) = 0; virtual void ReceiveUDP() = 0; @@ -198,9 +152,55 @@ class ParticipantUDPGeneric : public ParticipantSocket { #pragma endregion Receive -public: - static ParticipantRegistry registry; + public: + /// @brief class which manages all known participants + class Registry { + public: + /// @brief Retrieve a participant by its address + /// @param ipAddress The IP address of the participant + /// @param port The port number of the participant + /// @return The participant or a nullptr when it could not be found + ParticipantSocket* Get(const char* ipAddress, unsigned int port); + /// @brief Retrieve a participant by its network ID + /// @param networkID The network ID of the participant + /// @return The participant or a nullptr when it could not be found + ParticipantSocket* Get(unsigned char networkID); + /// @brief Add a participant with the given details + /// @param ipAddress The IP address of the participant + /// @param port The port number of the participant + /// @return The added participant + ParticipantSocket* Add(const char* ipAddress, unsigned int port); + + /// @brief Add a participant + /// @param participant The participant to add + void Add(ParticipantSocket* participant); + + /// @brief Remove a participant + /// @param participant The participant to remove + void Remove(ParticipantSocket* participant); + + private: +#if defined(NO_STD) + public: + ParticipantSocket** GetAll() const; + int count = 0; + + private: + ParticipantSocket** participants; +#else + public: + /// @brief Get all participants + /// @return All participants + const std::list& GetAll() const; + + private: + /// @brief The list of known participants + std::list participants; +#endif + }; + + static Registry registry; }; } // namespace RoboidControl