#29 refactor APRS Handling, prepare for more testable code

pull/32/head
Geoffrey Merck 3 years ago
parent f90d5655ce
commit d86b36c2f0

@ -23,13 +23,13 @@
#include "APRSFixedIdFrameProvider.h" #include "APRSFixedIdFrameProvider.h"
#include "StringUtils.h" #include "StringUtils.h"
CAPRSFixedIdFrameProvider::CAPRSFixedIdFrameProvider() : CAPRSFixedIdFrameProvider::CAPRSFixedIdFrameProvider(const std::string& gateway) :
CAPRSIdFrameProvider(20U) // Initial timeout of 20 seconds CAPRSIdFrameProvider(gateway, 20U) // Initial timeout of 20 seconds
{ {
} }
bool CAPRSFixedIdFrameProvider::buildAPRSFramesInt(const std::string& gateway, const CAPRSEntry * entry, std::vector<CAPRSFrame *>& frames) bool CAPRSFixedIdFrameProvider::buildAPRSFramesInt(const CAPRSEntry * entry, std::vector<CAPRSFrame *>& frames)
{ {
if (entry == nullptr) if (entry == nullptr)
return false; return false;
@ -114,9 +114,9 @@ bool CAPRSFixedIdFrameProvider::buildAPRSFramesInt(const std::string& gateway, c
lon.c_str(), (entry->getLongitude() < 0.0F) ? 'W' : 'E', lon.c_str(), (entry->getLongitude() < 0.0F) ? 'W' : 'E',
entry->getRange() * 0.6214, entry->getAGL() * 3.28, band.c_str(), desc.c_str()); entry->getRange() * 0.6214, entry->getAGL() * 3.28, band.c_str(), desc.c_str());
CAPRSFrame * frame = new CAPRSFrame(gateway + "-S", CAPRSFrame * frame = new CAPRSFrame(m_gateway + "-S",
"APD5T1", "APD5T1",
{ "TCPIP*", "qAC" , gateway + "-GS" }, { "TCPIP*", "qAC" , m_gateway + "-GS" },
body, APFT_OBJECT); body, APFT_OBJECT);
frames.push_back(frame); frames.push_back(frame);

@ -23,8 +23,8 @@
class CAPRSFixedIdFrameProvider : public CAPRSIdFrameProvider class CAPRSFixedIdFrameProvider : public CAPRSIdFrameProvider
{ {
public: public:
CAPRSFixedIdFrameProvider(); CAPRSFixedIdFrameProvider(const std::string& gateway);
protected: protected:
virtual bool buildAPRSFramesInt(const std::string& gateway, const CAPRSEntry * aprsEntry, std::vector<CAPRSFrame *>& frames); virtual bool buildAPRSFramesInt(const CAPRSEntry * aprsEntry, std::vector<CAPRSFrame *>& frames);
}; };

@ -24,8 +24,8 @@
#include "StringUtils.h" #include "StringUtils.h"
#include "Log.h" #include "Log.h"
CAPRSGPSDIdFrameProvider::CAPRSGPSDIdFrameProvider(std::string address, std::string port) : CAPRSGPSDIdFrameProvider::CAPRSGPSDIdFrameProvider(const std::string& gateway, const std::string& address, const std::string& port) :
CAPRSIdFrameProvider(20U), CAPRSIdFrameProvider(gateway, 20U),
m_gpsdAddress(address), m_gpsdAddress(address),
m_gpsdPort(port), m_gpsdPort(port),
m_gpsdData(), m_gpsdData(),
@ -56,7 +56,7 @@ void CAPRSGPSDIdFrameProvider::close()
} }
} }
bool CAPRSGPSDIdFrameProvider::buildAPRSFramesInt(const std::string& gateway, const CAPRSEntry * entry, std::vector<CAPRSFrame *>& frames) bool CAPRSGPSDIdFrameProvider::buildAPRSFramesInt(const CAPRSEntry * entry, std::vector<CAPRSFrame *>& frames)
{ {
if(!m_hasConnection) { if(!m_hasConnection) {
this->start(); this->start();
@ -182,9 +182,9 @@ bool CAPRSGPSDIdFrameProvider::buildAPRSFramesInt(const std::string& gateway, co
body.append(CStringUtils::string_format("RNG%04.0lf %s %s\r\n", entry->getRange() * 0.6214, band.c_str(), desc.c_str())); body.append(CStringUtils::string_format("RNG%04.0lf %s %s\r\n", entry->getRange() * 0.6214, band.c_str(), desc.c_str()));
CAPRSFrame * frame = new CAPRSFrame(gateway + "-S", CAPRSFrame * frame = new CAPRSFrame(m_gateway + "-S",
"APD5T1", "APD5T1",
{ "TCPIP*", "qAC" , gateway + "-GS" }, { "TCPIP*", "qAC" , m_gateway + "-GS" },
body, APFT_OBJECT); body, APFT_OBJECT);
@ -201,9 +201,9 @@ bool CAPRSGPSDIdFrameProvider::buildAPRSFramesInt(const std::string& gateway, co
lat.c_str(), (rawLatitude < 0.0) ? 'S' : 'N', lat.c_str(), (rawLatitude < 0.0) ? 'S' : 'N',
lon.c_str(), (rawLongitude < 0.0) ? 'W' : 'E'); lon.c_str(), (rawLongitude < 0.0) ? 'W' : 'E');
frame = new CAPRSFrame(gateway, frame = new CAPRSFrame(m_gateway,
"APD5T2", "APD5T2",
{ "TCPIP*", "qAC" , gateway + "-GS" }, { "TCPIP*", "qAC" , m_gateway + "-GS" },
body, APFT_POSITION); body, APFT_POSITION);
frames.push_back(frame); frames.push_back(frame);

@ -29,13 +29,13 @@
class CAPRSGPSDIdFrameProvider : public CAPRSIdFrameProvider class CAPRSGPSDIdFrameProvider : public CAPRSIdFrameProvider
{ {
public: public:
CAPRSGPSDIdFrameProvider(std::string address, std::string port); CAPRSGPSDIdFrameProvider(const std::string& gateway, const std::string& address, const std::string& port);
virtual void start(); virtual void start();
virtual void close(); virtual void close();
protected: protected:
virtual bool buildAPRSFramesInt(const std::string& gateway, const CAPRSEntry * aprsEntry, std::vector<CAPRSFrame *>& frames); virtual bool buildAPRSFramesInt(const CAPRSEntry * aprsEntry, std::vector<CAPRSFrame *>& frames);
private: private:
std::string m_gpsdAddress; std::string m_gpsdAddress;

@ -18,10 +18,10 @@
*/ */
#include <cassert> #include <cassert>
#include <boost/algorithm/string.hpp>
#include <cmath> #include <cmath>
#include <cassert> #include <cassert>
#include <algorithm> #include <algorithm>
#include "StringUtils.h" #include "StringUtils.h"
#include "Log.h" #include "Log.h"
#include "APRSHandler.h" #include "APRSHandler.h"
@ -33,24 +33,12 @@
#include "APRSFormater.h" #include "APRSFormater.h"
#include "APRSUtils.h" #include "APRSUtils.h"
CAPRSHandler::CAPRSHandler(const std::string& hostname, unsigned int port, const std::string& gateway, const std::string& password, const std::string& address) : CAPRSHandler::CAPRSHandler(IAPRSHandlerThread* thread) :
m_thread(NULL), m_thread(thread),
m_gateway(),
m_address(),
m_port(0U),
m_array(), m_array(),
m_idFrameProvider(nullptr) m_idFrameProvider(nullptr)
{ {
assert(!hostname.empty()); assert(thread != nullptr);
assert(port > 0U);
assert(!gateway.empty());
assert(!password.empty());
m_thread = new CAPRSHandlerThread(gateway, password, address, hostname, port);
m_gateway = gateway;
m_gateway = m_gateway.substr(0, LONG_CALLSIGN_LENGTH - 1U);
boost::trim(m_gateway);
} }
CAPRSHandler::~CAPRSHandler() CAPRSHandler::~CAPRSHandler()
@ -60,6 +48,7 @@ CAPRSHandler::~CAPRSHandler()
} }
m_array.clear(); m_array.clear();
delete m_thread;
} }
void CAPRSHandler::setPort(const std::string& callsign, const std::string& band, double frequency, double offset, double range, double latitude, double longitude, double agl) void CAPRSHandler::setPort(const std::string& callsign, const std::string& band, double frequency, double offset, double range, double latitude, double longitude, double agl)
@ -210,7 +199,7 @@ void CAPRSHandler::sendIdFrames()
{ {
for(auto entry : m_array) { for(auto entry : m_array) {
std::vector<CAPRSFrame *> frames; std::vector<CAPRSFrame *> frames;
if(m_idFrameProvider->buildAPRSFrames(m_gateway, entry.second, frames)) { if(m_idFrameProvider->buildAPRSFrames(entry.second, frames)) {
for(auto frame : frames) { for(auto frame : frames) {
m_thread->write(*frame); m_thread->write(*frame);
delete frame; delete frame;

@ -26,7 +26,6 @@
#include "APRSEntry.h" #include "APRSEntry.h"
#include "APRSHandlerThread.h"
#include "UDPReaderWriter.h" #include "UDPReaderWriter.h"
#include "APRSCollector.h" #include "APRSCollector.h"
#include "DStarDefines.h" #include "DStarDefines.h"
@ -34,10 +33,11 @@
#include "AMBEData.h" #include "AMBEData.h"
#include "Timer.h" #include "Timer.h"
#include "APRSIdFrameProvider.h" #include "APRSIdFrameProvider.h"
#include "IAPRSHandlerThread.h"
class CAPRSHandler { class CAPRSHandler {
public: public:
CAPRSHandler(const std::string& hostname, unsigned int port, const std::string& gateway, const std::string& password, const std::string& address); CAPRSHandler(IAPRSHandlerThread * thread);
~CAPRSHandler(); ~CAPRSHandler();
bool open(); bool open();
@ -61,10 +61,7 @@ public:
void addReadAPRSCallback(IReadAPRSFrameCallback* cb); void addReadAPRSCallback(IReadAPRSFrameCallback* cb);
private: private:
CAPRSHandlerThread* m_thread; IAPRSHandlerThread* m_thread;
std::string m_gateway;
in_addr m_address;
unsigned int m_port;
std::unordered_map<std::string,CAPRSEntry *> m_array; std::unordered_map<std::string,CAPRSEntry *> m_array;
CAPRSIdFrameProvider * m_idFrameProvider; CAPRSIdFrameProvider * m_idFrameProvider;

@ -25,11 +25,11 @@
#include "RingBuffer.h" #include "RingBuffer.h"
#include "Timer.h" #include "Timer.h"
#include "Thread.h" #include "Thread.h"
#include "ReadAPRSFrameCallback.h" #include "IAPRSHandlerThread.h"
#include "APRSFrame.h" #include "APRSFrame.h"
class CAPRSHandlerThread : public CThread { class CAPRSHandlerThread : public CThread, IAPRSHandlerThread {
public: public:
CAPRSHandlerThread(const std::string& callsign, const std::string& password, const std::string& address, const std::string& hostname, unsigned int port); CAPRSHandlerThread(const std::string& callsign, const std::string& password, const std::string& address, const std::string& hostname, unsigned int port);
CAPRSHandlerThread(const std::string& callsign, const std::string& password, const std::string& address, const std::string& hostname, unsigned int port, const std::string& filter); CAPRSHandlerThread(const std::string& callsign, const std::string& password, const std::string& address, const std::string& hostname, unsigned int port, const std::string& filter);

@ -17,13 +17,21 @@
*/ */
#include <cassert> #include <cassert>
#include <boost/algorithm/string.hpp>
#include "APRSIdFrameProvider.h" #include "APRSIdFrameProvider.h"
CAPRSIdFrameProvider::CAPRSIdFrameProvider(unsigned int timeout) : CAPRSIdFrameProvider::CAPRSIdFrameProvider(const std::string& gateway, unsigned int timeout) :
m_gateway(),
m_timer(1000U) m_timer(1000U)
{ {
assert(!gateway.empty());
m_timer.start(timeout); m_timer.start(timeout);
m_gateway = gateway;
m_gateway = m_gateway.substr(0, LONG_CALLSIGN_LENGTH - 1U);
boost::trim(m_gateway);
} }
CAPRSIdFrameProvider::~CAPRSIdFrameProvider() CAPRSIdFrameProvider::~CAPRSIdFrameProvider()
@ -31,11 +39,11 @@ CAPRSIdFrameProvider::~CAPRSIdFrameProvider()
} }
bool CAPRSIdFrameProvider::buildAPRSFrames(const std::string& gateway, const CAPRSEntry * entry, std::vector<CAPRSFrame *> & frames) bool CAPRSIdFrameProvider::buildAPRSFrames(const CAPRSEntry * entry, std::vector<CAPRSFrame *> & frames)
{ {
assert(entry != nullptr); assert(entry != nullptr);
return buildAPRSFramesInt(gateway, entry, frames); return buildAPRSFramesInt(entry, frames);
} }
bool CAPRSIdFrameProvider::wantsToSend() bool CAPRSIdFrameProvider::wantsToSend()

@ -27,23 +27,25 @@
class CAPRSIdFrameProvider class CAPRSIdFrameProvider
{ {
public: public:
CAPRSIdFrameProvider(unsigned int timeOut); CAPRSIdFrameProvider(const std::string& gateway, unsigned int timeOut);
virtual ~CAPRSIdFrameProvider(); virtual ~CAPRSIdFrameProvider();
bool buildAPRSFrames(const std::string& gateway, const CAPRSEntry * aprsEntry, std::vector<CAPRSFrame *>& frames); bool buildAPRSFrames(const CAPRSEntry * aprsEntry, std::vector<CAPRSFrame *>& frames);
void clock(unsigned int ms) { m_timer.clock(ms); } void clock(unsigned int ms) { m_timer.clock(ms); }
bool wantsToSend(); bool wantsToSend();
virtual void start() { }; virtual void start() { };
virtual void close() { }; virtual void close() { };
protected: protected:
virtual bool buildAPRSFramesInt(const std::string& gateway, const CAPRSEntry * aprsEntry, std::vector<CAPRSFrame *>& frames) = 0; virtual bool buildAPRSFramesInt(const CAPRSEntry * aprsEntry, std::vector<CAPRSFrame *>& frames) = 0;
void setTimeout(unsigned int timeout) void setTimeout(unsigned int timeout)
{ {
m_timer.start(timeout); m_timer.start(timeout);
} }
protected:
std::string m_gateway;
private: private:
CTimer m_timer; CTimer m_timer;
}; };

@ -0,0 +1,35 @@
/*
* Copyright (C) 2021, 2022 by Geoffrey Merck F4FXL / KC3FRA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#pragma once
#include "APRSFrame.h"
#include "ReadAPRSFrameCallback.h"
class IAPRSHandlerThread
{
public:
virtual ~IAPRSHandlerThread() { } ;
virtual bool start() = 0;
virtual bool isConnected() const = 0;
virtual void write(CAPRSFrame& frame) = 0;
virtual void clock(unsigned int ms) = 0;
virtual void stop() = 0;
virtual void addReadAPRSCallback(IReadAPRSFrameCallback* cb) = 0;
};

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2010,2011 by Jonathan Naylor G4KLX * Copyright (C) 2010,2011 by Jonathan Naylor G4KLX
* Copyright (c) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA * Copyright (c) 2021,2022 by Geoffrey Merck F4FXL / KC3FRA
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -48,6 +48,7 @@
#include "APRSGPSDIdFrameProvider.h" #include "APRSGPSDIdFrameProvider.h"
#include "APRSFixedIdFrameProvider.h" #include "APRSFixedIdFrameProvider.h"
#include "Daemon.h" #include "Daemon.h"
#include "APRSHandlerThread.h"
CDStarGatewayApp * CDStarGatewayApp::g_app = nullptr; CDStarGatewayApp * CDStarGatewayApp::g_app = nullptr;
const std::string BANNER_1 = CStringUtils::string_format("%s Copyright (C) %s\n", FULL_PRODUCT_NAME.c_str(), VENDOR_NAME.c_str()); const std::string BANNER_1 = CStringUtils::string_format("%s Copyright (C) %s\n", FULL_PRODUCT_NAME.c_str(), VENDOR_NAME.c_str());
@ -194,13 +195,14 @@ bool CDStarGatewayApp::createThread()
m_config->getAPRS(aprsConfig); m_config->getAPRS(aprsConfig);
CAPRSHandler * aprsWriter = NULL; CAPRSHandler * aprsWriter = NULL;
if(aprsConfig.enabled && !aprsConfig.password.empty()) { if(aprsConfig.enabled && !aprsConfig.password.empty()) {
aprsWriter = new CAPRSHandler(aprsConfig.hostname, aprsConfig.port, gatewayConfig.callsign, aprsConfig.password, gatewayConfig.address); CAPRSHandlerThread* thread = new CAPRSHandlerThread(gatewayConfig.callsign, aprsConfig.password, gatewayConfig.address, aprsConfig.hostname, aprsConfig.port);
aprsWriter = new CAPRSHandler((IAPRSHandlerThread *)thread);
if(aprsWriter->open()) { if(aprsWriter->open()) {
#ifdef USE_GPSD #ifdef USE_GPSD
CAPRSIdFrameProvider * idFrameProvider = aprsConfig.m_positionSource == POSSRC_GPSD ? (CAPRSIdFrameProvider *)new CAPRSGPSDIdFrameProvider(gpsdConfig.m_address, gpsdConfig.m_port) CAPRSIdFrameProvider * idFrameProvider = aprsConfig.m_positionSource == POSSRC_GPSD ? (CAPRSIdFrameProvider *)new CAPRSGPSDIdFrameProvider(gatewayConfig.callsign, gpsdConfig.m_address, gpsdConfig.m_port)
: new CAPRSFixedIdFrameProvider(); : new CAPRSFixedIdFrameProvider(gatewayConfig.callsign);
#else #else
CAPRSIdFrameProvider * idFrameProvider = new CAPRSFixedIdFrameProvider(); CAPRSIdFrameProvider * idFrameProvider = new CAPRSFixedIdFrameProvider(gatewayConfig.callsign);
#endif #endif
idFrameProvider->start(); idFrameProvider->start();
aprsWriter->setIdFrameProvider(idFrameProvider); aprsWriter->setIdFrameProvider(idFrameProvider);

@ -984,7 +984,7 @@ void CDStarGatewayThread::processDExtra()
case DE_HEADER: { case DE_HEADER: {
CHeaderData* header = m_dextraPool->readHeader(); CHeaderData* header = m_dextraPool->readHeader();
if (header != NULL) { if (header != NULL) {
// CLog::logInfo("DExtra header - My: %s/%s Your: %s Rpt1: %s Rpt2: %s", header->getMyCall1().c_str(), header->getMyCall2().c_str(), header->getYourCall().c_str(), header->getRptCall1().c_str(), header->getRptCall2().c_str()); CLog::logInfo("DExtra header - My: %s/%s Your: %s Rpt1: %s Rpt2: %s", header->getMyCall1().c_str(), header->getMyCall2().c_str(), header->getYourCall().c_str(), header->getRptCall1().c_str(), header->getRptCall2().c_str());
CDExtraHandler::process(*header); CDExtraHandler::process(*header);
delete header; delete header;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.