Further refactoring for better safety and maintainability

This commit is contained in:
2025-04-06 22:46:06 +02:00
parent 47694df8a3
commit 3231120a1e
11 changed files with 291 additions and 406 deletions
+26 -51
View File
@@ -14,16 +14,17 @@
#include "SPITObject.h"
#include "SPITSocket.h"
#include "esp_log.h"
unordered_map<string, SPITObject *> SPITObject::sSpitObjects;
unordered_map<string, SPITObject *> SPITObject::sSpitObjectsRemote;
unordered_map<string, unique_ptr<SPITObject>> SPITObject::sSpitObjects;
unordered_map<string, unique_ptr<SPITObject>> SPITObject::sSpitObjectsRemote;
SPITObject *SPITObject::getSPITObject(const string &id) {
auto i = sSpitObjects.find(id);
if (i == sSpitObjects.end()) {
return nullptr;
}
return i->second;
return i->second.get();
}
/**
* Returns the reference of the SPITObject with ID == id.
@@ -40,7 +41,7 @@ SPITObject *SPITObject::getSPITObjectRemote(const string &id) {
if (i == sSpitObjectsRemote.end()) {
return nullptr;
}
return i->second;
return i->second.get();
}
/**
* Returns the reference of the remote SPITObject with ID == id.
@@ -52,16 +53,18 @@ const SPITObject &SPITObject::getSPITObjectRemoteRef(const string &id) {
return *sSpitObjectsRemote.at(id);
}
void SPITObject::addSPITObject(SPITObject &spitObject) {
sSpitObjects[spitObject.getID()] = &spitObject;
void SPITObject::addSPITObject(unique_ptr<SPITObject> spitObject) {
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitObject->getID().c_str(), spitObject->getName().c_str());
sSpitObjects[spitObject->getID()] = move(spitObject);
}
void SPITObject::addSPITObjectRemote(SPITObject &spitObject) {
sSpitObjectsRemote[spitObject.getID()] = &spitObject;
void SPITObject::addSPITObjectRemote(unique_ptr<SPITObject> spitObject) {
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitObject->getID().c_str(), spitObject->getName().c_str());
sSpitObjectsRemote[spitObject->getID()] = move(spitObject);
}
bool SPITObject::removeSPITObject(const SPITObject &spitObject) {
return removeSPITObject(spitObject.getID());
}
bool SPITObject::removeSPITObject(const string & spitObjectID) {
bool SPITObject::removeSPITObject(const string &spitObjectID) {
auto i = sSpitObjects.find(spitObjectID);
if (i == sSpitObjects.end()) {
return false;
@@ -72,7 +75,7 @@ bool SPITObject::removeSPITObject(const string & spitObjectID) {
bool SPITObject::removeSPITObjectRemote(const SPITObject &spitObject) {
return removeSPITObjectRemote(spitObject.getID());
}
bool SPITObject::removeSPITObjectRemote(const string & spitObjectID) {
bool SPITObject::removeSPITObjectRemote(const string &spitObjectID) {
auto i = sSpitObjectsRemote.find(spitObjectID);
if (i == sSpitObjectsRemote.end()) {
return false;
@@ -83,8 +86,8 @@ bool SPITObject::removeSPITObjectRemote(const string & spitObjectID) {
SPITObject *SPITObject::getSPITObject(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjects.begin(), p_Index);
return i->second;
auto i = next(sSpitObjects.begin(), p_Index);
return i->second.get();
}
return nullptr;
}
@@ -96,7 +99,7 @@ SPITObject *SPITObject::getSPITObject(int p_Index) {
*/
const SPITObject &SPITObject::getSPITObjectRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjects.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjects.begin(), p_Index);
auto i = next(sSpitObjects.begin(), p_Index);
return *i->second;
} else {
throw "SPITObject index out of range";
@@ -104,8 +107,8 @@ const SPITObject &SPITObject::getSPITObjectRef(int p_Index) {
}
SPITObject *SPITObject::getSPITObjectRemote(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjectsRemote.begin(), p_Index);
return i->second;
auto i = next(sSpitObjectsRemote.begin(), p_Index);
return i->second.get();
} else {
return nullptr;
}
@@ -118,76 +121,48 @@ SPITObject *SPITObject::getSPITObjectRemote(int p_Index) {
*/
const SPITObject &SPITObject::getSPITObjectRemoteRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitObjectsRemote.size()) {
unordered_map<string, SPITObject *>::iterator i = next(sSpitObjectsRemote.begin(), p_Index);
auto i = next(sSpitObjectsRemote.begin(), p_Index);
return *i->second;
} else {
throw "Remote SPITObject index out of range";
}
}
void SPITObject::clearSPITObjects() {
SPITObject *ptrSPITObject;
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
ptrSPITObject = i->second;
i = sSpitObjects.erase(i);
delete ptrSPITObject;
}
}
void SPITObject::clearSPITObjectsRemote() {
SPITObject *ptrSPITObject;
for (auto i = sSpitObjectsRemote.begin(); i != sSpitObjectsRemote.end(); i++) {
ptrSPITObject = i->second;
i = sSpitObjectsRemote.erase(i);
delete ptrSPITObject;
}
}
/**
* clears all unconfirmed SPITObjects not created by this SPIT_Client.
*/
void SPITObject::clearUnconfirmedSPITObjects() {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
SPITObject *ptrSPITObject = i->second;
SPITObject *ptrSPITObject = i->second.get();
if (ptrSPITObject->getCreatedByClient() == false && ptrSPITObject->getConfirmed() == false) {
i = sSpitObjects.erase(i);
delete ptrSPITObject;
}
}
}
void SPITObject::eventProjectClosed() {
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
SPITObject *ptrSPITObject = i->second;
SPITObject *ptrSPITObject = i->second.get();
if (ptrSPITObject->getCreatedByClient() == true) {
continue;
}
i = sSpitObjects.erase(i);
delete ptrSPITObject;
}
// Remote
for (auto i = sSpitObjectsRemote.begin(); i != sSpitObjectsRemote.end(); i++) {
SPITObject *ptrSPITObject = i->second;
i = sSpitObjectsRemote.erase(i);
delete ptrSPITObject;
}
sSpitObjectsRemote.clear();
}
/**
* sets confirmed = false for all SPITObjects.
* This should be called when the client has loaded a project.
*/
void SPITObject::setConfirmedFalse() {
SPITObject *ptrSPITObject;
for (auto i = sSpitObjects.begin(); i != sSpitObjects.end(); i++) {
ptrSPITObject = i->second;
ptrSPITObject->setConfirmed(false);
i->second->setConfirmed(false);
}
}
SPITObject::SPITObject() {
m_ID = createGUID();
SPITObject::SPITObject(string p_ID) {
m_ID = p_ID;
}
bool SPITObject::setMessageOP(SPIT_Message::ReportSPITObject *p_ReportSPITObject) {
@@ -404,7 +379,7 @@ bool SPITObject::setServerRemote(bool p_BelongstoServer) {
}
/**
* This is a virtual construct - ActionAssignedType is not implemented in the SPITProtocol.
* This is used when an action is associated with a SPITType.
* This is used when an action is associated with a SPITType.
* Then this action is done for every SPITObject with the SPITType.
* @return true = an action is assigned for the SPITType.
*/
+5 -10
View File
@@ -17,17 +17,12 @@
unordered_map<string, SPITPlayer *> SPITPlayer::s_SPITPlayers;
SPITPlayer *SPITPlayer::getSPITPlayer(string p_PlayerID) {
{
auto i = s_SPITPlayers.find(p_PlayerID);
if (i == s_SPITPlayers.end()) {
s_SPITPlayers[p_PlayerID] = new SPITPlayer(p_PlayerID);
}
i = s_SPITPlayers.find(p_PlayerID);
if (i == s_SPITPlayers.end())
return NULL;
return i->second;
SPITPlayer &SPITPlayer::getSPITPlayer(string p_PlayerID) {
auto i = s_SPITPlayers.find(p_PlayerID);
if (i == s_SPITPlayers.end()) {
s_SPITPlayers[p_PlayerID] = new SPITPlayer(p_PlayerID);
}
return *s_SPITPlayers.at(p_PlayerID);
}
void SPITPlayer::clearPlayers() {
+22 -43
View File
@@ -17,15 +17,15 @@
static const char *TAG = "SPIT";
unordered_map<string, SPITType *> SPITType::sSpitTypes;
unordered_map<string, SPITType *> SPITType::sSpitTypesRemote;
unordered_map<string, unique_ptr<SPITType>> SPITType::sSpitTypes;
unordered_map<string, unique_ptr<SPITType>> SPITType::sSpitTypesRemote;
SPITType *SPITType::getSPITType(const string &id) {
auto i = sSpitTypes.find(id);
if (i == sSpitTypes.end()) {
return nullptr;
}
return i->second;
return i->second.get();
}
/**
* Returns the reference of the SPITType with ID == id.
@@ -42,7 +42,7 @@ SPITType *SPITType::getSPITTypeRemote(const string &id) {
if (i == sSpitTypesRemote.end()) {
return nullptr;
}
return i->second;
return i->second.get();
}
/**
@@ -55,11 +55,13 @@ const SPITType &SPITType::getSPITTypeRemoteRef(const string &id) {
return *sSpitTypesRemote.at(id);
}
void SPITType::addSPITType(SPITType &spitType) {
sSpitTypes[spitType.getID()] = &spitType;
void SPITType::addSPITType(unique_ptr<SPITType> spitType) {
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitType->getID().c_str(), spitType->getName().c_str());
sSpitTypes[spitType->getID()] = move(spitType);
}
void SPITType::addSPITTypeRemote(SPITType &spitType) {
sSpitTypesRemote[spitType.getID()] = &spitType;
void SPITType::addSPITTypeRemote(unique_ptr<SPITType> spitType) {
ESP_LOGD("OBJ", "adding spitObject %s/%s", spitType->getID().c_str(), spitType->getName().c_str());
sSpitTypesRemote[spitType->getID()] = move(spitType);
}
bool SPITType::removeSPITType(const SPITType &spitType) {
auto i = sSpitTypes.find(spitType.getID());
@@ -79,12 +81,11 @@ bool SPITType::removeSPITTypeRemote(const SPITType &spitType) {
}
SPITType *SPITType::getSPITType(int p_Index) {
SPITType *pSpitType = nullptr;
if (p_Index < sSpitTypes.size()) {
unordered_map<string, SPITType *>::iterator i = next(sSpitTypes.begin(), p_Index);
pSpitType = i->second;
auto i = next(sSpitTypes.begin(), p_Index);
return i->second.get();
}
return pSpitType;
return nullptr;
}
/**
* Returns a reference to the SPITType at position p_Index.
@@ -94,7 +95,7 @@ SPITType *SPITType::getSPITType(int p_Index) {
*/
const SPITType &SPITType::getSPITTypeRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitTypes.size()) {
unordered_map<string, SPITType *>::iterator i = next(sSpitTypes.begin(), p_Index);
auto i = next(sSpitTypes.begin(), p_Index);
return *i->second;
} else {
throw "SPITType index out of range";
@@ -102,8 +103,8 @@ const SPITType &SPITType::getSPITTypeRef(int p_Index) {
}
SPITType *SPITType::getSPITTypeRemote(int p_Index) {
if (p_Index < sSpitTypesRemote.size()) {
unordered_map<string, SPITType *>::iterator i = next(sSpitTypesRemote.begin(), p_Index);
return i->second;
auto i = next(sSpitTypesRemote.begin(), p_Index);
return i->second.get();
}
return nullptr;
}
@@ -115,43 +116,23 @@ SPITType *SPITType::getSPITTypeRemote(int p_Index) {
*/
const SPITType &SPITType::getSPITTypeRemoteRef(int p_Index) {
if (p_Index >= 0 && p_Index < sSpitTypesRemote.size()) {
unordered_map<string, SPITType *>::iterator i = next(sSpitTypesRemote.begin(), p_Index);
auto i = next(sSpitTypesRemote.begin(), p_Index);
return *i->second;
} else {
throw "Remote SPITType index out of range";
}
}
void SPITType::clearSPITTypes() {
for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) {
SPITType *pSpitType = i->second;
i = sSpitTypes.erase(i);
delete pSpitType;
}
}
void SPITType::clearSPITTypesRemote() {
for (auto i = sSpitTypesRemote.begin(); i != sSpitTypesRemote.end(); i++) {
SPITType *ptrSPITType = i->second;
i = sSpitTypesRemote.erase(i);
delete ptrSPITType;
}
}
void SPITType::eventProjectClosed() {
for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) {
SPITType *ptrSPITType = i->second;
SPITType *ptrSPITType = i->second.get();
if (ptrSPITType->getCreatedByClient() == true) {
continue;
}
i = sSpitTypes.erase(i);
delete ptrSPITType;
}
// Remote
for (auto i = sSpitTypesRemote.begin(); i != sSpitTypesRemote.end(); i++) {
SPITType *ptrSPITType = i->second;
i = sSpitTypesRemote.erase(i);
delete ptrSPITType;
}
sSpitTypesRemote.clear();
}
/**
@@ -160,17 +141,15 @@ void SPITType::eventProjectClosed() {
*/
void SPITType::setConfirmedFalse() {
for (auto i = sSpitTypes.begin(); i != sSpitTypes.end(); i++) {
SPITType *ptrSPITType = i->second;
ptrSPITType->setConfirmed(false);
i->second->setConfirmed(false);
}
}
SPITType::SPITType() {
m_ID = createGUID();
SPITType::SPITType(string p_ID) {
m_ID = p_ID;
}
SPITType::SPITType(string id, string grpName, string name, bool fixed, bool projectIndependent, long long defaultDelay, long long defaultDuration) {
m_ID = createGUID();
m_ActionAssigned = false;
m_Confirmed = false;
m_ID = id;
+125 -184
View File
@@ -264,7 +264,7 @@ int SPIT_Client::sendSPITMessage(unique_ptr<SPIT_Message::OPPart> spitMessageOPP
m_MessageNumber_Send = 1;
else
m_MessageNumber_Send++;
opPart.getHeader()->setMessageNumber(m_MessageNumber_Send);
int opFlag = 0;
@@ -296,7 +296,7 @@ bool SPIT_Client::startReceiveSPITMessages() {
return true;
}
void SPIT_Client::stopReceiveSPITMessages() {
m_StopReceiveSPITMessages = true;
m_StopReceiveSPITMessages = true;
}
void SPIT_Client::receiveSPITMessage(SPIT_Client &rM) {
@@ -318,14 +318,7 @@ void SPIT_Client::receiveSPITMessage(SPIT_Client &rM) {
}
void SPIT_Client::resetMessageNumberReceived() {
SPIT_Message *ptrOvertakingSPIT_Message;
m_MessageNumber_Received = -1;
for (auto it = m_OvertakingSPIT_Messages.begin(); it != m_OvertakingSPIT_Messages.end(); it++) {
ptrOvertakingSPIT_Message = *it;
delete ptrOvertakingSPIT_Message;
// m_OvertakingSPIT_Messages.erase(it);
}
m_OvertakingSPIT_Messages.clear();
}
@@ -344,11 +337,7 @@ void SPIT_Client::checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage)
return;
}
int intActualMessageNumber;
int intNextMessageNumber;
SPIT_Message *ptrOvertakingSPIT_Message;
intActualMessageNumber = (int)(p_SPITMessage->getHeader()->getMessageNumber() & 0xFF);
int intActualMessageNumber = (int)(p_SPITMessage->getHeader()->getMessageNumber() & 0xFF);
if (intActualMessageNumber == 0) {
parseSPIT_Message(std::move(p_SPITMessage));
@@ -360,7 +349,7 @@ void SPIT_Client::checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage)
return;
}
intNextMessageNumber = m_MessageNumber_Received + 1;
int intNextMessageNumber = m_MessageNumber_Received + 1;
if (intNextMessageNumber > 255 || intNextMessageNumber < 1)
intNextMessageNumber = 1;
@@ -376,12 +365,12 @@ void SPIT_Client::checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage)
if (intNextMessageNumber > 255 || intNextMessageNumber < 1)
intNextMessageNumber = 1;
ptrOvertakingSPIT_Message = *it;
auto &ptrOvertakingSPIT_Message = *it;
intActualMessageNumber = (int)(ptrOvertakingSPIT_Message->getHeader()->getMessageNumber() & 0xFF);
if (intActualMessageNumber == intNextMessageNumber) {
m_MessageNumber_Received = intActualMessageNumber;
// parseSPIT_Message(ptrOvertakingSPIT_Message);
parseSPIT_Message(move(ptrOvertakingSPIT_Message));
m_OvertakingSPIT_Messages.erase(it);
it = m_OvertakingSPIT_Messages.begin();
@@ -392,33 +381,28 @@ void SPIT_Client::checkParseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage)
} else {
// p_SPITMessage has overtaken another message
addOvertakingSPIT_Message(p_SPITMessage.get());
addOvertakingSPIT_Message(move(p_SPITMessage));
}
}
int SPIT_Client::s_MAX_OVERTAKING_SPIT_MESSAGES = 10;
void SPIT_Client::addOvertakingSPIT_Message(SPIT_Message *p_SPIT_Message) {
if (p_SPIT_Message == nullptr)
void SPIT_Client::addOvertakingSPIT_Message(unique_ptr<SPIT_Message> p_SPIT_Message) {
if (!p_SPIT_Message) {
return;
int intMessageNumber;
SPIT_Message *ptrOvertakingSPIT_Message;
int intOvertakingMessageNumber;
if (m_OvertakingSPIT_Messages.size() > s_MAX_OVERTAKING_SPIT_MESSAGES) {
}
if (m_OvertakingSPIT_Messages.size() > MAX_OVERTAKING_SPIT_MESSAGES) {
ESP_LOGD(TAG, "SPIT_Client receiveing: perhaps message lost !!!!!");
}
intMessageNumber = (int)(p_SPIT_Message->getHeader()->getMessageNumber() & 0xFF);
for (auto it = m_OvertakingSPIT_Messages.begin(); it != m_OvertakingSPIT_Messages.end(); it++) {
ptrOvertakingSPIT_Message = *it;
intOvertakingMessageNumber = (int)(ptrOvertakingSPIT_Message->getHeader()->getMessageNumber() & 0xFF);
int intMessageNumber = (int)(p_SPIT_Message->getHeader()->getMessageNumber() & 0xFF);
for (auto it = m_OvertakingSPIT_Messages.begin(); it != m_OvertakingSPIT_Messages.end(); ++it) {
const auto &overtaking = *it;
int intOvertakingMessageNumber = (int)(overtaking->getHeader()->getMessageNumber() & 0xFF);
if (intMessageNumber < intOvertakingMessageNumber) {
m_OvertakingSPIT_Messages.insert(it, p_SPIT_Message);
m_OvertakingSPIT_Messages.insert(it, move(p_SPIT_Message));
return;
}
}
m_OvertakingSPIT_Messages.push_back(p_SPIT_Message);
m_OvertakingSPIT_Messages.push_back(move(p_SPIT_Message));
}
void SPIT_Client::parseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage) {
@@ -612,8 +596,7 @@ void SPIT_Client::disconnectInternal(int p_Cause) {
}
void SPIT_Client::parseConnectAnswer(SPIT_Message::ConnectAnswer *p_ConnectAnswer) {
int intOldConnectionStatus;
intOldConnectionStatus = m_ConnectionStatus;
int intOldConnectionStatus = m_ConnectionStatus;
if (p_ConnectAnswer->getClientID() != m_ID)
return;
@@ -726,6 +709,7 @@ void SPIT_Client::parseCloseSPITProject(SPIT_Message::CloseSPITProject *p_CloseS
return;
string stringProjectID;
mCallbacks.onProjectUpdate(ProjUpdate::ServerClosed, p_CloseSPITProject->getProjectID());
SPITPlayer::clearPlayers();
}
string SPIT_Client::getServerProjectID() { return m_ServerProjectID; }
@@ -926,13 +910,17 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPIT
if (mCallbacks.onCheckSpitTypeVeto(p_ReportSPITType) == false) {
return;
}
ptrSPITType = new SPITType();
auto spitType = make_unique<SPITType>(p_ReportSPITType->getID());
ptrSPITType = spitType.get();
SPITType::addSPITType(move(spitType));
boolNew = true;
}
} else {
ptrSPITType = SPITType::getSPITTypeRemote(p_ReportSPITType->getID());
if (ptrSPITType == nullptr) {
ptrSPITType = new SPITType();
auto spitType = make_unique<SPITType>(p_ReportSPITType->getID());
ptrSPITType = spitType.get();
SPITType::addSPITTypeRemote(move(spitType));
boolNew = true;
}
}
@@ -944,13 +932,7 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPIT
auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed;
// add or change SPITType
if (ptrSPITType->getServerRemote() == false) {
SPITType::addSPITType(*ptrSPITType);
mCallbacks.onSpitType(eventType, *ptrSPITType);
} else {
SPITType::addSPITTypeRemote(*ptrSPITType);
mCallbacks.onSpitTypeRemote(eventType, *ptrSPITType);
}
mCallbacks.onSpitType(eventType, ptrSPITType->getServerRemote(), *ptrSPITType);
if (ptrSPITType != nullptr) {
if (ptrSPITType->setConfirmed(true) == true || boolChanged == true) {
@@ -961,42 +943,37 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPIT
}
void SPIT_Client::parseRemoveSPITType(SPIT_Message::RemoveSPITType *p_RemoveSPITType) {
// printf("parse RemoveSPITType %s\n", p_RemoveSPITType->getID());
if (p_RemoveSPITType == nullptr)
if (!p_RemoveSPITType) {
return;
string stringSPITTypeID;
}
bool boolRemote = false;
SPITType *ptrSPITType;
stringSPITTypeID = p_RemoveSPITType->getID();
string stringSPITTypeID = p_RemoveSPITType->getID();
{
ptrSPITType = SPITType::getSPITTypeRemote(stringSPITTypeID);
if (ptrSPITType != nullptr) {
boolRemote = true;
SPITType *ptrSPITType = SPITType::getSPITTypeRemote(stringSPITTypeID);
if (!ptrSPITType) {
boolRemote = true;
} else {
ptrSPITType = SPITType::getSPITType(stringSPITTypeID);
if (!ptrSPITType) {
boolRemote = false;
} else {
ptrSPITType = SPITType::getSPITType(stringSPITTypeID);
if (ptrSPITType != nullptr) {
boolRemote = false;
} else {
// No SPITType with stringSPITTypeID is listet
return;
}
}
if (ptrSPITType->getCreatedByClient() && ptrSPITType->getFixed()) {
// the SPITyte is created by client himself and it is fixed -> don't remove the SPITType!!
// Send the SPITTyp back to the server
sendSPITTypeInternal(*ptrSPITType);
// No SPITType with stringSPITTypeID is listet
return;
}
}
if (boolRemote == false) {
mCallbacks.onSpitType(ObjectEvent::Deleted, *ptrSPITType);
SPITType::removeSPITType(*ptrSPITType);
} else {
mCallbacks.onSpitTypeRemote(ObjectEvent::Deleted, *ptrSPITType);
SPITType::removeSPITTypeRemote(*ptrSPITType);
}
delete ptrSPITType;
if (ptrSPITType->getCreatedByClient() && ptrSPITType->getFixed()) {
// the SPITyte is created by client himself and it is fixed -> don't remove the SPITType!!
// Send the SPITTyp back to the server
sendSPITTypeInternal(*ptrSPITType);
return;
}
mCallbacks.onSpitType(ObjectEvent::Deleted, boolRemote, *ptrSPITType);
if (boolRemote == false) {
SPITType::removeSPITType(*ptrSPITType);
} else {
SPITType::removeSPITTypeRemote(*ptrSPITType);
}
}
@@ -1031,7 +1008,7 @@ bool SPIT_Client::changeSPITType(SPITType *p_SPITType) {
ptrSPITType->setName(p_SPITType->getName());
ptrSPITType->setProjectIndepended(p_SPITType->getProjectIndepended());
mCallbacks.onSpitType(ObjectEvent::Changed, *ptrSPITType);
mCallbacks.onSpitType(ObjectEvent::Changed, false, *ptrSPITType);
sendSPITTypeInternal(*ptrSPITType);
return true;
@@ -1055,9 +1032,8 @@ int SPIT_Client::removeSPITType(string p_SPITTypeID) {
return -1;
auto sMRemoveSPITType = make_unique<SPIT_Message::RemoveSPITType>();
ptrSPITType->fillMessageOP(*sMRemoveSPITType);
mCallbacks.onSpitType(ObjectEvent::Deleted, *ptrSPITType);
mCallbacks.onSpitType(ObjectEvent::Deleted, false, *ptrSPITType);
SPITType::removeSPITType(*ptrSPITType);
delete ptrSPITType;
return sendSPITMessage(move(sMRemoveSPITType));
}
return -1;
@@ -1071,7 +1047,9 @@ SPITType *SPIT_Client::createSPITType(string p_ID, string p_GroupName, string p_
bool boolNew;
SPITType *ptrSPITType = SPITType::getSPITType(p_ID);
if (ptrSPITType == nullptr) {
ptrSPITType = new SPITType(p_ID, p_GroupName, p_Name, p_FIxed, p_ProjectIndepended, p_DefaultDelay, p_DefaultDuration);
auto spitType = make_unique<SPITType>(p_ID, p_GroupName, p_Name, p_FIxed, p_ProjectIndepended, p_DefaultDelay, p_DefaultDuration);
ptrSPITType = spitType.get();
SPITType::addSPITType(move(spitType));
ptrSPITType->setCreatedByClient(true);
boolNew = true;
} else {
@@ -1086,8 +1064,7 @@ SPITType *SPIT_Client::createSPITType(string p_ID, string p_GroupName, string p_
}
auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed;
SPITType::addSPITType(*ptrSPITType);
mCallbacks.onSpitType(eventType, *ptrSPITType);
mCallbacks.onSpitType(eventType, false, *ptrSPITType);
sendSPITTypeInternal(*ptrSPITType);
return ptrSPITType;
}
@@ -1103,6 +1080,7 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
bool boolRemote = false;
bool boolChanged = false;
{
ESP_LOGD(TAG, "parseReportSPITObject %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str());
if (p_ReportSPITObject->getServerRemote() == false) {
boolRemote = false;
ptrSPITObject = SPITObject::getSPITObject(p_ReportSPITObject->getID());
@@ -1114,7 +1092,6 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
// SpitObject exists but has no corresponding SPITType -> remove the SPITObject without sending a message
// Maybe the SPITObject has changed the SPITTypeID to a SPITTypeID of another board
SPITObject::removeSPITObject(*ptrSPITObject);
delete ptrSPITObject;
}
return;
}
@@ -1124,8 +1101,9 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
ESP_LOGD(TAG, "ObjectVeto %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str());
return;
}
ptrSPITObject = new SPITObject();
auto spitObj = make_unique<SPITObject>(p_ReportSPITObject->getID());
ptrSPITObject = spitObj.get();
SPITObject::addSPITObject(move(spitObj));
boolNew = true;
} else {
// The user's app my veto the SPITType
@@ -1133,7 +1111,6 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
// SpitObject exists and User APp denied the SPITObject -> remove the SPITObject without sending a message
SPITObject::removeSPITObject(*ptrSPITObject);
ESP_LOGD(TAG, "TypeVeto %s / %s", p_ReportSPITObject->getID().c_str(), p_ReportSPITObject->getName().c_str());
delete ptrSPITObject;
return;
}
}
@@ -1146,12 +1123,15 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
return;
}
if (ptrSPITObject == nullptr) {
ptrSPITObject = new SPITObject();
auto spitObj = make_unique<SPITObject>(p_ReportSPITObject->getID());
ptrSPITObject = spitObj.get();
SPITObject::addSPITObjectRemote(move(spitObj));
boolNew = true;
}
}
if (!ptrSPITObject->setMessageOP(p_ReportSPITObject)) {
ESP_LOGE(TAG, "Change not allowed");
// setMessageOP error - the SPITObject can't be changed, it may be fixed and crated by this client
sendSPITObjectInternal(ptrSPITObject);
return;
@@ -1160,17 +1140,10 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
if (boolRemote == false) {
// set the virtual action assignment
ptrSPITObject->setActionAssignedType(ptrSPITType->getActionAssigned());
SPITObject::addSPITObject(*ptrSPITObject);
} else {
SPITObject::addSPITObjectRemote(*ptrSPITObject);
}
auto eventType = boolNew ? ObjectEvent::Created : ObjectEvent::Changed;
if (boolRemote) {
mCallbacks.onSpitObjectRemote(eventType, *ptrSPITType, *ptrSPITObject);
} else {
mCallbacks.onSpitObject(eventType, *ptrSPITType, *ptrSPITObject);
}
mCallbacks.onSpitObject(eventType, boolRemote, *ptrSPITType, *ptrSPITObject);
if (ptrSPITObject->setConfirmed(true) == true || boolChanged == true) {
// Confirmation has changed
@@ -1180,33 +1153,20 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject *p_Report
}
void SPIT_Client::parseRemoveSPITObject(SPIT_Message::RemoveSPITObject *p_RemoveSPITObject) {
// printf("parse RemoveSPITObject %s\n", p_RemoveSPITObject->getID());
if (p_RemoveSPITObject == nullptr)
if (!p_RemoveSPITObject) {
return;
bool boolRemote = false;
SPITObject *ptrSPITObject;
SPITType *ptrSPITType;
}
{
ptrSPITObject = SPITObject::getSPITObjectRemote(p_RemoveSPITObject->getID());
if (ptrSPITObject != nullptr) {
boolRemote = true;
ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
if (ptrSPITType != nullptr) {
if (boolRemote) {
mCallbacks.onSpitObjectRemote(ObjectEvent::Deleted, *ptrSPITType, *ptrSPITObject);
} else {
mCallbacks.onSpitObject(ObjectEvent::Deleted, *ptrSPITType, *ptrSPITObject);
}
} else {
// No associated SPITType found
// delete the SPITObject without a notification
}
SPITObject::removeSPITObjectRemote(*ptrSPITObject);
delete ptrSPITObject;
} else {
// call removeSPITObject, this will notify the server that the object is removed
removeSPITObject(p_RemoveSPITObject->getID());
SPITObject *ptrSPITObject = SPITObject::getSPITObjectRemote(p_RemoveSPITObject->getID());
if (ptrSPITObject != nullptr) {
SPITType *ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
if (ptrSPITType != nullptr) {
mCallbacks.onSpitObject(ObjectEvent::Deleted, true, *ptrSPITType, *ptrSPITObject);
}
SPITObject::removeSPITObjectRemote(*ptrSPITObject);
} else {
// call removeSPITObject, this will notify the server that the object is removed
removeSPITObject(p_RemoveSPITObject->getID());
}
}
@@ -1256,7 +1216,7 @@ bool SPIT_Client::changeSPITObject(SPITObject *p_SPITObject) {
ptrSPITObject->setValueFactor(p_SPITObject->getValueFactor());
ptrSPITObject->setValueUse(p_SPITObject->getValueUse());
mCallbacks.onSpitObject(ObjectEvent::Changed, *ptrSPITType, *ptrSPITObject);
mCallbacks.onSpitObject(ObjectEvent::Changed, false, *ptrSPITType, *ptrSPITObject);
sendSPITObjectInternal(ptrSPITObject);
}
return true;
@@ -1286,9 +1246,8 @@ int SPIT_Client::removeSPITObject(string p_SPITObjectID) {
if (ptrSPITType == nullptr)
return -1;
ptrSPITObject->fillMessageOP(*sMRemoveSPITObject);
mCallbacks.onSpitObject(ObjectEvent::Deleted, *ptrSPITType, *ptrSPITObject);
mCallbacks.onSpitObject(ObjectEvent::Deleted, false, *ptrSPITType, *ptrSPITObject);
SPITObject::removeSPITObject(*ptrSPITObject);
delete ptrSPITObject;
return sendSPITMessage(move(sMRemoveSPITObject));
}
return -1;
@@ -1298,38 +1257,33 @@ int SPIT_Client::removeSPITObject(string p_SPITObjectID) {
void SPIT_Client::parsePlaySPITStart(SPIT_Message::PlaySPITStart *p_PlaySPITStart) {
ESP_LOGD(TAG, "parse PlaySPITStart ID: %s Name: %s Running: %d", p_PlaySPITStart->getPlayerID().c_str(), p_PlaySPITStart->getPlayerName().c_str(), (int)p_PlaySPITStart->getPlayerIsRunning());
SPITPlayer *ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITStart->getPlayerID());
if (ptrSPITPlayer == nullptr)
return;
ptrSPITPlayer->setIsRunning(p_PlaySPITStart->getPlayerIsRunning());
ptrSPITPlayer->setName(p_PlaySPITStart->getPlayerName());
ptrSPITPlayer->setType(p_PlaySPITStart->getPlayerType());
ptrSPITPlayer->setTime(p_PlaySPITStart->getTime());
ptrSPITPlayer->startPlaySequence();
SPITPlayer &spitPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITStart->getPlayerID());
spitPlayer.setIsRunning(p_PlaySPITStart->getPlayerIsRunning());
spitPlayer.setName(p_PlaySPITStart->getPlayerName());
spitPlayer.setType(p_PlaySPITStart->getPlayerType());
spitPlayer.setTime(p_PlaySPITStart->getTime());
spitPlayer.startPlaySequence();
mCallbacks.onPlay(ptrSPITPlayer->getID(), ptrSPITPlayer->getName(), ptrSPITPlayer->getType(), ptrSPITPlayer->getIsRunning(), ptrSPITPlayer->getTime());
mCallbacks.onPlay(spitPlayer.getID(), spitPlayer.getName(), spitPlayer.getType(), spitPlayer.getIsRunning(), spitPlayer.getTime());
}
void SPIT_Client::parsePlaySPITObject(SPIT_Message::PlaySPITObject *p_PlaySPITObject) {
SPITPlayer *ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITObject->getPlayerID());
if (ptrSPITPlayer == nullptr)
return;
SPITPlayer &ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITObject->getPlayerID());
SPITObject *ptrSPITObject = SPITObject::getSPITObject(p_PlaySPITObject->getSPITObjectID());
if (ptrSPITObject == nullptr) {
return;
}
SPITPlayer::PlaySPITType *ptrPlaySPITType = ptrSPITPlayer->addSPITType_Playing(ptrSPITObject->getSPITTypeID());
SPITPlayer::PlaySPITType *ptrPlaySPITType = ptrSPITPlayer.addSPITType_Playing(ptrSPITObject->getSPITTypeID());
if (ptrPlaySPITType == nullptr) {
return;
}
ptrPlaySPITType->setPlayerIsRunning(ptrSPITPlayer->getIsRunning());
ptrPlaySPITType->setPlayerIsRunning(ptrSPITPlayer.getIsRunning());
ptrPlaySPITType->addValue(((double)p_PlaySPITObject->getValue() / 1000000), ((double)ptrSPITObject->getValueFactor() / 1000000));
}
void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
SPITPlayer *ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID());
if (ptrSPITPlayer == nullptr)
return;
void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
SPITPlayer &ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID());
// unordered_map<string, SPITPlayer::PlaySPITType*>* ptrPlayObjects;
// bool boolStarted;
// bool boolStopped;
@@ -1358,59 +1312,46 @@ void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd *p_PlaySPITEnd) {
// mCallbacks.onPlaySpitType(ptrPlayObject->getPlayerID(), boolStarted, boolStopped, *ptrSPITType, *ptrSPITObject, *ptrPlayObject);
// }
// }
// ptrSPITPlayer->endPlaySequence();
// ptrSPITPlayer.endPlaySequence();
// SPITPlayer *ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID());
// if (ptrSPITPlayer == nullptr)
// return;
// SPITPlayer &ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID());
unordered_map<string, SPITPlayer::PlaySPITType *> *ptrPlaySPITTypes = ptrSPITPlayer->getPlaySPITTypes();
unordered_map<string, SPITPlayer::PlaySPITType *> *ptrPlaySPITTypes = ptrSPITPlayer.getPlaySPITTypes();
for (auto i = ptrPlaySPITTypes->begin(); i != ptrPlaySPITTypes->end(); i++) {
SPITPlayer::PlaySPITType *ptrPlaySPITType = i->second;
bool boolStarted = false;
bool boolStopped = false;
if (ptrPlaySPITType->getIsNew()) {
boolStarted = true;
}
if (ptrPlaySPITType->getIsPlayed() == false) {
boolStopped = true;
}
{
SPITType *ptrSPITType = SPITType::getSPITType(ptrPlaySPITType->getSPITTypeID());
if (ptrSPITType == nullptr) {
// set isplayed to false, the ptrPlayObject will be removed by ptrSPITPlayer->endPlaySequence()
ptrPlaySPITType->setIsPlayed(false);
continue;
}
mCallbacks.onPlaySpitType(ptrPlaySPITType->getPlayerID(), ptrPlaySPITType->getSPITTypeID(), ptrPlaySPITType->getPlayerIsRunning(), boolStarted, boolStopped, ptrPlaySPITType->getValue());
bool boolStarted = ptrPlaySPITType->getIsNew();
bool boolStopped = (ptrPlaySPITType->getIsPlayed() == false);
SPITType *ptrSPITType = SPITType::getSPITType(ptrPlaySPITType->getSPITTypeID());
if (ptrSPITType == nullptr) {
// set isplayed to false, the ptrPlayObject will be removed by ptrSPITPlayer.endPlaySequence()
ptrPlaySPITType->setIsPlayed(false);
continue;
}
mCallbacks.onPlaySpitType(ptrPlaySPITType->getPlayerID(), ptrPlaySPITType->getSPITTypeID(), ptrPlaySPITType->getPlayerIsRunning(), boolStarted, boolStopped, ptrPlaySPITType->getValue());
}
ptrSPITPlayer->endPlaySequence();
ptrSPITPlayer.endPlaySequence();
}
void SPIT_Client::parseReportPlayStatus(SPIT_Message::ReportPlayStatus *p_ReportPlayStatus) {
ESP_LOGD(TAG, "parseReportPlayStatus");
SPITType *ptrSPITType = NULL;
{
SPITObject *ptrSPITObject = SPITObject::getSPITObjectRemote(p_ReportPlayStatus->getID());
SPITObject *ptrSPITObject = SPITObject::getSPITObjectRemote(p_ReportPlayStatus->getID());
if (ptrSPITObject == nullptr) {
ptrSPITObject = SPITObject::getSPITObject(p_ReportPlayStatus->getID());
if (ptrSPITObject == nullptr) {
ptrSPITObject = SPITObject::getSPITObject(p_ReportPlayStatus->getID());
if (ptrSPITObject == nullptr) {
return;
}
}
if (ptrSPITObject->getServerRemote() == false) {
ptrSPITType = SPITType::getSPITType(ptrSPITObject->getSPITTypeID());
} else {
ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
}
if (ptrSPITType == nullptr) {
return;
}
mCallbacks.onPlayRemoteObjectStatus(*ptrSPITType, *ptrSPITObject, p_ReportPlayStatus->getFrameInsideObject(), p_ReportPlayStatus->getValue());
}
if (ptrSPITObject->getServerRemote() == false) {
ptrSPITType = SPITType::getSPITType(ptrSPITObject->getSPITTypeID());
} else {
ptrSPITType = SPITType::getSPITTypeRemote(ptrSPITObject->getSPITTypeID());
}
if (ptrSPITType == nullptr) {
return;
}
mCallbacks.onPlayRemoteObjectStatus(*ptrSPITType, *ptrSPITObject, p_ReportPlayStatus->getFrameInsideObject(), p_ReportPlayStatus->getValue());
}
//--------- Remote ----------
@@ -1517,15 +1458,15 @@ void SPIT_Client::processSocketTask(SPIT_Client &instance) {
strParts[intIndex] = stringPart;
intIndex++;
}
auto server = make_unique<SAD_DetectedServer> (
strParts[SAD_DetectedServer::PARAMINDEX_NAME],
strParts[SAD_DetectedServer::PARAMINDEX_ID],
strParts[SAD_DetectedServer::PARAMINDEX_COMPUTERNAME],
stringServerIP,
strParts[SAD_DetectedServer::PARAMINDEX_PORT],
strParts[SAD_DetectedServer::PARAMINDEX_CLIENTIP],
strParts[SAD_DetectedServer::PARAMINDEX_ADDITIONS],
auto server = make_unique<SAD_DetectedServer>(
strParts[SAD_DetectedServer::PARAMINDEX_NAME],
strParts[SAD_DetectedServer::PARAMINDEX_ID],
strParts[SAD_DetectedServer::PARAMINDEX_COMPUTERNAME],
stringServerIP,
strParts[SAD_DetectedServer::PARAMINDEX_PORT],
strParts[SAD_DetectedServer::PARAMINDEX_CLIENTIP],
strParts[SAD_DetectedServer::PARAMINDEX_ADDITIONS],
strParts[SAD_DetectedServer::PARAMINDEX_SERVERPROJECTNAME]);
SAD_DetectedServer::addDetectedServer(move(server));