RoboidControl-cpp/Participant.cpp

165 lines
5.1 KiB
C++

#include "Participant.h"
#include <string.h>
namespace RoboidControl {
std::list<Participant*> Participant::participants;
Participant::Participant() {}
Participant::Participant(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;
}
Participant::~Participant() {
delete[] this->ipAddress;
}
void Participant::Update(unsigned long currentTimeMs) {
for (Thing* thing : this->things) {
if (thing != nullptr)
thing->Update(currentTimeMs, true);
}
}
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->ipAddress, participant->port);
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 participant " << 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
// << "[" << (int)thingId << "]\n";
return nullptr;
}
void Participant::Add(Thing* thing, bool checkId) {
if (checkId && thing->id == 0) {
// allocate a new thing ID
#if defined(NO_STD)
thing->id = this->thingCount + 1;
this->things[this->thingCount++] = thing;
#else
thing->id = (unsigned char)this->things.size() + 1;
this->things.push_back(thing);
#endif
// std::cout << "Add thing with generated ID " << this->ipAddress << ":"
// << this->port << "[" << (int)thing->id << "]\n";
} else {
Thing* foundThing = Get(thing->id);
if (foundThing == nullptr) {
#if defined(NO_STD)
this->things[this->thingCount++] = thing;
#else
this->things.push_back(thing);
#endif
// std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
// "["
// << (int)thing->id << "]\n";
} else {
// std::cout << "Did not add, existing thing " << this->ipAddress << ":"
// << this->port << "[" << (int)thing->id << "]\n";
}
}
}
void Participant::Remove(Thing* thing) {
#if defined(NO_STD)
for (unsigned char thingIx = 0; thingIx < this->thingCount; thingIx++)
if (this->things[thingIx] == thing)
this->things[thingIx] = nullptr;
// compacting
unsigned char lastThingIx = 0;
for (unsigned char thingIx = 0; thingIx < this->thingCount; thingIx++) {
if (this->things[thingIx] == nullptr)
continue;
this->things[lastThingIx] = this->things[thingIx];
lastThingIx++;
}
this->thingCount = lastThingIx;
#else
this->things.remove_if([thing](Thing* obj) { return obj == thing; });
std::cout << "Removing [" << (int)thing->networkId << "/" << (int)thing->id
<< "] list size = " << this->things.size() << "\n";
#endif
}
// void Participant::UpdateAll(unsigned long currentTimeMs) {
// // Not very efficient, but it works for now.
// for (Thing* thing : this->things) {
// if (thing != nullptr && thing->GetParent() == nullptr) { // update all
// root things
// // std::cout << " update " << (int)ix << " thingid " << (int)thing->id
// // << "\n";
// thing->Update(currentTimeMs);
// }
// }
// }
} // namespace RoboidControl