refactoring Sever detection
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -16,29 +17,31 @@ class I_SAD_Client {
|
||||
|
||||
class SAD_DetectedServer {
|
||||
public:
|
||||
SAD_DetectedServer() {}
|
||||
SAD_DetectedServer(const SAD_DetectedServer &orig)
|
||||
: m_ID(orig.m_ID),
|
||||
m_Name(orig.m_Name),
|
||||
m_ComputerName(orig.m_ComputerName),
|
||||
m_IP(orig.m_IP),
|
||||
m_ClientIP(orig.m_ClientIP),
|
||||
m_Port(orig.m_Port),
|
||||
m_Additions(orig.m_Additions) {
|
||||
SAD_DetectedServer(string p_Name, string p_ID, string p_ComputerName,
|
||||
string p_IP, string p_Port, string p_ClientIP,
|
||||
string p_Additions, string projName)
|
||||
: m_Name(p_Name),
|
||||
m_ID(p_ID),
|
||||
m_ComputerName(p_ComputerName),
|
||||
m_IP(p_IP),
|
||||
m_Port(p_Port),
|
||||
m_ClientIP(p_IP),
|
||||
m_Additions(p_Additions),
|
||||
m_ServerProjectName(projName) {
|
||||
}
|
||||
|
||||
private:
|
||||
string m_ID{};
|
||||
string m_Name{};
|
||||
string m_ID{};
|
||||
string m_ComputerName{};
|
||||
string m_IP{};
|
||||
string m_ClientIP{};
|
||||
string m_Port{};
|
||||
string m_ClientIP{};
|
||||
string m_Additions{};
|
||||
string m_ServerProjectName{};
|
||||
bool m_New;
|
||||
bool m_Changed;
|
||||
bool m_FoundAgain;
|
||||
bool m_New{true};
|
||||
bool m_Changed{};
|
||||
bool m_FoundAgain{};
|
||||
|
||||
public:
|
||||
string getID() const { return m_ID; }
|
||||
@@ -49,23 +52,19 @@ class SAD_DetectedServer {
|
||||
string getClientIP() const { return m_ClientIP; }
|
||||
string getAdditions() const { return m_Additions; }
|
||||
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 getChanged() const { return m_Changed; }
|
||||
bool getFoundAgain() const { return m_FoundAgain; }
|
||||
|
||||
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 updateProjectName(string newProjectName) {
|
||||
m_ServerProjectName = newProjectName;
|
||||
if (m_ServerProjectName != newProjectName) {
|
||||
m_Changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
// SAD_Client m_ThreadSend will send to Multicast addresses
|
||||
void resetFlags() {
|
||||
@@ -75,31 +74,69 @@ class SAD_DetectedServer {
|
||||
}
|
||||
|
||||
private:
|
||||
static unordered_map<string, SAD_DetectedServer *> s_DetectedServers;
|
||||
static unordered_map<string, unique_ptr<SAD_DetectedServer>> sKnownServers;
|
||||
|
||||
public:
|
||||
static const int PARAMINDEX_PRECODE = 0;
|
||||
static const int PARAMINDEX_SPITTASKID = 1;
|
||||
static const int PARAMINDEX_NAME = 2;
|
||||
static const int PARAMINDEX_ID = 3;
|
||||
static const int PARAMINDEX_COMPUTERNAME = 4;
|
||||
static const int PARAMINDEX_PORT = 5;
|
||||
static const int PARAMINDEX_CLIENTIP = 6;
|
||||
static const int PARAMINDEX_ADDITIONS = 7;
|
||||
static const int PARAMINDEX_SERVERPROJECTNAME = 8;
|
||||
static const int PARAMINDEX_IP = 9;
|
||||
static void addDetectedServer(string &id, string p_ServerParams[]);
|
||||
static void removeDetectedServer(SAD_DetectedServer &server);
|
||||
static void clearDetectedServers();
|
||||
static constexpr int PARAMINDEX_PRECODE = 0;
|
||||
static constexpr int PARAMINDEX_SPITTASKID = 1;
|
||||
static constexpr int PARAMINDEX_NAME = 2;
|
||||
static constexpr int PARAMINDEX_ID = 3;
|
||||
static constexpr int PARAMINDEX_COMPUTERNAME = 4;
|
||||
static constexpr int PARAMINDEX_PORT = 5;
|
||||
static constexpr int PARAMINDEX_CLIENTIP = 6;
|
||||
static constexpr int PARAMINDEX_ADDITIONS = 7;
|
||||
static constexpr int PARAMINDEX_SERVERPROJECTNAME = 8;
|
||||
static constexpr int PARAMINDEX_IP = 9;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @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>
|
||||
@@ -107,5 +144,7 @@ class SAD_DetectedServer {
|
||||
* @param p_ServerID the id of the SPITServer
|
||||
* @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
|
||||
void parseReportSPITType(SPIT_Message::ReportSPITType *p_ReportSPITType);
|
||||
void parseRemoveSPITType(SPIT_Message::RemoveSPITType *p_RemoveSPITType);
|
||||
int sendSPITTypeInternal(SPITType *p_SPITType);
|
||||
int sendSPITTypeInternal(SPITType &p_SPITType);
|
||||
|
||||
public:
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user