static participant list
This commit is contained in:
parent
80127e0041
commit
619695c90d
@ -4,6 +4,8 @@
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
std::list<Participant*> Participant::participants;
|
||||
|
||||
Participant::Participant() {}
|
||||
|
||||
Participant::Participant(const char* ipAddress, int port) {
|
||||
@ -34,12 +36,63 @@ void Participant::Update(unsigned long currentTimeMs) {
|
||||
}
|
||||
}
|
||||
|
||||
Participant* Participant::GetParticipant(const char* ipAddress,
|
||||
unsigned int port) {
|
||||
for (Participant* participant : Participant::participants) {
|
||||
if (participant == nullptr)
|
||||
continue;
|
||||
if (strcmp(participant->ipAddress, ipAddress) == 0 &&
|
||||
participant->port == port)
|
||||
return participant;
|
||||
}
|
||||
std::cout << "Could not find participant " << ipAddress << ":" << (int)port
|
||||
<< std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Participant* Participant::GetParticipant(unsigned char participantId) {
|
||||
for (Participant* participant : Participant::participants) {
|
||||
if (participant == nullptr)
|
||||
continue;
|
||||
if (participant->networkId == participantId)
|
||||
return participant;
|
||||
}
|
||||
std::cout << "Could not find participant " << (int)participantId << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Participant* Participant::AddParticipant(const char* ipAddress, unsigned int port) {
|
||||
Participant* participant = new Participant(ipAddress, port);
|
||||
Participant::AddParticipant(participant);
|
||||
return participant;
|
||||
}
|
||||
|
||||
void Participant::AddParticipant(Participant* participant) {
|
||||
Participant* foundParticipant =
|
||||
Participant::GetParticipant(participant->networkId);
|
||||
if (foundParticipant == nullptr) {
|
||||
#if defined(NO_STD)
|
||||
this->things[this->thingCount++] = thing;
|
||||
#else
|
||||
Participant::participants.push_back(participant);
|
||||
#endif
|
||||
std::cout << "Add participant " << participant->ipAddress << ":"
|
||||
<< participant->port << "[" << (int)participant->networkId
|
||||
<< "]\n";
|
||||
} else {
|
||||
std::cout << "Did not add, existing thing " << participant->ipAddress << ":"
|
||||
<< participant->port << "[" << (int)participant->networkId
|
||||
<< "]\n";
|
||||
}
|
||||
}
|
||||
|
||||
Thing* Participant::Get(unsigned char thingId) {
|
||||
for (Thing* thing : this->things) {
|
||||
if (thing->id == thingId)
|
||||
return thing;
|
||||
}
|
||||
// std::cout << "Could not find thing " << this->ipAddress << ":" << this->port
|
||||
// std::cout << "Could not find thing " << this->ipAddress << ":" <<
|
||||
// this->port
|
||||
// << "[" << (int)thingId << "]\n";
|
||||
return nullptr;
|
||||
}
|
||||
@ -64,7 +117,8 @@ void Participant::Add(Thing* thing, bool checkId) {
|
||||
#else
|
||||
this->things.push_back(thing);
|
||||
#endif
|
||||
// std::cout << "Add thing " << this->ipAddress << ":" << this->port << "["
|
||||
// std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
|
||||
// "["
|
||||
// << (int)thing->id << "]\n";
|
||||
} else {
|
||||
// std::cout << "Did not add, existing thing " << this->ipAddress << ":"
|
||||
|
@ -18,7 +18,7 @@ class Participant {
|
||||
const char* ipAddress = "0.0.0.0";
|
||||
/// @brief The port number for UDP communication with the participant. This is
|
||||
/// 0 for isolated participants.
|
||||
int port = 0;
|
||||
unsigned int port = 0;
|
||||
|
||||
/// @brief The network Id to identify the participant.
|
||||
/// @note This field is likely to disappear in future versions
|
||||
@ -40,11 +40,19 @@ class Participant {
|
||||
unsigned char thingCount = 0;
|
||||
Thing* things[MAX_THING_COUNT];
|
||||
#else
|
||||
/// @brief The list of known participants
|
||||
static std::list<Participant*> participants;
|
||||
|
||||
/// @brief The list of things managed by this participant
|
||||
std::list<Thing*> things;
|
||||
#endif
|
||||
|
||||
public:
|
||||
static Participant* GetParticipant(const char* ipAddress, unsigned int port);
|
||||
static Participant* GetParticipant(unsigned char participantId);
|
||||
static Participant* AddParticipant(const char* ipAddress, unsigned int port);
|
||||
static void AddParticipant(Participant* participant);
|
||||
|
||||
/// @brief Find a thing managed by this participant
|
||||
/// @param networkId The network ID for the thing
|
||||
/// @param thingId The ID of the thing
|
||||
|
@ -31,6 +31,7 @@ ParticipantUDP::ParticipantUDP(int port) {
|
||||
this->remoteSite = nullptr;
|
||||
if (this->port == 0)
|
||||
this->isIsolated = true;
|
||||
Participant::AddParticipant(this);
|
||||
}
|
||||
|
||||
ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
|
||||
@ -39,6 +40,7 @@ ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
|
||||
this->isIsolated = true;
|
||||
else
|
||||
this->remoteSite = new Participant(ipAddress, port);
|
||||
Participant::AddParticipant(this);
|
||||
}
|
||||
|
||||
static ParticipantUDP* isolatedParticipant = nullptr;
|
||||
@ -135,27 +137,6 @@ void ParticipantUDP::ReceiveUDP() {
|
||||
#endif
|
||||
}
|
||||
|
||||
Participant* ParticipantUDP::GetParticipant(const char* ipAddress, int port) {
|
||||
for (Participant* sender : this->senders) {
|
||||
if (strcmp(sender->ipAddress, ipAddress) == 0 && sender->port == port)
|
||||
return sender;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Participant* ParticipantUDP::AddParticipant(const char* ipAddress, int port) {
|
||||
// std::cout << "New Participant " << ipAddress << ":" << port << "\n";
|
||||
Participant* participant = new Participant(ipAddress, port);
|
||||
#if defined(NO_STD)
|
||||
participant->networkId = this->senderCount;
|
||||
this->senders[this->senderCount++] = participant;
|
||||
#else
|
||||
participant->networkId = (unsigned char)this->senders.size();
|
||||
this->senders.push_back(participant);
|
||||
#endif
|
||||
return participant;
|
||||
}
|
||||
|
||||
#pragma region Send
|
||||
|
||||
void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
|
||||
@ -179,14 +160,12 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) {
|
||||
// std::cout << "send msg " << (int)this->buffer[0] << " to "
|
||||
// << remoteParticipant->ipAddress << std::endl;
|
||||
int bufferSize = msg->Serialize(this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
return true;
|
||||
|
||||
std::cout << "send msg " << (int)this->buffer[0] << " to "
|
||||
<< remoteParticipant->ipAddress << std::endl;
|
||||
// std::cout << "send msg " << (int)this->buffer[0] << " to "
|
||||
// << remoteParticipant->ipAddress << std::endl;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
@ -259,6 +238,8 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
void ParticipantUDP::ReceiveData(unsigned char packetSize,
|
||||
char* senderIpAddress,
|
||||
unsigned int senderPort) {
|
||||
std::cout << "Receive data from " << senderIpAddress << ":" << senderPort
|
||||
<< std::endl;
|
||||
Participant* sender = this->GetParticipant(senderIpAddress, senderPort);
|
||||
if (sender == nullptr) {
|
||||
sender = this->AddParticipant(senderIpAddress, senderPort);
|
||||
@ -346,13 +327,13 @@ void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) {
|
||||
<< " -> " << (int)msg->networkId << "\n";
|
||||
#endif
|
||||
|
||||
if (this->networkId != msg->networkId) {
|
||||
if (this->networkId != msg->networkId)
|
||||
this->networkId = msg->networkId;
|
||||
|
||||
// std::cout << this->things.size() << " things\n";
|
||||
for (Thing* thing : this->things)
|
||||
this->SendThingInfo(sender, thing);
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {
|
||||
#if defined(DEBUG)
|
||||
@ -421,7 +402,9 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
|
||||
<< "/" << (int)msg->thingId << "] ";
|
||||
#endif
|
||||
|
||||
Thing* thing = sender->Get(msg->thingId);
|
||||
Participant* owner = Participant::GetParticipant(msg->networkId);
|
||||
if (owner != nullptr) {
|
||||
Thing* thing = owner->Get(msg->thingId);
|
||||
if (thing != nullptr)
|
||||
thing->ProcessBinary(msg->data);
|
||||
#if !defined(NO_STD)
|
||||
@ -432,6 +415,7 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
|
||||
std::cout << std::endl;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Receive
|
||||
#pragma endregion
|
||||
|
@ -112,12 +112,12 @@ class ParticipantUDP : public Participant {
|
||||
unsigned int senderPort);
|
||||
void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant);
|
||||
|
||||
#if defined(NO_STD)
|
||||
unsigned char senderCount = 0;
|
||||
Participant* senders[MAX_SENDER_COUNT];
|
||||
#else
|
||||
std::list<Participant*> senders;
|
||||
#endif
|
||||
// #if defined(NO_STD)
|
||||
// unsigned char senderCount = 0;
|
||||
// Participant* senders[MAX_SENDER_COUNT];
|
||||
// #else
|
||||
// std::list<Participant*> senders;
|
||||
// #endif
|
||||
|
||||
protected:
|
||||
unsigned long nextPublishMe = 0;
|
||||
@ -126,8 +126,8 @@ class ParticipantUDP : public Participant {
|
||||
|
||||
void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
|
||||
Participant* GetParticipant(const char* ipAddress, int port);
|
||||
Participant* AddParticipant(const char* ipAddress, int port);
|
||||
//Participant* GetParticipant(const char* ipAddress, int port);
|
||||
// Participant* AddParticipant(const char* ipAddress, int port);
|
||||
|
||||
void ReceiveUDP();
|
||||
|
||||
|
@ -16,11 +16,11 @@ SiteServer::SiteServer(int port) {
|
||||
this->ipAddress = "0.0.0.0";
|
||||
this->port = port;
|
||||
|
||||
#if defined(NO_STD)
|
||||
this->senders[this->senderCount++] = this;
|
||||
#else
|
||||
this->senders.push_back(this);
|
||||
#endif
|
||||
// #if defined(NO_STD)
|
||||
// this->senders[this->senderCount++] = this;
|
||||
// #else
|
||||
// this->senders.push_back(this);
|
||||
// #endif
|
||||
|
||||
SetupUDP(port, ipAddress, 0);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user