86 lines
2.1 KiB
C++

#include "SiteServer.h"
#include "Things/TemperatureSensor.h"
#if !defined(NO_STD)
#include <functional>
#include <memory>
#endif
namespace RoboidControl {
#pragma region Init
SiteServer::SiteServer(int port) {
this->name = "Site Server";
this->publishInterval = 0;
this->ipAddress = "0.0.0.0";
this->port = port;
SetupUDP(port, ipAddress, 0);
}
#pragma endregion Init
#pragma region Update
void SiteServer::UpdateMyThings(unsigned long currentTimeMs) {
for (Thing* thing : this->things) {
if (thing == nullptr)
continue;
thing->Update(currentTimeMs, true);
if (this->isIsolated == false) {
// Send to all other participants
for (Participant* participant : Participant::participants) {
if (participant == nullptr || participant == this)
continue;
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing);
this->Send(participant, poseMsg);
delete poseMsg;
BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing);
this->Send(participant, binaryMsg);
delete binaryMsg;
}
}
}
}
#pragma endregion Update
#pragma region Receive
void SiteServer::Process(Participant* sender, ParticipantMsg* msg) {
if (msg->networkId != sender->networkId) {
// std::cout << this->name << " received New Client -> " <<
// sender->ipAddress
// << ":" << (int)sender->port << "\n";
NetworkIdMsg* msg = new NetworkIdMsg(sender->networkId);
this->Send(sender, msg);
delete msg;
}
}
void SiteServer::Process(Participant* sender, NetworkIdMsg* msg) {}
void SiteServer::Process(Participant* sender, ThingMsg* msg) {
Thing* thing = sender->Get(msg->thingId);
if (thing == nullptr)
new Thing(sender, (Thing::Type)msg->thingType, msg->thingId);
if (msg->parentId != 0) {
thing->SetParent(Get(msg->parentId));
if (thing->GetParent() != nullptr)
std::cout << "Could not find parent [" << (int)msg->networkId << "/"
<< (int)msg->parentId << "]\n";
} else
thing->SetParent(nullptr);
}
#pragma endregion Receive
} // namespace RoboidControl