refactoring on Server detection

This commit is contained in:
2025-04-04 02:08:34 +02:00
parent 1208bee756
commit 74f1c31193
6 changed files with 174 additions and 323 deletions
-39
View File
@@ -1,39 +0,0 @@
#pragma once
#include <string>
using namespace std;
class SAD_DetectedServer;
class I_SAD_Client {
public:
/**
* If a new server was found by the ServerAutoDetect mechanism, this function is called.<br>
* To do so the SAD_Client ServerAutoDetect has to be startet - see SAD_Client::startServerAutoDetect.<br>
* If a server can be reached via several IP addresses, the IP address is selected according to the following preference:<br>
* 1.) If a loopback address is available, it is used.<br>
* 2.) If an IP4 address is available, it is used.<br>
* 3.) In the other cases the IP6 address is used.<br>
* @param p_ID the id of the server (every started server should generate an id)
* @param p_Name the name of the SPIT server
* @param p_ComputerName the Hostname of the cpmputer on which the SPIT server is running
* @param p_IP the selected ip address of the SPIT server
* @param p_Port the port of the SPIT server
* @param p_Addition sometimes server may have additional informations
* @param p_IsIP4 the server ip is a ip4 address
*/
virtual void eventServerDetected(SAD_DetectedServer &server) = 0;
/**
* If a SPITServer is not more found, this function is called.
* To do so the SAD_Client ServerAutoDetect has to be startet - see SAD_Client::startServerAutoDetect.<br>
* @param p_ID the id of the server (every started server should generate an id)
* @param p_Name the name of the SPIT server
* @param p_ComputerName the Hostname of the cpmputer on which the SPIT server is running
* @param p_IP the selected ip address of the SPIT server
* @param p_Port the port of the SPIT server
* @param p_Addition sometimes server may have additional informations
* @param p_IsIP4 the server ip is a ip4 address
*/
virtual void eventServerChanged(SAD_DetectedServer &server) = 0;
virtual void eventServerLost(SAD_DetectedServer &server) = 0;
};
+37 -45
View File
@@ -1,36 +1,41 @@
#pragma once
#include "I_SAD_Client.hpp"
#include <list>
#include <string>
#include <unordered_map>
using namespace std;
class SAD_DetectedServer {
class SAD_DetectedServer;
class I_SAD_Client {
public:
SAD_DetectedServer();
SAD_DetectedServer(const SAD_DetectedServer &orig) {
m_ID = orig.getID();
m_Name = orig.getName();
m_ComputerName = orig.getComputerName();
m_IP = orig.getIP();
m_ClientIP = orig.getClientIP();
m_Port = orig.getPort();
m_Additions = orig.getAdditions();
virtual void eventServerDetected(SAD_DetectedServer &server) = 0;
virtual void eventServerChanged(SAD_DetectedServer &server) = 0;
virtual void eventServerLost(SAD_DetectedServer &server) = 0;
};
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();
private:
string m_ID;
string m_Name;
string m_ComputerName;
string m_IP;
string m_ClientIP;
string m_Port;
string m_Additions;
string m_ServerProjectName;
string m_ID{};
string m_Name{};
string m_ComputerName{};
string m_IP{};
string m_ClientIP{};
string m_Port{};
string m_Additions{};
string m_ServerProjectName{};
bool m_New;
bool m_Changed;
bool m_FoundAgain;
@@ -46,14 +51,14 @@ class SAD_DetectedServer {
string getProjectName() const { return m_ServerProjectName; }
private:
void setID(string p_ID) { m_ID = p_ID; }
void setName(string p_Name) { m_Name = p_Name; }
void setComputerName(string p_ComputerName) { m_ComputerName = p_ComputerName; }
void setIP(string p_IP) { m_IP = p_IP; }
void setClientIP(string p_IP) { m_ClientIP = p_IP; }
void setPort(string p_Port) { m_Port = p_Port; }
void setAdditions(string p_Additions) { m_Additions = p_Additions; }
void setProjectName(string p_ServerProjectName);
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; }
@@ -63,7 +68,7 @@ class SAD_DetectedServer {
void setFoundAgain() { m_FoundAgain = true; }
// SAD_Client m_ThreadSend will send to Multicast addresses
void startCycleSAD() {
void resetFlags() {
m_Changed = false;
m_FoundAgain = false;
m_New = false;
@@ -83,21 +88,8 @@ class SAD_DetectedServer {
static const int PARAMINDEX_ADDITIONS = 7;
static const int PARAMINDEX_SERVERPROJECTNAME = 8;
static const int PARAMINDEX_IP = 9;
/**
* Used by SAD_Client to add a found SPITServer - don't use this manually
* @param p_IsIP4 true = the server is reached via a IP4 address<br>false = the server is reahed via a IP6 address
* @param p_ServerID the id of the server, every instance of a SPITServer has an own id
* @param p_ServerParams the server's parameter
*/
static void addDetectedServer(string p_ServerID, string p_ServerParams[]);
/**
* Used by SAD_Client to remove a SPITServer - don't use this manually
* @param p_DetectedServer
*/
static void addDetectedServer(string &id, string p_ServerParams[]);
static void removeDetectedServer(SAD_DetectedServer &server);
/**
* Used by SAD_Client to clear all found SPITServers - don't use this manually
*/
static void clearDetectedServers();
/**
* Used by SAD_Client internally - don't use this manually
@@ -115,5 +107,5 @@ 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(string p_ServerID);
static const SAD_DetectedServer &getRefDetectedServer(const string &id);
};
+24 -33
View File
@@ -85,7 +85,6 @@ class SPITSocket {
m_ClientIP = "";
m_ClientPort = "";
m_BoundClientPort = "";
m_BoundClientFamily = 0;
m_IsBound = false;
createSocket_UDP();
}
@@ -107,60 +106,52 @@ class SPITSocket {
string m_ClientIP;
string m_ClientPort; // port given by the user maybe "0"
string m_BoundClientPort; // port set after bind
int m_BoundClientFamily;
bool m_IsBound;
public:
/**
* Sets the ip address and port for server and client.
* @param p_ServerIP
* @param p_ServerPort
* @param p_ClientIP
* @param p_ClientPort
*/
void setConnectionParams(string p_ServerIP, string p_ServerPort, string p_ClientIP, string p_ClientPort) {
m_ServerIP = p_ServerIP;
m_ServerPort = p_ServerPort;
m_ClientIP = p_ClientIP;
m_ClientPort = p_ClientPort;
// Sets the ip address and port for server and client.
void setConnectionParams(const string &serverIp, const string &serverPort, const string &clientIp, const string &clientPort) {
m_ServerIP = serverIp;
m_ServerPort = serverPort;
m_ClientIP = clientIp;
m_ClientPort = clientPort;
}
/**
* Send a message to the server as set by setConnectionParams
* @param p_MessageBytes byte buffer containing the message
* @param p_MessageLength length of the messaeg
* @param message byte buffer containing the message
* @param length length of the message
* @return
*/
int sendMessage(const char *p_MessageBytes, int p_MessageLength) {
int sendMessage(const char *message, int length) {
if (m_ServerPort.length() <= 0)
return -1;
int intResult;
{
if (m_IsBound == false)
return -1;
intResult = sendMessageTo(m_BoundClientFamily, m_ServerIP, m_ServerPort, p_MessageBytes, p_MessageLength);
intResult = sendMessageTo(m_ServerIP, m_ServerPort, message, length);
}
return intResult;
}
int receiveMessage(int p_InetFamily, char *p_Buffer, int p_BufferLength, string *p_ServerIP);
int setBroadcast(bool p_Broadcast);
int bindSocket(string p_ServerIP, string p_ClientIP, string p_ClientPort);
int receiveMessage(char *p_Buffer, int p_BufferLength, string *serverIp) const;
int setBroadcast(bool p_Broadcast) const;
int bindSocket(string clientIp, string clientPort);
// * Internal usage - don't use this manually
// * Use sendMessage instead
int sendMessageTo(int p_InetFamily, string p_ServerIP, string p_Port, const char *p_MessageBytes, int p_MessageLength);
int sendMessageTo(const string& ip, const string&port, const char *message, int msgLength);
string getServerIP() { return m_ServerIP; }
string getServerPort() { return m_ServerPort; }
string getClientIP() { return m_ClientIP; }
string getClientPort() { return m_ClientPort; }
string getBoundClientPort() { return m_BoundClientPort; }
void setBoundClientPort(string p_Port) { m_BoundClientPort = p_Port; }
void setBoundClientFamily(int p_Family) { m_BoundClientFamily = p_Family; }
int getBoundClientFamily() { return m_BoundClientFamily; }
string getServerIP() const { return m_ServerIP; }
string getServerPort() const { return m_ServerPort; }
string getClientIP() const { return m_ClientIP; }
string getClientPort() const { return m_ClientPort; }
string getBoundClientPort() const { return m_BoundClientPort; }
void setBoundClientPort(const string &p_Port) { m_BoundClientPort = p_Port; }
void setIsBound(bool p_IsBound) { m_IsBound = p_IsBound; }
bool getIsBound() { return m_IsBound; }
bool getIsBound() const { return m_IsBound; }
private:
};
#endif /* SPITSOCKET_H */