refactor: simplifying project until runs on embedded system
This commit is contained in:
+4
-25
@@ -1,23 +1,9 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: SAD_ClientInterface.h
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 16. April 2019, 13:08
|
||||
*/
|
||||
|
||||
#ifndef SAD_CLIENTINTERFACE_H
|
||||
#define SAD_CLIENTINTERFACE_H
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class SAD_ClientInterface {
|
||||
class I_SAD_Client {
|
||||
public:
|
||||
/**
|
||||
* If a new server was found by the ServerAutoDetect mechanism, this function is called.<br>
|
||||
@@ -32,11 +18,9 @@ public:
|
||||
* @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_isLoopback the server ip is a loopback address
|
||||
* @param p_IsIP4 the server ip is a ip4 address
|
||||
* @param p_IsIP6 the server ip is a ip6 address
|
||||
*/
|
||||
virtual void eventServerDetected(string p_ID, string p_Name, string p_ComputerName, string p_IP, string p_Port, string p_Addition, string p_CLientIP, bool p_isLoopback, bool p_IsIP4, bool p_IsIP6) = 0;
|
||||
virtual void eventServerDetected(string p_ID, string p_Name, string p_ComputerName, string p_IP, string p_Port, string p_Addition, string p_CLientIP, bool p_IsIP4) = 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>
|
||||
@@ -46,12 +30,7 @@ public:
|
||||
* @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_isLoopback the server ip is a loopback address
|
||||
* @param p_IsIP4 the server ip is a ip4 address
|
||||
* @param p_IsIP6 the server ip is a ip6 address
|
||||
*/
|
||||
virtual void eventServerLost(string p_ID, string p_Name, string p_ComputerName, string p_IP, string p_Port, string p_Addition, string p_CLientIP, bool p_isLoopback, bool p_IsIP4, bool p_IsIP6) = 0;
|
||||
virtual void eventServerLost(string p_ID, string p_Name, string p_ComputerName, string p_IP, string p_Port, string p_Addition, string p_CLientIP, bool p_IsIP4) = 0;
|
||||
};
|
||||
|
||||
#endif /* SAD_CLIENTINTERFACE_H */
|
||||
|
||||
@@ -15,105 +15,73 @@
|
||||
#define MYSPITAPP_H
|
||||
|
||||
#include "SPIT_Client.h"
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include "SPITType.h"
|
||||
#include "SPITObject.h"
|
||||
#include "SPITPlayer.h"
|
||||
#include "SAD_DetectedServer.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class MyApp : SPIT_ClientInterface {
|
||||
public:
|
||||
MyApp();
|
||||
virtual ~MyApp();
|
||||
class MyApp {
|
||||
public:
|
||||
MyApp();
|
||||
// Client's infos is set by the client
|
||||
static string s_ClientName;
|
||||
static string s_ProjectName;
|
||||
void init();
|
||||
|
||||
//Client's infos is set by the client
|
||||
static string s_ClientName;
|
||||
void loadClientProject();
|
||||
private:
|
||||
SPIT_Client _SpitClient; // pointer to an instance of the SPIT_Client, which do the main work for you
|
||||
|
||||
private:
|
||||
SPIT_Client* m_SPIT_Client; //pointer to an instance of the SPIT_Client, which do the main work for you
|
||||
// Server's project infos filled by eventProjectReport
|
||||
static string m_ServerProjectID;
|
||||
static string m_ServerProjectName;
|
||||
static int m_ServerFramesPerSecond; // set when the server reports his project, see eventProjectReport
|
||||
// liveSHOWsoftware will use 44100 frames per second as time resolution
|
||||
// All times are given in frames,
|
||||
|
||||
//MySPITApp client project infos they are transferred after the server send his project infos, see eventProjectReport
|
||||
string m_ClientProjectID; //can be an id or simple the project's name
|
||||
string m_ClientProjectName; //the name of the client's (this) project
|
||||
public:
|
||||
//----- ServerAutoDetection -----
|
||||
static void onServerDetected(const SAD_DetectedServer &p_DetectedServer); // a SPITServer is detected
|
||||
static void onServerLost(const SAD_DetectedServer &p_DetectedServer); // a prior detected SPITServer is lost
|
||||
|
||||
//Server's info filled by eventConnectionStatus when SPIT_Client::CONNECTION_OK
|
||||
string m_ServerID;
|
||||
string m_ServerName;
|
||||
string m_ServerComputerName;
|
||||
int m_ServerStepTime;
|
||||
//----- Connection -----
|
||||
void eventConnectionStatus(string p_ServerID, string p_ServerComputerName, string p_ServerName, int p_ServerStepTime, int p_ConnectionStatus, int p_OldConnectionStatus);
|
||||
|
||||
//Server's project infos filled by eventProjectReport
|
||||
string m_ServerProjectID;
|
||||
string m_ServerProjectName;
|
||||
int m_ServerFramesPerSecond; //set when the server reports his project, see eventProjectReport
|
||||
//liveSHOWsoftware will use 44100 frames per second as time resolution
|
||||
//All times are given in frames,
|
||||
//----- Project -----
|
||||
static void onStartProjectTransfer();
|
||||
static void onEndProjectTransfer();
|
||||
static void onProjectReport(string p_ProjectID, string p_ProjectName, int p_FramesPerSeoond);
|
||||
static void onProjectClosed(string p_ProjectID);
|
||||
|
||||
//Reconnect to the server when connection is lost see eventConnectionStatus and eventServerDetected
|
||||
bool m_FlagReconnect; //true = on eventServerDetected connect to the server identified by m_ServerID
|
||||
//----- SPITType -----
|
||||
static void onSpitType(bool p_New, const SPITType &p_SPITType);
|
||||
static void onSpitTypeRemote(bool p_New, const SPITType &p_SPITType);
|
||||
static void onSpitTypeRemoved(const SPITType &p_SPITType);
|
||||
static void onSpitTypeRemoteRemoved(const SPITType &p_SPITType);
|
||||
|
||||
//----- SPITObject -----
|
||||
static void onSpitObject(bool p_New, const SPITType &p_SPITType, const SPITObject &p_SPITObject);
|
||||
static void onSpitObjectRemote(bool p_New, const SPITType &p_SPITType, const SPITObject &p_SPITObject);
|
||||
static void onSpitObjectRemoved(const SPITType &p_SPITType, const SPITObject &p_SPITObject);
|
||||
static void onSpitObjectRemoteRemoved(const SPITType &p_SPITType, const SPITObject &p_SPITObject);
|
||||
|
||||
//----- Play SPITObjects -----
|
||||
static void onPlay(string p_PlayerID, string p_PlayerName, char p_PlayerType, bool p_PlayerIsRunning, long long p_TimeFrames);
|
||||
static void onPlayObject(string p_PlayerID, bool p_Started, bool p_Stopped, const SPITType &p_SPITType, const SPITObject &p_SPITObject, const SPITPlayer::PlayObject &p_PlayObject);
|
||||
|
||||
//----- Server changes status of one of his REMOTE SPITObjects -----
|
||||
// REMOTE only Server->Client RemoteObject's play status
|
||||
static void onPlayRemoteObjectStatus(const SPITType &p_SPITType, const SPITObject &p_SPITObject, long long p_FrameInsideObject, int p_Value);
|
||||
|
||||
public:
|
||||
//---------- SPIT_ClientInterface -------------
|
||||
string getClientProjectID(); //should return the client's project id, maybe the same as project name
|
||||
string getClientProjectName();
|
||||
|
||||
//----- ServerAutoDetection -----
|
||||
void startServerAutoDetect(); //starts the autodetection of SPITServers in the local network area (see eventServerDetected)
|
||||
void stopServerAutoDetect(); //stopps the autodetection
|
||||
void eventServerDetected(const SAD_DetectedServer& p_DetectedServer); //a SPITServer is detected
|
||||
void eventServerLost(const SAD_DetectedServer& p_DetectedServer); //a prior detected SPITServer is lost
|
||||
|
||||
//----- Connection -----
|
||||
void eventConnectionStatus(string p_ServerID, string p_ServerComputerName, string p_ServerName, int p_ServerStepTime, int p_ConnectionStatus, int p_OldConnectionStatus);
|
||||
|
||||
//----- Project -----
|
||||
void eventProjectReport(string p_ProjectID, string p_ProjectName, int p_FramesPerSeoond);
|
||||
void eventProjectClosed(string p_ProjectID);
|
||||
|
||||
void eventStartProjectTransfer();
|
||||
void eventEndProjectTransfer();
|
||||
|
||||
|
||||
//----- SPITType -----
|
||||
|
||||
void eventSPITType(bool p_New, const SPITType& p_SPITType);
|
||||
void eventSPITTypeRemote(bool p_New, const SPITType& p_SPITType);
|
||||
void eventSPITTypeRemoved(const SPITType& p_SPITType);
|
||||
void eventSPITTypeRemoteRemoved(const SPITType& p_SPITType);
|
||||
|
||||
//----- SPITObject -----
|
||||
void eventSPITObject(bool p_New, const SPITType& p_SPITType, const SPITObject& p_SPITObject);
|
||||
void eventSPITObjectRemote(bool p_New, const SPITType& p_SPITType, const SPITObject& p_SPITObject);
|
||||
void eventSPITObjectRemoved(const SPITType& p_SPITType, const SPITObject& p_SPITObject);
|
||||
void eventSPITObjectRemoteRemoved(const SPITType& p_SPITType, const SPITObject& p_SPITObject);
|
||||
|
||||
//----- Play SPITObjects -----
|
||||
void eventPlay(string p_PlayerID, string p_PlayerName, char p_PlayerType, bool p_PlayerIsRunning, long long p_TimeFrames);
|
||||
void eventPlayObject(string p_PlayerID, bool p_Started, bool p_Stopped, const SPITType& p_SPITType, const SPITObject& p_SPITObject, const SPITPlayer::PlayObject& p_PlayObject);
|
||||
|
||||
|
||||
//----- Server changes status of one of his REMOTE SPITObjects -----
|
||||
//REMOTE only Server->Client RemoteObject's play status
|
||||
void eventPlayRemoteObjectStatus(const SPITType& p_SPITType, const SPITObject& p_SPITObject, long long p_FrameInsideObject, int p_Value);
|
||||
|
||||
void sendRemoteObject(string p_SPITObjectID, int p_Valueppm = 0, char p_ValueFlag = 0x00);
|
||||
//-----
|
||||
void testLiveShowRemote();
|
||||
|
||||
//------ User's options normally should not be used, the server is master of things ------
|
||||
public:
|
||||
//----- SPITType -----
|
||||
int removeSPITType(string p_SPITTypeID);
|
||||
int changeSPITType(SPITType* p_SPITType);
|
||||
|
||||
//----- SPITObject -----
|
||||
int removeSPITObject(string p_SPITObjectID);
|
||||
int changeSPITObject(SPITObject* p_SPITObject);
|
||||
void sendRemoteObject(string p_SPITObjectID, int p_Valueppm = 0, char p_ValueFlag = 0x00);
|
||||
//-----
|
||||
void testLiveShowRemote();
|
||||
|
||||
//------ User's options normally should not be used, the server is master of things ------
|
||||
public:
|
||||
};
|
||||
|
||||
#endif /* MYSPITAPP_H */
|
||||
|
||||
|
||||
@@ -1,60 +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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: ServerAutoDetetc_Client.h
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 16. April 2019, 08:57
|
||||
*/
|
||||
#ifndef SERVERAUTODETETC_CLIENT_H
|
||||
#define SERVERAUTODETETC_CLIENT_H
|
||||
#include "_SPITMACROS.h"
|
||||
#include "SPIT_Handler.h"
|
||||
#include "SPITSocket.h"
|
||||
#include "SAD_DetectedServer.h"
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include "Mutex.hpp"
|
||||
#include "Semaphore.hpp"
|
||||
#include "Task.hpp"
|
||||
|
||||
|
||||
static const string SERVERAUTODETECT_PRECODE = "#SAD#";
|
||||
static const string SERVERAUTODETECT_SEPERATOR = "#,#";
|
||||
static const string SERVERAUTODETECT_TASKID = "#spit#";
|
||||
static const string SERVERAUTODETECT_PORT = "15110";
|
||||
|
||||
class SAD_Client {
|
||||
|
||||
//----- SAD Client Interface ------
|
||||
private:
|
||||
static SAD_ClientInterface* s_SADClientInterface;
|
||||
|
||||
//----- SAD_Client -----
|
||||
public:
|
||||
private:
|
||||
static fos::Mutex s_MutexSAD;
|
||||
static fos::Semaphore s_ConditionStopSAD;
|
||||
static bool s_StopSAD;
|
||||
static fos::Task* s_ThreadSend;
|
||||
static fos::Task* s_ThreadReceiveIP4;
|
||||
|
||||
static fos::Mutex s_MutexSocket;
|
||||
static SPITSocket* s_SPITSocket;
|
||||
|
||||
static void cycleStart();
|
||||
static void cycleEnd();
|
||||
static void sendAutoDetectServer();
|
||||
static void receiveAutoDetectServer_IP4();
|
||||
|
||||
public:
|
||||
static void startAutoDetectServer(SAD_ClientInterface* p_SADClientInterface, SPITSocket* p_SPITSocket);
|
||||
static void stopAutoDetectServer(SAD_ClientInterface* p_SADClientInterface);
|
||||
};
|
||||
|
||||
#endif /* SERVERAUTODETETC_CLIENT_H */
|
||||
|
||||
+26
-135
@@ -1,30 +1,12 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
* This class describes a detected SPITServer, found by SAD_Client.<br>
|
||||
* File: SAD_DetectedServer.h
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 4. Mai 2019, 13:51
|
||||
*/
|
||||
|
||||
#ifndef SAD_DETECTEDSERVER_H
|
||||
#define SAD_DETECTEDSERVER_H
|
||||
|
||||
#include "SAD_ClientInterface.h"
|
||||
#include "I_SAD_Client.hpp"
|
||||
#include <string>
|
||||
#include "Mutex.hpp"
|
||||
#include <unordered_map>
|
||||
#include <list>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
class SAD_DetectedServer {
|
||||
|
||||
public:
|
||||
@@ -34,9 +16,7 @@ public:
|
||||
m_Name = orig.getName();
|
||||
m_ComputerName = orig.getComputerName();
|
||||
m_IP = orig.getIP();
|
||||
m_IPIsLoopback = orig.getIpIsLoopback();
|
||||
m_IPIsIP4 = orig.getIpIsIP4();
|
||||
m_IPIsIP6 = orig.getIpIsIP6();
|
||||
m_ClientIP = orig.getClientIP();
|
||||
m_Port = orig.getPort();
|
||||
m_Additions = orig.getAdditions();
|
||||
@@ -48,9 +28,7 @@ public:
|
||||
string m_Name;
|
||||
string m_ComputerName;
|
||||
string m_IP;
|
||||
bool m_IPIsLoopback;
|
||||
bool m_IPIsIP4;
|
||||
bool m_IPIsIP6;
|
||||
string m_ClientIP;
|
||||
string m_Port;
|
||||
string m_Additions;
|
||||
@@ -59,123 +37,40 @@ public:
|
||||
bool m_FoundAgain;
|
||||
|
||||
public:
|
||||
/**
|
||||
*
|
||||
* @return ID of the SPITServer
|
||||
*/
|
||||
string getID() const {
|
||||
return m_ID;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return name of the SPITServer
|
||||
*/
|
||||
string getName() const {
|
||||
return m_Name;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return name of the computer where the SPITServer runs
|
||||
*/
|
||||
string getComputerName() const {
|
||||
return m_ComputerName;
|
||||
}
|
||||
/**
|
||||
* @return IP address at which the server can be reached
|
||||
*/
|
||||
string getIP() const {
|
||||
return m_IP;
|
||||
}
|
||||
/**
|
||||
* @return true = the SPITServer's IP address is a loopback address
|
||||
*/
|
||||
bool getIpIsLoopback() const {
|
||||
return m_IPIsLoopback;
|
||||
}
|
||||
/**
|
||||
* @return true = the SPITServer's IP address is an IP4 address
|
||||
*/
|
||||
bool getIpIsIP4() const {
|
||||
return m_IPIsIP4;
|
||||
}
|
||||
/**
|
||||
* @return true = the SPITServer's IP address is an IP6 address
|
||||
*/
|
||||
bool getIpIsIP6() const {
|
||||
return m_IPIsIP6;
|
||||
}
|
||||
/**
|
||||
* @return port of the SPITServer
|
||||
*/
|
||||
string getPort() const {
|
||||
return m_Port;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return The IP address of the client under which this client reached the server.
|
||||
*/
|
||||
string getClientIP() const {
|
||||
return m_ClientIP;
|
||||
}
|
||||
/**
|
||||
* Sometimes the SPITServer has additional informations
|
||||
* @return
|
||||
*/
|
||||
string getAdditions() const {
|
||||
return m_Additions;
|
||||
}
|
||||
string getID() const { return m_ID; }
|
||||
string getName() const { return m_Name; }
|
||||
string getComputerName() const { return m_ComputerName; }
|
||||
string getIP() const { return m_IP; }
|
||||
bool getIpIsIP4() const { return m_IPIsIP4; }
|
||||
string getPort() const { return m_Port; }
|
||||
string getClientIP() const { return m_ClientIP; }
|
||||
string getAdditions() const { return m_Additions; }
|
||||
private:
|
||||
void setID(string p_ID);
|
||||
void setName(string p_Name);
|
||||
void setComputerName(string p_ComputerName);
|
||||
void setIP(string p_IP);
|
||||
void setClientIP(string p_IP);
|
||||
void setPort(string p_Port);
|
||||
void setAdditions(string p_Additions);
|
||||
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 setIPIsLoopback(bool p_IsLoopback) {
|
||||
m_IPIsLoopback = p_IsLoopback;
|
||||
}
|
||||
void setIPIsIP4(bool p_IsIP4) {
|
||||
m_IPIsIP4 = p_IsIP4;
|
||||
}
|
||||
void setIPIsIP6(bool p_IsIP6) {
|
||||
m_IPIsIP6 = p_IsIP6;
|
||||
}
|
||||
void setIPIsIP4(bool p_IsIP4) { m_IPIsIP4 = p_IsIP4; }
|
||||
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 setFoundAgain() { m_FoundAgain = true; }
|
||||
|
||||
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 setFoundAgain() {
|
||||
m_FoundAgain = true;
|
||||
}
|
||||
//SAD_Client m_ThreadSend will send to Multicast addresses
|
||||
void startCycleSAD() {
|
||||
m_IPIsLoopback = false;
|
||||
m_IPIsIP4 = false;
|
||||
m_IPIsIP6 = false;
|
||||
m_Changed = false;
|
||||
m_FoundAgain = false;
|
||||
}
|
||||
//SAD_Client m_Thread_Send has paused in the meantime m_ThreadReceive may received messages from servers
|
||||
void endCycleSAD() {
|
||||
|
||||
}
|
||||
void endCycleSAD() { }
|
||||
private:
|
||||
static fos::Mutex s_MutexDetectedServer;
|
||||
static unordered_map<string, SAD_DetectedServer*> s_DetectedServers;
|
||||
public:
|
||||
static const int PARAMINDEX_PRECODE = 0;
|
||||
@@ -211,7 +106,7 @@ public:
|
||||
* Used by SAD_Client internally -don't use this manually
|
||||
* @param p_SADClientInterface
|
||||
*/
|
||||
static void cycleEnd(SAD_ClientInterface* p_SADClientInterface);
|
||||
static void cycleEnd(I_SAD_Client& p_SADClientInterface);
|
||||
|
||||
/**
|
||||
* Returns a reference of the DetectedServer with the id p_ServerID.<br>
|
||||
@@ -222,7 +117,6 @@ public:
|
||||
static const SAD_DetectedServer& getRefDetectedServer(string p_ServerID) {
|
||||
SAD_DetectedServer* ptrDetectedServer = NULL;
|
||||
{
|
||||
fos::MutexGuard o_Lock(s_MutexDetectedServer);
|
||||
auto i = s_DetectedServers.find(p_ServerID);
|
||||
if (i != s_DetectedServers.end()) {
|
||||
ptrDetectedServer = i->second;
|
||||
@@ -237,6 +131,3 @@ public:
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* SAD_DETECTEDSERVER_H */
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include "SPITType.h"
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include "Mutex.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -27,13 +26,9 @@ class SPITObjectListener;
|
||||
class SPITObject {
|
||||
private:
|
||||
|
||||
static fos::Mutex s_MutexSPITObject;
|
||||
static unordered_map<string, SPITObject*> s_SPITObjects;
|
||||
static unordered_map<string, SPITObject*> s_SPITObjectsRemote;
|
||||
public:
|
||||
static fos::Mutex* getMutex() {
|
||||
return &s_MutexSPITObject;
|
||||
}
|
||||
static SPITObject* getSPITObject(string p_ID);
|
||||
static const SPITObject& getSPITObjectRef(string p_ID);
|
||||
static bool addSPITObject(SPITObject* p_ptrSpitObject);
|
||||
|
||||
@@ -119,10 +119,6 @@ public:
|
||||
|
||||
//----- SPITPlayer ------
|
||||
public:
|
||||
static fos::Mutex s_Mutex;
|
||||
static fos::Mutex* getMutex() {
|
||||
return &s_Mutex;
|
||||
}
|
||||
//list the SPITPlayers
|
||||
static unordered_map<string, SPITPlayer*> s_SPITPlayers; //all actual played objects
|
||||
static SPITPlayer* getSPITPlayer(string p_PlayerID); //if a player does not exist creates a new one
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* SPITSocket is the base class for WindowsSocket, UnixSocket,..<br>
|
||||
* SPITSocket is the base class for WindowsSocket, UnixSocket,.
|
||||
* Used by SPIT_Client and SAD_Client.
|
||||
* File: SPITSocket.h
|
||||
* Author: Hagi
|
||||
@@ -15,66 +15,92 @@
|
||||
|
||||
#ifndef SPITSOCKET_H
|
||||
#define SPITSOCKET_H
|
||||
#include "_SPITMACROS.h"
|
||||
#include "Mutex.hpp"
|
||||
#include <unordered_map>
|
||||
|
||||
#include <string>
|
||||
#include "esp_netif.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include "Task.hpp"
|
||||
#include "esp_log.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** Returns an guid */
|
||||
string createGUID();
|
||||
/** Returns the name of the client's host computer*/
|
||||
string getClientHostName();
|
||||
|
||||
class SPITSocket{
|
||||
public:
|
||||
static int FAMILY_IP4;
|
||||
static int FAMILY_IP6;
|
||||
static const int FAMILY_IP4 = AF_INET;
|
||||
|
||||
//-------- nested classes --------------
|
||||
|
||||
public:
|
||||
/**
|
||||
* IPAddress class is used to fetch all available IP addresses of the computer.<br>
|
||||
* This is for internal usage.<br>
|
||||
* IPAddress class is used to fetch all available IP addresses of the computer
|
||||
* This is for internal usage
|
||||
*/
|
||||
class IPAddress {
|
||||
struct IPAddress {
|
||||
public:
|
||||
IPAddress();
|
||||
~IPAddress();
|
||||
private:
|
||||
string m_IPAddress;
|
||||
string m_SubnetMask;
|
||||
bool m_Found;
|
||||
public:
|
||||
const string& getIPAddress() {
|
||||
return m_IPAddress;
|
||||
}
|
||||
const string& getSubnetMask() {
|
||||
return m_SubnetMask;
|
||||
}
|
||||
bool getFound() {
|
||||
return m_Found;
|
||||
}
|
||||
IPAddress(const string& p_IPAddress, const string& p_SubnetMask)
|
||||
: m_IPAddress(p_IPAddress), m_SubnetMask(p_SubnetMask) { }
|
||||
const string m_IPAddress;
|
||||
const string m_SubnetMask;
|
||||
|
||||
void setIPAddress(const string& p_IPAddress);
|
||||
void setSubnetMask(const string& p_SubnetMask);
|
||||
void setFound(bool p_Found);
|
||||
/**
|
||||
* Calculates the broadcast address from a ip4 address
|
||||
* @param p_IPAddress the ip4 address
|
||||
* @return returns the broadcast address
|
||||
*/
|
||||
string getBroadcastIP4() const {
|
||||
struct in_addr broadcast;
|
||||
unsigned long ulIPAddress;
|
||||
unsigned long ulSubnetMask;
|
||||
|
||||
ulIPAddress = inet_addr(m_IPAddress.data());
|
||||
ulSubnetMask = inet_addr(m_SubnetMask.data());
|
||||
broadcast.s_addr = ulIPAddress | ~ulSubnetMask;
|
||||
|
||||
return inet_ntoa(broadcast);
|
||||
}
|
||||
};
|
||||
const IPAddress mOwnIp;
|
||||
static IPAddress getAdaptersInfo();
|
||||
bool createSocket_UDP();
|
||||
static const int INVALID_SOCKET = -1;
|
||||
|
||||
private:
|
||||
static fos::Mutex s_MutexIPAddresses;
|
||||
int m_UDPSocketIP4;
|
||||
|
||||
public:
|
||||
static unordered_map<string, IPAddress*> s_IPAddresses;
|
||||
static void addIPAddress(string p_IPAddress, string p_SubnetMask);
|
||||
static void removeIPAddress(string p_IPAddress);
|
||||
static int getIPAddressCount();
|
||||
static IPAddress* getIPAddress(int p_Index);
|
||||
static void clearIPAddresses();
|
||||
static void resetFoundIPAddresses();
|
||||
static void removeNotFoundIPAddresses();
|
||||
|
||||
|
||||
//-------- class --------------
|
||||
SPITSocket(IPAddress ip) : mOwnIp(ip) {
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
|
||||
SPITSocket();
|
||||
virtual ~SPITSocket();
|
||||
m_ServerIP = "";
|
||||
m_ServerPort = "";
|
||||
m_ClientIP = "";
|
||||
m_ClientPort = "";
|
||||
m_BoundClientPort = "";
|
||||
m_BoundClientFamily = 0;
|
||||
m_IsBound = false;
|
||||
createSocket_UDP();
|
||||
|
||||
}
|
||||
virtual ~SPITSocket() {
|
||||
ESP_LOGD("UDP", "Shutting down ...");
|
||||
|
||||
if (m_UDPSocketIP4 != INVALID_SOCKET) {
|
||||
shutdown(m_UDPSocketIP4, 2);
|
||||
close(m_UDPSocketIP4);
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
}
|
||||
fos::Task::delayMs(100);
|
||||
m_IsBound = false;
|
||||
}
|
||||
private:
|
||||
string m_ServerIP;
|
||||
string m_ServerPort;
|
||||
@@ -83,7 +109,7 @@ private:
|
||||
string m_BoundClientPort; //port set after bind
|
||||
int m_BoundClientFamily;
|
||||
bool m_IsBound;
|
||||
fos::Mutex m_MutexSocket;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets the ip address and port for server and client.
|
||||
@@ -92,79 +118,49 @@ public:
|
||||
* @param p_ClientIP
|
||||
* @param p_ClientPort
|
||||
*/
|
||||
void setConnectionParams(string p_ServerIP, string p_ServerPort, string p_ClientIP, string 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the server as set by setConnectionParams.<br>
|
||||
* 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
|
||||
* @return
|
||||
*/
|
||||
int sendMessage(const char* p_MessageBytes, int p_MessageLength);
|
||||
/**
|
||||
* Call this for receiving a message.<br>
|
||||
* This will block until a message is received or the underlying socket is closed.<br>
|
||||
* @param p_InetFamily either AF_INET or AF_INET6<br>
|
||||
* The SPITSocket creates two udp sockets, one for IP4 and one for IP6.<br>
|
||||
* This parameter decides from which socket should a message recieved.<br>
|
||||
* @param p_Buffer the buffer containing the received message later.
|
||||
* @param p_BufferLength the size of the buffer
|
||||
* @param p_ServerIP a pointer to a string, when a messages is received the string will contain the ip address of the sender/server.<br>
|
||||
* if p_ServerIP is NULL - no detection of the server will be done.
|
||||
* @return
|
||||
*/
|
||||
virtual int receiveMessage(int p_InetFamily, char* p_Buffer, int p_BufferLength, string* p_ServerIP) = 0;
|
||||
int sendMessage(const char* p_MessageBytes, int p_MessageLength) {
|
||||
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);
|
||||
}
|
||||
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_ClientIP, string p_ClientPort);
|
||||
|
||||
/**
|
||||
* Returns the broadcast address of an ip4 address.<br>
|
||||
* @param p_IPAdress
|
||||
* @return
|
||||
*/
|
||||
virtual string getBroadcastIP4(IPAddress* p_IPAdress) = 0;
|
||||
/**
|
||||
* Set whether the sockat are brodcast sockets or not
|
||||
* @param p_Broadcast true = the sockets are broadcast sockets
|
||||
* @return 0 = ok, -1 = error
|
||||
*/
|
||||
virtual int setBroadcast(bool p_Broadcast) = 0;
|
||||
/**
|
||||
* Bind the client's udp socket to an ip address and port.<br>
|
||||
* @param p_ClientIP the ip address either IP4 or IP6
|
||||
* @param p_ClientPort the client port number.<br>
|
||||
* "0" = bind to any free port.<br>
|
||||
* @return
|
||||
*/
|
||||
virtual int bindSocket(string p_ClientIP, string p_ClientPort) = 0;
|
||||
/**
|
||||
* Internal usage - don't use this manually.<br>
|
||||
* Use sendMessage instead.<br>
|
||||
* @param p_InetFamily
|
||||
* @param p_ServerIP
|
||||
* @param p_Port
|
||||
* @param p_MessageBytes
|
||||
* @param p_MessageLength
|
||||
* @return
|
||||
*/
|
||||
virtual int sendMessageTo(int p_InetFamily, string p_ServerIP, string p_Port, const char* p_MessageBytes, int p_MessageLength) = 0;
|
||||
/**
|
||||
* Internal usage - don't use this manually.<br>
|
||||
*/
|
||||
virtual void getAdaptersInfo() = 0;
|
||||
|
||||
|
||||
string getServerIP();
|
||||
string getServerPort();
|
||||
string getClientIP();
|
||||
string getClientPort();
|
||||
string getBoundClientPort();
|
||||
void setBoundClientPort(string p_Port);
|
||||
void setBoundClientFamily(int p_Family);
|
||||
int getBoundClientFamily();
|
||||
void setIsBound(bool p_IsBound);
|
||||
bool getIsBound();
|
||||
|
||||
// * 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);
|
||||
|
||||
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; }
|
||||
void setIsBound(bool p_IsBound) { m_IsBound = p_IsBound; }
|
||||
bool getIsBound() { return m_IsBound; }
|
||||
};
|
||||
|
||||
#endif /* SPITSOCKET_H */
|
||||
|
||||
@@ -14,12 +14,10 @@
|
||||
#ifndef SPITTYPE_H
|
||||
#define SPITTYPE_H
|
||||
|
||||
#include "_SPITMACROS.h"
|
||||
#include "SPITSocket.h"
|
||||
#include "SPIT_Message.h"
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include "Mutex.hpp"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -27,16 +25,10 @@ class SPITTypeListener;
|
||||
|
||||
class SPITType {
|
||||
private:
|
||||
static fos::Mutex s_MutexSPITType;
|
||||
static unordered_map<string, SPITType*> s_SPITTypes;
|
||||
static unordered_map<string, SPITType*> s_SPITTypesRemote;
|
||||
|
||||
|
||||
|
||||
public:
|
||||
static fos::Mutex* getMutex() {
|
||||
return &s_MutexSPITType;
|
||||
}
|
||||
static SPITType* getSPITType(string p_ID);
|
||||
static const SPITType& getSPITTypeRef(string p_ID);
|
||||
static bool addSPITType(SPITType* p_ptrSpitType);
|
||||
@@ -82,15 +74,9 @@ private:
|
||||
void initMembers();
|
||||
|
||||
public:
|
||||
string getID() const{
|
||||
return m_ID;
|
||||
}
|
||||
string getGroupName() const {
|
||||
return m_GroupName;
|
||||
}
|
||||
string getName() const {
|
||||
return m_Name;
|
||||
}
|
||||
string getID() const{ return m_ID; }
|
||||
string getGroupName() const { return m_GroupName; }
|
||||
string getName() const { return m_Name; }
|
||||
/**
|
||||
* Returns if the SPITType triggers an action on the Server or on the client.
|
||||
* @return false = the action is triggered on the client The client waits for the server to play objects of this SPITType<br>
|
||||
@@ -107,22 +93,11 @@ public:
|
||||
* This is used by the server and client to tell if the other side has registered the changes.
|
||||
* @return true = other side has registered the changes.
|
||||
*/
|
||||
bool getConfirmed() const {
|
||||
return m_Confirmed;
|
||||
}
|
||||
bool getFixed() const {
|
||||
return m_Fixed;
|
||||
}
|
||||
long long getDefaultDelay() const {
|
||||
return m_DefaultDelay;
|
||||
}
|
||||
long long getDefaultDuration() const {
|
||||
return m_DefaultDuration;
|
||||
}
|
||||
|
||||
bool getCreatedByClient() const {
|
||||
return m_CreatedByClient;
|
||||
}
|
||||
bool getConfirmed() const { return m_Confirmed; }
|
||||
bool getFixed() const { return m_Fixed; }
|
||||
long long getDefaultDelay() const { return m_DefaultDelay; }
|
||||
long long getDefaultDuration() const { return m_DefaultDuration; }
|
||||
bool getCreatedByClient() const { return m_CreatedByClient; }
|
||||
|
||||
/**
|
||||
* Virtual construct - means ActionAssigned for SPITTypes is is not implemented in SPITProtocol.<br>
|
||||
|
||||
@@ -14,30 +14,42 @@
|
||||
#ifndef SPIT_CLIENT_H
|
||||
#define SPIT_CLIENT_H
|
||||
|
||||
#include "_SPITMACROS.h"
|
||||
#include "UnixSocket.h"
|
||||
|
||||
#include "SPITSocket.h"
|
||||
#include "SAD_Client.h"
|
||||
#include "SAD_ClientInterface.h"
|
||||
#include "SAD_DetectedServer.h"
|
||||
#include "SPIT_ClientInterface.h"
|
||||
#include "SAD_DetectedServer.hpp"
|
||||
#include "SPIT_Message.h"
|
||||
#include "Watchdog.h"
|
||||
#include "SPITType.h"
|
||||
#include "SPITObject.h"
|
||||
#include "SPIT_Handler.h"
|
||||
#include "SPITPlayer.h"
|
||||
#include <memory>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//static char nullChar[] = "\0";
|
||||
class SPIT_Client : I_SAD_Client {
|
||||
|
||||
class SPIT_Client : SAD_ClientInterface, Watchdog::Listener {
|
||||
public:
|
||||
/** Maximal length of a SPITMessage*/
|
||||
static const int MAXSPITMESSAGEBYTECOUNT = 512;
|
||||
|
||||
struct Callbacks {
|
||||
void(*onServerDetect)(const SAD_DetectedServer &p_DetectedServer);
|
||||
void(*onServerLost)(const SAD_DetectedServer &p_DetectedServer);
|
||||
void(*onStartProjectTransfer)();
|
||||
void(*onEndProjectTransfer)();
|
||||
void(*onProjectClosed)(string);
|
||||
void(*onProjectReport)(string p_ProjectID, string p_ProjectName, int p_FramesPerSeoond);
|
||||
void(*onSpitType)(bool p_New, const SPITType &p_SPITType);
|
||||
void(*onSpitTypeRemote)(bool p_New, const SPITType &p_SPITType);
|
||||
void(*onSpitObject)(bool p_New, const SPITType &p_SPITType, const SPITObject &p_SPITObject);
|
||||
void(*onSpitObjectRemote)(bool p_New, const SPITType &p_SPITType, const SPITObject &p_SPITObject);
|
||||
void(*onSpitTypeRemoved)(const SPITType &p_SPITType);
|
||||
void(*onSpitTypeRemoteRemoved)(const SPITType &p_SPITType);
|
||||
void(*onSpitObjectRemoved)(const SPITType &p_SPITType, const SPITObject &p_SPITObject);
|
||||
void(*onSpitObjectRemoteRemoved)(const SPITType &p_SPITType, const SPITObject &p_SPITObject);
|
||||
void(*onPlay)(string p_PlayerID, string p_PlayerName, char p_PlayerType, bool p_PlayerIsRunning, long long p_TimeFrames);
|
||||
void(*onPlayObject)(string p_PlayerID, bool p_Started, bool p_Stopped, const SPITType &p_SPITType, const SPITObject &p_SPITObject, const SPITPlayer::PlayObject &p_PlayObject);
|
||||
void(*onPlayRemoteObjectStatus)(const SPITType &p_SPITType, const SPITObject &p_SPITObject, long long p_FrameInsideObject, int p_Value);
|
||||
};
|
||||
|
||||
//----- Connection Status -----
|
||||
public:
|
||||
/** No Connection established.<br>Either no connect tried or disconnected*/
|
||||
@@ -46,7 +58,7 @@ public:
|
||||
static const int CONNECTION_NONESERVER = 1;
|
||||
/** The connection is lost<br>Either because of a network crash or because the server no longer responds.*/
|
||||
static const int CONNECTION_LOST = 2;
|
||||
/** Connection to the server is established.*/
|
||||
/** Connection to the serv er is established.*/
|
||||
static const int CONNECTION_OK = 3;
|
||||
/** A connection to the server is tried.*/
|
||||
static const int CONNECTION_TRYCONNECTION = 4;
|
||||
@@ -72,48 +84,38 @@ public:
|
||||
|
||||
//----- SPIT_Client -----
|
||||
public:
|
||||
static SPITSocket* createSPITSocket();
|
||||
public:
|
||||
SPIT_Client(SPIT_ClientInterface* p_SPITClientInterface);
|
||||
SPIT_Client(string name, string projName, Callbacks calls);
|
||||
virtual ~SPIT_Client();
|
||||
private:
|
||||
/** SPITClientInterface, normally the user app should implement this*/
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
SPITSocket* m_SPITSocket;
|
||||
fos::Mutex m_MutexSPITSocket;
|
||||
fos::TaskT<SPIT_Client&> * m_ThreadReceive;
|
||||
Callbacks mCallbacks;
|
||||
|
||||
fos::TaskT<SPIT_Client&> * m_ThreadReceive{};
|
||||
bool m_StopReceiveSPITMessages;
|
||||
|
||||
int m_ConnectionStatus;
|
||||
bool m_DisconnectedByServer; //flag true = when server disconnects
|
||||
bool m_DisconnectedByServer{}; //flag true = when server disconnects
|
||||
string m_ID;
|
||||
string m_Name;
|
||||
string m_ProjName;
|
||||
string m_ComputerName;
|
||||
|
||||
//Server's info filled by parseConnectAnswer
|
||||
string m_ServerID;
|
||||
string m_ServerName;
|
||||
string m_ServerComputerName;
|
||||
int m_ServerStepTime;
|
||||
string m_ServerID{};
|
||||
string m_ServerName{};
|
||||
string m_ServerComputerName{};
|
||||
int m_ServerStepTime{-1};
|
||||
|
||||
|
||||
Watchdog* m_Watchdog;
|
||||
unsigned char m_MessageNumber_Send;
|
||||
int m_MessageNumber_Received;
|
||||
|
||||
//--- Server Project ---
|
||||
string m_ServerProjectID;
|
||||
string m_ServerProjectName;
|
||||
int m_ServerFramesPerSecond;
|
||||
|
||||
public:
|
||||
SPIT_ClientInterface* getSPITClientInterface() {
|
||||
return m_SPITClientInterface;
|
||||
}
|
||||
|
||||
public:
|
||||
/** The ID of the client.<br>Every client instance should set an unique id*/
|
||||
string getID();
|
||||
/** Set the name of the client, should be done by the user app*/
|
||||
void setName(string p_Name);
|
||||
/** The name of the client, should be set by the user app*/
|
||||
string getName();
|
||||
//----- Computer Params -----
|
||||
@@ -153,9 +155,11 @@ private:
|
||||
bool startReceiveSPITMessages();
|
||||
void stopReceiveSPITMessages();
|
||||
static void receiveSPITMessage(SPIT_Client& rM);
|
||||
int sendSPITMessage(SPIT_Message::OPPart* p_SPITMessageOPPart);
|
||||
int sendSPITMessage(SPIT_Message::OPPart& p_SPITMessageOPPart);
|
||||
void fillHeader(SPIT_Message::Header* p_Header);
|
||||
|
||||
// Reconnect to the server when connection is lost see eventConnectionStatus and eventServerDetected
|
||||
bool _doReconnect{}; // true = on eventServerDetected connect to the server identified by m_ServerID
|
||||
|
||||
//--------------- ServerAutoDetection SAD ---------------
|
||||
public:
|
||||
@@ -164,47 +168,9 @@ public:
|
||||
/** Stops the autodetection of SPITServers in the local network*/
|
||||
void stopServerAutoDetect();
|
||||
/** Called every time a new SPITServer is detetced in the local network */
|
||||
void eventServerDetected(string p_ID, string p_Name, string p_ComputerName, string p_IP, string p_Port, string p_Addition, string p_ClientIP, bool p_isLoopback, bool p_IsIP4, bool p_IsIP6);
|
||||
void eventServerDetected(string p_ID, string p_Name, string p_ComputerName, string p_IP, string p_Port, string p_Addition, string p_ClientIP, bool p_IsIP4);
|
||||
/** Called every time a prior detected SPITServer is is no longer found.*/
|
||||
void eventServerLost(string p_ID, string p_Name, string p_ComputerName, string p_IP, string p_Port, string p_Addition, string p_ClientIP, bool p_isLoopback, bool p_IsIP4, bool p_IsIP6);
|
||||
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyServerDetected: virtual public SPIT_Handler::Runnable {
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
SAD_DetectedServer m_DetectedServer;
|
||||
public:
|
||||
NotifyServerDetected(SPIT_ClientInterface* p_SPITClientInterface, const SAD_DetectedServer& p_DetetctedServer) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_DetectedServer = p_DetetctedServer;
|
||||
}
|
||||
~NotifyServerDetected(){}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventServerDetected(m_DetectedServer);
|
||||
}
|
||||
}
|
||||
};
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyServerLost: virtual public SPIT_Handler::Runnable {
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
SAD_DetectedServer m_DetectedServer;
|
||||
public:
|
||||
NotifyServerLost(SPIT_ClientInterface* p_SPITClientInterface, const SAD_DetectedServer& p_DetetctedServer) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_DetectedServer = p_DetetctedServer;
|
||||
}
|
||||
~NotifyServerLost() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventServerLost(m_DetectedServer);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void eventServerLost(string p_ID, string p_Name, string p_ComputerName, string p_IP, string p_Port, string p_Addition, string p_ClientIP, bool p_IsIP4);
|
||||
|
||||
|
||||
//--------------- Connection ---------------
|
||||
@@ -228,7 +194,7 @@ public:
|
||||
|
||||
private:
|
||||
void disconnectInternal(int p_Cause);
|
||||
void parseSPIT_Message(SPIT_Message* p_SPITMessage);
|
||||
void parseSPIT_Message(unique_ptr<SPIT_Message> p_SPITMessage);
|
||||
void parseConnectAnswer(SPIT_Message::ConnectAnswer* p_ConnectAnswer);
|
||||
void parseDisconnect(SPIT_Message::Disconnect* p_Disconnect);
|
||||
void parseWatchdog(SPIT_Message::Watchdog* p_Watchdog);
|
||||
@@ -238,36 +204,6 @@ public:
|
||||
void sendWatchdog();
|
||||
/** Used internally. When the connection to the SPITServer is lost an eventConnectionStatus will occur*/
|
||||
void eventWatchdogLost();
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyConnectionStatus : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyConnectionStatus(SPIT_ClientInterface* p_SPITClientInterface, string p_ServerID, string p_ServerComputerName, string p_ServerName, int p_ServerStepTime,
|
||||
int p_NewConnectionStatus, int p_OldConnectionStatus) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_ServerID = p_ServerID;
|
||||
m_ServerComputerName = p_ServerComputerName;
|
||||
m_ServerName = p_ServerName;
|
||||
m_ServerStepTime = p_ServerStepTime;
|
||||
m_NewConnectionStatus = p_NewConnectionStatus;
|
||||
m_OldConnectionStatus = p_OldConnectionStatus;
|
||||
}
|
||||
~NotifyConnectionStatus() {}
|
||||
void run (){
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventConnectionStatus(m_ServerComputerName, m_ServerID, m_ServerName, m_ServerStepTime, m_NewConnectionStatus, m_OldConnectionStatus);
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
string m_ServerID;
|
||||
string m_ServerComputerName;
|
||||
string m_ServerName;
|
||||
int m_ServerStepTime;
|
||||
int m_NewConnectionStatus;
|
||||
int m_OldConnectionStatus;
|
||||
};
|
||||
|
||||
//--------------- SPIT Project ---------------
|
||||
private:
|
||||
void parseReportSPITProject(SPIT_Message::ReportSPITProject* p_ReportSPITProject);
|
||||
@@ -280,83 +216,10 @@ public:
|
||||
/** Returns frame rate per second. This is the time resolution in which the server transmits time data. The unit of time data is always frames.*/
|
||||
int getServerFramesPerSecond();
|
||||
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyReportProject : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyReportProject(SPIT_ClientInterface* p_SPITClientInterface, string p_ProjectID, string p_ProjectName, int p_FramesPerSecond) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_ProjectID = p_ProjectID;
|
||||
m_ProjectName = p_ProjectName;
|
||||
m_FramesPerSecond = p_FramesPerSecond;
|
||||
}
|
||||
~NotifyReportProject() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventProjectReport(m_ProjectID, m_ProjectName, m_FramesPerSecond);
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
string m_ProjectID;
|
||||
string m_ProjectName;
|
||||
int m_FramesPerSecond;
|
||||
};
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyProjectClosed : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyProjectClosed(SPIT_ClientInterface* p_SPITClientInterface, string p_ProjectID) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_ProjectID = p_ProjectID;
|
||||
}
|
||||
~NotifyProjectClosed() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventProjectClosed(m_ProjectID);
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
string m_ProjectID;
|
||||
};
|
||||
|
||||
private:
|
||||
void parseRequestProjectTransfer(SPIT_Message::RequestProjectTransfer* p_RequestProjectTransfer);
|
||||
void parseStartProjectTransfer(SPIT_Message::StartProjectTransfer* p_StartProjectTransfer);
|
||||
void parseEndProjectTransfer(SPIT_Message::EndProjectTransfer* p_EndProjectTransfer);
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyStartProjectTransfer : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyStartProjectTransfer(SPIT_ClientInterface* p_SPITClientInterface) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
}
|
||||
~NotifyStartProjectTransfer() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventStartProjectTransfer();
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
};
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyEndProjectTransfer : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyEndProjectTransfer(SPIT_ClientInterface* p_SPITClientInterface) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
}
|
||||
~NotifyEndProjectTransfer() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventEndProjectTransfer();
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
};
|
||||
|
||||
|
||||
// --------------- SPIT Types ---------------
|
||||
@@ -408,60 +271,6 @@ public:
|
||||
*/
|
||||
int removeSPITType(string p_SPITTypeID);
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyReportSPITType : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyReportSPITType(bool p_Remote, bool p_New, SPIT_ClientInterface* p_SPITClientInterface, const SPITType& p_SPITType) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_Remote = p_Remote;
|
||||
m_New = p_New;
|
||||
m_SPITType = p_SPITType;
|
||||
}
|
||||
~NotifyReportSPITType() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
if (m_Remote == false) {
|
||||
m_SPITClientInterface->eventSPITType(m_New, m_SPITType);
|
||||
}
|
||||
else {
|
||||
m_SPITClientInterface->eventSPITTypeRemote(m_New, m_SPITType);
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
bool m_Remote;
|
||||
bool m_New;
|
||||
SPITType m_SPITType;
|
||||
};
|
||||
|
||||
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyRemoveSPITType : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyRemoveSPITType(bool p_Remote, SPIT_ClientInterface* p_SPITClientInterface, const SPITType& p_SPITType) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_Remote = p_Remote;
|
||||
m_SPITType = p_SPITType;
|
||||
}
|
||||
~NotifyRemoveSPITType() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
if (m_Remote == false) {
|
||||
m_SPITClientInterface->eventSPITTypeRemoved(m_SPITType);
|
||||
}
|
||||
else {
|
||||
m_SPITClientInterface->eventSPITTypeRemoteRemoved(m_SPITType);
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
bool m_Remote;
|
||||
SPITType m_SPITType;
|
||||
};
|
||||
|
||||
|
||||
//--------------- SPIT Objects ---------------
|
||||
private:
|
||||
@@ -484,63 +293,6 @@ public:
|
||||
*/
|
||||
int removeSPITObject(string p_SPITObjectID);
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyReportSPITObject : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyReportSPITObject(bool p_Remote, bool p_New, SPIT_ClientInterface* p_SPITClientInterface, const SPITType& p_SPITType, const SPITObject& p_SPITObject) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_Remote = p_Remote;
|
||||
m_New = p_New;
|
||||
m_SPITType = p_SPITType;
|
||||
m_SPITObject = p_SPITObject;
|
||||
}
|
||||
~NotifyReportSPITObject() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
if (m_Remote == false) {
|
||||
m_SPITClientInterface->eventSPITObject(m_New, m_SPITType, m_SPITObject);
|
||||
}
|
||||
else {
|
||||
m_SPITClientInterface->eventSPITObjectRemote(m_New, m_SPITType, m_SPITObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
bool m_Remote;
|
||||
bool m_New;
|
||||
SPITType m_SPITType;
|
||||
SPITObject m_SPITObject;
|
||||
};
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyRemoveSPITObject : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyRemoveSPITObject(bool p_Remote, SPIT_ClientInterface* p_SPITClientInterface, const SPITType& p_SPITType, const SPITObject& p_SPITObject) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_Remote = p_Remote;
|
||||
m_SPITType = p_SPITType;
|
||||
m_SPITObject = p_SPITObject;
|
||||
}
|
||||
~NotifyRemoveSPITObject() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
if (m_Remote == false) {
|
||||
m_SPITClientInterface->eventSPITObjectRemoved(m_SPITType, m_SPITObject);
|
||||
}
|
||||
else {
|
||||
m_SPITClientInterface->eventSPITObjectRemoteRemoved(m_SPITType, m_SPITObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
bool m_Remote;
|
||||
SPITType m_SPITType;
|
||||
SPITObject m_SPITObject;
|
||||
};
|
||||
|
||||
|
||||
|
||||
//--------------- Play SPIT Objects ---------------
|
||||
private:
|
||||
@@ -549,90 +301,21 @@ private:
|
||||
void parsePlaySPITEnd(SPIT_Message::PlaySPITEnd* p_PlaySPITEnd);
|
||||
void parseReportPlayStatus(SPIT_Message::ReportPlayStatus* p_ReportPlayStatus);
|
||||
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyPlaySPITStart : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyPlaySPITStart(SPIT_ClientInterface* p_SPITClientInterface, string p_PlayerID, string p_PlayerName, char p_PlayerType, bool p_IsRunning, long long p_TimeFrame) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_PlayerID = p_PlayerID;
|
||||
m_PlayerName = p_PlayerName;
|
||||
m_PlayerType = p_PlayerType;
|
||||
m_IsRunning = p_IsRunning;
|
||||
m_TimeFrames = p_TimeFrame;
|
||||
}
|
||||
~NotifyPlaySPITStart() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventPlay(m_PlayerID, m_PlayerName, m_PlayerType, m_IsRunning, m_TimeFrames);
|
||||
}
|
||||
}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
string m_PlayerID;
|
||||
string m_PlayerName;
|
||||
char m_PlayerType;
|
||||
bool m_IsRunning;
|
||||
long long m_TimeFrames;
|
||||
};
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyPlaySPITObject : public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyPlaySPITObject(bool p_Started, bool p_Stopped,
|
||||
SPIT_ClientInterface* p_SPITClientInterface, const SPITType& p_SPITType, const SPITObject& p_SPITObject, const SPITPlayer::PlayObject& p_PlayObject) {
|
||||
m_Started = p_Started;
|
||||
m_Stopped = p_Stopped;
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_SPITType = p_SPITType;
|
||||
m_SPITObject = p_SPITObject;
|
||||
m_PlayObject = p_PlayObject;
|
||||
}
|
||||
~NotifyPlaySPITObject() {}
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventPlayObject(m_PlayObject.getPlayerID(), m_Started, m_Stopped, m_SPITType, m_SPITObject, m_PlayObject);
|
||||
}
|
||||
}
|
||||
private:
|
||||
bool m_Started;
|
||||
bool m_Stopped;
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
SPITObject m_SPITObject;
|
||||
SPITType m_SPITType;
|
||||
SPITPlayer::PlayObject m_PlayObject;
|
||||
|
||||
};
|
||||
public:
|
||||
/** Helper class to notify the SPITClientInterface/user's app*/
|
||||
class NotifyReportPlayStatus: public SPIT_Handler::Runnable {
|
||||
public:
|
||||
NotifyReportPlayStatus(SPIT_ClientInterface* p_SPITClientInterface, const SPITType& p_SPITType, const SPITObject& p_SPITObject,
|
||||
long long p_FrameInsideObject, int p_Value) {
|
||||
m_SPITClientInterface = p_SPITClientInterface;
|
||||
m_SPITObject = p_SPITObject;
|
||||
m_SPITType = p_SPITType;
|
||||
m_FrameInsideObject = p_FrameInsideObject;
|
||||
m_Value = p_Value;
|
||||
}
|
||||
~NotifyReportPlayStatus() {}
|
||||
private:
|
||||
SPIT_ClientInterface* m_SPITClientInterface;
|
||||
SPITObject m_SPITObject;
|
||||
SPITType m_SPITType;
|
||||
long long m_FrameInsideObject;
|
||||
int m_Value;
|
||||
public:
|
||||
void run() {
|
||||
if (m_SPITClientInterface != NULL) {
|
||||
m_SPITClientInterface->eventPlayRemoteObjectStatus(m_SPITType, m_SPITObject, m_FrameInsideObject, m_Value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
public:
|
||||
//--------- Remote ----------
|
||||
void sendRemoteObject(string p_SPITObjectID, int p_Valueppm = 0, char p_ValueFlag = 0x00);
|
||||
|
||||
private:
|
||||
static void processSocketTask(SPIT_Client & instance);
|
||||
fos::TaskT<SPIT_Client &>* sThread{};
|
||||
bool abortSearch{};
|
||||
bool sAnswered{};
|
||||
static fos::Semaphore sStartSad;
|
||||
SPITSocket * mSPITSocket;
|
||||
void answerWatchdog() {
|
||||
sAnswered = true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif /* SPIT_CLIENT_H */
|
||||
|
||||
@@ -1,82 +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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: SPIT_ClientInterface.h
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 17. April 2019, 16:15
|
||||
*/
|
||||
|
||||
#ifndef SPIT_CLIENTINTERFACE_H
|
||||
#define SPIT_CLIENTINTERFACE_H
|
||||
|
||||
//#include <string>
|
||||
|
||||
#include "SPITType.h"
|
||||
#include "SPITObject.h"
|
||||
#include "SPITPlayer.h"
|
||||
#include "SAD_DetectedServer.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class SPIT_ClientInterface {
|
||||
public:
|
||||
|
||||
virtual string getClientProjectID() = 0;
|
||||
virtual string getClientProjectName() = 0;
|
||||
|
||||
/**
|
||||
* If a new server was found by the ServerAutoDetect mechanism, this function is called.
|
||||
* 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_isLoopback the server ip is a loopback address
|
||||
* @param p_IsIP4 the server ip is a ip4 address
|
||||
* @param p_IsIP6 the server ip is a ip6 address
|
||||
*/
|
||||
virtual void eventServerDetected(const SAD_DetectedServer& p_DetectedServer) = 0;
|
||||
virtual void eventServerLost(const SAD_DetectedServer& p_DetectedServer) = 0;
|
||||
|
||||
virtual void eventConnectionStatus(string p_ServerID, string p_ServerComputerName, string p_ServerName, int p_ServerStepTime, int p_ConnectionStatus, int p_OldConnectionStatus) = 0;
|
||||
|
||||
virtual void eventProjectReport(string p_ProjectID, string p_ProjectName, int p_FramesPerSeoond) = 0;
|
||||
virtual void eventProjectClosed(string p_ProjectID) = 0;
|
||||
|
||||
|
||||
virtual void eventStartProjectTransfer() = 0;
|
||||
virtual void eventEndProjectTransfer() = 0;
|
||||
|
||||
virtual void eventSPITType(bool p_New, const SPITType& p_SPITType) = 0;
|
||||
virtual void eventSPITTypeRemote(bool p_New, const SPITType& p_SPITType) = 0;
|
||||
|
||||
virtual void eventSPITTypeRemoved(const SPITType& p_SPITType) = 0;
|
||||
virtual void eventSPITTypeRemoteRemoved(const SPITType& p_SPITType) = 0;
|
||||
|
||||
|
||||
virtual void eventSPITObject(bool p_New, const SPITType& p_SPITType, const SPITObject& p_SPITObject) = 0;
|
||||
virtual void eventSPITObjectRemote(bool p_New, const SPITType& p_SPITType, const SPITObject& p_SPITObject) = 0;
|
||||
virtual void eventSPITObjectRemoved(const SPITType& p_SPITType, const SPITObject& p_SPITObject) = 0;
|
||||
virtual void eventSPITObjectRemoteRemoved(const SPITType& p_SPITType, const SPITObject& p_SPITObject) = 0;
|
||||
|
||||
virtual void eventPlay(string p_PlayerID, string p_PlayerName, char p_PlayerType, bool p_PlayerIsRunning, long long p_TimeFrames) = 0;
|
||||
|
||||
virtual void eventPlayObject(string p_PlayerID, bool p_Started, bool p_Stopped, const SPITType& p_SPITType, const SPITObject& p_SPITObject, const SPITPlayer::PlayObject& p_PlayObject) = 0;
|
||||
|
||||
|
||||
//REMOTE Server->Client
|
||||
virtual void eventPlayRemoteObjectStatus(const SPITType& p_SPITType, const SPITObject& p_SPITObject, long long p_FrameInsideObject, int p_Value) = 0;
|
||||
};
|
||||
|
||||
#endif /* SPIT_CLIENTINTERFACE_H */
|
||||
|
||||
@@ -1,48 +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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: SPIT_Handler.h
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 24. April 2019, 11:17
|
||||
*/
|
||||
|
||||
#ifndef SPIT_HANDLER_H
|
||||
#define SPIT_HANDLER_H
|
||||
|
||||
#include "Mutex.hpp"
|
||||
#include "Semaphore.hpp"
|
||||
#include "Task.hpp"
|
||||
#include <list>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class SPIT_Handler {
|
||||
public:
|
||||
class Runnable {
|
||||
public:
|
||||
Runnable() {};
|
||||
virtual ~Runnable();
|
||||
virtual void run() = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
static list<Runnable*> s_Runnables;
|
||||
static fos::Mutex s_MutexLock;
|
||||
static fos::Semaphore s_ConditionVariable;
|
||||
static fos::Task* s_HandlerThread;
|
||||
|
||||
static bool s_WakeUp;
|
||||
static bool s_Close;
|
||||
static void run();
|
||||
public:
|
||||
static void addRunnable(Runnable* p_Runnable);
|
||||
static void closeHandler();
|
||||
};
|
||||
|
||||
#endif /* SPIT_HANDLER_H */
|
||||
|
||||
@@ -1,51 +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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: UnixSocket.h
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 12. April 2019, 15:58
|
||||
*/
|
||||
|
||||
#ifndef UNIXSOCKET_H
|
||||
#define UNIXSOCKET_H
|
||||
|
||||
#include "_SPITMACROS.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "SPITSocket.h"
|
||||
|
||||
class UnixSocket : public SPITSocket{
|
||||
public:
|
||||
static const int INVALID_SOCKET = -1;
|
||||
public:
|
||||
UnixSocket();
|
||||
virtual ~UnixSocket();
|
||||
private:
|
||||
int m_UDPSocketIP4;
|
||||
int m_UDPSocketIP6;
|
||||
public:
|
||||
void getAdaptersInfo();
|
||||
string getBroadcastIP4(IPAddress* p_IPAdress);
|
||||
int setBroadcast(bool p_Broadcast);
|
||||
|
||||
static int init();
|
||||
static int cleanUp();
|
||||
bool createSocket_UDP();
|
||||
int bindSocket(string p_ClientIP, string p_ClientPort);
|
||||
int sendMessageTo(int p_InetFamily, string p_ServerIP, string p_ServerPort, const char* p_MessageBytes, int p_MessageLength);
|
||||
int receiveMessage(int p_InetFamily, char* p_Buffer, int p_BufferLength, string* p_ServerIP);
|
||||
};
|
||||
|
||||
#endif /* UNIXSOCKET_H */
|
||||
|
||||
@@ -1,56 +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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: Watchdog.h
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 22. April 2019, 13:02
|
||||
*/
|
||||
|
||||
#ifndef WATCHDOG_H
|
||||
#define WATCHDOG_H
|
||||
|
||||
#include "_SPITMACROS.h"
|
||||
#include "Mutex.hpp"
|
||||
#include "Semaphore.hpp"
|
||||
#include "Task.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class Watchdog {
|
||||
public:
|
||||
class Listener {
|
||||
public:
|
||||
virtual void sendWatchdog()=0;
|
||||
virtual void eventWatchdogLost()=0;
|
||||
};
|
||||
|
||||
static int WATCHDOGTIME;
|
||||
static bool s_StopWatchdog;
|
||||
|
||||
Watchdog(Listener* p_Listener);
|
||||
virtual ~Watchdog();
|
||||
|
||||
private:
|
||||
Listener* m_Listener;
|
||||
|
||||
fos::Mutex m_MutexLock;
|
||||
fos::Semaphore m_ConditionVariable;
|
||||
fos::TaskT<Watchdog&>* m_WatchdogThread;
|
||||
|
||||
bool m_Answered;
|
||||
|
||||
static void runWatchdog(Watchdog& wd);
|
||||
public:
|
||||
void startWatchdog();
|
||||
void answerWatchdog();
|
||||
void stopWatchdog();
|
||||
};
|
||||
|
||||
#endif /* WATCHDOG_H */
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <unistd.h>
|
||||
// #include <uuid/uuid.h>
|
||||
|
||||
// To output debug information (printf calls in the code)
|
||||
// you must set or comment out this precompiler definition
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
/** Returns an guid */
|
||||
string createGUID();
|
||||
/** Returns the name of the client's host computer*/
|
||||
string getClientHostName();
|
||||
+269
-591
File diff suppressed because it is too large
Load Diff
@@ -1,219 +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_Client is used for ServerAutoDetection (SAD).
|
||||
*
|
||||
* File: ServerAutoDetetc_Client.cpp
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 16. April 2019, 08:57
|
||||
*/
|
||||
|
||||
#include "SAD_Client.h"
|
||||
|
||||
//----- initialze static members -----
|
||||
bool SAD_Client::s_StopSAD = true;
|
||||
fos::Mutex SAD_Client::s_MutexSAD;
|
||||
fos::Semaphore SAD_Client::s_ConditionStopSAD;
|
||||
fos::Task* SAD_Client::s_ThreadSend = NULL;
|
||||
fos::Task* SAD_Client::s_ThreadReceiveIP4 = NULL;
|
||||
|
||||
SAD_ClientInterface* SAD_Client::s_SADClientInterface = NULL;
|
||||
|
||||
fos::Mutex SAD_Client::s_MutexSocket;
|
||||
SPITSocket* SAD_Client::s_SPITSocket = NULL;
|
||||
|
||||
|
||||
|
||||
void SAD_Client::startAutoDetectServer(SAD_ClientInterface* p_SADClientInterface, SPITSocket* p_SpitSocket) {
|
||||
if (p_SADClientInterface == NULL) return;
|
||||
if (p_SpitSocket == NULL) return;
|
||||
|
||||
s_SADClientInterface = p_SADClientInterface;
|
||||
{
|
||||
fos::MutexGuard o_UniqueLock(s_MutexSAD);
|
||||
if (s_StopSAD == false) return;
|
||||
s_StopSAD = false;
|
||||
SAD_DetectedServer::clearDetectedServers();
|
||||
}
|
||||
|
||||
s_SPITSocket = p_SpitSocket;
|
||||
|
||||
s_ThreadSend = new fos::Task(SAD_Client::sendAutoDetectServer, "sendDetect");
|
||||
s_ThreadReceiveIP4 = new fos::Task(SAD_Client::receiveAutoDetectServer_IP4, "getDetect");
|
||||
}
|
||||
|
||||
void SAD_Client::stopAutoDetectServer(SAD_ClientInterface* p_SADClientInterface) {
|
||||
if (s_SADClientInterface != p_SADClientInterface) return;
|
||||
|
||||
{
|
||||
fos::MutexGuard o_UniqueLock(s_MutexSAD);
|
||||
if (s_StopSAD == true) return;
|
||||
s_StopSAD = true;
|
||||
}
|
||||
s_ConditionStopSAD.give();
|
||||
if (s_ThreadSend != NULL) {
|
||||
delete s_ThreadSend;
|
||||
s_ThreadSend = NULL;
|
||||
}
|
||||
|
||||
//wait for receiving threads
|
||||
if (s_ThreadReceiveIP4 != NULL) {
|
||||
delete s_ThreadReceiveIP4;
|
||||
s_ThreadReceiveIP4 = NULL;
|
||||
}
|
||||
{
|
||||
fos::MutexGuard o_LockSocket(s_MutexSocket);
|
||||
if (s_SPITSocket != NULL) {
|
||||
delete s_SPITSocket;
|
||||
s_SPITSocket = NULL;
|
||||
}
|
||||
}
|
||||
{
|
||||
fos::MutexGuard o_UniqueLock(s_MutexSAD);
|
||||
SAD_DetectedServer::clearDetectedServers();
|
||||
}
|
||||
}
|
||||
|
||||
void SAD_Client::sendAutoDetectServer() {
|
||||
SPITSocket::IPAddress* o_IPAddress;
|
||||
string stringBroadcastIP4;
|
||||
string stringDatagram;
|
||||
int intDatagramLength;
|
||||
bool boolDone;
|
||||
|
||||
stringDatagram = SERVERAUTODETECT_PRECODE;
|
||||
stringDatagram.append(SERVERAUTODETECT_SEPERATOR);
|
||||
stringDatagram.append(SERVERAUTODETECT_TASKID);
|
||||
intDatagramLength = stringDatagram.length();
|
||||
|
||||
while(s_StopSAD == false) {
|
||||
|
||||
//---- IP4 ---
|
||||
cycleStart();
|
||||
{
|
||||
fos::MutexGuard o_LockSocket(s_MutexSocket);
|
||||
if (s_SPITSocket == NULL) {
|
||||
return;
|
||||
}
|
||||
s_SPITSocket->getAdaptersInfo();
|
||||
|
||||
for (int i = 0; i < SPITSocket::getIPAddressCount(); i++) {
|
||||
o_IPAddress = SPITSocket::getIPAddress(i);
|
||||
if (o_IPAddress == NULL) break;
|
||||
stringBroadcastIP4 = s_SPITSocket->getBroadcastIP4(o_IPAddress);
|
||||
if (s_SPITSocket->sendMessageTo(SPITSocket::FAMILY_IP4, stringBroadcastIP4, SERVERAUTODETECT_PORT, stringDatagram.data(), intDatagramLength) < 0) {
|
||||
}
|
||||
else {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if (s_StopSAD == true) {
|
||||
return;
|
||||
}
|
||||
//wait for 3000 milliseconds or
|
||||
//s_StopAutoDetectServer == true see stopAutoDetectServer
|
||||
fos::MutexGuard o_LockWait(s_MutexSAD);
|
||||
boolDone = s_ConditionStopSAD.take(3000);
|
||||
if (not boolDone) // timeout occurred
|
||||
{
|
||||
s_StopSAD = true;
|
||||
}
|
||||
if (s_StopSAD == true) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
cycleEnd();
|
||||
}
|
||||
int intTest = 0;
|
||||
}
|
||||
|
||||
void SAD_Client::receiveAutoDetectServer_IP4() {
|
||||
char buffer[512+1];
|
||||
int intReceivedByteCount;
|
||||
string stringMessage;
|
||||
string stringServerIP;
|
||||
|
||||
int intIndex = 0;
|
||||
int intPos = 0;
|
||||
int intOldPos = 0;
|
||||
string stringPart;
|
||||
string stringParts[9];
|
||||
|
||||
//sendAutoDetectServer will bind the socket to a port
|
||||
//wait a while to be sendAutoDetectServer first
|
||||
fos::Task::delayMs(500);
|
||||
|
||||
while (s_StopSAD == false) {
|
||||
{
|
||||
fos::MutexGuard o_LockIP4(s_MutexSocket);
|
||||
if (s_SPITSocket == NULL) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
intReceivedByteCount = s_SPITSocket->receiveMessage(SPITSocket::FAMILY_IP4, buffer, 512, &stringServerIP);
|
||||
if (intReceivedByteCount <= 0) {
|
||||
if (s_StopSAD == false) {
|
||||
//To reduce the processor load if a socket error occurs, we let the thread sleep a bit.
|
||||
fos::Task::delayMs(100);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (s_StopSAD == true) {
|
||||
return;
|
||||
}
|
||||
//add null chararcter
|
||||
if (intReceivedByteCount < 512) {
|
||||
buffer[intReceivedByteCount] = '\0';
|
||||
}
|
||||
else {
|
||||
buffer[512] = '\0';
|
||||
}
|
||||
stringMessage = buffer;
|
||||
//Check if precode is ok
|
||||
if (stringMessage.find(SERVERAUTODETECT_PRECODE.data(),0) != 0) {
|
||||
return;
|
||||
}
|
||||
intIndex = 0;
|
||||
intPos = 0;
|
||||
intOldPos = 0;
|
||||
while(intIndex < 8) {
|
||||
intPos = stringMessage.find(SERVERAUTODETECT_SEPERATOR.data(),intOldPos);
|
||||
if (intPos == string::npos) {
|
||||
stringPart = "";
|
||||
}
|
||||
else {
|
||||
stringPart = stringMessage.substr(intOldPos, intPos-intOldPos);
|
||||
intOldPos = intPos + SERVERAUTODETECT_SEPERATOR.length();
|
||||
}
|
||||
stringParts[intIndex] = stringPart;
|
||||
intIndex++;
|
||||
}
|
||||
stringParts[8] = stringServerIP;
|
||||
SAD_DetectedServer::addDetectedServer(true, stringParts[SAD_DetectedServer::PARAMINDEX_ID], stringParts);
|
||||
}
|
||||
int inTest = 0;
|
||||
}
|
||||
|
||||
void SAD_Client::cycleStart() {
|
||||
{
|
||||
fos::MutexGuard o_UniqueLock(s_MutexSAD);
|
||||
if (s_StopSAD == true) return;
|
||||
}
|
||||
// printf("SAD CYCLE START\n");
|
||||
SAD_DetectedServer::cycleStart();
|
||||
}
|
||||
void SAD_Client::cycleEnd() {
|
||||
{
|
||||
fos::MutexGuard o_UniqueLock(s_MutexSAD);
|
||||
if (s_StopSAD == true) return;
|
||||
}
|
||||
SAD_DetectedServer::cycleEnd(s_SADClientInterface);
|
||||
// printf("SAD CYCLE END\n");
|
||||
}
|
||||
@@ -12,9 +12,11 @@
|
||||
* Created on 4. Mai 2019, 13:51
|
||||
*/
|
||||
|
||||
#include "SAD_DetectedServer.h"
|
||||
#include "SAD_DetectedServer.hpp"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char * TAG = "SpitSrv";
|
||||
|
||||
fos::Mutex SAD_DetectedServer::s_MutexDetectedServer;
|
||||
unordered_map<string, SAD_DetectedServer*> SAD_DetectedServer::s_DetectedServers;
|
||||
|
||||
SAD_DetectedServer::~SAD_DetectedServer() {
|
||||
@@ -27,47 +29,19 @@ SAD_DetectedServer::SAD_DetectedServer() {
|
||||
m_ClientIP = "";
|
||||
m_Port = "";
|
||||
m_Additions = "";
|
||||
m_IPIsLoopback = false;
|
||||
m_IPIsIP4 = false;
|
||||
m_IPIsIP6 = false;
|
||||
}
|
||||
|
||||
void SAD_DetectedServer::setID(string p_ID) {
|
||||
m_ID = p_ID;
|
||||
}
|
||||
void SAD_DetectedServer::setName(string p_Name) {
|
||||
m_Name = p_Name;
|
||||
}
|
||||
void SAD_DetectedServer::setComputerName(string p_ComputerName) {
|
||||
m_ComputerName = p_ComputerName;
|
||||
}
|
||||
void SAD_DetectedServer::setIP(string p_IP) {
|
||||
m_IP = p_IP;
|
||||
}
|
||||
void SAD_DetectedServer::setClientIP(string p_IP) {
|
||||
m_ClientIP = p_IP;
|
||||
}
|
||||
void SAD_DetectedServer::setPort(string p_Port) {
|
||||
m_Port = p_Port;
|
||||
}
|
||||
void SAD_DetectedServer::setAdditions(string p_Additions) {
|
||||
m_Additions = p_Additions;
|
||||
}
|
||||
|
||||
void SAD_DetectedServer::cycleStart() {
|
||||
{
|
||||
fos::MutexGuard o_lock(s_MutexDetectedServer);
|
||||
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
|
||||
i->second->startCycleSAD();
|
||||
}
|
||||
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
|
||||
i->second->startCycleSAD();
|
||||
}
|
||||
}
|
||||
void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
|
||||
void SAD_DetectedServer::cycleEnd(I_SAD_Client& sADClientInterface) {
|
||||
list<SAD_DetectedServer*> o_LostServers;
|
||||
list<SAD_DetectedServer*> o_NewServers;
|
||||
SAD_DetectedServer* ptrDetectedServer;
|
||||
{
|
||||
fos::MutexGuard o_lock(s_MutexDetectedServer);
|
||||
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
|
||||
ptrDetectedServer = i->second;
|
||||
if (ptrDetectedServer->getFoundAgain() == false) {
|
||||
@@ -80,7 +54,6 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
|
||||
ptrDetectedServer->endCycleSAD();
|
||||
}
|
||||
else if (ptrDetectedServer->getChanged()) {
|
||||
// p_SADClientInterface->
|
||||
ptrDetectedServer->endCycleSAD();
|
||||
}
|
||||
else {
|
||||
@@ -91,12 +64,10 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
|
||||
//Notify client interface about lost servers
|
||||
for (auto i = o_LostServers.begin(); i != o_LostServers.end(); i++) {
|
||||
ptrDetectedServer = *i;
|
||||
if (p_SADClientInterface != NULL) {
|
||||
p_SADClientInterface->eventServerLost(ptrDetectedServer->getID(), ptrDetectedServer->getName(),
|
||||
sADClientInterface.eventServerLost(ptrDetectedServer->getID(), ptrDetectedServer->getName(),
|
||||
ptrDetectedServer->getComputerName(), ptrDetectedServer->getIP(), ptrDetectedServer->getPort(), ptrDetectedServer->getAdditions(),
|
||||
ptrDetectedServer->getClientIP(),
|
||||
ptrDetectedServer->getIpIsLoopback(), ptrDetectedServer->getIpIsIP4(), ptrDetectedServer->getIpIsIP6());
|
||||
}
|
||||
ptrDetectedServer->getIpIsIP4());
|
||||
}
|
||||
//Remove lost servers
|
||||
for (auto i = o_LostServers.begin(); i != o_LostServers.end(); i++) {
|
||||
@@ -107,12 +78,10 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
|
||||
|
||||
for (auto i = o_NewServers.begin(); i != o_NewServers.end(); i++) {
|
||||
ptrDetectedServer = *i;
|
||||
if (p_SADClientInterface != NULL) {
|
||||
p_SADClientInterface->eventServerDetected(ptrDetectedServer->getID(), ptrDetectedServer->getName(),
|
||||
sADClientInterface.eventServerDetected(ptrDetectedServer->getID(), ptrDetectedServer->getName(),
|
||||
ptrDetectedServer->getComputerName(), ptrDetectedServer->getIP(), ptrDetectedServer->getPort(), ptrDetectedServer->getAdditions(),
|
||||
ptrDetectedServer->getClientIP(),
|
||||
ptrDetectedServer->getIpIsLoopback(), ptrDetectedServer->getIpIsIP4(), ptrDetectedServer->getIpIsIP6());
|
||||
}
|
||||
ptrDetectedServer->getIpIsIP4());
|
||||
}
|
||||
o_NewServers.clear();
|
||||
|
||||
@@ -126,7 +95,6 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) {
|
||||
void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, string p_ServerParams[]) {
|
||||
SAD_DetectedServer* ptrDetectedServer;
|
||||
{
|
||||
fos::MutexGuard o_Lock(s_MutexDetectedServer);
|
||||
auto i = s_DetectedServers.find(p_ServerID);
|
||||
if (i != s_DetectedServers.end()) {
|
||||
ptrDetectedServer = i->second;
|
||||
@@ -138,7 +106,6 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri
|
||||
}
|
||||
ptrDetectedServer->setFoundAgain();
|
||||
|
||||
if (ptrDetectedServer->getIpIsLoopback() == false) {
|
||||
//second prefer IP4
|
||||
if (p_IsIP4 == true) {
|
||||
ptrDetectedServer->setIP(p_ServerParams[PARAMINDEX_IP]);
|
||||
@@ -147,10 +114,8 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri
|
||||
}
|
||||
else if (ptrDetectedServer->getIpIsIP4() == false){
|
||||
ptrDetectedServer->setIP(p_ServerParams[PARAMINDEX_IP]);
|
||||
ptrDetectedServer->setIPIsIP6(true);
|
||||
ptrDetectedServer->setClientIP(p_ServerParams[PARAMINDEX_CLIENTIP]);
|
||||
}
|
||||
}
|
||||
ptrDetectedServer->setName(p_ServerParams[PARAMINDEX_NAME]);
|
||||
ptrDetectedServer->setComputerName(p_ServerParams[PARAMINDEX_COMPUTERNAME]);
|
||||
ptrDetectedServer->setPort(p_ServerParams[PARAMINDEX_PORT]);
|
||||
@@ -158,7 +123,6 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri
|
||||
|
||||
s_DetectedServers[ptrDetectedServer->getID()] = ptrDetectedServer;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Used by SAD_Client to remove a SPITServer - don't use this manually
|
||||
@@ -167,7 +131,6 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri
|
||||
void SAD_DetectedServer::removeDetectedServer(SAD_DetectedServer* p_DetectedServer) {
|
||||
SAD_DetectedServer* o_DetectedServer;
|
||||
{
|
||||
fos::MutexGuard o_Lock(s_MutexDetectedServer);
|
||||
auto i = s_DetectedServers.find(p_DetectedServer->getID());
|
||||
if (i != s_DetectedServers.end()) {
|
||||
o_DetectedServer = i->second;
|
||||
@@ -183,7 +146,6 @@ void SAD_DetectedServer::removeDetectedServer(SAD_DetectedServer* p_DetectedServ
|
||||
void SAD_DetectedServer::clearDetectedServers() {
|
||||
SAD_DetectedServer* ptrDetectedServer;
|
||||
{
|
||||
fos::MutexGuard o_Lock(s_MutexDetectedServer);
|
||||
for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) {
|
||||
ptrDetectedServer = i->second;
|
||||
i = s_DetectedServers.erase(i);
|
||||
|
||||
@@ -13,15 +13,13 @@
|
||||
*/
|
||||
|
||||
#include "SPITObject.h"
|
||||
#include "_SPITMACROS.h"
|
||||
#include "SPITSocket.h"
|
||||
|
||||
unordered_map<string, SPITObject*> SPITObject::s_SPITObjects;
|
||||
unordered_map<string, SPITObject*> SPITObject::s_SPITObjectsRemote;
|
||||
fos::Mutex SPITObject::s_MutexSPITObject;
|
||||
|
||||
SPITObject* SPITObject::getSPITObject(string p_ID) {
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
auto i = s_SPITObjects.find(p_ID);
|
||||
if (i == s_SPITObjects.end()) {
|
||||
return NULL;
|
||||
@@ -37,7 +35,6 @@
|
||||
*/
|
||||
const SPITObject& SPITObject::getSPITObjectRef(string p_ID) {
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
auto i = s_SPITObjects.find(p_ID);
|
||||
if (i == s_SPITObjects.end()) {
|
||||
throw "SPITObject no SPITObject with given id";
|
||||
@@ -48,7 +45,6 @@
|
||||
|
||||
SPITObject* SPITObject::getSPITObjectRemote(string p_ID) {
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
auto i = s_SPITObjectsRemote.find(p_ID);
|
||||
if (i == s_SPITObjectsRemote.end()) {
|
||||
return NULL;
|
||||
@@ -64,7 +60,6 @@
|
||||
*/
|
||||
const SPITObject& SPITObject::getSPITObjectRemoteRef(string p_ID) {
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
auto i = s_SPITObjectsRemote.find(p_ID);
|
||||
if (i == s_SPITObjectsRemote.end()) {
|
||||
throw "Remote SPITObject no SPITObject with given id";
|
||||
@@ -76,7 +71,6 @@
|
||||
bool SPITObject::addSPITObject(SPITObject* p_ptrSPITObject) {
|
||||
if (p_ptrSPITObject == NULL) return false;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
s_SPITObjects[p_ptrSPITObject->getID()] = p_ptrSPITObject;
|
||||
return true;
|
||||
}
|
||||
@@ -84,7 +78,6 @@
|
||||
bool SPITObject::addSPITObjectRemote(SPITObject* p_ptrSPITObject) {
|
||||
if (p_ptrSPITObject == NULL) return false;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
s_SPITObjectsRemote[p_ptrSPITObject->getID()] = p_ptrSPITObject;
|
||||
return true;
|
||||
}
|
||||
@@ -92,7 +85,6 @@
|
||||
bool SPITObject::removeSPITObject(SPITObject* p_ptrSPITObject) {
|
||||
if (p_ptrSPITObject == NULL) return false;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
auto i = s_SPITObjects.find(p_ptrSPITObject->getID());
|
||||
if (i == s_SPITObjects.end()) {
|
||||
return false;
|
||||
@@ -104,7 +96,6 @@
|
||||
bool SPITObject::removeSPITObjectRemote(SPITObject* p_ptrSPITObject) {
|
||||
if (p_ptrSPITObject == NULL) return false;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
auto i = s_SPITObjectsRemote.find(p_ptrSPITObject->getID());
|
||||
if (i == s_SPITObjectsRemote.end()) {
|
||||
return false;
|
||||
@@ -123,7 +114,6 @@
|
||||
SPITObject* SPITObject::getSPITObject(int p_Index) {
|
||||
SPITObject* ptrSPITObject = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
if (p_Index >= 0 && p_Index < s_SPITObjects.size()) {
|
||||
unordered_map<string, SPITObject*>::iterator i = next(s_SPITObjects.begin(), p_Index);
|
||||
ptrSPITObject = i->second;
|
||||
@@ -143,7 +133,6 @@
|
||||
const SPITObject& SPITObject::getSPITObjectRef(int p_Index) {
|
||||
SPITObject* ptrSPITObject = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
if (p_Index >= 0 && p_Index < s_SPITObjects.size()) {
|
||||
unordered_map<string, SPITObject*>::iterator i = next(s_SPITObjects.begin(), p_Index);
|
||||
ptrSPITObject = i->second;
|
||||
@@ -157,7 +146,6 @@
|
||||
SPITObject* SPITObject::getSPITObjectRemote(int p_Index) {
|
||||
SPITObject* ptrSPITObject = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
if (p_Index >= 0 && p_Index < s_SPITObjectsRemote.size()) {
|
||||
unordered_map<string, SPITObject*>::iterator i = next(s_SPITObjectsRemote.begin(), p_Index);
|
||||
ptrSPITObject = i->second;
|
||||
@@ -177,7 +165,6 @@
|
||||
const SPITObject& SPITObject::getSPITObjectRemoteRef(int p_Index) {
|
||||
SPITObject* ptrSPITObject = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
if (p_Index >= 0 && p_Index < s_SPITObjectsRemote.size()) {
|
||||
unordered_map<string, SPITObject*>::iterator i = next(s_SPITObjectsRemote.begin(), p_Index);
|
||||
ptrSPITObject = i->second;
|
||||
@@ -192,7 +179,6 @@
|
||||
void SPITObject::clearSPITObjects() {
|
||||
SPITObject* ptrSPITObject;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) {
|
||||
ptrSPITObject = i->second;
|
||||
i = s_SPITObjects.erase(i);
|
||||
@@ -206,7 +192,6 @@
|
||||
void SPITObject::clearSPITObjectsRemote() {
|
||||
SPITObject* ptrSPITObject;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
for (auto i = s_SPITObjectsRemote.begin(); i != s_SPITObjectsRemote.end(); i++) {
|
||||
ptrSPITObject = i->second;
|
||||
i = s_SPITObjectsRemote.erase(i);
|
||||
@@ -227,7 +212,6 @@
|
||||
void SPITObject::setConfirmedFalse() {
|
||||
SPITObject* ptrSPITObject;
|
||||
{
|
||||
fos::MutexGuard o_LockObject(s_MutexSPITObject);
|
||||
for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) {
|
||||
ptrSPITObject = i->second;
|
||||
ptrSPITObject->setConfirmed(false);
|
||||
|
||||
@@ -14,12 +14,10 @@
|
||||
|
||||
#include "SPITPlayer.h"
|
||||
|
||||
fos::Mutex SPITPlayer::s_Mutex;
|
||||
unordered_map<string, SPITPlayer*> SPITPlayer::s_SPITPlayers;
|
||||
|
||||
SPITPlayer* SPITPlayer::getSPITPlayer(string p_PlayerID) {
|
||||
{
|
||||
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
|
||||
auto i = s_SPITPlayers.find(p_PlayerID);
|
||||
if (i == s_SPITPlayers.end()) {
|
||||
s_SPITPlayers[p_PlayerID] = new SPITPlayer(p_PlayerID);
|
||||
@@ -33,7 +31,6 @@ SPITPlayer* SPITPlayer::getSPITPlayer(string p_PlayerID) {
|
||||
void SPITPlayer::clearPlayers() {
|
||||
SPITPlayer* ptrSPITPlayer;
|
||||
{
|
||||
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
|
||||
for (auto i = s_SPITPlayers.begin(); i != s_SPITPlayers.end(); i++) {
|
||||
ptrSPITPlayer = i->second;
|
||||
i = s_SPITPlayers.erase(i);
|
||||
@@ -54,7 +51,6 @@ SPITPlayer::SPITPlayer(string p_PlayerID) {
|
||||
|
||||
SPITPlayer::~SPITPlayer() {
|
||||
{
|
||||
fos::MutexGuard o_LockPlayer(s_Mutex);
|
||||
PlayObject* ptrPlayObject;
|
||||
for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) {
|
||||
ptrPlayObject = i->second;
|
||||
@@ -92,7 +88,6 @@ unordered_map<string, SPITPlayer::PlayObject*>* SPITPlayer::getPlayObjects() {
|
||||
SPITPlayer::PlayObject* SPITPlayer::addSPITObject_Playing (string p_SPITObjectID) {
|
||||
PlayObject* ptrPlayObject;
|
||||
{
|
||||
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
|
||||
auto i = m_PlayObjects.find(p_SPITObjectID);
|
||||
if (i == m_PlayObjects.end()) {
|
||||
ptrPlayObject = new PlayObject(this, p_SPITObjectID);
|
||||
@@ -111,7 +106,6 @@ SPITPlayer::PlayObject* SPITPlayer::addSPITObject_Playing (string p_SPITObjectID
|
||||
void SPITPlayer::startPlaySequence() {
|
||||
PlayObject* ptrPlayObject;
|
||||
{
|
||||
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
|
||||
for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) {
|
||||
ptrPlayObject = i->second;
|
||||
ptrPlayObject->startPlaySequence();
|
||||
@@ -131,7 +125,6 @@ SPITPlayer::PlayObject* SPITPlayer::getPlayObject(string p_SPITObjectID) {
|
||||
void SPITPlayer::endPlaySequence() {
|
||||
PlayObject* ptrPlayObject;
|
||||
{
|
||||
fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex());
|
||||
for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) {
|
||||
ptrPlayObject = i->second;
|
||||
if (ptrPlayObject->getIsPlayed() == false) {
|
||||
|
||||
+265
-181
@@ -5,197 +5,281 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* SPITSOcket is the base class for WindowsSocket and UnixSocket.
|
||||
* File: SPITSocket.cpp
|
||||
* Unix implementation of a SPITSocket.
|
||||
*
|
||||
* File: UnixSocket.cpp
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 12. April 2019, 15:40
|
||||
* Created on 12. April 2019, 15:58
|
||||
*/
|
||||
|
||||
#include "SPITSocket.h"
|
||||
#include "Task.hpp"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp/uuid.h"
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
//----- initialize static members -----
|
||||
fos::Mutex SPITSocket::s_MutexIPAddresses;
|
||||
unordered_map<string, SPITSocket::IPAddress*> SPITSocket::s_IPAddresses;
|
||||
int SPITSocket::FAMILY_IP4 = 2;
|
||||
int SPITSocket::FAMILY_IP6 = 23;
|
||||
/**
|
||||
* Creates socket. If there exist some, they will be closed.
|
||||
* @return
|
||||
*/
|
||||
bool SPITSocket::createSocket_UDP() {
|
||||
bool boolSuccess = true;
|
||||
int intResult = -1;
|
||||
int intOptionValue4 = 1;
|
||||
|
||||
// ip4
|
||||
if (m_UDPSocketIP4 != INVALID_SOCKET) {
|
||||
close(m_UDPSocketIP4);
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
m_UDPSocketIP4 = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (m_UDPSocketIP4 == INVALID_SOCKET) {
|
||||
boolSuccess = false;
|
||||
} else {
|
||||
intResult = setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_REUSEADDR, (void *)&intOptionValue4, sizeof(intOptionValue4));
|
||||
}
|
||||
|
||||
// // set the m_UDPSocketIP4 in NON BLOCKING mode because
|
||||
// // if the UDPSockets are in blocking mode the receiveMessage does not return in every constellation
|
||||
// int intFlags;
|
||||
// intFlags = fcntl(m_UDPSocketIP4, F_GETFL);
|
||||
// fcntl(m_UDPSocketIP4, F_SETFL, intFlags | O_NONBLOCK);
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 1; // seconds
|
||||
tv.tv_usec = 0; // microseconds (0.0 sec)
|
||||
|
||||
// This sets the receive timeout
|
||||
setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
|
||||
|
||||
SPITSocket::SPITSocket() {
|
||||
m_ServerIP = "";
|
||||
m_ServerPort = "";
|
||||
m_ClientIP = "";
|
||||
m_ClientPort = "";
|
||||
m_BoundClientPort = "";
|
||||
m_BoundClientFamily = 0;
|
||||
m_IsBound = false;
|
||||
if (intResult < 0) {
|
||||
boolSuccess = false;
|
||||
}
|
||||
return boolSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds the socket to the client's ip address and port. The socket option SO_REUSEADDR is set to true.
|
||||
* @param p_ClientIP ip address of the client
|
||||
* @param p_ClientPort port of the client.
|
||||
* if p_Port == "0" any free port will be used
|
||||
* @return -1 id bind fails, o if bind is ok
|
||||
*
|
||||
*/
|
||||
int SPITSocket::bindSocket(string p_ClientIP, string p_ClientPort) {
|
||||
addrinfo *clientAI;
|
||||
struct sockaddr_in *ptrIP4SocketAddress;
|
||||
struct sockaddr_in o_IP4SocketAddress;
|
||||
int intSocklen;
|
||||
string stringClientPort;
|
||||
if (p_ClientPort.length() <= 0)
|
||||
stringClientPort = "0";
|
||||
else
|
||||
stringClientPort = p_ClientPort;
|
||||
// int intResult = SOCKET_ERROR;
|
||||
int intResult = 0;
|
||||
if (getaddrinfo(p_ClientIP.data(), p_ClientPort.data(), NULL, &clientAI) != 0) {
|
||||
return -1;
|
||||
}
|
||||
switch (clientAI->ai_family) {
|
||||
case AF_INET:
|
||||
if (m_UDPSocketIP4 == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
ptrIP4SocketAddress = reinterpret_cast<sockaddr_in *>(clientAI->ai_addr);
|
||||
intSocklen = sizeof(*ptrIP4SocketAddress);
|
||||
// copy the ptrIP4SocketAddress, may be not filled correct by getaddrinfo
|
||||
memset(&o_IP4SocketAddress, 0, intSocklen);
|
||||
o_IP4SocketAddress.sin_addr = ptrIP4SocketAddress->sin_addr;
|
||||
o_IP4SocketAddress.sin_family = ptrIP4SocketAddress->sin_family;
|
||||
// o_IP4SocketAddress.sin_zero = ptrIP4SocketAddress->sin_zero;
|
||||
o_IP4SocketAddress.sin_port = ptrIP4SocketAddress->sin_port;
|
||||
intResult = bind(m_UDPSocketIP4, (const sockaddr *)(ptrIP4SocketAddress), (socklen_t)intSocklen);
|
||||
if (intResult < 0) {
|
||||
break;
|
||||
}
|
||||
setBoundClientFamily(AF_INET);
|
||||
if (intResult == 0) {
|
||||
if (getsockname(m_UDPSocketIP4, (sockaddr *)ptrIP4SocketAddress, (socklen_t *)&intSocklen) != -1) {
|
||||
setBoundClientPort(to_string((int)ptrIP4SocketAddress->sin_port));
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
setBoundClientFamily(0);
|
||||
intResult = -1;
|
||||
break;
|
||||
}
|
||||
freeaddrinfo(clientAI);
|
||||
|
||||
if (intResult == -1) {
|
||||
setIsBound(false);
|
||||
} else {
|
||||
setIsBound(true);
|
||||
}
|
||||
return intResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether the sockat are brodcast sockets or not. Set the sockopt SO_BROADCAST to 0 or 1
|
||||
* @param p_Broadcast true SO_BROADCAST is set
|
||||
* false = SO_BROADCAST is not set
|
||||
* @return 0 = ok, -1 = error
|
||||
*/
|
||||
int SPITSocket::setBroadcast(bool p_Broadcast) {
|
||||
int intResult;
|
||||
int intOptionValue = 1;
|
||||
u_char charOptionValue = 0x01;
|
||||
// IP4
|
||||
if (p_Broadcast) {
|
||||
intOptionValue = 1;
|
||||
} else {
|
||||
intOptionValue = 0;
|
||||
}
|
||||
intResult = setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_BROADCAST, &intOptionValue, sizeof(int));
|
||||
// intResult = setsockopt(m_UDPSocketIP4, IPPROTO_IP, IP_MULTICAST_LOOP, (void*)&charOptionValue, sizeof(charOptionValue));
|
||||
|
||||
return intResult;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param p_InetFamily AF_INET or AF_INET6
|
||||
* @param p_ServerIP the ip address of the server
|
||||
* @param p_ServerPort the port of the server
|
||||
* @param p_MessageBytes buffer containing the message
|
||||
* @param p_MessageLength the length of the message
|
||||
* @return -1 send fails
|
||||
* 0 send is done
|
||||
*/
|
||||
int SPITSocket::sendMessageTo(int p_InetFamily, string p_ServerIP, string p_ServerPort, const char *p_MessageBytes, int p_MessageLength) {
|
||||
if (m_UDPSocketIP4 == INVALID_SOCKET)
|
||||
return -1;
|
||||
struct addrinfo *ptrAddressInfo;
|
||||
struct sockaddr_in *ptrIP4SocketAddress;
|
||||
struct sockaddr_in o_IP4SocketAddress;
|
||||
int intSocklen;
|
||||
int intResult = -1;
|
||||
|
||||
// prepare AddressInfo
|
||||
if (getaddrinfo(p_ServerIP.data(), p_ServerPort.data(), NULL, &ptrAddressInfo) != 0) {
|
||||
return -1;
|
||||
}
|
||||
switch (ptrAddressInfo->ai_family) {
|
||||
case AF_INET:
|
||||
ptrIP4SocketAddress = reinterpret_cast<sockaddr_in *>(ptrAddressInfo->ai_addr);
|
||||
intSocklen = sizeof(*ptrIP4SocketAddress);
|
||||
if (m_UDPSocketIP4 != INVALID_SOCKET) {
|
||||
// copy the ptrIP4SocketAddress, may be not filled correct by getaddrinfo
|
||||
memset(&o_IP4SocketAddress, 0, intSocklen);
|
||||
o_IP4SocketAddress.sin_addr = ptrIP4SocketAddress->sin_addr;
|
||||
o_IP4SocketAddress.sin_family = ptrIP4SocketAddress->sin_family;
|
||||
o_IP4SocketAddress.sin_port = ptrIP4SocketAddress->sin_port;
|
||||
intResult = sendto(m_UDPSocketIP4, p_MessageBytes, p_MessageLength, 0, (const sockaddr *)&o_IP4SocketAddress, (socklen_t)intSocklen);
|
||||
if (intResult < 0) {
|
||||
printf("UnixSocket::sendMessageTo IP4 sendto %s FAILED %s\n", p_ServerIP.data(), strerror(errno));
|
||||
printf("UnixSocket::sendMessageTo ret=%d %s / %s / %d\n", intResult,
|
||||
p_ServerPort.data(), p_MessageBytes, p_MessageLength);
|
||||
}
|
||||
freeaddrinfo(ptrAddressInfo);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
intResult = -1;
|
||||
}
|
||||
|
||||
if (intResult == -1) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* Call this for receiving a message
|
||||
* This will block until a message is received or the underlying socket is closed
|
||||
* @param p_InetFamily either AF_INET or AF_INET6<br>
|
||||
* The SPITSocket creates two udp sockets, one for IP4 and one for IP6
|
||||
* This parameter decides from which socket should a message recieved
|
||||
* @param p_Buffer the buffer containing the received message later.
|
||||
* @param p_BufferLength the size of the buffer
|
||||
* @param p_ServerIP a pointer to a string, when a messages is received the string will contain the ip address of the sender/server
|
||||
* if p_ServerIP is NULL - no detection of the server will be done.
|
||||
* @return
|
||||
*/
|
||||
int SPITSocket::receiveMessage(int p_InetFamily, char *p_Buffer, int p_BufferLength, string *p_ServerIP) {
|
||||
int intResult = -1;
|
||||
// ADDRINFO *AI;
|
||||
struct sockaddr_in o_IP4SocketAddress;
|
||||
int intSocklen;
|
||||
|
||||
switch (p_InetFamily) {
|
||||
case AF_INET:
|
||||
if (m_UDPSocketIP4 == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
intSocklen = sizeof(o_IP4SocketAddress);
|
||||
memset(&o_IP4SocketAddress, 0, intSocklen);
|
||||
intResult = recvfrom(m_UDPSocketIP4, (void *)p_Buffer, p_BufferLength, 0 , (sockaddr *)&o_IP4SocketAddress, (socklen_t *)&intSocklen);
|
||||
if (intResult >= 0) {
|
||||
if (p_ServerIP != NULL) {
|
||||
char bufferIP4[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &(o_IP4SocketAddress.sin_addr), bufferIP4, intSocklen);
|
||||
*p_ServerIP = bufferIP4;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return intResult;
|
||||
}
|
||||
|
||||
|
||||
SPITSocket::~SPITSocket() {
|
||||
{
|
||||
fos::MutexGuard o_LockSocket(m_MutexSocket);
|
||||
m_IsBound = false;
|
||||
SPITSocket::IPAddress SPITSocket::getAdaptersInfo() {
|
||||
// iterate over active interfaces, and print out IPs
|
||||
esp_netif_t *netif = NULL;
|
||||
while ((netif = esp_netif_next_unsafe(netif)) != NULL) {
|
||||
ESP_LOGI("SPIT", "Connected to %s", esp_netif_get_desc(netif));
|
||||
esp_netif_ip_info_t ip;
|
||||
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
|
||||
ESP_LOGI("SPIT", "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));
|
||||
ESP_LOGI("SPIT", "- IPv4 netmask: " IPSTR ",", IP2STR(&ip.netmask));
|
||||
char buffer[64];
|
||||
|
||||
sprintf(buffer, IPSTR, IP2STR(&ip.ip));
|
||||
string stringIP4 = buffer;
|
||||
sprintf(buffer, IPSTR, IP2STR(&ip.netmask));
|
||||
string stringSubnet = buffer;
|
||||
return {stringIP4, stringSubnet};
|
||||
}
|
||||
return {"0.0.0.0", "0.0.0.0"};
|
||||
}
|
||||
|
||||
string createGUID() {
|
||||
// uuid_t id;
|
||||
// uuid_generate(id);
|
||||
// char *guid_string = new char[100];
|
||||
// uuid_unparse(id, guid_string);
|
||||
// string stringGUID = guid_string;
|
||||
// return stringGUID;
|
||||
|
||||
uuid_t uu;
|
||||
char uu_str[UUID_STR_LEN];
|
||||
|
||||
uuid_generate(uu);
|
||||
uuid_unparse(uu, uu_str);
|
||||
string stringGUID = uu_str;
|
||||
return stringGUID;
|
||||
}
|
||||
/** The name of the client's host computer*/
|
||||
string getClientHostName() {
|
||||
string stringHostName = "";
|
||||
const char * hostname;
|
||||
esp_netif_get_hostname(esp_netif_get_default_netif(), &hostname);
|
||||
if (hostname != nullptr) {
|
||||
stringHostName = hostname;
|
||||
}
|
||||
}
|
||||
void SPITSocket::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;
|
||||
}
|
||||
string SPITSocket::getServerIP() {
|
||||
return m_ServerIP;
|
||||
}
|
||||
string SPITSocket::getServerPort(){
|
||||
return m_ServerPort;
|
||||
}
|
||||
string SPITSocket::getClientIP() {
|
||||
return m_ClientIP;
|
||||
}
|
||||
string SPITSocket::getClientPort() {
|
||||
return m_ClientPort;
|
||||
}
|
||||
string SPITSocket::getBoundClientPort() {
|
||||
return m_BoundClientPort;
|
||||
}
|
||||
void SPITSocket::setBoundClientPort(string p_Port) {
|
||||
m_BoundClientPort = p_Port;
|
||||
}
|
||||
int SPITSocket::getBoundClientFamily() {
|
||||
return m_BoundClientFamily;
|
||||
}
|
||||
void SPITSocket::setBoundClientFamily(int p_Family) {
|
||||
m_BoundClientFamily = p_Family;
|
||||
}
|
||||
|
||||
void SPITSocket::setIsBound(bool p_IsBound) {
|
||||
{
|
||||
fos::MutexGuard o_LockSocket(m_MutexSocket);
|
||||
m_IsBound = p_IsBound;
|
||||
}
|
||||
}
|
||||
bool SPITSocket::getIsBound() {
|
||||
return m_IsBound;
|
||||
}
|
||||
|
||||
int SPITSocket::sendMessage(const char* p_MessageBytes, int p_MessageLength) {
|
||||
if (m_ServerPort.length() <= 0) return -1;
|
||||
int intResult;
|
||||
{
|
||||
fos::MutexGuard o_LockSocket(m_MutexSocket);
|
||||
if (m_IsBound == false) return -1;
|
||||
intResult = sendMessageTo(m_BoundClientFamily, m_ServerIP, m_ServerPort, p_MessageBytes, p_MessageLength);
|
||||
}
|
||||
return intResult;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//---------- IP Adresses ---------
|
||||
void SPITSocket::addIPAddress(string p_IPAddress, string p_SubnetMask) {
|
||||
IPAddress* o_IPAddress;
|
||||
{
|
||||
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
|
||||
auto i = s_IPAddresses.find(p_IPAddress);
|
||||
if (i != s_IPAddresses.end()) {
|
||||
o_IPAddress = i->second;
|
||||
}
|
||||
else {
|
||||
o_IPAddress = new IPAddress();
|
||||
}
|
||||
o_IPAddress->setIPAddress(p_IPAddress);
|
||||
o_IPAddress->setSubnetMask(p_SubnetMask);
|
||||
o_IPAddress->setFound(true);
|
||||
s_IPAddresses[o_IPAddress->getIPAddress()] = o_IPAddress;
|
||||
}
|
||||
}
|
||||
void SPITSocket::removeIPAddress(string p_IPAddress) {
|
||||
IPAddress* o_IPAddress;
|
||||
{
|
||||
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
|
||||
auto i = s_IPAddresses.find(p_IPAddress);
|
||||
if (i != s_IPAddresses.end()) {
|
||||
o_IPAddress = i->second;
|
||||
i = s_IPAddresses.erase(i);
|
||||
delete o_IPAddress;
|
||||
}
|
||||
}
|
||||
}
|
||||
int SPITSocket::getIPAddressCount() {
|
||||
return s_IPAddresses.size();
|
||||
}
|
||||
SPITSocket::IPAddress* SPITSocket::getIPAddress(int p_Index) {
|
||||
IPAddress* ptr_IPAddress = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
|
||||
if (p_Index <= s_IPAddresses.size()) {
|
||||
unordered_map<string, IPAddress*>::iterator i = next(s_IPAddresses.begin(), p_Index);
|
||||
ptr_IPAddress = i->second;
|
||||
}
|
||||
return ptr_IPAddress;
|
||||
}
|
||||
}
|
||||
void SPITSocket::clearIPAddresses() {
|
||||
IPAddress* o_IPAddress;
|
||||
{
|
||||
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
|
||||
for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) {
|
||||
o_IPAddress = i->second;
|
||||
i = s_IPAddresses.erase(i);
|
||||
delete o_IPAddress;
|
||||
if (i == s_IPAddresses.end()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SPITSocket::resetFoundIPAddresses() {
|
||||
IPAddress* o_IPAddress;
|
||||
{
|
||||
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
|
||||
for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) {
|
||||
o_IPAddress = i->second;
|
||||
o_IPAddress->setFound(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
void SPITSocket::removeNotFoundIPAddresses() {
|
||||
IPAddress* o_IPAddress;
|
||||
{
|
||||
fos::MutexGuard o_LockIP(s_MutexIPAddresses);
|
||||
for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) {
|
||||
o_IPAddress = i->second;
|
||||
if (o_IPAddress->getFound() == false) {
|
||||
i = s_IPAddresses.erase(i);
|
||||
delete o_IPAddress;
|
||||
if (i == s_IPAddresses.end()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//------------ IPAddress -------------------
|
||||
SPITSocket::IPAddress::IPAddress() {
|
||||
m_IPAddress = "";
|
||||
m_SubnetMask = "";
|
||||
m_Found = false;
|
||||
}
|
||||
SPITSocket::IPAddress::~IPAddress() {
|
||||
m_Found = false;
|
||||
}
|
||||
void SPITSocket::IPAddress::setIPAddress(const string& p_IPAddress) {
|
||||
m_IPAddress = p_IPAddress;
|
||||
}
|
||||
void SPITSocket::IPAddress::setSubnetMask(const string& p_SubnetMask) {
|
||||
m_SubnetMask = p_SubnetMask;
|
||||
}
|
||||
void SPITSocket::IPAddress::setFound(bool p_Found) {
|
||||
m_Found = p_Found;
|
||||
return stringHostName;;
|
||||
}
|
||||
|
||||
@@ -14,15 +14,15 @@
|
||||
|
||||
|
||||
#include "SPITType.h"
|
||||
#include "_SPITMACROS.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char * TAG = "SPIT";
|
||||
|
||||
unordered_map<string, SPITType*> SPITType::s_SPITTypes;
|
||||
unordered_map<string, SPITType*> SPITType::s_SPITTypesRemote;
|
||||
fos::Mutex SPITType::s_MutexSPITType;
|
||||
|
||||
SPITType* SPITType::getSPITType(string p_ID) {
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
auto i = s_SPITTypes.find(p_ID);
|
||||
if (i == s_SPITTypes.end()) {
|
||||
return NULL;
|
||||
@@ -38,7 +38,6 @@
|
||||
*/
|
||||
const SPITType& SPITType::getSPITTypeRef(string p_ID) {
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
auto i = s_SPITTypes.find(p_ID);
|
||||
if (i == s_SPITTypes.end()) {
|
||||
throw "SPITType no SPITType with given ID";
|
||||
@@ -49,7 +48,6 @@
|
||||
|
||||
SPITType* SPITType::getSPITTypeRemote(string p_ID) {
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
auto i = s_SPITTypesRemote.find(p_ID);
|
||||
if (i == s_SPITTypesRemote.end()) {
|
||||
return NULL;
|
||||
@@ -65,7 +63,6 @@
|
||||
*/
|
||||
const SPITType& SPITType::getSPITTypeRemoteRef(string p_ID) {
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
auto i = s_SPITTypesRemote.find(p_ID);
|
||||
if (i == s_SPITTypesRemote.end()) {
|
||||
throw "Remote SPITType no SPITType with given ID";
|
||||
@@ -77,7 +74,6 @@
|
||||
bool SPITType::addSPITType(SPITType* p_ptrSpitType) {
|
||||
if (p_ptrSpitType == NULL) return false;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
s_SPITTypes[p_ptrSpitType->getID()] = p_ptrSpitType;
|
||||
return true;
|
||||
}
|
||||
@@ -85,7 +81,6 @@
|
||||
bool SPITType::addSPITTypeRemote(SPITType* p_ptrSpitType) {
|
||||
if (p_ptrSpitType == NULL) return false;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
s_SPITTypesRemote[p_ptrSpitType->getID()] = p_ptrSpitType;
|
||||
return true;
|
||||
}
|
||||
@@ -93,7 +88,6 @@
|
||||
bool SPITType::removeSPITType(SPITType* p_ptrSpitType) {
|
||||
if (p_ptrSpitType == NULL) return false;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
auto i = s_SPITTypes.find(p_ptrSpitType->getID());
|
||||
if (i == s_SPITTypes.end()) {
|
||||
return false;
|
||||
@@ -105,7 +99,6 @@
|
||||
bool SPITType::removeSPITTypeRemote(SPITType* p_ptrSpitType) {
|
||||
if (p_ptrSpitType == NULL) return false;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
auto i = s_SPITTypesRemote.find(p_ptrSpitType->getID());
|
||||
if (i == s_SPITTypesRemote.end()) {
|
||||
return false;
|
||||
@@ -124,7 +117,6 @@
|
||||
SPITType* SPITType::getSPITType(int p_Index) {
|
||||
SPITType* ptrSPITType = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
if (p_Index < s_SPITTypes.size()) {
|
||||
unordered_map<string, SPITType*>::iterator i = next(s_SPITTypes.begin(), p_Index);
|
||||
ptrSPITType = i->second;
|
||||
@@ -141,7 +133,6 @@
|
||||
const SPITType& SPITType::getSPITTypeRef(int p_Index) {
|
||||
SPITType* ptrSPITType = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
if (p_Index >= 0 && p_Index < s_SPITTypes.size()) {
|
||||
unordered_map<string, SPITType*>::iterator i = next(s_SPITTypes.begin(), p_Index);
|
||||
ptrSPITType = i->second;
|
||||
@@ -155,7 +146,6 @@
|
||||
SPITType* SPITType::getSPITTypeRemote(int p_Index) {
|
||||
SPITType* ptrSPITType = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
if (p_Index < s_SPITTypesRemote.size()) {
|
||||
unordered_map<string, SPITType*>::iterator i = next(s_SPITTypesRemote.begin(), p_Index);
|
||||
ptrSPITType = i->second;
|
||||
@@ -172,7 +162,6 @@
|
||||
const SPITType& SPITType::getSPITTypeRemoteRef(int p_Index) {
|
||||
SPITType* ptrSPITType = NULL;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
if (p_Index >= 0 && p_Index < s_SPITTypesRemote.size()) {
|
||||
unordered_map<string, SPITType*>::iterator i = next(s_SPITTypesRemote.begin(), p_Index);
|
||||
ptrSPITType = i->second;
|
||||
@@ -187,7 +176,6 @@
|
||||
void SPITType::clearSPITTypes() {
|
||||
SPITType* ptrSPITType;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
for (auto i = s_SPITTypes.begin(); i != s_SPITTypes.end(); i++) {
|
||||
ptrSPITType = i->second;
|
||||
i = s_SPITTypes.erase(i);
|
||||
@@ -201,7 +189,6 @@
|
||||
void SPITType::clearSPITTypesRemote() {
|
||||
SPITType* ptrSPITType;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
for (auto i = s_SPITTypesRemote.begin(); i != s_SPITTypesRemote.end(); i++) {
|
||||
ptrSPITType = i->second;
|
||||
i = s_SPITTypesRemote.erase(i);
|
||||
@@ -220,7 +207,6 @@
|
||||
void SPITType::setConfirmedFalse() {
|
||||
SPITType* ptrSPITType;
|
||||
{
|
||||
fos::MutexGuard o_LockType(s_MutexSPITType);
|
||||
for (auto i = s_SPITTypes.begin(); i != s_SPITTypes.end(); i++) {
|
||||
ptrSPITType = i->second;
|
||||
ptrSPITType->setConfirmed(false);
|
||||
|
||||
+392
-420
File diff suppressed because it is too large
Load Diff
@@ -1,84 +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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* SPITHandler lists runnable instances and processes them in order.
|
||||
* File: SPIT_Handler.cpp
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 24. April 2019, 11:18
|
||||
*/
|
||||
|
||||
#include "SPIT_Handler.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
fos::Mutex SPIT_Handler::s_MutexLock;
|
||||
fos::Semaphore SPIT_Handler::s_ConditionVariable(1000);
|
||||
list<SPIT_Handler::Runnable*> SPIT_Handler::s_Runnables;
|
||||
bool SPIT_Handler::s_Close = false;
|
||||
fos::Task* SPIT_Handler::s_HandlerThread = nullptr;
|
||||
SPIT_Handler::Runnable::~Runnable() {}
|
||||
|
||||
|
||||
void SPIT_Handler::addRunnable(Runnable* p_Runnable) {
|
||||
if(s_HandlerThread == nullptr) {
|
||||
s_HandlerThread = new fos::Task(SPIT_Handler::run, "spitHandler");
|
||||
}
|
||||
|
||||
if (p_Runnable == NULL) return;
|
||||
{
|
||||
fos::MutexGuard o_Unique_Lock(s_MutexLock);
|
||||
if (s_Close == true) return;
|
||||
s_Runnables.push_back(p_Runnable);
|
||||
}
|
||||
s_ConditionVariable.give();
|
||||
}
|
||||
|
||||
void SPIT_Handler::closeHandler() {
|
||||
if (s_Close == true) return;
|
||||
{
|
||||
fos::MutexGuard o_Unique_Lock(s_MutexLock);
|
||||
if (s_Close == true) return;
|
||||
|
||||
s_Close = true;
|
||||
}
|
||||
s_ConditionVariable.give();
|
||||
}
|
||||
void SPIT_Handler::run() {
|
||||
Runnable* o_Runnable;
|
||||
bool boolWait = false;
|
||||
while (s_Close == false) {
|
||||
boolWait = false;
|
||||
{
|
||||
fos::MutexGuard o_Lock(s_MutexLock);
|
||||
|
||||
if (s_Runnables.empty()) {
|
||||
s_ConditionVariable.take();
|
||||
}
|
||||
if (s_Close == true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (s_Runnables.empty()) {
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
o_Runnable = s_Runnables.front();
|
||||
s_Runnables.erase(s_Runnables.begin());
|
||||
}
|
||||
}
|
||||
|
||||
if (o_Runnable != NULL) {
|
||||
try {
|
||||
o_Runnable->run();
|
||||
delete o_Runnable;
|
||||
}
|
||||
catch(...) {
|
||||
ESP_LOGW("SPIT", "ERROR SPITHandler o_Runable->run()");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,377 +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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Unix implementation of a SPITSocket.
|
||||
*
|
||||
* File: UnixSocket.cpp
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 12. April 2019, 15:58
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "_SPITMACROS.h"
|
||||
#include "UnixSocket.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_log.h"
|
||||
#include "Task.hpp"
|
||||
|
||||
|
||||
UnixSocket::UnixSocket() : SPITSocket(){
|
||||
SPITSocket::FAMILY_IP4 = AF_INET;
|
||||
SPITSocket::FAMILY_IP6 = AF_INET6;
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
m_UDPSocketIP6 = INVALID_SOCKET;
|
||||
createSocket_UDP();
|
||||
}
|
||||
|
||||
|
||||
UnixSocket::~UnixSocket() {
|
||||
|
||||
int intResultIP4;
|
||||
int intResultIP6;
|
||||
if (m_UDPSocketIP4 != INVALID_SOCKET) {
|
||||
// intResultIP4 = shutdown(m_UDPSocketIP4, 2);
|
||||
intResultIP4 = close(m_UDPSocketIP4);
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
}
|
||||
if (m_UDPSocketIP6 != INVALID_SOCKET) {
|
||||
// intResultIP6 = shutdown(m_UDPSocketIP6, 2);
|
||||
intResultIP6 = close(m_UDPSocketIP6);
|
||||
m_UDPSocketIP6 = INVALID_SOCKET;
|
||||
}
|
||||
fos::Task::delayMs(100);
|
||||
}
|
||||
|
||||
int UnixSocket::init() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int UnixSocket::cleanUp() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates two new sockets.<br>
|
||||
* One socket for IP4 and one for IP6.<br>
|
||||
* If there exist some, they will be closed.<br>
|
||||
* @return
|
||||
*/
|
||||
bool UnixSocket::createSocket_UDP() {
|
||||
bool boolSuccess = true;
|
||||
int intResult = -1;
|
||||
int intOptionValue4 = 1;
|
||||
int intOptionValue6 = 1;
|
||||
u_char charOptionValue = 0x01;
|
||||
|
||||
//ip4
|
||||
if (m_UDPSocketIP4 != INVALID_SOCKET) {
|
||||
close(m_UDPSocketIP4);
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
}
|
||||
if (m_UDPSocketIP6 != INVALID_SOCKET) {
|
||||
close(m_UDPSocketIP6);
|
||||
m_UDPSocketIP6 = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
|
||||
m_UDPSocketIP4 = socket(AF_INET,SOCK_DGRAM, 0);
|
||||
if (m_UDPSocketIP4 == INVALID_SOCKET) {
|
||||
boolSuccess = false;
|
||||
}
|
||||
else {
|
||||
intResult = setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_REUSEADDR, (void*)&intOptionValue4, sizeof(intOptionValue4));
|
||||
}
|
||||
|
||||
m_UDPSocketIP6 = socket(AF_INET6,SOCK_DGRAM, 0);
|
||||
if (m_UDPSocketIP6 == INVALID_SOCKET) {
|
||||
boolSuccess = false;
|
||||
close(m_UDPSocketIP4);
|
||||
m_UDPSocketIP4 = INVALID_SOCKET;
|
||||
}
|
||||
else {
|
||||
intResult = setsockopt(m_UDPSocketIP6, SOL_SOCKET, SO_REUSEADDR, (void*)&intOptionValue6, sizeof(intOptionValue6));
|
||||
}
|
||||
//set the m_UDPSocketIP4 and m_UDPSocketIP6 in NON BLOCKING mode because
|
||||
//if the UDPSockets are in blocking mode the receiveMessage does not return in every constellation
|
||||
int intFlags;
|
||||
intFlags = fcntl(m_UDPSocketIP4, F_GETFL);
|
||||
fcntl(m_UDPSocketIP4, F_SETFL, intFlags | O_NONBLOCK);
|
||||
intFlags = fcntl(m_UDPSocketIP6, F_GETFL);
|
||||
fcntl(m_UDPSocketIP6, F_SETFL, intFlags | O_NONBLOCK);
|
||||
|
||||
if (intResult < 0) {
|
||||
boolSuccess = false;
|
||||
}
|
||||
return boolSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds the socket to the client's ip address and port.<br>
|
||||
* The socket option SO_REUSEADDR is set to true.<br>
|
||||
* @param p_ClientIP ip address of the client
|
||||
* @param p_ClientPort port of the client.<br>
|
||||
* if p_Port == "0" any free port will be used<br>
|
||||
* @return -1 id bind fails, o if bind is ok
|
||||
*
|
||||
*/
|
||||
int UnixSocket::bindSocket(string p_ClientIP, string p_ClientPort) {
|
||||
addrinfo *clientAI;
|
||||
struct sockaddr_in* ptrIP4SocketAddress;
|
||||
struct sockaddr_in o_IP4SocketAddress;
|
||||
struct sockaddr_in6* ptrIP6SocketAddress;
|
||||
struct sockaddr_in6 o_IP6SocketAddress;
|
||||
int intSocklen;
|
||||
string stringClientPort;
|
||||
if (p_ClientPort.length() <= 0) stringClientPort = "0";
|
||||
else stringClientPort = p_ClientPort;
|
||||
//int intResult = SOCKET_ERROR;
|
||||
int intResult = 0;
|
||||
if (getaddrinfo(p_ClientIP.data(), p_ClientPort.data(), NULL, &clientAI) != 0) {
|
||||
return -1;
|
||||
}
|
||||
switch(clientAI->ai_family) {
|
||||
case AF_INET:
|
||||
if (m_UDPSocketIP4 == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
ptrIP4SocketAddress = reinterpret_cast<sockaddr_in*>(clientAI->ai_addr);
|
||||
intSocklen = sizeof (*ptrIP4SocketAddress);
|
||||
//copy the ptrIP4SocketAddress, may be not filled correct by getaddrinfo
|
||||
memset(&o_IP4SocketAddress, 0, intSocklen);
|
||||
o_IP4SocketAddress.sin_addr = ptrIP4SocketAddress->sin_addr;
|
||||
o_IP4SocketAddress.sin_family = ptrIP4SocketAddress->sin_family;
|
||||
//o_IP4SocketAddress.sin_zero = ptrIP4SocketAddress->sin_zero;
|
||||
o_IP4SocketAddress.sin_port = ptrIP4SocketAddress->sin_port;
|
||||
intResult = bind(m_UDPSocketIP4, (const sockaddr*)(ptrIP4SocketAddress),(socklen_t)intSocklen);
|
||||
if (intResult < 0) {
|
||||
break;
|
||||
}
|
||||
setBoundClientFamily(AF_INET);
|
||||
if (intResult == 0) {
|
||||
if (getsockname(m_UDPSocketIP4, (sockaddr*)ptrIP4SocketAddress, (socklen_t*)&intSocklen) != -1) {
|
||||
setBoundClientPort(to_string((int)ptrIP4SocketAddress->sin_port));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case AF_INET6:
|
||||
if (m_UDPSocketIP6 == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
ptrIP6SocketAddress = reinterpret_cast<sockaddr_in6*>(clientAI->ai_addr);
|
||||
intSocklen = sizeof (*ptrIP6SocketAddress);
|
||||
//copy the ptrIP6SocketAddress, may be not filled correct by getaddrinfo
|
||||
o_IP6SocketAddress.sin6_addr = ptrIP6SocketAddress->sin6_addr;
|
||||
o_IP6SocketAddress.sin6_family = ptrIP6SocketAddress->sin6_family;
|
||||
o_IP6SocketAddress.sin6_flowinfo = ptrIP6SocketAddress->sin6_flowinfo;
|
||||
o_IP6SocketAddress.sin6_port = ptrIP6SocketAddress->sin6_port;
|
||||
o_IP6SocketAddress.sin6_scope_id = ptrIP6SocketAddress->sin6_scope_id;
|
||||
intResult = bind(m_UDPSocketIP6, (const sockaddr*)(ptrIP6SocketAddress), (socklen_t)intSocklen);
|
||||
if (intResult < 0) {
|
||||
break;
|
||||
}
|
||||
setBoundClientFamily(AF_INET6);
|
||||
|
||||
if (intResult == 0) {
|
||||
if (getsockname(m_UDPSocketIP6, (sockaddr*)ptrIP6SocketAddress, (socklen_t*)&intSocklen) != -1) {
|
||||
setBoundClientPort(to_string((int)ptrIP6SocketAddress->sin6_port));
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
setBoundClientFamily(0);
|
||||
intResult = -1;
|
||||
break;
|
||||
}
|
||||
freeaddrinfo(clientAI);
|
||||
|
||||
if (intResult == -1) {
|
||||
setIsBound(false);
|
||||
}
|
||||
else {
|
||||
setIsBound(true);
|
||||
}
|
||||
return intResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sockopt SO_BROADCAST to 0 or 1
|
||||
* @param p_Broadcast true SO_BROADCAST is set<br>
|
||||
* false = SO_BROADCAST is not set
|
||||
* @return 0 = ok, -1 = error
|
||||
*/
|
||||
int UnixSocket::setBroadcast(bool p_Broadcast) {
|
||||
int intResult;
|
||||
int intOptionValue = 1;
|
||||
u_char charOptionValue = 0x01;
|
||||
//IP4
|
||||
if (p_Broadcast) {
|
||||
intOptionValue = 1;
|
||||
}
|
||||
else {
|
||||
intOptionValue = 0;
|
||||
}
|
||||
intResult = setsockopt(m_UDPSocketIP4, SOL_SOCKET, SO_BROADCAST, &intOptionValue, sizeof(int));
|
||||
// intResult = setsockopt(m_UDPSocketIP4, IPPROTO_IP, IP_MULTICAST_LOOP, (void*)&charOptionValue, sizeof(charOptionValue));
|
||||
|
||||
//IP6
|
||||
// intResult = setsockopt(m_UDPSocketIP6, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (void*)&charOptionValue, sizeof(charOptionValue));
|
||||
|
||||
return intResult;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param p_InetFamily AF_INET or AF_INET6
|
||||
* @param p_ServerIP the ip address of the server
|
||||
* @param p_ServerPort the port of the server
|
||||
* @param p_MessageBytes buffer containing the message
|
||||
* @param p_MessageLength the length of the message
|
||||
* @return -1 send fails<br>
|
||||
* 0 send is done
|
||||
*/
|
||||
int UnixSocket::sendMessageTo(int p_InetFamily, string p_ServerIP, string p_ServerPort, const char* p_MessageBytes, int p_MessageLength) {
|
||||
if (m_UDPSocketIP4 == INVALID_SOCKET && m_UDPSocketIP6 == INVALID_SOCKET) return -1;
|
||||
struct addrinfo* ptrAddressInfo;
|
||||
struct sockaddr_in* ptrIP4SocketAddress;
|
||||
struct sockaddr_in o_IP4SocketAddress;
|
||||
struct sockaddr_in6* ptrIP6SocketAddress;
|
||||
struct sockaddr_in6 o_IP6SocketAddress;
|
||||
int intSocklen;
|
||||
int intResult = -1;
|
||||
|
||||
// prepare AddressInfo
|
||||
if (getaddrinfo(p_ServerIP.data(), p_ServerPort.data(), NULL, &ptrAddressInfo) != 0) {
|
||||
return -1;
|
||||
}
|
||||
switch(ptrAddressInfo->ai_family) {
|
||||
case AF_INET:
|
||||
ptrIP4SocketAddress = reinterpret_cast<sockaddr_in*>(ptrAddressInfo->ai_addr);
|
||||
intSocklen = sizeof(*ptrIP4SocketAddress);
|
||||
if (m_UDPSocketIP4 != INVALID_SOCKET) {
|
||||
//copy the ptrIP4SocketAddress, may be not filled correct by getaddrinfo
|
||||
memset(&o_IP4SocketAddress, 0, intSocklen);
|
||||
o_IP4SocketAddress.sin_addr = ptrIP4SocketAddress->sin_addr;
|
||||
o_IP4SocketAddress.sin_family = ptrIP4SocketAddress->sin_family;
|
||||
o_IP4SocketAddress.sin_port = ptrIP4SocketAddress->sin_port;
|
||||
intResult = sendto(m_UDPSocketIP4, p_MessageBytes, p_MessageLength, 0, (const sockaddr*)&o_IP4SocketAddress, (socklen_t)intSocklen);
|
||||
if (intResult < 0) printf("UnixSocket::sendMessageTo IP4 sendto %s FAILED %s\n", p_ServerIP.data(), strerror(errno));
|
||||
freeaddrinfo(ptrAddressInfo);
|
||||
}
|
||||
break;
|
||||
case AF_INET6:
|
||||
ptrIP6SocketAddress = reinterpret_cast<sockaddr_in6*>(ptrAddressInfo->ai_addr);
|
||||
intSocklen = sizeof(*ptrIP6SocketAddress);
|
||||
if (m_UDPSocketIP6 != INVALID_SOCKET) {
|
||||
//copy the ptrIP6SocketAddress, may be not filled correct by getaddrinfo
|
||||
o_IP6SocketAddress.sin6_addr = ptrIP6SocketAddress->sin6_addr;
|
||||
o_IP6SocketAddress.sin6_family = ptrIP6SocketAddress->sin6_family;
|
||||
o_IP6SocketAddress.sin6_flowinfo = ptrIP6SocketAddress->sin6_flowinfo;
|
||||
o_IP6SocketAddress.sin6_port = ptrIP6SocketAddress->sin6_port;
|
||||
o_IP6SocketAddress.sin6_scope_id = ptrIP6SocketAddress->sin6_scope_id;
|
||||
intResult = sendto(m_UDPSocketIP6, p_MessageBytes, p_MessageLength, 0, (const sockaddr*)&o_IP6SocketAddress, (socklen_t)intSocklen);
|
||||
if (intResult < 0) printf("UnixSocket::sendMessageTo IP6 sendto %s FAILED %s\n", p_ServerIP.data(), strerror(errno));
|
||||
freeaddrinfo(ptrAddressInfo);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
intResult = -1;
|
||||
}
|
||||
|
||||
if (intResult == -1) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* Starts receiving a message
|
||||
* @param p_InetFamily AF_INET or AF_INET6
|
||||
* @param p_Buffer a char* buffer large enough to hold the expected data
|
||||
* @param p_BufferLength the length of the buffer
|
||||
* @param p_ServerIP = NULL, the ip address is not determined<br>
|
||||
* != NULL after receiving a message p_ServerID contains the ip address of the sender/server
|
||||
* @return
|
||||
*/
|
||||
int UnixSocket::receiveMessage(int p_InetFamily, char* p_Buffer, int p_BufferLength, string* p_ServerIP) {
|
||||
int intResult = -1;
|
||||
// ADDRINFO *AI;
|
||||
struct sockaddr_in o_IP4SocketAddress;
|
||||
struct sockaddr_in6 o_IP6SocketAddress;
|
||||
int intSocklen;
|
||||
|
||||
switch(p_InetFamily) {
|
||||
case AF_INET:
|
||||
if (m_UDPSocketIP4 == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
intSocklen = sizeof(o_IP4SocketAddress);
|
||||
memset(&o_IP4SocketAddress, 0, intSocklen);
|
||||
intResult = recvfrom(m_UDPSocketIP4, (void*)p_Buffer, p_BufferLength, 0, (sockaddr*)&o_IP4SocketAddress, (socklen_t*)&intSocklen);
|
||||
if (intResult >= 0) {
|
||||
if (p_ServerIP != NULL) {
|
||||
char bufferIP4[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &(o_IP4SocketAddress.sin_addr), bufferIP4, intSocklen);
|
||||
*p_ServerIP = bufferIP4;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case AF_INET6:
|
||||
if (m_UDPSocketIP6 == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
intSocklen = sizeof(o_IP6SocketAddress);
|
||||
memset(&o_IP6SocketAddress, 0, intSocklen);
|
||||
intResult = recvfrom(m_UDPSocketIP6, (void*)p_Buffer, p_BufferLength, 0, (sockaddr*)&o_IP6SocketAddress, (socklen_t*)&intSocklen);
|
||||
if (intResult >= 0) {
|
||||
if (p_ServerIP != NULL) {
|
||||
char bufferIP6[INET6_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET6, &(o_IP6SocketAddress.sin6_addr), bufferIP6, intSocklen);
|
||||
*p_ServerIP = bufferIP6;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return intResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the broadcast address from a ip4 address.<br>
|
||||
* @param p_IPAddress the ip4 address
|
||||
* @return returns the broadcast address
|
||||
*/
|
||||
string UnixSocket::getBroadcastIP4(SPITSocket::IPAddress* p_IPAddress) {
|
||||
struct in_addr broadcast;
|
||||
unsigned long ulIPAddress;
|
||||
unsigned long ulSubnetMask;
|
||||
|
||||
ulIPAddress = inet_addr(p_IPAddress->getIPAddress().data());
|
||||
ulSubnetMask = inet_addr(p_IPAddress->getSubnetMask().data());
|
||||
broadcast.s_addr = ulIPAddress | ~ulSubnetMask;
|
||||
|
||||
return inet_ntoa(broadcast);
|
||||
}
|
||||
|
||||
void UnixSocket::getAdaptersInfo() {
|
||||
resetFoundIPAddresses();
|
||||
// iterate over active interfaces, and print out IPs
|
||||
esp_netif_t *netif = NULL;
|
||||
while ((netif = esp_netif_next_unsafe(netif)) != NULL) {
|
||||
ESP_LOGI("SPIT", "Connected to %s", esp_netif_get_desc(netif));
|
||||
esp_netif_ip_info_t ip;
|
||||
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
|
||||
ESP_LOGI("SPIT", "- IPv4 address: " IPSTR ",", IP2STR(&ip.ip));
|
||||
ESP_LOGI("SPIT", "- IPv4 netmask: " IPSTR ",", IP2STR(&ip.netmask));
|
||||
char buffer[64];
|
||||
|
||||
sprintf(buffer, IPSTR, IP2STR(&ip.ip));
|
||||
string stringIP4 = buffer;
|
||||
sprintf(buffer, IPSTR, IP2STR(&ip.netmask));
|
||||
string stringSubnet = buffer;
|
||||
addIPAddress(stringIP4, stringSubnet);
|
||||
}
|
||||
removeNotFoundIPAddresses();
|
||||
}
|
||||
@@ -1,82 +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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* SPITWatchdog sends watchdog messages to the server from time to time.
|
||||
* If there is no response from the server an eventWatchdogLost is triggered.
|
||||
*
|
||||
* File: Watchdog.cpp
|
||||
* Author: Hagi
|
||||
*
|
||||
* Created on 22. April 2019, 13:02
|
||||
*/
|
||||
|
||||
|
||||
#include "Watchdog.h"
|
||||
|
||||
int Watchdog::WATCHDOGTIME = 3000;
|
||||
bool Watchdog::s_StopWatchdog = true;
|
||||
|
||||
Watchdog::Watchdog(Listener* p_Listener) {
|
||||
s_StopWatchdog = true;
|
||||
m_Listener = p_Listener;
|
||||
m_WatchdogThread = NULL;
|
||||
}
|
||||
|
||||
|
||||
Watchdog::~Watchdog() {
|
||||
stopWatchdog();
|
||||
}
|
||||
|
||||
void Watchdog::startWatchdog() {
|
||||
if (s_StopWatchdog == false) return;
|
||||
{
|
||||
fos::Mutex o_UniqueLock(m_MutexLock);
|
||||
s_StopWatchdog = false;
|
||||
}
|
||||
m_WatchdogThread = new fos::TaskT<Watchdog&>(Watchdog::runWatchdog, *this, "spitWD");
|
||||
}
|
||||
void Watchdog::stopWatchdog() {
|
||||
if (m_WatchdogThread != NULL) {
|
||||
{
|
||||
fos::Mutex o_UniqueLock(m_MutexLock);
|
||||
if (s_StopWatchdog == true) {
|
||||
return;
|
||||
}
|
||||
s_StopWatchdog = true;
|
||||
}
|
||||
m_ConditionVariable.give();
|
||||
delete m_WatchdogThread;
|
||||
m_WatchdogThread = NULL;
|
||||
}
|
||||
}
|
||||
void Watchdog::answerWatchdog() {
|
||||
m_Answered = true;
|
||||
}
|
||||
|
||||
void Watchdog::runWatchdog(Watchdog& wd) {
|
||||
bool boolDone;
|
||||
while (wd.s_StopWatchdog == false) {
|
||||
wd.m_Answered = false;
|
||||
wd.m_Listener->sendWatchdog();
|
||||
{
|
||||
fos::Mutex o_UniqueLock(wd.m_MutexLock);
|
||||
boolDone = wd.m_ConditionVariable.take(WATCHDOGTIME);
|
||||
if (wd.s_StopWatchdog == true) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (boolDone) {
|
||||
return;
|
||||
}
|
||||
if (wd.m_Answered == false) {
|
||||
wd.stopWatchdog();
|
||||
wd.m_Listener->eventWatchdogLost();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,36 +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.
|
||||
*/
|
||||
|
||||
#include "_SPITMACROS.h"
|
||||
#include "esp/uuid.h"
|
||||
#include "esp_netif.h"
|
||||
|
||||
string createGUID() {
|
||||
// uuid_t id;
|
||||
// uuid_generate(id);
|
||||
// char *guid_string = new char[100];
|
||||
// uuid_unparse(id, guid_string);
|
||||
// string stringGUID = guid_string;
|
||||
// return stringGUID;
|
||||
|
||||
uuid_t uu;
|
||||
char uu_str[UUID_STR_LEN];
|
||||
|
||||
uuid_generate(uu);
|
||||
uuid_unparse(uu, uu_str);
|
||||
string stringGUID = uu_str;
|
||||
return stringGUID;
|
||||
}
|
||||
/** The name of the client's host computer*/
|
||||
string getClientHostName() {
|
||||
string stringHostName = "";
|
||||
const char * hostname;
|
||||
esp_netif_get_hostname(esp_netif_get_default_netif(), &hostname);
|
||||
if (hostname != nullptr) {
|
||||
stringHostName = hostname;
|
||||
}
|
||||
return stringHostName;;
|
||||
}
|
||||
+30
-11
@@ -36,6 +36,9 @@ struct {
|
||||
|
||||
#include "watchMain.hpp"
|
||||
|
||||
string MyApp::s_ClientName = "CatasWatchDev";
|
||||
string MyApp::s_ProjectName = "CatastrphicProj";
|
||||
|
||||
extern "C" { // This switch allows the ROS C-implementation to find this main
|
||||
void app_main(void);
|
||||
}
|
||||
@@ -614,6 +617,21 @@ void MovementTask() {
|
||||
}
|
||||
}
|
||||
|
||||
void SpitClientApp() {
|
||||
ESP_LOGI(TAG, "Starting SPIT task");
|
||||
auto ptrMyApp = std::make_unique<MyApp>();
|
||||
|
||||
|
||||
// load a client project and start auto detection of SPITServers
|
||||
ptrMyApp->init();
|
||||
|
||||
while (true) {
|
||||
fos::Task::delayMs(10000);
|
||||
ESP_LOGI(TAG, "Test LiveShow");
|
||||
ptrMyApp->testLiveShowRemote(); //This is an excample how to remote control the liveSHOWsoftware
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
@@ -633,6 +651,7 @@ void app_main(void) {
|
||||
static fos::TaskT<int> telnetSocket(telnetSocketTask, ipVersion, "tcpServer", 4096);
|
||||
static fos::TaskT<int> dmxNode(dmxSocketTask, ipVersion, "dmxNode", 4096);
|
||||
// static Task otaUpdate(SystemFunctions::OtaTaskEntry, "otaTask", 8192);
|
||||
static fos::Task spit(SpitClientApp,"SpitClient");
|
||||
|
||||
auto &store = NvsApi::getInstance();
|
||||
auto logger = store.getValue(ConfigKeys.syslogDest);
|
||||
@@ -640,17 +659,17 @@ void app_main(void) {
|
||||
Syslog::setupTask(IpAdr{logger, 0});
|
||||
}
|
||||
|
||||
auto broker = store.getValue(ConfigKeys.mqttBroker);
|
||||
if (broker != "") {
|
||||
ESP_LOGI(TAG, "Initializing MQTT");
|
||||
auto prefix = store.getValue(ConfigKeys.mqttPrefix);
|
||||
auto &client = MqttClient::init(broker, prefix);
|
||||
if (!client.isRunning()) {
|
||||
client.addSubscription(MqttTopics.moveTo, [](std::string d) { moveToSubs(d); });
|
||||
client.addSubscription(MqttTopics.startShuffle, [](std::string d) { startShuffle(d); });
|
||||
client.start();
|
||||
}
|
||||
}
|
||||
// auto broker = store.getValue(ConfigKeys.mqttBroker);
|
||||
// if (broker != "") {
|
||||
// ESP_LOGI(TAG, "Initializing MQTT");
|
||||
// auto prefix = store.getValue(ConfigKeys.mqttPrefix);
|
||||
// auto &client = MqttClient::init(broker, prefix);
|
||||
// if (!client.isRunning()) {
|
||||
// client.addSubscription(MqttTopics.moveTo, [](std::string d) { moveToSubs(d); });
|
||||
// client.addSubscription(MqttTopics.startShuffle, [](std::string d) { startShuffle(d); });
|
||||
// client.start();
|
||||
// }
|
||||
// }
|
||||
|
||||
while (true) {
|
||||
fos::Task::delayMs(5000);
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include "Processor.hpp"
|
||||
#include "nvsFunctions.hpp"
|
||||
#include "mqttClient.hpp"
|
||||
#include "MyApp.h"
|
||||
|
||||
// #include "esp_chip_info.h"
|
||||
// #include "esp_flash.h"
|
||||
|
||||
Reference in New Issue
Block a user