Fix crash @NameMsg, proper RemoteParticipant things

This commit is contained in:
Pascal Serrarens 2025-02-08 11:28:25 +01:00
parent c19508179d
commit 969fbad6a2
20 changed files with 209 additions and 221 deletions

View File

@ -10,8 +10,12 @@ NameMsg::NameMsg(const char *buffer) {
this->networkId = buffer[ix++]; this->networkId = buffer[ix++];
this->thingId = buffer[ix++]; this->thingId = buffer[ix++];
this->nameLength = buffer[ix++]; this->nameLength = buffer[ix++];
this->name = &buffer[ix]; // dangerous! name should not be used anymore after // the name string in the buffer is not \0 terminated!
// buffer has been re-used... char* name = new char[this->nameLength+1];
for (int i = 0; i < this->nameLength; i++)
name[i] = buffer[ix++];
name[this->nameLength] = '\0';
this->name = name;
} }
NameMsg::NameMsg(unsigned char networkId, Thing *thing) { NameMsg::NameMsg(unsigned char networkId, Thing *thing) {
@ -24,15 +28,9 @@ NameMsg::NameMsg(unsigned char networkId, Thing *thing) {
this->name = thing->name; // dangerous! this->name = thing->name; // dangerous!
} }
// NameMsg::NameMsg(unsigned char networkId, unsigned char thingId, NameMsg::~NameMsg() {
// const char *name, unsigned char nameLength) { delete[] this->name;
// this->networkId = networkId; }
// this->thingId = thingId;
// this->name = name;
// this->nameLength = nameLength;
// }
NameMsg::~NameMsg() {}
unsigned char NameMsg::Serialize(char *buffer) { unsigned char NameMsg::Serialize(char *buffer) {
if (this->nameLength == 0 || this->name == nullptr) if (this->nameLength == 0 || this->name == nullptr)

View File

@ -28,7 +28,7 @@ Participant::Participant(int port) {
this->ipAddress = "0.0.0.0"; this->ipAddress = "0.0.0.0";
this->port = port; this->port = port;
this->participants.push_back(this); this->senders.push_back(this);
// int randomPort = (rand() % (65535 - 49152 + 1)) + 49152; // int randomPort = (rand() % (65535 - 49152 + 1)) + 49152;
this->localPort = port; this->localPort = port;
@ -39,7 +39,7 @@ Participant::Participant(const char *ipAddress, int port) {
this->ipAddress = ipAddress; this->ipAddress = ipAddress;
this->port = port; this->port = port;
this->participants.push_back(this); this->senders.push_back(this);
// int randomPort = (rand() % (65535 - 49152 + 1)) + 49152; // int randomPort = (rand() % (65535 - 49152 + 1)) + 49152;
this->localPort = port; // randomPort; this->localPort = port; // randomPort;
@ -107,32 +107,34 @@ void Participant::ReceiveUDP() {
} }
Participant *Participant::GetParticipant(const char *ipAddress, int port) { Participant *Participant::GetParticipant(const char *ipAddress, int port) {
for (Participant *participant : this->participants) { for (Participant *sender : this->senders) {
if (participant->ipAddress == ipAddress && participant->port == port) if (sender->ipAddress == ipAddress && sender->port == port)
return participant; return sender;
} }
return nullptr; return nullptr;
} }
Participant *Participant::AddParticipant(const char *ipAddress, int port) { Participant *Participant::AddParticipant(const char *ipAddress, int port) {
std::cout << "New Participant " << ipAddress << ":" << port << "\n";
Participant *participant = new Participant(ipAddress, port); Participant *participant = new Participant(ipAddress, port);
participant->networkId = (unsigned char)this->participants.size(); participant->networkId = (unsigned char)this->senders.size();
this->participants.push_back(participant); this->senders.push_back(participant);
return participant; return participant;
} }
#pragma region Send #pragma region Send
void Participant::SendThingInfo(Thing *thing) { void Participant::SendThingInfo(RemoteParticipant *remoteParticipant,
Thing *thing) {
std::cout << "Send thing info\n"; std::cout << "Send thing info\n";
ThingMsg *thingMsg = new ThingMsg(this->networkId, thing); ThingMsg *thingMsg = new ThingMsg(this->networkId, thing);
this->Send(thingMsg); this->Send(remoteParticipant, thingMsg);
delete thingMsg; delete thingMsg;
NameMsg *nameMsg = new NameMsg(this->networkId, thing); NameMsg *nameMsg = new NameMsg(this->networkId, thing);
this->Send(nameMsg); this->Send(remoteParticipant, nameMsg);
delete nameMsg; delete nameMsg;
ModelUrlMsg *modelMsg = new ModelUrlMsg(this->networkId, thing); ModelUrlMsg *modelMsg = new ModelUrlMsg(this->networkId, thing);
this->Send(modelMsg); this->Send(remoteParticipant, modelMsg);
delete modelMsg; delete modelMsg;
} }
@ -154,16 +156,20 @@ void Passer::Control::Participant::PublishThingInfo(Thing *thing) {
delete customMsg; delete customMsg;
} }
bool Participant::Send(IMessage *msg) { bool Participant::Send(RemoteParticipant *remoteParticipant, IMessage *msg) {
int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0)
return true;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
UdpWindows *thisWindows = static_cast<UdpWindows *>(this); UdpWindows *thisWindows = static_cast<UdpWindows *>(this);
return thisWindows->Send(msg); return thisWindows->Send(remoteParticipant, bufferSize);
#elif defined(__unix__) || defined(__APPLE__) #elif defined(__unix__) || defined(__APPLE__)
UdpPosix *thisPosix = static_cast<UdpPosix *>(this); UdpPosix *thisPosix = static_cast<UdpPosix *>(this);
return thisPosix->Send(msg); return thisPosix->Send(remoteParticipant, bufferSize);
#elif defined(ARDUINO) #elif defined(ARDUINO)
UdpArduino *thisArduino = static_cast<UdpArduino *>(this); UdpArduino *thisArduino = static_cast<UdpArduino *>(this);
return thisArduino->Send(msg); return thisArduino->Send(remoteParticipant, bufferSize);
#endif #endif
} }
@ -186,7 +192,7 @@ bool Participant::Publish(IMessage *msg) {
#pragma region Receive #pragma region Receive
void Participant::ReceiveData(unsigned char bufferSize, void Participant::ReceiveData(unsigned char bufferSize,
Participant *remoteParticipant) { RemoteParticipant *remoteParticipant) {
unsigned char msgId = this->buffer[0]; unsigned char msgId = this->buffer[0];
// std::cout << "receive msg " << (int)msgId << "\n"; // std::cout << "receive msg " << (int)msgId << "\n";
switch (msgId) { switch (msgId) {
@ -194,7 +200,7 @@ void Participant::ReceiveData(unsigned char bufferSize,
ClientMsg *msg = new ClientMsg(this->buffer); ClientMsg *msg = new ClientMsg(this->buffer);
Process(remoteParticipant, msg); Process(remoteParticipant, msg);
delete msg; delete msg;
} } break;
case NetworkIdMsg::id: { case NetworkIdMsg::id: {
NetworkIdMsg *msg = new NetworkIdMsg(this->buffer); NetworkIdMsg *msg = new NetworkIdMsg(this->buffer);
Process(remoteParticipant, msg); Process(remoteParticipant, msg);
@ -202,59 +208,50 @@ void Participant::ReceiveData(unsigned char bufferSize,
} break; } break;
case InvestigateMsg::id: { case InvestigateMsg::id: {
InvestigateMsg *msg = new InvestigateMsg(this->buffer); InvestigateMsg *msg = new InvestigateMsg(this->buffer);
Process(msg); Process(remoteParticipant, msg);
delete msg; delete msg;
} break; } break;
case ThingMsg::id: { case ThingMsg::id: {
ThingMsg *msg = new ThingMsg(this->buffer); ThingMsg *msg = new ThingMsg(this->buffer);
Process(msg); Process(remoteParticipant, msg);
delete msg; delete msg;
} break; } break;
case NameMsg::id: { case NameMsg::id: {
NameMsg *msg = new NameMsg(this->buffer); NameMsg *msg = new NameMsg(this->buffer);
Process(msg); Process(remoteParticipant, msg);
delete msg; delete msg;
} break; } break;
case PoseMsg::id: { case PoseMsg::id: {
PoseMsg *msg = new PoseMsg(this->buffer); PoseMsg *msg = new PoseMsg(this->buffer);
Process(msg); Process(remoteParticipant, msg);
delete msg; delete msg;
} break; } break;
case CustomMsg::id: { case CustomMsg::id: {
CustomMsg *msg = new CustomMsg(this->buffer); CustomMsg *msg = new CustomMsg(this->buffer);
Process(msg); Process(remoteParticipant, msg);
delete msg; delete msg;
} break; } break;
}; };
} }
void Participant::Process(Participant *sender, ClientMsg *msg) {} void Participant::Process(RemoteParticipant *sender, ClientMsg *msg) {}
void Participant::Process(Participant *sender, NetworkIdMsg *msg) { void Participant::Process(RemoteParticipant *sender, NetworkIdMsg *msg) {
std::cout << this->name << " receive network id " << (int)this->networkId std::cout << this->name << ": process NetworkId [" << (int)this->networkId
<< " " << (int)msg->networkId << "\n"; << "/" << (int)msg->networkId << "]\n";
if (this->networkId != msg->networkId) { if (this->networkId != msg->networkId) {
this->networkId = msg->networkId; this->networkId = msg->networkId;
// Thing **allThings = Thing::GetAllThings(); for (Thing *thing : this->things)
// for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { this->SendThingInfo(sender, thing);
// Thing *thing = allThings[ix];
// if (thing == nullptr)
// continue;
// sender->SendThingInfo(thing);
// }
for (Thing *thing : this->things) {
sender->SendThingInfo(thing);
}
} }
} }
void Participant::Process(InvestigateMsg *msg) {} void Participant::Process(RemoteParticipant *sender, InvestigateMsg *msg) {}
void Participant::Process(ThingMsg *msg) {} void Participant::Process(RemoteParticipant *sender, ThingMsg *msg) {}
void Participant::Process(NameMsg *msg) { void Participant::Process(RemoteParticipant *sender, NameMsg *msg) {
Thing *thing = this->Get(msg->networkId, msg->thingId); Thing *thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr) { if (thing != nullptr) {
int nameLength = msg->nameLength; int nameLength = msg->nameLength;
char *thingName = new char[nameLength + 1]; char *thingName = new char[nameLength + 1];
@ -266,69 +263,21 @@ void Participant::Process(NameMsg *msg) {
} }
} }
void Participant::Process(PoseMsg *msg) {} void Participant::Process(RemoteParticipant *sender, PoseMsg *msg) {}
void Participant::Process(CustomMsg *msg) { void Participant::Process(RemoteParticipant *sender, CustomMsg *msg) {
Thing *thing = this->Get(msg->networkId, msg->thingId); // std::cout << this->name << ": process Binary [" << (int)this->networkId << "/"
// << (int)msg->networkId << "]\n";
Thing *thing = sender->Get(msg->networkId, msg->thingId);
if (thing != nullptr) if (thing != nullptr)
thing->ProcessBytes(msg->bytes); thing->ProcessBytes(msg->bytes);
else else
std::cout << "custom msg for unknown thing " << (int)msg->networkId << ":" std::cout << "custom msg for unknown thing " << (int)msg->networkId << ":"
<< (int)msg->thingId << "\n"; << (int)msg->thingId << "\n";
// std::cout << "Processed custom msg\n";
} }
// Receive // Receive
#pragma endregion #pragma endregion
#pragma region Things
Thing *Participant::Get(unsigned char networkId, unsigned char thingId) {
// std::cout << "Get " << (int)networkId << "/" << (int)thingId << " from "
// << this->things.size() << " things\n";
for (auto &thing : this->things) {
// std::cout << " ? " << (int)thing->networkId << "/" << (int)thing->id
// << "\n";
if (thing->networkId == networkId && thing->id == thingId) {
return thing;
}
}
return nullptr;
}
int Participant::Add(Thing *newThing) {
for (Thing *thing : this->things) {
if (thing == newThing) {
std::cout << "Thing already exists, not adding\n";
return thing->id;
}
}
std::cout << "Adding " << (int)newThing->networkId << "/" << (int)newThing->id
<< "\n";
this->things.push_back(newThing);
return this->things.size();
}
void Participant::Remove(Thing *thing) {
this->things.remove_if([thing](Thing *obj) { return obj == thing; });
std::cout << "Removing " << thing->networkId << "/" << thing->id
<< " list size = " << this->things.size() << "\n";
}
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);
}
}
}
#pragma endregion
} // namespace Control } // namespace Control
} // namespace Passer } // namespace Passer

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Messages.h" //#include "Messages/"
#include "Messages/ClientMsg.h" #include "Messages/ClientMsg.h"
#include "Messages/CustomMsg.h" #include "Messages/CustomMsg.h"
#include "Messages/InvestigateMsg.h" #include "Messages/InvestigateMsg.h"
@ -9,6 +9,7 @@
#include "Messages/NetworkIdMsg.h" #include "Messages/NetworkIdMsg.h"
#include "Messages/PoseMsg.h" #include "Messages/PoseMsg.h"
#include "Messages/ThingMsg.h" #include "Messages/ThingMsg.h"
#include "RemoteParticipant.h"
#include <list> #include <list>
@ -27,16 +28,16 @@ namespace Passer {
namespace Control { namespace Control {
/// @brief A participant is device which can communicate with other participants /// @brief A participant is device which can communicate with other participants
class Participant { class Participant : public RemoteParticipant {
public: public:
char buffer[1024]; char buffer[1024];
long publishInterval = 3000; // 3 seconds long publishInterval = 3000; // 3 seconds
unsigned char networkId = 0; // unsigned char networkId = 0;
const char *name = "Participant"; const char *name = "Participant";
const char *ipAddress = "0.0.0.0"; // const char *ipAddress = "0.0.0.0";
int port = 0; // int port = 0;
int localPort = 0; int localPort = 0;
#if defined(ARDUINO) #if defined(ARDUINO)
@ -67,22 +68,22 @@ public:
virtual void Update(unsigned long currentTimeMs = 0); virtual void Update(unsigned long currentTimeMs = 0);
std::list<Thing *> things; // std::list<Thing *> things;
Thing *Get(unsigned char networkId, unsigned char thingId); // Thing *Get(unsigned char networkId, unsigned char thingId);
int Add(Thing *thing); // int Add(Thing *thing);
void Remove(Thing *thing); // void Remove(Thing *thing);
void UpdateAll(unsigned long currentTimeMs); // void UpdateAll(unsigned long currentTimeMs);
void SendThingInfo(Thing *thing); void SendThingInfo(RemoteParticipant* remoteParticipant, Thing *thing);
void PublishThingInfo(Thing *thing); void PublishThingInfo(Thing *thing);
bool Send(IMessage *msg); bool Send(RemoteParticipant* remoteParticipant, IMessage *msg);
bool Publish(IMessage *msg); bool Publish(IMessage *msg);
void ReceiveData(unsigned char bufferSize, Participant *remoteParticipant); void ReceiveData(unsigned char bufferSize, RemoteParticipant *remoteParticipant);
protected: protected:
std::list<Participant *> participants; std::list<Participant *> senders;
unsigned long nextPublishMe = 0; unsigned long nextPublishMe = 0;
@ -93,13 +94,13 @@ protected:
void ReceiveUDP(); void ReceiveUDP();
virtual void Process(Participant *sender, ClientMsg *msg); virtual void Process(RemoteParticipant *sender, ClientMsg *msg);
virtual void Process(Participant *sender, NetworkIdMsg *msg); virtual void Process(RemoteParticipant *sender, NetworkIdMsg *msg);
virtual void Process(InvestigateMsg *msg); virtual void Process(RemoteParticipant* sender, InvestigateMsg *msg);
virtual void Process(ThingMsg *msg); virtual void Process(RemoteParticipant* sender, ThingMsg *msg);
virtual void Process(NameMsg *msg); virtual void Process(RemoteParticipant* sender, NameMsg *msg);
virtual void Process(PoseMsg *msg); virtual void Process(RemoteParticipant* sender, PoseMsg *msg);
virtual void Process(CustomMsg *msg); virtual void Process(RemoteParticipant* sender, CustomMsg *msg);
}; };
} // namespace Control } // namespace Control

54
RemoteParticipant.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "RemoteParticipant.h"
namespace Passer {
namespace Control {
RemoteParticipant::RemoteParticipant() {}
RemoteParticipant::RemoteParticipant(const char *ipAddress, int port) {
this->ipAddress = ipAddress;
this->port = port;
}
Thing *RemoteParticipant::Get(unsigned char networkId, unsigned char thingId) {
for (Thing *thing : this->things) {
if (thing->networkId == networkId && thing->id == thingId)
return thing;
}
// std::cout << "Could not find thing " << this->ipAddress << ":" << this->port
// << "[" << (int)networkId << "/" << (int)thingId << "]\n";
return nullptr;
}
void RemoteParticipant::Add(Thing *thing) {
Thing *foundThing = Get(thing->networkId, thing->id);
if (foundThing == nullptr) {
this->things.push_back(thing);
std::cout << "Add thing " << this->ipAddress << ":" << this->port << "["
<< (int)thing->networkId << "/" << (int)thing->id << "]\n";
} else
std::cout << "Did not add, existing thing " << this->ipAddress << ":" << this->port << "["
<< (int)thing->networkId << "/" << (int)thing->id << "]\n";
}
void RemoteParticipant::Remove(Thing *thing) {
this->things.remove_if([thing](Thing *obj) { return obj == thing; });
std::cout << "Removing " << thing->networkId << "/" << thing->id
<< " list size = " << this->things.size() << "\n";
}
void RemoteParticipant::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 Control
} // namespace Passer

28
RemoteParticipant.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include "Thing.h"
namespace Passer {
namespace Control {
class RemoteParticipant {
public:
const char *ipAddress = "0.0.0.0";
int port = 0;
unsigned char networkId = 0;
RemoteParticipant();
RemoteParticipant(const char *ipAddress, int port);
protected:
std::list<Thing *> things;
public:
Thing *Get(unsigned char networkId, unsigned char thingId);
void Add(Thing *thing);
void Remove(Thing *thing);
void UpdateAll(unsigned long currentTimeMs);
};
} // namespace Control
} // namespace Passer

View File

@ -1,6 +1,6 @@
#include "TemperatureSensor.h" #include "TemperatureSensor.h"
#include "LowLevelMessages.h" #include "Messages/LowLevelMessages.h"
namespace Passer { namespace Passer {
namespace Control { namespace Control {

View File

@ -15,40 +15,37 @@ SiteServer::SiteServer(int port) {
this->ipAddress = "0.0.0.0"; this->ipAddress = "0.0.0.0";
this->port = port; this->port = port;
this->participants.push_back(this); this->senders.push_back(this);
SetupUDP(port, ipAddress, 0); SetupUDP(port, ipAddress, 0);
Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor); Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor);
} }
void SiteServer::Process(Participant *sender, ClientMsg *msg) { void SiteServer::Process(RemoteParticipant *sender, ClientMsg *msg) {
if (msg->networkId == 0) { if (msg->networkId == 0) {
std::cout << this->name << " received New Client -> " << sender->ipAddress std::cout << this->name << " received New Client -> " << sender->ipAddress
<< " " << (int)sender->networkId << "\n"; << " " << (int)sender->networkId << "\n";
NetworkIdMsg *msg = new NetworkIdMsg(sender->networkId); NetworkIdMsg *msg = new NetworkIdMsg(sender->networkId);
sender->Send(msg); //sender->Send(msg);
this->Send(sender, msg);
delete msg; delete msg;
} }
} }
void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {} void SiteServer::Process(RemoteParticipant *sender, NetworkIdMsg *msg) {}
void SiteServer::Process(ThingMsg *msg) { void SiteServer::Process(RemoteParticipant *sender, ThingMsg *msg) {
Thing *thing = sender->Get(msg->networkId, msg->thingId);
Thing *thing = this->Get(msg->networkId, msg->thingId);
if (thing == nullptr) { if (thing == nullptr) {
std::cout << "could not find thing " << (int)msg->networkId << "/"
<< (int)msg->thingId << "\n";
auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
if (thingMsgProcessor != thingMsgProcessors.end()) { // found item Thing *newThing;
Thing *newThing = thingMsgProcessor->second(msg->networkId, msg->thingId); if (thingMsgProcessor != thingMsgProcessors.end()) // found item
this->Add(newThing); newThing = thingMsgProcessor->second(msg->networkId, msg->thingId);
} else { else
Thing *newThing = new Thing(this, msg->networkId, msg->thingId, newThing = new Thing(sender, msg->networkId, msg->thingId,
(Thing::Type)msg->thingType); (Thing::Type)msg->thingType);
this->Add(newThing); sender->Add(newThing);
}
} }
} }

View File

@ -26,9 +26,9 @@ public:
protected: protected:
unsigned long nextPublishMe = 0; unsigned long nextPublishMe = 0;
virtual void Process(Participant *sender, ClientMsg *msg) override; virtual void Process(RemoteParticipant *sender, ClientMsg *msg) override;
virtual void Process(Participant *sender, NetworkIdMsg *msg) override; virtual void Process(RemoteParticipant *sender, NetworkIdMsg *msg) override;
virtual void Process(ThingMsg *msg) override; virtual void Process(RemoteParticipant* sender, ThingMsg *msg) override;
using ThingConstructor = using ThingConstructor =
std::function<Thing *(unsigned char networkId, unsigned char thingId)>; std::function<Thing *(unsigned char networkId, unsigned char thingId)>;

View File

@ -15,48 +15,29 @@ Thing::Thing(unsigned char thingType) {
this->id = 0; this->id = 0;
this->type = thingType; this->type = thingType;
this->networkId = 0; this->networkId = 0;
this->Init();
// int thingId = Thing::Add(this);
// if (thingId < 0) {
// std::cout << "ERROR: Thing store is full\n";
// this->id = 0; // what to do when we cannot store any more things?
// } else
// this->id = thingId;
this->linearVelocity = Spherical16::zero; this->linearVelocity = Spherical16::zero;
this->angularVelocity = Spherical16::zero; this->angularVelocity = Spherical16::zero;
} }
Passer::Control::Thing::Thing(Participant *participant, unsigned char networkId, Passer::Control::Thing::Thing(RemoteParticipant *participant, unsigned char networkId,
unsigned char thingId, Type thingType) { unsigned char thingId, Type thingType) {
// no participant reference yet.. // no participant reference yet..
this->participant = participant;
this->networkId = networkId; this->networkId = networkId;
this->id = thingId; this->id = thingId;
this->type = (unsigned char)thingType; this->type = (unsigned char)thingType;
this->Init();
// thingId = Thing::Add(this);
// if (thingId < 0) {
// std::cout << "ERROR: Thing store is full\n";
// this->id = 0; // what to do when we cannot store any more things?
// } else
// this->id = thingId;
this->linearVelocity = Spherical16::zero; this->linearVelocity = Spherical16::zero;
this->angularVelocity = Spherical16::zero; this->angularVelocity = Spherical16::zero;
std::cout << "Created thing " << (int)this->networkId << "/" << (int)this->id // std::cout << "Created thing " << (int)this->networkId << "/" << (int)this->id
<< "\n"; // << "\n";
} }
void Thing::Terminate() { void Thing::Terminate() {
// Thing::Remove(this); // Thing::Remove(this);
} }
void Thing::Init() {}
Thing *Thing::FindThing(const char *name) { Thing *Thing::FindThing(const char *name) {
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
Thing *child = this->children[childIx]; Thing *child = this->children[childIx];

19
Thing.h
View File

@ -7,7 +7,7 @@
namespace Passer { namespace Passer {
namespace Control { namespace Control {
class Participant; class RemoteParticipant;
#define THING_STORE_SIZE 256 #define THING_STORE_SIZE 256
// IMPORTANT: values higher than 256 will need to change the Thing::id type // IMPORTANT: values higher than 256 will need to change the Thing::id type
@ -16,7 +16,7 @@ class Participant;
/// @brief A thing is the basic building block /// @brief A thing is the basic building block
class Thing { class Thing {
public: public:
// Participant *client; RemoteParticipant *participant;
unsigned char networkId = 0; unsigned char networkId = 0;
/// @char The id of the thing /// @char The id of the thing
unsigned char id = 0; unsigned char id = 0;
@ -41,7 +41,7 @@ public:
Thing(Type thingType = Type::Undetermined); Thing(Type thingType = Type::Undetermined);
Thing(unsigned char thingType); Thing(unsigned char thingType);
Thing(Participant *participant, unsigned char networkId, Thing(RemoteParticipant *participant, unsigned char networkId,
unsigned char thingId, Type thingType = Type::Undetermined); unsigned char thingId, Type thingType = Type::Undetermined);
Thing *FindThing(const char *name); Thing *FindThing(const char *name);
@ -124,21 +124,8 @@ public:
}; };
virtual void ProcessBytes(char *bytes) { (void)bytes; }; virtual void ProcessBytes(char *bytes) { (void)bytes; };
protected:
virtual void Init();
//------------ All things
// public:
// static Thing *Get(unsigned char networkId, unsigned char thingId);
// static int Add(Thing *thing);
// static void Remove(Thing *thing);
// static void UpdateAll(unsigned long currentTimeMs);
// static std::list<Thing *> allThings;
}; };
// static std::list<Thing *> allThings;
} // namespace Control } // namespace Control
} // namespace Passer } // namespace Passer
using namespace Passer::Control; using namespace Passer::Control;

View File

@ -50,11 +50,11 @@ void UdpArduino::Receive() {
this->GetParticipant(sender_ipAddress, sender_port); this->GetParticipant(sender_ipAddress, sender_port);
if (remoteParticipant == nullptr) { if (remoteParticipant == nullptr) {
remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port);
std::cout << "New sender " << sender_ipAddress << ":" << sender_port // std::cout << "New sender " << sender_ipAddress << ":" << sender_port
<< "\n"; // << "\n";
std::cout << "New remote participant " << remoteParticipant->ipAddress // std::cout << "New remote participant " << remoteParticipant->ipAddress
<< ":" << remoteParticipant->port << " " // << ":" << remoteParticipant->port << " "
<< (int)remoteParticipant->networkId << "\n"; // << (int)remoteParticipant->networkId << "\n";
} }
ReceiveData(packetSize, remoteParticipant); ReceiveData(packetSize, remoteParticipant);
@ -63,13 +63,9 @@ void UdpArduino::Receive() {
#endif #endif
} }
bool UdpArduino::Send(IMessage *msg) { bool UdpArduino::Send(RemoteParticipant* remoteParticipant, int bufferSize) {
#if ARDUINO #if ARDUINO
int bufferSize = msg->Serialize(this->buffer); udp.beginPacket(remoteParticipant->ipAddress, remoteParticipant->port);
if (bufferSize <= 0)
return true;
udp.beginPacket(this->remoteIpAddress, this->remotePort);
udp.write(buffer, bufferSize); udp.write(buffer, bufferSize);
udp.endPacket(); udp.endPacket();

View File

@ -9,7 +9,7 @@ class UdpArduino : public Participant {
public: public:
void Setup(int localPort, const char *remoteIpAddress, int remotePort); void Setup(int localPort, const char *remoteIpAddress, int remotePort);
void Receive(); void Receive();
bool Send(IMessage *msg); bool Send(RemoteParticipant* remoteParticipant, int bufferSize);
bool Publish(IMessage *msg); bool Publish(IMessage *msg);
protected: protected:

View File

@ -81,11 +81,11 @@ void UdpPosix::Receive() {
this->GetParticipant(sender_ipAddress, sender_port); this->GetParticipant(sender_ipAddress, sender_port);
if (remoteParticipant == nullptr) { if (remoteParticipant == nullptr) {
remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port);
std::cout << "New sender " << sender_ipAddress << ":" << sender_port // std::cout << "New sender " << sender_ipAddress << ":" << sender_port
<< "\n"; // << "\n";
std::cout << "New remote participant " << remoteParticipant->ipAddress // std::cout << "New remote participant " << remoteParticipant->ipAddress
<< ":" << remoteParticipant->port << " " // << ":" << remoteParticipant->port << " "
<< (int)remoteParticipant->networkId << "\n"; // << (int)remoteParticipant->networkId << "\n";
} }
ReceiveData(packetSize, remoteParticipant); ReceiveData(packetSize, remoteParticipant);
@ -94,17 +94,20 @@ void UdpPosix::Receive() {
#endif #endif
} }
bool UdpPosix::Send( bool UdpPosix::Send(RemoteParticipant *remoteParticipant, int bufferSize) {
IMessage *msg) { // Send the message to the specified address and port
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
int bufferSize = msg->Serialize(this->buffer); // Set up the destination address
if (bufferSize <= 0) // char ip_str[INET_ADDRSTRLEN];
return true; // inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
// std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port)
// << "\n";
struct sockaddr_in dest_addr;
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(remoteParticipant->port);
dest_addr.sin_addr.s_addr = inet_addr(remoteParticipant->ipAddress);
char ip_str[INET_ADDRSTRLEN]; // Send the message
inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port)
<< "\n";
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, int sent_bytes = sendto(sock, this->buffer, bufferSize, 0,
(struct sockaddr *)&remote_addr, sizeof(remote_addr)); (struct sockaddr *)&remote_addr, sizeof(remote_addr));
if (sent_bytes < 0) { if (sent_bytes < 0) {

View File

@ -9,7 +9,7 @@ class UdpPosix : public Participant {
public: public:
void Setup(int localPort, const char *remoteIpAddress, int remotePort); void Setup(int localPort, const char *remoteIpAddress, int remotePort);
void Receive(); void Receive();
bool Send(IMessage *msg); bool Send(RemoteParticipant* remoteParticipant, int bufferSize);
bool Publish(IMessage *msg); bool Publish(IMessage *msg);
}; };

View File

@ -144,14 +144,8 @@ void UdpWindows::Receive() {
#endif #endif
} }
bool UdpWindows::Send( bool UdpWindows::Send(RemoteParticipant *remoteParticipant, int bufferSize) {
IMessage *msg) { // Send the message to the specified address and port
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
int bufferSize = msg->Serialize(this->buffer);
// std::cout << "buffer size " << bufferSize << "\n";
if (bufferSize <= 0)
return true;
char ip_str[INET_ADDRSTRLEN]; char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port) std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port)

View File

@ -9,7 +9,7 @@ class UdpWindows : public Participant {
public: public:
void Setup(int localPort, const char *remoteIpAddress, int remotePort); void Setup(int localPort, const char *remoteIpAddress, int remotePort);
void Receive(); void Receive();
bool Send(IMessage *msg); bool Send(RemoteParticipant* remoteParticipant, int bufferSize);
bool Publish(IMessage *msg); bool Publish(IMessage *msg);
}; };