refactoring Sever detection
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
@@ -16,29 +17,31 @@ class I_SAD_Client {
|
|||||||
|
|
||||||
class SAD_DetectedServer {
|
class SAD_DetectedServer {
|
||||||
public:
|
public:
|
||||||
SAD_DetectedServer() {}
|
SAD_DetectedServer(string p_Name, string p_ID, string p_ComputerName,
|
||||||
SAD_DetectedServer(const SAD_DetectedServer &orig)
|
string p_IP, string p_Port, string p_ClientIP,
|
||||||
: m_ID(orig.m_ID),
|
string p_Additions, string projName)
|
||||||
m_Name(orig.m_Name),
|
: m_Name(p_Name),
|
||||||
m_ComputerName(orig.m_ComputerName),
|
m_ID(p_ID),
|
||||||
m_IP(orig.m_IP),
|
m_ComputerName(p_ComputerName),
|
||||||
m_ClientIP(orig.m_ClientIP),
|
m_IP(p_IP),
|
||||||
m_Port(orig.m_Port),
|
m_Port(p_Port),
|
||||||
m_Additions(orig.m_Additions) {
|
m_ClientIP(p_IP),
|
||||||
|
m_Additions(p_Additions),
|
||||||
|
m_ServerProjectName(projName) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string m_ID{};
|
|
||||||
string m_Name{};
|
string m_Name{};
|
||||||
|
string m_ID{};
|
||||||
string m_ComputerName{};
|
string m_ComputerName{};
|
||||||
string m_IP{};
|
string m_IP{};
|
||||||
string m_ClientIP{};
|
|
||||||
string m_Port{};
|
string m_Port{};
|
||||||
|
string m_ClientIP{};
|
||||||
string m_Additions{};
|
string m_Additions{};
|
||||||
string m_ServerProjectName{};
|
string m_ServerProjectName{};
|
||||||
bool m_New;
|
bool m_New{true};
|
||||||
bool m_Changed;
|
bool m_Changed{};
|
||||||
bool m_FoundAgain;
|
bool m_FoundAgain{};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
string getID() const { return m_ID; }
|
string getID() const { return m_ID; }
|
||||||
@@ -49,23 +52,19 @@ class SAD_DetectedServer {
|
|||||||
string getClientIP() const { return m_ClientIP; }
|
string getClientIP() const { return m_ClientIP; }
|
||||||
string getAdditions() const { return m_Additions; }
|
string getAdditions() const { return m_Additions; }
|
||||||
string getProjectName() const { return m_ServerProjectName; }
|
string getProjectName() const { return m_ServerProjectName; }
|
||||||
|
|
||||||
private:
|
|
||||||
void setID(const string& p_ID) { m_ID = p_ID; }
|
|
||||||
void setName(const string& p_Name) { m_Name = p_Name; }
|
|
||||||
void setComputerName(const string& p_ComputerName) { m_ComputerName = p_ComputerName; }
|
|
||||||
void setIP(const string& p_IP) { m_IP = p_IP; }
|
|
||||||
void setClientIP(const string& p_IP) { m_ClientIP = p_IP; }
|
|
||||||
void setPort(const string& p_Port) { m_Port = p_Port; }
|
|
||||||
void setAdditions(const string& p_Additions) { m_Additions = p_Additions; }
|
|
||||||
void setProjectName(const string& projName);
|
|
||||||
|
|
||||||
bool getNew() const { return m_New; }
|
bool getNew() const { return m_New; }
|
||||||
bool getChanged() const { return m_Changed; }
|
bool getChanged() const { return m_Changed; }
|
||||||
bool getFoundAgain() const { return m_FoundAgain; }
|
bool getFoundAgain() const { return m_FoundAgain; }
|
||||||
|
|
||||||
void setChanged() { m_Changed = true; }
|
void setChanged() { m_Changed = true; }
|
||||||
void setNew(bool p_New) { m_New = p_New; }
|
void setKnown() { m_New = false; }
|
||||||
void setFoundAgain() { m_FoundAgain = true; }
|
void setFoundAgain() { m_FoundAgain = true; }
|
||||||
|
void updateProjectName(string newProjectName) {
|
||||||
|
m_ServerProjectName = newProjectName;
|
||||||
|
if (m_ServerProjectName != newProjectName) {
|
||||||
|
m_Changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SAD_Client m_ThreadSend will send to Multicast addresses
|
// SAD_Client m_ThreadSend will send to Multicast addresses
|
||||||
void resetFlags() {
|
void resetFlags() {
|
||||||
@@ -75,31 +74,69 @@ class SAD_DetectedServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static unordered_map<string, SAD_DetectedServer *> s_DetectedServers;
|
static unordered_map<string, unique_ptr<SAD_DetectedServer>> sKnownServers;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static const int PARAMINDEX_PRECODE = 0;
|
static constexpr int PARAMINDEX_PRECODE = 0;
|
||||||
static const int PARAMINDEX_SPITTASKID = 1;
|
static constexpr int PARAMINDEX_SPITTASKID = 1;
|
||||||
static const int PARAMINDEX_NAME = 2;
|
static constexpr int PARAMINDEX_NAME = 2;
|
||||||
static const int PARAMINDEX_ID = 3;
|
static constexpr int PARAMINDEX_ID = 3;
|
||||||
static const int PARAMINDEX_COMPUTERNAME = 4;
|
static constexpr int PARAMINDEX_COMPUTERNAME = 4;
|
||||||
static const int PARAMINDEX_PORT = 5;
|
static constexpr int PARAMINDEX_PORT = 5;
|
||||||
static const int PARAMINDEX_CLIENTIP = 6;
|
static constexpr int PARAMINDEX_CLIENTIP = 6;
|
||||||
static const int PARAMINDEX_ADDITIONS = 7;
|
static constexpr int PARAMINDEX_ADDITIONS = 7;
|
||||||
static const int PARAMINDEX_SERVERPROJECTNAME = 8;
|
static constexpr int PARAMINDEX_SERVERPROJECTNAME = 8;
|
||||||
static const int PARAMINDEX_IP = 9;
|
static constexpr int PARAMINDEX_IP = 9;
|
||||||
static void addDetectedServer(string &id, string p_ServerParams[]);
|
|
||||||
static void removeDetectedServer(SAD_DetectedServer &server);
|
|
||||||
static void clearDetectedServers();
|
|
||||||
/**
|
/**
|
||||||
* Used by SAD_Client internally - don't use this manually
|
* Used by SAD_Client to add a found SPITServer - don't use this manually
|
||||||
|
* @param id the id of the server, every instance of a SPITServer has an own id
|
||||||
|
* @param p_ServerParams the server's parameter
|
||||||
*/
|
*/
|
||||||
static void cycleStart();
|
static void addDetectedServer(unique_ptr<SAD_DetectedServer> detected) {
|
||||||
|
if (auto i = sKnownServers.find(detected->getID()); i != sKnownServers.end()) {
|
||||||
|
auto &known = *(i->second.get());
|
||||||
|
known.setFoundAgain();
|
||||||
|
if (detected->getProjectName() != known.getProjectName()) {
|
||||||
|
known.updateProjectName(detected->getProjectName());
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sKnownServers[detected->getID()] = move(detected);
|
||||||
|
}
|
||||||
|
static void removeDetectedServer(SAD_DetectedServer &server) {
|
||||||
|
auto i = sKnownServers.find(server.getID());
|
||||||
|
if (i != sKnownServers.end()) {
|
||||||
|
i = sKnownServers.erase(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void clearDetectedServers() {
|
||||||
|
sKnownServers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void resetServerFlags() {
|
||||||
|
for (auto &srv : sKnownServers) {
|
||||||
|
srv.second->resetFlags();
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Used by SAD_Client internally -don't use this manually
|
* Used by SAD_Client internally -don't use this manually
|
||||||
* @param p_SADClientInterface
|
* @param p_SADClientInterface
|
||||||
*/
|
*/
|
||||||
static void cycleEnd(I_SAD_Client &p_SADClientInterface);
|
static void evaluateDetectedServers(I_SAD_Client &sadClient) {
|
||||||
|
for (auto &srv : sKnownServers) {
|
||||||
|
SAD_DetectedServer &known = *srv.second;
|
||||||
|
if (known.getFoundAgain() == false) {
|
||||||
|
sadClient.eventServerLost(known);
|
||||||
|
removeDetectedServer(known);
|
||||||
|
} else if (known.getNew()) {
|
||||||
|
known.setKnown();
|
||||||
|
sadClient.eventServerDetected(known);
|
||||||
|
} else if (known.getChanged()) {
|
||||||
|
sadClient.eventServerChanged(known);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a reference of the DetectedServer with the id p_ServerID.<br>
|
* Returns a reference of the DetectedServer with the id p_ServerID.<br>
|
||||||
@@ -107,5 +144,7 @@ class SAD_DetectedServer {
|
|||||||
* @param p_ServerID the id of the SPITServer
|
* @param p_ServerID the id of the SPITServer
|
||||||
* @return the reference of the SAD_DetectedServer instance with getID() == p_ServerID
|
* @return the reference of the SAD_DetectedServer instance with getID() == p_ServerID
|
||||||
*/
|
*/
|
||||||
static const SAD_DetectedServer &getRefDetectedServer(const string &id);
|
static const SAD_DetectedServer &getRefDetectedServer(const string &id) {
|
||||||
|
return *sKnownServers.at(id);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -285,7 +285,7 @@ class SPIT_Client : I_SAD_Client {
|
|||||||
// receive SPITTypes
|
// receive SPITTypes
|
||||||
void parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPITType);
|
void parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPITType);
|
||||||
void parseRemoveSPITType(SPIT_Message::RemoveSPITType *p_RemoveSPITType);
|
void parseRemoveSPITType(SPIT_Message::RemoveSPITType *p_RemoveSPITType);
|
||||||
int sendSPITTypeInternal(SPITType *p_SPITType);
|
int sendSPITTypeInternal(SPITType &p_SPITType);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,105 +0,0 @@
|
|||||||
/*
|
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
|
||||||
* To change this template file, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* SAD_DetectedServer holds information about SPIT Server detected by ServerAutoDetection (SAD).
|
|
||||||
* File: SAD_DetectedServer.cpp
|
|
||||||
* Author: Hagi
|
|
||||||
*
|
|
||||||
* Created on 4. Mai 2019, 13:51
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "SAD_DetectedServer.hpp"
|
|
||||||
#include "esp_log.h"
|
|
||||||
|
|
||||||
static const char *TAG = "SpitSrv";
|
|
||||||
|
|
||||||
unordered_map<string, SAD_DetectedServer *> SAD_DetectedServer::s_DetectedServers;
|
|
||||||
|
|
||||||
void SAD_DetectedServer::setProjectName(const string& projName) {
|
|
||||||
if ((m_ServerProjectName.compare(projName) != 0) && (m_New == false)) {
|
|
||||||
m_Changed = true;
|
|
||||||
}
|
|
||||||
m_ServerProjectName = projName;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SAD_DetectedServer::cycleStart() {
|
|
||||||
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
|
|
||||||
i->second->resetFlags();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SAD_DetectedServer::cycleEnd(I_SAD_Client &sadClient) {
|
|
||||||
for (auto &srv : s_DetectedServers) {
|
|
||||||
SAD_DetectedServer *ptrDetectedServer = srv.second;
|
|
||||||
if (ptrDetectedServer->getFoundAgain() == false) {
|
|
||||||
sadClient.eventServerLost(*ptrDetectedServer);
|
|
||||||
removeDetectedServer(*ptrDetectedServer);
|
|
||||||
} else if (ptrDetectedServer->getNew()) {
|
|
||||||
ptrDetectedServer->setNew(false);
|
|
||||||
sadClient.eventServerDetected(*ptrDetectedServer);
|
|
||||||
} else if (ptrDetectedServer->getChanged()) {
|
|
||||||
sadClient.eventServerChanged(*ptrDetectedServer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used by SAD_Client to add a found SPITServer - don't use this manually
|
|
||||||
* @param id the id of the server, every instance of a SPITServer has an own id
|
|
||||||
* @param p_ServerParams the server's parameter
|
|
||||||
*/
|
|
||||||
void SAD_DetectedServer::addDetectedServer(string &id, string p_ServerParams[]) {
|
|
||||||
SAD_DetectedServer *detectedServer;
|
|
||||||
{
|
|
||||||
auto i = s_DetectedServers.find(id);
|
|
||||||
if (i != s_DetectedServers.end()) {
|
|
||||||
detectedServer = i->second;
|
|
||||||
detectedServer->setFoundAgain();
|
|
||||||
} else {
|
|
||||||
detectedServer = new SAD_DetectedServer();
|
|
||||||
detectedServer->setID(p_ServerParams[PARAMINDEX_ID]);
|
|
||||||
detectedServer->setNew(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
detectedServer->setIP(p_ServerParams[PARAMINDEX_IP]);
|
|
||||||
detectedServer->setClientIP(p_ServerParams[PARAMINDEX_CLIENTIP]);
|
|
||||||
detectedServer->setName(p_ServerParams[PARAMINDEX_NAME]);
|
|
||||||
detectedServer->setComputerName(p_ServerParams[PARAMINDEX_COMPUTERNAME]);
|
|
||||||
detectedServer->setPort(p_ServerParams[PARAMINDEX_PORT]);
|
|
||||||
detectedServer->setAdditions(p_ServerParams[PARAMINDEX_ADDITIONS]);
|
|
||||||
detectedServer->setProjectName(p_ServerParams[PARAMINDEX_SERVERPROJECTNAME]);
|
|
||||||
|
|
||||||
s_DetectedServers[detectedServer->getID()] = detectedServer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Used by SAD_Client to remove a SPITServer - don't use this manually
|
|
||||||
* @param p_DetectedServer
|
|
||||||
*/
|
|
||||||
void SAD_DetectedServer::removeDetectedServer(SAD_DetectedServer &server) {
|
|
||||||
auto i = s_DetectedServers.find(server.getID());
|
|
||||||
if (i != s_DetectedServers.end()) {
|
|
||||||
SAD_DetectedServer *o_DetectedServer = i->second;
|
|
||||||
i = s_DetectedServers.erase(i);
|
|
||||||
delete o_DetectedServer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used by SAD_Client to clear all found SPITServers - don't use this manually
|
|
||||||
*/
|
|
||||||
void SAD_DetectedServer::clearDetectedServers() {
|
|
||||||
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
|
|
||||||
SAD_DetectedServer *ptrDetectedServer = i->second;
|
|
||||||
i = s_DetectedServers.erase(i);
|
|
||||||
delete ptrDetectedServer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const SAD_DetectedServer &SAD_DetectedServer::getRefDetectedServer(const string &id) {
|
|
||||||
return *s_DetectedServers.at(id);
|
|
||||||
}
|
|
||||||
@@ -1488,6 +1488,7 @@ void SPIT_Client::sendRemoteObject(string p_SPITObjectID, int p_Valueppm, char p
|
|||||||
}
|
}
|
||||||
|
|
||||||
fos::Semaphore SPIT_Client::sStartSad;
|
fos::Semaphore SPIT_Client::sStartSad;
|
||||||
|
unordered_map<string, unique_ptr<SAD_DetectedServer>> SAD_DetectedServer::sKnownServers;
|
||||||
|
|
||||||
void SPIT_Client::processSocketTask(SPIT_Client &instance) {
|
void SPIT_Client::processSocketTask(SPIT_Client &instance) {
|
||||||
ESP_LOGI(TAG, "Starting Socket managing");
|
ESP_LOGI(TAG, "Starting Socket managing");
|
||||||
@@ -1507,7 +1508,7 @@ void SPIT_Client::processSocketTask(SPIT_Client &instance) {
|
|||||||
if (instance.abortSearch)
|
if (instance.abortSearch)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
SAD_DetectedServer::cycleStart();
|
SAD_DetectedServer::resetServerFlags();
|
||||||
string stringBroadcastIP4 = instance.mSPITSocket->mOwnIp.getBroadcastIP4();
|
string stringBroadcastIP4 = instance.mSPITSocket->mOwnIp.getBroadcastIP4();
|
||||||
instance.mSPITSocket->sendMessageTo(stringBroadcastIP4, SERVERAUTODETECT_PORT, stringDatagram.data(), intDatagramLength);
|
instance.mSPITSocket->sendMessageTo(stringBroadcastIP4, SERVERAUTODETECT_PORT, stringDatagram.data(), intDatagramLength);
|
||||||
{
|
{
|
||||||
@@ -1536,7 +1537,7 @@ void SPIT_Client::processSocketTask(SPIT_Client &instance) {
|
|||||||
int intIndex = 0;
|
int intIndex = 0;
|
||||||
int intOldPos = 0;
|
int intOldPos = 0;
|
||||||
string stringPart;
|
string stringPart;
|
||||||
string stringParts[10];
|
string strParts[10];
|
||||||
|
|
||||||
while (intIndex < SAD_DetectedServer::PARAMINDEX_IP) {
|
while (intIndex < SAD_DetectedServer::PARAMINDEX_IP) {
|
||||||
int intPos = stringMessage.find(SERVERAUTODETECT_SEPERATOR.data(), intOldPos);
|
int intPos = stringMessage.find(SERVERAUTODETECT_SEPERATOR.data(), intOldPos);
|
||||||
@@ -1546,14 +1547,24 @@ void SPIT_Client::processSocketTask(SPIT_Client &instance) {
|
|||||||
stringPart = stringMessage.substr(intOldPos, intPos - intOldPos);
|
stringPart = stringMessage.substr(intOldPos, intPos - intOldPos);
|
||||||
intOldPos = intPos + SERVERAUTODETECT_SEPERATOR.length();
|
intOldPos = intPos + SERVERAUTODETECT_SEPERATOR.length();
|
||||||
}
|
}
|
||||||
stringParts[intIndex] = stringPart;
|
strParts[intIndex] = stringPart;
|
||||||
intIndex++;
|
intIndex++;
|
||||||
}
|
}
|
||||||
stringParts[SAD_DetectedServer::PARAMINDEX_IP] = stringServerIP;
|
|
||||||
SAD_DetectedServer::addDetectedServer(stringParts[SAD_DetectedServer::PARAMINDEX_ID], stringParts);
|
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));
|
||||||
}
|
}
|
||||||
fos::Task::delayMs(100);
|
fos::Task::delayMs(100);
|
||||||
SAD_DetectedServer::cycleEnd(instance);
|
SAD_DetectedServer::evaluateDetectedServers(instance);
|
||||||
|
|
||||||
instance.abortSearch = false;
|
instance.abortSearch = false;
|
||||||
// if(instance.m_ConnectionStatus == CONNECTION_OK) break;
|
// if(instance.m_ConnectionStatus == CONNECTION_OK) break;
|
||||||
|
|||||||
Reference in New Issue
Block a user