diff --git a/LowLevelMessages.cpp b/Messages/LowLevelMessages.cpp similarity index 100% rename from LowLevelMessages.cpp rename to Messages/LowLevelMessages.cpp diff --git a/LowLevelMessages.h b/Messages/LowLevelMessages.h similarity index 100% rename from LowLevelMessages.h rename to Messages/LowLevelMessages.h diff --git a/Messages.cpp b/Messages/Messages.cpp similarity index 100% rename from Messages.cpp rename to Messages/Messages.cpp diff --git a/Messages.h b/Messages/Messages.h similarity index 100% rename from Messages.h rename to Messages/Messages.h diff --git a/Messages/NameMsg.cpp b/Messages/NameMsg.cpp index 6c0d054..198cb03 100644 --- a/Messages/NameMsg.cpp +++ b/Messages/NameMsg.cpp @@ -10,8 +10,12 @@ NameMsg::NameMsg(const char *buffer) { this->networkId = buffer[ix++]; this->thingId = buffer[ix++]; this->nameLength = buffer[ix++]; - this->name = &buffer[ix]; // dangerous! name should not be used anymore after - // buffer has been re-used... + // the name string in the buffer is not \0 terminated! + 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) { @@ -24,15 +28,9 @@ NameMsg::NameMsg(unsigned char networkId, Thing *thing) { this->name = thing->name; // dangerous! } -// NameMsg::NameMsg(unsigned char networkId, unsigned char thingId, -// const char *name, unsigned char nameLength) { -// this->networkId = networkId; -// this->thingId = thingId; -// this->name = name; -// this->nameLength = nameLength; -// } - -NameMsg::~NameMsg() {} +NameMsg::~NameMsg() { + delete[] this->name; +} unsigned char NameMsg::Serialize(char *buffer) { if (this->nameLength == 0 || this->name == nullptr) diff --git a/Participant.cpp b/Participant.cpp index fa595d5..f2e2066 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -28,7 +28,7 @@ Participant::Participant(int port) { this->ipAddress = "0.0.0.0"; this->port = port; - this->participants.push_back(this); + this->senders.push_back(this); // int randomPort = (rand() % (65535 - 49152 + 1)) + 49152; this->localPort = port; @@ -39,7 +39,7 @@ Participant::Participant(const char *ipAddress, int port) { this->ipAddress = ipAddress; this->port = port; - this->participants.push_back(this); + this->senders.push_back(this); // int randomPort = (rand() % (65535 - 49152 + 1)) + 49152; this->localPort = port; // randomPort; @@ -107,32 +107,34 @@ void Participant::ReceiveUDP() { } Participant *Participant::GetParticipant(const char *ipAddress, int port) { - for (Participant *participant : this->participants) { - if (participant->ipAddress == ipAddress && participant->port == port) - return participant; + for (Participant *sender : this->senders) { + if (sender->ipAddress == ipAddress && sender->port == port) + return sender; } return nullptr; } Participant *Participant::AddParticipant(const char *ipAddress, int port) { + std::cout << "New Participant " << ipAddress << ":" << port << "\n"; Participant *participant = new Participant(ipAddress, port); - participant->networkId = (unsigned char)this->participants.size(); - this->participants.push_back(participant); + participant->networkId = (unsigned char)this->senders.size(); + this->senders.push_back(participant); return participant; } #pragma region Send -void Participant::SendThingInfo(Thing *thing) { +void Participant::SendThingInfo(RemoteParticipant *remoteParticipant, + Thing *thing) { std::cout << "Send thing info\n"; ThingMsg *thingMsg = new ThingMsg(this->networkId, thing); - this->Send(thingMsg); + this->Send(remoteParticipant, thingMsg); delete thingMsg; NameMsg *nameMsg = new NameMsg(this->networkId, thing); - this->Send(nameMsg); + this->Send(remoteParticipant, nameMsg); delete nameMsg; ModelUrlMsg *modelMsg = new ModelUrlMsg(this->networkId, thing); - this->Send(modelMsg); + this->Send(remoteParticipant, modelMsg); delete modelMsg; } @@ -154,16 +156,20 @@ void Passer::Control::Participant::PublishThingInfo(Thing *thing) { 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) UdpWindows *thisWindows = static_cast(this); - return thisWindows->Send(msg); + return thisWindows->Send(remoteParticipant, bufferSize); #elif defined(__unix__) || defined(__APPLE__) UdpPosix *thisPosix = static_cast(this); - return thisPosix->Send(msg); + return thisPosix->Send(remoteParticipant, bufferSize); #elif defined(ARDUINO) UdpArduino *thisArduino = static_cast(this); - return thisArduino->Send(msg); + return thisArduino->Send(remoteParticipant, bufferSize); #endif } @@ -186,7 +192,7 @@ bool Participant::Publish(IMessage *msg) { #pragma region Receive void Participant::ReceiveData(unsigned char bufferSize, - Participant *remoteParticipant) { + RemoteParticipant *remoteParticipant) { unsigned char msgId = this->buffer[0]; // std::cout << "receive msg " << (int)msgId << "\n"; switch (msgId) { @@ -194,7 +200,7 @@ void Participant::ReceiveData(unsigned char bufferSize, ClientMsg *msg = new ClientMsg(this->buffer); Process(remoteParticipant, msg); delete msg; - } + } break; case NetworkIdMsg::id: { NetworkIdMsg *msg = new NetworkIdMsg(this->buffer); Process(remoteParticipant, msg); @@ -202,59 +208,50 @@ void Participant::ReceiveData(unsigned char bufferSize, } break; case InvestigateMsg::id: { InvestigateMsg *msg = new InvestigateMsg(this->buffer); - Process(msg); + Process(remoteParticipant, msg); delete msg; } break; case ThingMsg::id: { ThingMsg *msg = new ThingMsg(this->buffer); - Process(msg); + Process(remoteParticipant, msg); delete msg; } break; case NameMsg::id: { NameMsg *msg = new NameMsg(this->buffer); - Process(msg); + Process(remoteParticipant, msg); delete msg; } break; case PoseMsg::id: { PoseMsg *msg = new PoseMsg(this->buffer); - Process(msg); + Process(remoteParticipant, msg); delete msg; } break; case CustomMsg::id: { CustomMsg *msg = new CustomMsg(this->buffer); - Process(msg); + Process(remoteParticipant, msg); delete msg; } break; }; } -void Participant::Process(Participant *sender, ClientMsg *msg) {} +void Participant::Process(RemoteParticipant *sender, ClientMsg *msg) {} -void Participant::Process(Participant *sender, NetworkIdMsg *msg) { - std::cout << this->name << " receive network id " << (int)this->networkId - << " " << (int)msg->networkId << "\n"; +void Participant::Process(RemoteParticipant *sender, NetworkIdMsg *msg) { + std::cout << this->name << ": process NetworkId [" << (int)this->networkId + << "/" << (int)msg->networkId << "]\n"; if (this->networkId != msg->networkId) { this->networkId = msg->networkId; - // Thing **allThings = Thing::GetAllThings(); - // for (uint16_t ix = 0; ix < THING_STORE_SIZE; ix++) { - // Thing *thing = allThings[ix]; - // if (thing == nullptr) - // continue; - - // sender->SendThingInfo(thing); - // } - for (Thing *thing : this->things) { - sender->SendThingInfo(thing); - } + for (Thing *thing : this->things) + this->SendThingInfo(sender, 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) { - Thing *thing = this->Get(msg->networkId, msg->thingId); +void Participant::Process(RemoteParticipant *sender, NameMsg *msg) { + Thing *thing = sender->Get(msg->networkId, msg->thingId); if (thing != nullptr) { int nameLength = msg->nameLength; 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) { - Thing *thing = this->Get(msg->networkId, msg->thingId); +void Participant::Process(RemoteParticipant *sender, CustomMsg *msg) { + // std::cout << this->name << ": process Binary [" << (int)this->networkId << "/" + // << (int)msg->networkId << "]\n"; + Thing *thing = sender->Get(msg->networkId, msg->thingId); if (thing != nullptr) thing->ProcessBytes(msg->bytes); else std::cout << "custom msg for unknown thing " << (int)msg->networkId << ":" << (int)msg->thingId << "\n"; - // std::cout << "Processed custom msg\n"; } // Receive #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 Passer diff --git a/Participant.h b/Participant.h index 189fddf..913467e 100644 --- a/Participant.h +++ b/Participant.h @@ -1,6 +1,6 @@ #pragma once -#include "Messages.h" +//#include "Messages/" #include "Messages/ClientMsg.h" #include "Messages/CustomMsg.h" #include "Messages/InvestigateMsg.h" @@ -9,6 +9,7 @@ #include "Messages/NetworkIdMsg.h" #include "Messages/PoseMsg.h" #include "Messages/ThingMsg.h" +#include "RemoteParticipant.h" #include @@ -27,16 +28,16 @@ namespace Passer { namespace Control { /// @brief A participant is device which can communicate with other participants -class Participant { +class Participant : public RemoteParticipant { public: char buffer[1024]; long publishInterval = 3000; // 3 seconds - unsigned char networkId = 0; + // unsigned char networkId = 0; const char *name = "Participant"; - const char *ipAddress = "0.0.0.0"; - int port = 0; + // const char *ipAddress = "0.0.0.0"; + // int port = 0; int localPort = 0; #if defined(ARDUINO) @@ -67,22 +68,22 @@ public: virtual void Update(unsigned long currentTimeMs = 0); - std::list things; - Thing *Get(unsigned char networkId, unsigned char thingId); - int Add(Thing *thing); - void Remove(Thing *thing); - void UpdateAll(unsigned long currentTimeMs); + // std::list things; + // Thing *Get(unsigned char networkId, unsigned char thingId); + // int Add(Thing *thing); + // void Remove(Thing *thing); + // void UpdateAll(unsigned long currentTimeMs); - void SendThingInfo(Thing *thing); + void SendThingInfo(RemoteParticipant* remoteParticipant, Thing *thing); void PublishThingInfo(Thing *thing); - bool Send(IMessage *msg); + bool Send(RemoteParticipant* remoteParticipant, IMessage *msg); bool Publish(IMessage *msg); - void ReceiveData(unsigned char bufferSize, Participant *remoteParticipant); + void ReceiveData(unsigned char bufferSize, RemoteParticipant *remoteParticipant); protected: - std::list participants; + std::list senders; unsigned long nextPublishMe = 0; @@ -93,13 +94,13 @@ protected: void ReceiveUDP(); - virtual void Process(Participant *sender, ClientMsg *msg); - virtual void Process(Participant *sender, NetworkIdMsg *msg); - virtual void Process(InvestigateMsg *msg); - virtual void Process(ThingMsg *msg); - virtual void Process(NameMsg *msg); - virtual void Process(PoseMsg *msg); - virtual void Process(CustomMsg *msg); + virtual void Process(RemoteParticipant *sender, ClientMsg *msg); + virtual void Process(RemoteParticipant *sender, NetworkIdMsg *msg); + virtual void Process(RemoteParticipant* sender, InvestigateMsg *msg); + virtual void Process(RemoteParticipant* sender, ThingMsg *msg); + virtual void Process(RemoteParticipant* sender, NameMsg *msg); + virtual void Process(RemoteParticipant* sender, PoseMsg *msg); + virtual void Process(RemoteParticipant* sender, CustomMsg *msg); }; } // namespace Control diff --git a/RemoteParticipant.cpp b/RemoteParticipant.cpp new file mode 100644 index 0000000..51e3b8d --- /dev/null +++ b/RemoteParticipant.cpp @@ -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 \ No newline at end of file diff --git a/RemoteParticipant.h b/RemoteParticipant.h new file mode 100644 index 0000000..bf0f36d --- /dev/null +++ b/RemoteParticipant.h @@ -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 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 \ No newline at end of file diff --git a/Sensors/TemperatureSensor.cpp b/Sensors/TemperatureSensor.cpp index 9fe5c67..0792e60 100644 --- a/Sensors/TemperatureSensor.cpp +++ b/Sensors/TemperatureSensor.cpp @@ -1,6 +1,6 @@ #include "TemperatureSensor.h" -#include "LowLevelMessages.h" +#include "Messages/LowLevelMessages.h" namespace Passer { namespace Control { diff --git a/SiteServer.cpp b/SiteServer.cpp index f48ec3f..1bb7bb1 100644 --- a/SiteServer.cpp +++ b/SiteServer.cpp @@ -15,40 +15,37 @@ SiteServer::SiteServer(int port) { this->ipAddress = "0.0.0.0"; this->port = port; - this->participants.push_back(this); + this->senders.push_back(this); SetupUDP(port, ipAddress, 0); Register((unsigned char)Thing::Type::TemperatureSensor); } -void SiteServer::Process(Participant *sender, ClientMsg *msg) { +void SiteServer::Process(RemoteParticipant *sender, ClientMsg *msg) { if (msg->networkId == 0) { std::cout << this->name << " received New Client -> " << sender->ipAddress << " " << (int)sender->networkId << "\n"; NetworkIdMsg *msg = new NetworkIdMsg(sender->networkId); - sender->Send(msg); + //sender->Send(msg); + this->Send(sender, msg); delete msg; } } -void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {} +void SiteServer::Process(RemoteParticipant *sender, NetworkIdMsg *msg) {} -void SiteServer::Process(ThingMsg *msg) { - - Thing *thing = this->Get(msg->networkId, msg->thingId); +void SiteServer::Process(RemoteParticipant *sender, ThingMsg *msg) { + Thing *thing = sender->Get(msg->networkId, msg->thingId); if (thing == nullptr) { - std::cout << "could not find thing " << (int)msg->networkId << "/" - << (int)msg->thingId << "\n"; auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); - if (thingMsgProcessor != thingMsgProcessors.end()) { // found item - Thing *newThing = thingMsgProcessor->second(msg->networkId, msg->thingId); - this->Add(newThing); - } else { - Thing *newThing = new Thing(this, msg->networkId, msg->thingId, - (Thing::Type)msg->thingType); - this->Add(newThing); - } + Thing *newThing; + if (thingMsgProcessor != thingMsgProcessors.end()) // found item + newThing = thingMsgProcessor->second(msg->networkId, msg->thingId); + else + newThing = new Thing(sender, msg->networkId, msg->thingId, + (Thing::Type)msg->thingType); + sender->Add(newThing); } } diff --git a/SiteServer.h b/SiteServer.h index a87f59e..0500eb6 100644 --- a/SiteServer.h +++ b/SiteServer.h @@ -26,9 +26,9 @@ public: protected: unsigned long nextPublishMe = 0; - virtual void Process(Participant *sender, ClientMsg *msg) override; - virtual void Process(Participant *sender, NetworkIdMsg *msg) override; - virtual void Process(ThingMsg *msg) override; + virtual void Process(RemoteParticipant *sender, ClientMsg *msg) override; + virtual void Process(RemoteParticipant *sender, NetworkIdMsg *msg) override; + virtual void Process(RemoteParticipant* sender, ThingMsg *msg) override; using ThingConstructor = std::function; diff --git a/Thing.cpp b/Thing.cpp index b3237f8..bba3a7b 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -15,48 +15,29 @@ Thing::Thing(unsigned char thingType) { this->id = 0; this->type = thingType; 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->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) { // no participant reference yet.. + this->participant = participant; this->networkId = networkId; this->id = thingId; 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->angularVelocity = Spherical16::zero; - std::cout << "Created thing " << (int)this->networkId << "/" << (int)this->id - << "\n"; + // std::cout << "Created thing " << (int)this->networkId << "/" << (int)this->id + // << "\n"; } void Thing::Terminate() { // Thing::Remove(this); } -void Thing::Init() {} - Thing *Thing::FindThing(const char *name) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { Thing *child = this->children[childIx]; diff --git a/Thing.h b/Thing.h index 612d17f..1dd5e56 100644 --- a/Thing.h +++ b/Thing.h @@ -7,7 +7,7 @@ namespace Passer { namespace Control { -class Participant; +class RemoteParticipant; #define THING_STORE_SIZE 256 // 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 class Thing { public: - // Participant *client; + RemoteParticipant *participant; unsigned char networkId = 0; /// @char The id of the thing unsigned char id = 0; @@ -41,7 +41,7 @@ public: Thing(Type thingType = Type::Undetermined); Thing(unsigned char thingType); - Thing(Participant *participant, unsigned char networkId, + Thing(RemoteParticipant *participant, unsigned char networkId, unsigned char thingId, Type thingType = Type::Undetermined); Thing *FindThing(const char *name); @@ -124,21 +124,8 @@ public: }; 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 allThings; }; -// static std::list allThings; - } // namespace Control } // namespace Passer using namespace Passer::Control; \ No newline at end of file diff --git a/UdpArduino.cpp b/UdpArduino.cpp index 31d81ff..cb9cab0 100644 --- a/UdpArduino.cpp +++ b/UdpArduino.cpp @@ -50,11 +50,11 @@ void UdpArduino::Receive() { this->GetParticipant(sender_ipAddress, sender_port); if (remoteParticipant == nullptr) { remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); - std::cout << "New sender " << sender_ipAddress << ":" << sender_port - << "\n"; - std::cout << "New remote participant " << remoteParticipant->ipAddress - << ":" << remoteParticipant->port << " " - << (int)remoteParticipant->networkId << "\n"; + // std::cout << "New sender " << sender_ipAddress << ":" << sender_port + // << "\n"; + // std::cout << "New remote participant " << remoteParticipant->ipAddress + // << ":" << remoteParticipant->port << " " + // << (int)remoteParticipant->networkId << "\n"; } ReceiveData(packetSize, remoteParticipant); @@ -63,13 +63,9 @@ void UdpArduino::Receive() { #endif } -bool UdpArduino::Send(IMessage *msg) { +bool UdpArduino::Send(RemoteParticipant* remoteParticipant, int bufferSize) { #if ARDUINO - int bufferSize = msg->Serialize(this->buffer); - if (bufferSize <= 0) - return true; - - udp.beginPacket(this->remoteIpAddress, this->remotePort); + udp.beginPacket(remoteParticipant->ipAddress, remoteParticipant->port); udp.write(buffer, bufferSize); udp.endPacket(); diff --git a/UdpArduino.h b/UdpArduino.h index 62328b0..5b194b9 100644 --- a/UdpArduino.h +++ b/UdpArduino.h @@ -9,7 +9,7 @@ class UdpArduino : public Participant { public: void Setup(int localPort, const char *remoteIpAddress, int remotePort); void Receive(); - bool Send(IMessage *msg); + bool Send(RemoteParticipant* remoteParticipant, int bufferSize); bool Publish(IMessage *msg); protected: diff --git a/UdpPosix.cpp b/UdpPosix.cpp index a8a18af..08f216c 100644 --- a/UdpPosix.cpp +++ b/UdpPosix.cpp @@ -81,11 +81,11 @@ void UdpPosix::Receive() { this->GetParticipant(sender_ipAddress, sender_port); if (remoteParticipant == nullptr) { remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); - std::cout << "New sender " << sender_ipAddress << ":" << sender_port - << "\n"; - std::cout << "New remote participant " << remoteParticipant->ipAddress - << ":" << remoteParticipant->port << " " - << (int)remoteParticipant->networkId << "\n"; + // std::cout << "New sender " << sender_ipAddress << ":" << sender_port + // << "\n"; + // std::cout << "New remote participant " << remoteParticipant->ipAddress + // << ":" << remoteParticipant->port << " " + // << (int)remoteParticipant->networkId << "\n"; } ReceiveData(packetSize, remoteParticipant); @@ -94,17 +94,20 @@ void UdpPosix::Receive() { #endif } -bool UdpPosix::Send( - IMessage *msg) { // Send the message to the specified address and port +bool UdpPosix::Send(RemoteParticipant *remoteParticipant, int bufferSize) { #if defined(__unix__) || defined(__APPLE__) - int bufferSize = msg->Serialize(this->buffer); - if (bufferSize <= 0) - return true; + // Set up the destination address + // char 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) + // << "\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]; - inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN); - std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port) - << "\n"; + // Send the message int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr)); if (sent_bytes < 0) { diff --git a/UdpPosix.h b/UdpPosix.h index 13022cf..8dc2241 100644 --- a/UdpPosix.h +++ b/UdpPosix.h @@ -9,7 +9,7 @@ class UdpPosix : public Participant { public: void Setup(int localPort, const char *remoteIpAddress, int remotePort); void Receive(); - bool Send(IMessage *msg); + bool Send(RemoteParticipant* remoteParticipant, int bufferSize); bool Publish(IMessage *msg); }; diff --git a/UdpWindows.cpp b/UdpWindows.cpp index acf8498..9ddaf0a 100644 --- a/UdpWindows.cpp +++ b/UdpWindows.cpp @@ -144,14 +144,8 @@ void UdpWindows::Receive() { #endif } -bool UdpWindows::Send( - IMessage *msg) { // Send the message to the specified address and port +bool UdpWindows::Send(RemoteParticipant *remoteParticipant, int bufferSize) { #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]; inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN); std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port) diff --git a/UdpWindows.h b/UdpWindows.h index f284347..4a7b3ce 100644 --- a/UdpWindows.h +++ b/UdpWindows.h @@ -9,7 +9,7 @@ class UdpWindows : public Participant { public: void Setup(int localPort, const char *remoteIpAddress, int remotePort); void Receive(); - bool Send(IMessage *msg); + bool Send(RemoteParticipant* remoteParticipant, int bufferSize); bool Publish(IMessage *msg); };