diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ef10bb..3129eac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ else() . LinearAlgebra ) - file(GLOB srcs *.cpp) + file(GLOB srcs *.cpp Sensors/*.cpp) add_library(ControlCore STATIC ${srcs}) enable_testing() diff --git a/ClientMsg.h b/ClientMsg.h index ca643db..5115c7c 100644 --- a/ClientMsg.h +++ b/ClientMsg.h @@ -1,3 +1,5 @@ +#pragma once + #include "Messages.h" namespace Passer { diff --git a/Messages.cpp b/Messages.cpp index 64c1663..f5abd98 100644 --- a/Messages.cpp +++ b/Messages.cpp @@ -112,7 +112,7 @@ CustomMsg::CustomMsg(char *buffer) { unsigned char ix = 1; this->networkId = buffer[ix++]; this->thingId = buffer[ix++]; - this->data = + this->bytes = buffer + ix; // This is only valid because the code ensures the the msg // lifetime is shorter than the buffer lifetime... } diff --git a/Messages.h b/Messages.h index b9ba569..c5d7625 100644 --- a/Messages.h +++ b/Messages.h @@ -71,8 +71,8 @@ public: unsigned char thingId; Thing *thing; - unsigned char dataSize; - char *data; + unsigned char bytesSize; + char *bytes; CustomMsg(char *buffer); CustomMsg(unsigned char networkId, Thing *thing); diff --git a/Participant.cpp b/Participant.cpp index 723bd2f..e18ccb5 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -174,20 +174,24 @@ void Participant::ReceiveData(unsigned char bufferSize, delete msg; } break; case InvestigateMsg::id: { - InvestigateMsg msg = InvestigateMsg(this->buffer); - ProcessInvestigateMsg(msg); + InvestigateMsg *msg = new InvestigateMsg(this->buffer); + Process(msg); + delete msg; } break; case ThingMsg::id: { - ThingMsg msg = ThingMsg(this->buffer); - ProcessThingMsg(msg); + ThingMsg *msg = new ThingMsg(this->buffer); + Process(msg); + delete msg; } break; case PoseMsg::id: { - PoseMsg msg = PoseMsg(this->buffer); - ProcessPoseMsg(msg); + PoseMsg *msg = new PoseMsg(this->buffer); + Process(msg); + delete msg; } break; case CustomMsg::id: { - CustomMsg msg = CustomMsg(this->buffer); - ProcessCustomMsg(msg); + CustomMsg *msg = new CustomMsg(this->buffer); + Process(msg); + delete msg; } break; }; } @@ -213,13 +217,17 @@ void Participant::Process(Participant *sender, NetworkIdMsg *msg) { } } -void Participant::ProcessInvestigateMsg(InvestigateMsg msg) {} +void Participant::Process(InvestigateMsg *msg) {} -void Participant::ProcessThingMsg(ThingMsg msg) {} +void Participant::Process(ThingMsg *msg) {} -void Passer::Control::Participant::ProcessPoseMsg(PoseMsg msg) {} +void Participant::Process(PoseMsg *msg) {} -void Participant::ProcessCustomMsg(CustomMsg msg) {} +void Participant::Process(CustomMsg *msg) { + Thing *thing = Thing::Get(msg->networkId, msg->thingId); + if (thing != nullptr) + thing->ProcessBytes(msg->bytes); +} // Receive #pragma endregion \ No newline at end of file diff --git a/Participant.h b/Participant.h index 41a1149..9e6dc43 100644 --- a/Participant.h +++ b/Participant.h @@ -85,10 +85,10 @@ protected: virtual void Process(Participant *sender, ClientMsg *msg); virtual void Process(Participant *sender, NetworkIdMsg *msg); - virtual void ProcessInvestigateMsg(InvestigateMsg msg); - virtual void ProcessThingMsg(ThingMsg msg); - virtual void ProcessPoseMsg(PoseMsg msg); - virtual void ProcessCustomMsg(CustomMsg msg); + virtual void Process(InvestigateMsg *msg); + virtual void Process(ThingMsg *msg); + virtual void Process(PoseMsg *msg); + virtual void Process(CustomMsg *msg); }; } // namespace Control diff --git a/Sensors/TemperatureSensor.cpp b/Sensors/TemperatureSensor.cpp index 8e0034a..abba1c2 100644 --- a/Sensors/TemperatureSensor.cpp +++ b/Sensors/TemperatureSensor.cpp @@ -5,7 +5,11 @@ namespace Passer { namespace Control { -TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {} +// TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {} + +TemperatureSensor::TemperatureSensor(unsigned char networkId, + unsigned char thingId) + : Thing(nullptr, networkId, thingId, Type::TemperatureSensor) {} void TemperatureSensor::SetTemperature(float temp) { this->temp = temp; } diff --git a/Sensors/TemperatureSensor.h b/Sensors/TemperatureSensor.h index c9c3423..16365e8 100644 --- a/Sensors/TemperatureSensor.h +++ b/Sensors/TemperatureSensor.h @@ -7,7 +7,7 @@ namespace Control { class TemperatureSensor : public Thing { public: - TemperatureSensor(); + TemperatureSensor(unsigned char networkId, unsigned char thingId); virtual void SetTemperature(float temp); diff --git a/SiteServer.cpp b/SiteServer.cpp index e542161..66131bc 100644 --- a/SiteServer.cpp +++ b/SiteServer.cpp @@ -1,6 +1,11 @@ #include "SiteServer.h" -Passer::Control::SiteServer::SiteServer(int port) { +#include "Sensors/TemperatureSensor.h" + +namespace Passer { +namespace Control { + +SiteServer::SiteServer(int port) { this->name = "Site Server"; this->ipAddress = "0.0.0.0"; @@ -11,21 +16,37 @@ Passer::Control::SiteServer::SiteServer(int port) { SetupUDP(port, ipAddress, 0); } -void Passer::Control::SiteServer::Update(unsigned long currentTimeMs) { +void SiteServer::Update(unsigned long currentTimeMs) { this->ReceiveUDP(); Thing::UpdateAll(currentTimeMs); } -void Passer::Control::SiteServer::Process(Participant *sender, ClientMsg *msg) { +void SiteServer::Process(Participant *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); + std::cout << this->name << " received New Client -> " << sender->ipAddress + << " " << (int)sender->networkId << "\n"; + NetworkIdMsg *msg = new NetworkIdMsg(sender->networkId); sender->Send(msg); delete msg; } } -void Passer::Control::SiteServer::Process(Participant *sender, - NetworkIdMsg *msg) {} +void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {} + +void SiteServer::Process(ThingMsg *msg) { + Thing *thing = Thing::Get(msg->networkId, msg->thingId); + if (thing == nullptr) { + switch ((Thing::Type)msg->thingType) { + case Thing::Type::TemperatureSensor: + new TemperatureSensor(msg->networkId, msg->thingId); + break; + default: + new Thing(this, msg->networkId, msg->thingId, + (Thing::Type)msg->thingType); + break; + } + } +} + +} // namespace Control +} // namespace Passer diff --git a/SiteServer.h b/SiteServer.h index 94e37d5..0a90a87 100644 --- a/SiteServer.h +++ b/SiteServer.h @@ -17,6 +17,7 @@ protected: virtual void Process(Participant *sender, ClientMsg *msg) override; virtual void Process(Participant *sender, NetworkIdMsg *msg) override; + virtual void Process(ThingMsg *msg) override; }; } // namespace Control diff --git a/Thing.cpp b/Thing.cpp index 4a79cc8..638c03f 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -28,6 +28,26 @@ Thing::Thing(unsigned char thingType) { this->angularVelocity = Spherical16::zero; } +Passer::Control::Thing::Thing(Participant *participant, unsigned char networkId, + unsigned char thingId, Type thingType) { + // no participant reference yet.. + 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; +} + void Thing::Terminate() { Thing::Remove(this); } void Thing::Init() {} diff --git a/Thing.h b/Thing.h index bdb4b92..ba2be8f 100644 --- a/Thing.h +++ b/Thing.h @@ -7,6 +7,8 @@ namespace Passer { namespace Control { +class Participant; + #define THING_STORE_SIZE 256 // IMPORTANT: values higher than 256 will need to change the Thing::id type // to 16-bit or higher, breaking the networking protocol! @@ -39,6 +41,8 @@ public: Thing(Type thingType = Type::Undetermined); Thing(unsigned char thingType); + Thing(Participant *participant, unsigned char networkId, + unsigned char thingId, Type thingType = Type::Undetermined); Thing *FindThing(const char *name); // Thing *FindChild(unsigned char id);