Skip to content

Commit

Permalink
Refactor: RealTraffic working
Browse files Browse the repository at this point in the history
  • Loading branch information
TwinFan committed Dec 10, 2023
1 parent a9c1b33 commit c85c2a2
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 419 deletions.
4 changes: 1 addition & 3 deletions Include/LTADSBEx.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class ADSBExchangeConnection : public LTFlightDataChannel
public:
ADSBExchangeConnection ();
std::string GetURL (const positionTy& pos) override;
bool ProcessFetchedData (mapLTFlightDataTy& fdMap) override;
bool ProcessFetchedData () override;
std::string GetStatusText () const override; ///< return a human-readable staus
// // shall data of this channel be subject to LTFlightData::DataSmoothing?
// bool DoDataSmoothing (double& gndRange, double& airbRange) const override
Expand All @@ -149,12 +149,10 @@ class ADSBExchangeConnection : public LTFlightDataChannel

/// Process v2 data
void ProcessV2 (JSON_Object* pJAc, LTFlightData::FDKeyTy& fdKey,
mapLTFlightDataTy& fdMap,
const double tsCutOff, const double adsbxTime,
const positionTy& viewPos);
/// Process v1 data
void ProcessV1 (JSON_Object* pJAc, LTFlightData::FDKeyTy& fdKey,
mapLTFlightDataTy& fdMap,
const double tsCutOff, const double adsbxTime,
const positionTy& viewPos);

Expand Down
7 changes: 2 additions & 5 deletions Include/LTADSBHub.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
class ADSBHubConnection : public LTFlightDataChannel
{
protected:
// the map of flight data, where we deliver our data to
mapLTFlightDataTy& fdMap;

std::thread thrStream; ///< thread for the ADSBHub stream
TCPConnection tcpStream; ///< TCP connection to data.adsbhub.org:5002
volatile bool bStopThr=false; ///< stop signal to the thread
Expand Down Expand Up @@ -80,13 +77,13 @@ class ADSBHubConnection : public LTFlightDataChannel

public:
/// Constructor
ADSBHubConnection (mapLTFlightDataTy& _fdMap);
ADSBHubConnection ();
/// Destructor cleans up
~ADSBHubConnection () override;
/// Invokes APRS thread, or returns URL to fetch current data from live.glidernet.org
std::string GetURL (const positionTy&) override { return ""; }
/// @brief Processes the fetched data
bool ProcessFetchedData (mapLTFlightDataTy&) override { return true; };
bool ProcessFetchedData () override { return true; };
std::string GetStatusText () const override; ///< return a human-readable staus
bool FetchAllData(const positionTy& pos) override;
void DoDisabledProcessing() override { StreamClose(); }
Expand Down
13 changes: 7 additions & 6 deletions Include/LTChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,25 @@ class LTChannel
THR_ENDED, ///< Thread has ended, but is not yet joined
} ThrStatusTy;
/// Thread's state
volatile ThrStatusTy bThrStatus = THR_NONE;
volatile ThrStatusTy eThrStatus = THR_NONE;

private:
bool bValid = true; ///< valid connection?
int errCnt = 0; ///< number of errors tolerated

public:
/// Constructor just sets initial values
LTChannel (dataRefsLT ch, LTChannelType t, const char* chName) :
pszChName(chName), eType(t), channel(ch) {}
virtual ~LTChannel () {}
virtual ~LTChannel (); ///< Destructor makes sure the thread is stopped

void Start (); ///< Start the channel, typically starts a separate thread
void Stop (bool bWaitJoin); ///< Stop the channel
virtual void Start (); ///< Start the channel, typically starts a separate thread
virtual void Stop (bool bWaitJoin); ///< Stop the channel
bool isRunning () const ///< Is channel's thread running?
{ return thr.joinable(); }
virtual bool shallRun () const; ///< all conditions met to continue the thread loop?
/// Thread has ended but still needs to be joined
bool hasEnded () const { return bThrStatus == THR_ENDED; }
bool hasEnded () const { return eThrStatus == THR_ENDED; }

private:
void _Main(); ///< Thread main function, will call virtual Main()
Expand Down Expand Up @@ -118,7 +119,7 @@ class LTChannel

public:
virtual bool FetchAllData (const positionTy& pos) = 0;
virtual bool ProcessFetchedData (mapLTFlightDataTy& fd) = 0;
virtual bool ProcessFetchedData () = 0;
// TODO: Remove Disabled Processing, should be done during end of main thread / do something while disabled?
virtual void DoDisabledProcessing () {}
// TODO: Remove Close / (temporarily) close a connection, (re)open is with first call to FetchAll/ProcessFetchedData
Expand Down
2 changes: 1 addition & 1 deletion Include/LTFSCharter.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class FSCConnection : public LTFlightDataChannel
void CleanupCurl () override;
std::string GetURL (const positionTy& pos) override;
void ComputeBody (const positionTy& pos) override;
bool ProcessFetchedData (mapLTFlightDataTy& fdMap) override;
bool ProcessFetchedData () override;
bool FetchAllData(const positionTy& pos) override { return LTOnlineChannel::FetchAllData(pos); }
// // shall data of this channel be subject to LTFlightData::DataSmoothing?
// virtual bool DoDataSmoothing (double& gndRange, double& airbRange) const
Expand Down
8 changes: 3 additions & 5 deletions Include/LTForeFlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ constexpr std::chrono::milliseconds FF_INTVL = std::chrono::milliseconds(
class ForeFlightSender : public LTOutputChannel
{
protected:
// the map of flight data, data that we send out to ForeFlight
mapLTFlightDataTy& fdMap;
// thread
std::thread thrUdpSender;
volatile bool bStopUdpSender = true; // tells thread to stop
Expand All @@ -71,14 +69,14 @@ class ForeFlightSender : public LTOutputChannel
std::chrono::steady_clock::time_point lastStartOfTraffic;

public:
ForeFlightSender (mapLTFlightDataTy& _fdMap);
virtual ~ForeFlightSender ();
ForeFlightSender ();
~ForeFlightSender () override;

std::string GetURL (const positionTy&) override { return ""; } // don't need URL, no request/reply

// interface called from LTChannel
bool FetchAllData(const positionTy& pos) override;
bool ProcessFetchedData (mapLTFlightDataTy&) override { return true; }
bool ProcessFetchedData () override { return true; }
void DoDisabledProcessing() override;
void Close () override;

Expand Down
2 changes: 1 addition & 1 deletion Include/LTOpenGlider.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class OpenGliderConnection : public LTFlightDataChannel
/// Invokes APRS thread, or returns URL to fetch current data from live.glidernet.org
std::string GetURL (const positionTy& pos) override;
/// @brief Processes the fetched data
bool ProcessFetchedData (mapLTFlightDataTy& fdMap) override;
bool ProcessFetchedData () override;
std::string GetStatusText () const override; ///< return a human-readable staus
bool FetchAllData(const positionTy& pos) override { return LTOnlineChannel::FetchAllData(pos); }
void DoDisabledProcessing() override { Cleanup(); }
Expand Down
4 changes: 2 additions & 2 deletions Include/LTOpenSky.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class OpenSkyConnection : public LTFlightDataChannel
public:
OpenSkyConnection ();
std::string GetURL (const positionTy& pos) override;
bool ProcessFetchedData (mapLTFlightDataTy& fdMap) override;
bool ProcessFetchedData () override;
std::string GetStatusText () const override; ///< return a human-readable staus
// // shall data of this channel be subject to LTFlightData::DataSmoothing?
// bool DoDataSmoothing (double& gndRange, double& airbRange) const override
Expand Down Expand Up @@ -117,7 +117,7 @@ class OpenSkyAcMasterdata : public LTACMasterdataChannel
OpenSkyAcMasterdata (); ///< Constructor sets channel, name, and URLs
public:
std::string GetURL (const positionTy& pos) override; ///< Returns the master data or route URL to query
bool ProcessFetchedData (mapLTFlightDataTy& fdMap) override; ///< Process received master or route data
bool ProcessFetchedData () override; ///< Process received master or route data
protected:
void Main () override; ///< virtual thread main function
bool ProcessMasterData (JSON_Object* pJAc); ///< Process received aircraft master data
Expand Down
46 changes: 15 additions & 31 deletions Include/LTRealTraffic.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,21 @@ class RealTrafficConnection : public LTFlightDataChannel
std::recursive_mutex rtMutex;
// RealTraffic connection status
volatile rtStatusTy status = RT_STATUS_NONE;
// the map of flight data, where we deliver our data to
mapLTFlightDataTy& fdMap;

// tcp connection to send current position
std::thread thrTcpServer;
TCPConnection tcpPosSender;
volatile bool bStopTcp = false;
volatile bool thrTcpRunning = false;
// TCP connection to send current position
std::thread thrTcpServer; ///< thread of the TCP listening thread (short-lived)
TCPConnection tcpPosSender; ///< TCP connection to communicate with RealTraffic
/// Status of the separate TCP listening thread
volatile ThrStatusTy eTcpThrStatus = THR_NONE;
// current position which serves as center
positionTy posCamera;

// udp thread and its sockets
std::thread thrUdpListener;
// UDP sockets
UDPReceiver udpTrafficData;
#if APL == 1 || LIN == 1
// the self-pipe to shut down the UDP listener thread gracefully
SOCKET udpPipe[2] = { INVALID_SOCKET, INVALID_SOCKET };
#endif
volatile bool bStopUdp = false;
volatile bool thrUdpRunning = false;
double lastReceivedTime = 0.0; // copy of simTime
// map of last received datagrams for duplicate detection
std::map<unsigned long,RTUDPDatagramTy> mapDatagrams;
Expand All @@ -195,16 +190,15 @@ class RealTrafficConnection : public LTFlightDataChannel
double tsAdjust = 0.0;

public:
RealTrafficConnection (mapLTFlightDataTy& _fdMap);
virtual ~RealTrafficConnection ();
RealTrafficConnection ();

void Stop (bool bWaitJoin) override; ///< Stop the UDP listener gracefully

std::string GetURL (const positionTy&) override { return ""; } // don't need URL, no request/reply

// interface called from LTChannel
bool FetchAllData(const positionTy& pos) override;
bool ProcessFetchedData (mapLTFlightDataTy&) override { return true; }
void DoDisabledProcessing() override;
void Close () override;
bool FetchAllData(const positionTy& /*pos*/) override { return false; }
bool ProcessFetchedData () override { return false; }
// SetValid also sets internal status
void SetValid (bool _valid, bool bMsg = true) override;
// // shall data of this channel be subject to LTFlightData::DataSmoothing?
Expand All @@ -229,28 +223,18 @@ class RealTrafficConnection : public LTFlightDataChannel
protected:
void Main () override; ///< virtual thread main function

// Start/Stop
bool StartConnections ();
bool StopConnections ();

// MARK: TCP
void tcpConnection ();
static void tcpConnectionS (RealTrafficConnection* me) { me->tcpConnection();}
bool StopTcpConnection ();
void tcpConnection (); ///< main function of TCP listening thread, lives only until TCP connection established
void StartTcpConnection (); ///< start the TCP listening thread
void StopTcpConnection (); ///< stop the TCP listening thread

void SendMsg (const char* msg); ///< Send and log a message to RealTraffic
void SendTime (long long ts); ///< Send a timestamp to RealTraffic
void SendXPSimTime(); ///< Send XP's current simulated time to RealTraffic, adapted to "today or earlier"
void SendPos (const positionTy& pos, double speed_m); ///< Send position/speed info for own ship to RealTraffic
void SendUsersPlanePos(); ///< Send user's plane's position/speed to RealTraffic

// MARK: UDP
// UDP Listen: the main function for receiving UDP broadcasts
void udpListen ();
// just a wrapper to call a member function
static void udpListenS (RealTrafficConnection* me) { me->udpListen();}
bool StopUdpConnection ();

// MARK: Data Processing
// Process received datagrams
bool ProcessRecvedTrafficData (const char* traffic);
bool ProcessRTTFC (LTFlightData::FDKeyTy& fdKey, const std::vector<std::string>& tfc); ///< Process a RTTFC type message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,4 @@
uuid = "C8095CDD-1C39-4D01-99B0-6AD4723390BD"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "90BD2871-C6D9-4228-BC7E-B6812D7A3306"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Src/LTFlightData.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "2406"
endingLineNumber = "2406"
landmarkName = "LTFlightData::UpdateData(inStat, distance, masterDataType)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "B135B6A8-1262-4B89-AD72-6F5EF65C4B3B"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Src/LTFlightData.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "2410"
endingLineNumber = "2410"
landmarkName = "LTFlightData::UpdateData(inStat, distance, masterDataType)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "DDD4488A-9D35-4E8B-858A-AD8E7C3D9827"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Src/LTOpenSky.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "401"
endingLineNumber = "401"
landmarkName = "OpenSkyAcMasterdata::GetURL(positionTy)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "4BE80228-72EC-4D44-9D15-76615A747D71"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Src/LTOpenSky.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "416"
endingLineNumber = "416"
landmarkName = "OpenSkyAcMasterdata::ProcessFetchedData()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
14 changes: 6 additions & 8 deletions Src/LTADSBEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ std::string ADSBExchangeConnection::GetURL (const positionTy& pos)
}

// update shared flight data structures with received flight data
bool ADSBExchangeConnection::ProcessFetchedData (mapLTFlightDataTy& fdMap)
bool ADSBExchangeConnection::ProcessFetchedData ()
{
// some things depend on the key type
const char* sERR = keyTy == ADSBEX_KEY_EXCHANGE ? ADSBEX_ERR : ADSBEX_RAPID_ERR;
Expand Down Expand Up @@ -163,9 +163,9 @@ bool ADSBExchangeConnection::ProcessFetchedData (mapLTFlightDataTy& fdMap)
// Process the details, depends on version detected
try {
if (ver == 2)
ProcessV2(pJAc, fdKey, fdMap, tsCutOff, adsbxTime, viewPos);
ProcessV2(pJAc, fdKey, tsCutOff, adsbxTime, viewPos);
else if (ver == 1)
ProcessV1(pJAc, fdKey, fdMap, tsCutOff, adsbxTime, viewPos);
ProcessV1(pJAc, fdKey, tsCutOff, adsbxTime, viewPos);
} catch(const std::system_error& e) {
LOG_MSG(logERR, ERR_LOCK_ERROR, "mapFd", e.what());
} catch(...) {
Expand All @@ -184,7 +184,6 @@ bool ADSBExchangeConnection::ProcessFetchedData (mapLTFlightDataTy& fdMap)
// Process v2 data
void ADSBExchangeConnection::ProcessV2 (JSON_Object* pJAc,
LTFlightData::FDKeyTy& fdKey,
mapLTFlightDataTy& fdMap,
const double tsCutOff,
const double adsbxTime,
const positionTy& viewPos)
Expand Down Expand Up @@ -282,7 +281,7 @@ void ADSBExchangeConnection::ProcessV2 (JSON_Object* pJAc,

// get the fd object from the map, key is the transpIcao
// this fetches an existing or, if not existing, creates a new one
LTFlightData& fd = fdMap[fdKey];
LTFlightData& fd = mapFd[fdKey];

// also get the data access lock once and for all
// so following fetch/update calls only make quick recursive calls
Expand Down Expand Up @@ -333,7 +332,6 @@ void ADSBExchangeConnection::ProcessV2 (JSON_Object* pJAc,
// Process v1 data
void ADSBExchangeConnection::ProcessV1 (JSON_Object* pJAc,
LTFlightData::FDKeyTy& fdKey,
mapLTFlightDataTy& fdMap,
const double tsCutOff,
const double /*adsbxTime*/,
const positionTy& viewPos)
Expand Down Expand Up @@ -368,7 +366,7 @@ void ADSBExchangeConnection::ProcessV1 (JSON_Object* pJAc,

// get the fd object from the map, key is the transpIcao
// this fetches an existing or, if not existing, creates a new one
LTFlightData& fd = fdMap[fdKey];
LTFlightData& fd = mapFd[fdKey];

// also get the data access lock once and for all
// so following fetch/update calls only make quick recursive calls
Expand Down Expand Up @@ -496,7 +494,7 @@ void ADSBExchangeConnection::Main ()
tNextWakeup += std::chrono::seconds(dataRefs.GetFdRefreshIntvl());

// if enabled fetch data and process it
if (FetchAllData(pos) && ProcessFetchedData(mapFd))
if (FetchAllData(pos) && ProcessFetchedData())
// reduce error count if processed successfully
// as a chance to appear OK in the long run
DecErrCnt();
Expand Down
7 changes: 3 additions & 4 deletions Src/LTADSBHub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ constexpr int ADSBHUB_TIMEOUT_S = 60; ///< ADSBHub sends s
//

// Constructor
ADSBHubConnection::ADSBHubConnection (mapLTFlightDataTy& _fdMap) :
LTFlightDataChannel(DR_CHANNEL_ADSB_HUB, ADSBHUB_NAME),
fdMap(_fdMap)
ADSBHubConnection::ADSBHubConnection () :
LTFlightDataChannel(DR_CHANNEL_ADSB_HUB, ADSBHUB_NAME)
{
// purely informational
urlName = ADSBHUB_CHECK_NAME;
Expand Down Expand Up @@ -621,7 +620,7 @@ void ADSBHubConnection::ProcessPlaneData ()

// get the fd object from the map, key is the transpIcao
// this fetches an existing or, if not existing, creates a new one
LTFlightData& fd = fdMap[fdKey];
LTFlightData& fd = mapFd[fdKey];

// also get the data access lock once and for all
// so following fetch/update calls only make quick recursive calls
Expand Down
Loading

0 comments on commit c85c2a2

Please sign in to comment.