RemoteParticiapntUDP -> ParticipantSocket

This commit is contained in:
Pascal Serrarens 2025-06-30 08:46:39 +02:00
parent 19a5c5d4d5
commit a826817a0b
14 changed files with 120 additions and 103 deletions

View File

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

View File

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

View File

@ -182,7 +182,7 @@ bool ParticipantUDP::Send(IMessage* msg) {
return this->SendTo(this->remoteSite, bufferSize);
}
bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant,
bool ParticipantUDP::SendTo(ParticipantSocket* remoteParticipant,
int bufferSize) {
#if defined(IDF_VER)
uint16_t port = ntohs(dest_addr.sin_port);

View File

@ -23,7 +23,7 @@ class ParticipantUDP : public ParticipantUDPGeneric {
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive();
bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize);
bool SendTo(ParticipantSocket* remoteParticipant, int bufferSize);
bool Publish(IMessage* msg);
// bool SendTest();

View File

@ -0,0 +1,24 @@
#include "ParticipantSocket.h"
#include <string.h>
namespace RoboidControl {
ParticipantSocket::ParticipantSocket(const char* ipAddress, int port) {
// 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;
}
}

View File

@ -0,0 +1,28 @@
#pragma once
#include "Participant.h"
namespace RoboidControl {
class ParticipantSocket : public Participant {
public:
/// @brief Create a new participant with the given communcation info
/// @param ipAddress The IP address of the participant
/// @param port The UDP port of the participant
/// @remarks This does not belong here, it should move to ParticipantUDP or
/// something like that in the future
ParticipantSocket(const char* ipAddress, int port);
/// @brief The Ip Address of a participant.
/// @remarks This does not belong here, it should move to ParticipantUDP or
/// something like that in the future
const char* ipAddress = "0.0.0.0";
/// @brief The port number for UDP communication with the participant.
/// @remarks This does not belong here, it should move to ParticipantUDP or
/// something like that in the future
unsigned int port = 0;
bool Send(IMessage* msg) override;
};
}

View File

@ -12,7 +12,7 @@
#include "Things/DistanceSensor.h"
#include "Things/TouchSensor.h"
#include <string.h>
// #include <string.h>
namespace RoboidControl {
@ -20,10 +20,10 @@ namespace RoboidControl {
ParticipantRegistry ParticipantUDPGeneric::registry;
RemoteParticipantUDP* ParticipantRegistry::Get(const char* ipAddress,
ParticipantSocket* ParticipantRegistry::Get(const char* ipAddress,
unsigned int port) {
#if !defined(NO_STD)
for (RemoteParticipantUDP* participant : ParticipantRegistry::participants) {
for (ParticipantSocket* participant : ParticipantRegistry::participants) {
if (participant == nullptr)
continue;
if (strcmp(participant->ipAddress, ipAddress) == 0 &&
@ -39,9 +39,9 @@ RemoteParticipantUDP* ParticipantRegistry::Get(const char* ipAddress,
return nullptr;
}
RemoteParticipantUDP* ParticipantRegistry::Get(unsigned char participantId) {
ParticipantSocket* ParticipantRegistry::Get(unsigned char participantId) {
#if !defined(NO_STD)
for (RemoteParticipantUDP* participant : ParticipantRegistry::participants) {
for (ParticipantSocket* participant : ParticipantRegistry::participants) {
if (participant == nullptr)
continue;
if (participant->networkId == participantId)
@ -52,14 +52,14 @@ RemoteParticipantUDP* ParticipantRegistry::Get(unsigned char participantId) {
return nullptr;
}
RemoteParticipantUDP* ParticipantRegistry::Add(const char* ipAddress,
ParticipantSocket* ParticipantRegistry::Add(const char* ipAddress,
unsigned int port) {
RemoteParticipantUDP* participant = new RemoteParticipantUDP(ipAddress, port);
ParticipantSocket* participant = new ParticipantSocket(ipAddress, port);
Add(participant);
return participant;
}
void ParticipantRegistry::Add(RemoteParticipantUDP* participant) {
void ParticipantRegistry::Add(ParticipantSocket* participant) {
Participant* foundParticipant = Get(participant->networkId);
// Get(participant->ipAddress, participant->port);
@ -84,7 +84,7 @@ void ParticipantRegistry::Add(RemoteParticipantUDP* participant) {
}
}
void ParticipantRegistry::Remove(RemoteParticipantUDP* participant) {
void ParticipantRegistry::Remove(ParticipantSocket* participant) {
// participants.remove(participant);
}
@ -93,31 +93,14 @@ RemoteParticipantUDP** ParticipantRegistry::GetAll() const {
return ParticipantRegistry::participants;
}
#else
const std::list<RemoteParticipantUDP*>& ParticipantRegistry::GetAll() const {
const std::list<ParticipantSocket*>& ParticipantRegistry::GetAll() const {
return ParticipantRegistry::participants;
}
#endif
#pragma endregion ParticipantRegistry
RemoteParticipantUDP::RemoteParticipantUDP(const char* ipAddress, int port) {
// 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;
}
bool RemoteParticipantUDP::Send(IMessage* msg) {
bool ParticipantSocket::Send(IMessage* msg) {
// No message is actually sent, because this class has no networking
// implementation
return false;
@ -126,7 +109,7 @@ bool RemoteParticipantUDP::Send(IMessage* msg) {
#pragma region Init
ParticipantUDPGeneric::ParticipantUDPGeneric(int port)
: RemoteParticipantUDP("127.0.0.1", port) {
: ParticipantSocket("127.0.0.1", port) {
this->name = "ParticipantUDP";
this->remoteSite = nullptr;
if (this->port == 0)
@ -145,12 +128,12 @@ ParticipantUDPGeneric::ParticipantUDPGeneric(int port)
ParticipantUDPGeneric::ParticipantUDPGeneric(const char* ipAddress,
int port,
int localPort)
: RemoteParticipantUDP("127.0.0.1", localPort) {
: ParticipantSocket("127.0.0.1", localPort) {
this->name = "ParticipantUDP";
if (this->port == 0)
this->isIsolated = true;
else
this->remoteSite = new RemoteParticipantUDP(ipAddress, port);
this->remoteSite = new ParticipantSocket(ipAddress, port);
registry.Add(this);
this->root = Thing::LocalRoot(); // Participant::LocalParticipant->root;
@ -311,7 +294,7 @@ void ParticipantUDPGeneric::ReceiveData(unsigned char packetSize,
unsigned int senderPort) {
// std::cout << "Receive data from " << senderIpAddress << ":" << senderPort
// << std::endl;
RemoteParticipantUDP* sender =
ParticipantSocket* sender =
this->registry.Get(senderIpAddress, senderPort);
if (sender == nullptr) {
sender = this->registry.Add(senderIpAddress, senderPort);
@ -325,7 +308,7 @@ void ParticipantUDPGeneric::ReceiveData(unsigned char packetSize,
}
void ParticipantUDPGeneric::ReceiveData(unsigned char bufferSize,
RemoteParticipantUDP* sender) {
ParticipantSocket* sender) {
unsigned char msgId = this->buffer[0];
// std::cout << "receive msg " << (int)msgId << "\n";
// std::cout << " buffer size = " <<(int) bufferSize << "\n";
@ -398,7 +381,7 @@ void ParticipantUDPGeneric::ReceiveData(unsigned char bufferSize,
#endif
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
ParticipantMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": Process ParticipantMsg " << (int)msg->networkId
@ -406,7 +389,7 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
#endif
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
NetworkIdMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": process NetworkIdMsg " << (int)this->networkId
@ -422,7 +405,7 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
}
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
InvestigateMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": Process InvestigateMsg [" << (int)msg->networkId
@ -430,16 +413,16 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
#endif
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
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 << " "
<< (int)msg->parentId << "\n";
#endif
RemoteParticipantUDP* owner = registry.Get(msg->networkId);
ParticipantSocket* owner = registry.Get(msg->networkId);
if (owner == nullptr) {
owner = new RemoteParticipantUDP(sender->ipAddress, sender->port);
owner = new ParticipantSocket(sender->ipAddress, sender->port);
owner->networkId = msg->networkId;
registry.Add(owner);
}
@ -461,7 +444,7 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
thing->SetParent(nullptr);
}
Thing* ParticipantUDPGeneric::ProcessNewThing(RemoteParticipantUDP* owner,
Thing* ParticipantUDPGeneric::ProcessNewThing(ParticipantSocket* owner,
ThingMsg* msg,
bool isRemote) {
switch (msg->thingType) {
@ -476,7 +459,7 @@ Thing* ParticipantUDPGeneric::ProcessNewThing(RemoteParticipantUDP* owner,
}
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
NameMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": process NameMsg [" << (int)msg->networkId << "/"
@ -509,7 +492,7 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
#endif
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
ModelUrlMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": process ModelUrlMsg [" << (int)msg->networkId
@ -517,7 +500,7 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
#endif
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
PoseMsg* msg) {
#if !defined(DEBUG) && !defined(NO_STD)
std::cout << this->name << ": process PoseMsg [" << (int)this->networkId
@ -541,7 +524,7 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
thing->SetAngularVelocity(msg->angularVelocity);
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
BinaryMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": process BinaryMsg [" << (int)msg->networkId
@ -564,7 +547,7 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
}
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
TextMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": process TextMsg " << (int)msg->textLength << " "
@ -572,7 +555,7 @@ void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
#endif
}
void ParticipantUDPGeneric::Process(RemoteParticipantUDP* sender,
void ParticipantUDPGeneric::Process(ParticipantSocket* sender,
DestroyMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": process Destroy [" << (int)msg->networkId << "/"

View File

@ -10,7 +10,8 @@
#include "Messages/PoseMsg.h"
#include "Messages/TextMsg.h"
#include "Messages/ThingMsg.h"
#include "Participant.h"
#include "ParticipantSocket.h"
#if !defined(NO_STD)
#include <functional>
@ -31,26 +32,7 @@ namespace RoboidControl {
constexpr int MAX_SENDER_COUNT = 256;
class RemoteParticipantUDP : public Participant {
public:
/// @brief Create a new participant with the given communcation info
/// @param ipAddress The IP address of the participant
/// @param port The UDP port of the participant
/// @remarks This does not belong here, it should move to ParticipantUDP or
/// something like that in the future
RemoteParticipantUDP(const char* ipAddress, int port);
/// @brief The Ip Address of a participant.
/// @remarks This does not belong here, it should move to ParticipantUDP or
/// something like that in the future
const char* ipAddress = "0.0.0.0";
/// @brief The port number for UDP communication with the participant.
/// @remarks This does not belong here, it should move to ParticipantUDP or
/// something like that in the future
unsigned int port = 0;
bool Send(IMessage* msg) override;
};
/// @brief class which manages all known participants
class ParticipantRegistry {
@ -59,43 +41,43 @@ class ParticipantRegistry {
/// @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
RemoteParticipantUDP* Get(const char* ipAddress, unsigned int port);
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
RemoteParticipantUDP* Get(unsigned char networkID);
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
RemoteParticipantUDP* Add(const char* ipAddress, unsigned int port);
ParticipantSocket* Add(const char* ipAddress, unsigned int port);
/// @brief Add a participant
/// @param participant The participant to add
void Add(RemoteParticipantUDP* participant);
void Add(ParticipantSocket* participant);
/// @brief Remove a participant
/// @param participant The participant to remove
void Remove(RemoteParticipantUDP* participant);
void Remove(ParticipantSocket* participant);
private:
#if defined(NO_STD)
public:
RemoteParticipantUDP** GetAll() const;
ParticipantSocket** GetAll() const;
int count = 0;
private:
RemoteParticipantUDP** participants;
ParticipantSocket** participants;
#else
public:
/// @brief Get all participants
/// @return All participants
const std::list<RemoteParticipantUDP*>& GetAll() const;
const std::list<ParticipantSocket*>& GetAll() const;
private:
/// @brief The list of known participants
std::list<RemoteParticipantUDP*> participants;
std::list<ParticipantSocket*> participants;
#endif
};
@ -111,7 +93,7 @@ class ParticipantRegistry {
/// participant is created which can be obtained using
/// RoboidControl::IsolatedParticipant::Isolated().
/// @sa RoboidControl::Thing::Thing()
class ParticipantUDPGeneric : public RemoteParticipantUDP {
class ParticipantUDPGeneric : public ParticipantSocket {
#pragma region Init
public:
@ -139,7 +121,7 @@ class ParticipantUDPGeneric : public RemoteParticipantUDP {
bool isIsolated = false;
/// @brief The remote site when this participant is connected to a site
RemoteParticipantUDP* remoteSite = nullptr;
ParticipantSocket* remoteSite = nullptr;
/// The interval in milliseconds for publishing (broadcasting) data on the
/// local network
@ -192,27 +174,27 @@ class ParticipantUDPGeneric : public RemoteParticipantUDP {
void ReceiveData(unsigned char bufferSize,
char* senderIpAddress,
unsigned int senderPort);
void ReceiveData(unsigned char bufferSize, RemoteParticipantUDP* remoteParticipant);
void ReceiveData(unsigned char bufferSize, ParticipantSocket* remoteParticipant);
virtual void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort) = 0;
virtual void ReceiveUDP() = 0;
virtual void Process(RemoteParticipantUDP* sender, ParticipantMsg* msg);
virtual void Process(RemoteParticipantUDP* sender, NetworkIdMsg* msg);
virtual void Process(RemoteParticipantUDP* sender, InvestigateMsg* msg);
virtual void Process(ParticipantSocket* sender, ParticipantMsg* msg);
virtual void Process(ParticipantSocket* sender, NetworkIdMsg* msg);
virtual void Process(ParticipantSocket* sender, InvestigateMsg* msg);
virtual void Process(RemoteParticipantUDP* sender, ThingMsg* msg);
virtual Thing* ProcessNewThing(RemoteParticipantUDP* sender,
virtual void Process(ParticipantSocket* sender, ThingMsg* msg);
virtual Thing* ProcessNewThing(ParticipantSocket* sender,
ThingMsg* msg,
bool isRemote);
virtual void Process(RemoteParticipantUDP* sender, NameMsg* msg);
virtual void Process(RemoteParticipantUDP* sender, ModelUrlMsg* msg);
virtual void Process(RemoteParticipantUDP* sender, PoseMsg* msg);
virtual void Process(RemoteParticipantUDP* sender, BinaryMsg* msg);
virtual void Process(RemoteParticipantUDP* sender, TextMsg* msg);
virtual void Process(RemoteParticipantUDP* sender, DestroyMsg* msg);
virtual void Process(ParticipantSocket* sender, NameMsg* msg);
virtual void Process(ParticipantSocket* sender, ModelUrlMsg* msg);
virtual void Process(ParticipantSocket* sender, PoseMsg* msg);
virtual void Process(ParticipantSocket* sender, BinaryMsg* msg);
virtual void Process(ParticipantSocket* sender, TextMsg* msg);
virtual void Process(ParticipantSocket* sender, DestroyMsg* msg);
#pragma endregion Receive

View File

@ -56,7 +56,7 @@ void SiteServer::UpdateMyThings() {
#pragma region Receive
void SiteServer::Process(RemoteParticipantUDP* sender, ParticipantMsg* msg) {
void SiteServer::Process(ParticipantSocket* sender, ParticipantMsg* msg) {
if (msg->networkId != sender->networkId) {
// std::cout << this->name << " received New Client -> " <<
// sender->ipAddress
@ -67,9 +67,9 @@ void SiteServer::Process(RemoteParticipantUDP* sender, ParticipantMsg* msg) {
}
}
void SiteServer::Process(RemoteParticipantUDP* sender, NetworkIdMsg* msg) {}
void SiteServer::Process(ParticipantSocket* sender, NetworkIdMsg* msg) {}
void SiteServer::Process(RemoteParticipantUDP* sender, ThingMsg* msg) {
void SiteServer::Process(ParticipantSocket* sender, ThingMsg* msg) {
Thing* thing = sender->Get(msg->networkId, msg->thingId);
if (thing == nullptr) {
// new Thing(sender, (Thing::Type)msg->thingType, msg->thingId);

View File

@ -33,9 +33,9 @@ class SiteServer : public ParticipantUDPGeneric {
protected:
unsigned long nextPublishMe = 0;
virtual void Process(RemoteParticipantUDP* sender, ParticipantMsg* msg) override;
virtual void Process(RemoteParticipantUDP* sender, NetworkIdMsg* msg) override;
virtual void Process(RemoteParticipantUDP* sender, ThingMsg* msg) override;
virtual void Process(ParticipantSocket* sender, ParticipantMsg* msg) override;
virtual void Process(ParticipantSocket* sender, NetworkIdMsg* msg) override;
virtual void Process(ParticipantSocket* sender, ThingMsg* msg) override;
#pragma endregion Receive

View File

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

View File

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

View File

@ -102,7 +102,7 @@ void ParticipantUDP::Receive() {
#endif // _WIN32 || _WIN64
}
bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize) {
bool ParticipantUDP::SendTo(ParticipantSocket* remoteParticipant, int bufferSize) {
#if defined(_WIN32) || defined(_WIN64)
char 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 ParticipantUDPGeneric {
public:
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive();
bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize);
bool SendTo(ParticipantSocket* remoteParticipant, int bufferSize);
bool Publish(IMessage* msg);
protected: