Add a backoff timer for connections to aprs.fi.

This commit is contained in:
Jonathan Naylor 2020-03-03 15:49:13 +00:00
parent b8fbf58815
commit 81a818b235
3 changed files with 46 additions and 7 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2010-2014,2018 by Jonathan Naylor G4KLX * Copyright (C) 2010-2014,2018,2020 by Jonathan Naylor G4KLX
* *
* 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
@ -284,6 +284,8 @@ void CAPRSWriter::clock(unsigned int ms)
{ {
m_idTimer.clock(ms); m_idTimer.clock(ms);
m_thread->clock(ms);
if (m_socket != NULL) { if (m_socket != NULL) {
if (m_idTimer.hasExpired()) { if (m_idTimer.hasExpired()) {
pollGPS(); pollGPS();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2010-2014,2018 by Jonathan Naylor G4KLX * Copyright (C) 2010-2014,2018,2020 by Jonathan Naylor G4KLX
* *
* 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
@ -34,6 +34,8 @@ m_socket(hostname, port, address),
m_queue(20U), m_queue(20U),
m_exit(false), m_exit(false),
m_connected(false), m_connected(false),
m_reconnectTimer(1000U),
m_tries(0U),
m_APRSReadCallback(NULL), m_APRSReadCallback(NULL),
m_filter(wxT("")), m_filter(wxT("")),
m_clientName(wxT("ircDDBGateway")) m_clientName(wxT("ircDDBGateway"))
@ -59,6 +61,8 @@ m_socket(hostname, port, address),
m_queue(20U), m_queue(20U),
m_exit(false), m_exit(false),
m_connected(false), m_connected(false),
m_reconnectTimer(1000U),
m_tries(0U),
m_APRSReadCallback(NULL), m_APRSReadCallback(NULL),
m_filter(filter), m_filter(filter),
m_clientName(clientName) m_clientName(clientName)
@ -94,19 +98,28 @@ void* CAPRSWriterThread::Entry()
wxLogMessage(wxT("Starting the APRS Writer thread")); wxLogMessage(wxT("Starting the APRS Writer thread"));
m_connected = connect(); m_connected = connect();
if (!m_connected) {
wxLogError(wxT("Connect attempt to the APRS server has failed"));
startReconnectionTimer();
}
try { try {
while (!m_exit) { while (!m_exit) {
if (!m_connected) { if (!m_connected) {
m_connected = connect(); if (m_reconnectTimer.isRunning() && m_reconnectTimer.hasExpired()) {
m_reconnectTimer.stop();
if (!m_connected){ m_connected = connect();
wxLogError(wxT("Reconnect attempt to the APRS server has failed")); if (!m_connected) {
Sleep(10000UL); // 10 secs wxLogError(wxT("Reconnect attempt to the APRS server has failed"));
startReconnectionTimer();
}
} }
} }
if (m_connected) { if (m_connected) {
m_tries = 0U;
if(!m_queue.isEmpty()){ if(!m_queue.isEmpty()){
char* p = m_queue.getData(); char* p = m_queue.getData();
@ -120,6 +133,7 @@ void* CAPRSWriterThread::Entry()
m_connected = false; m_connected = false;
m_socket.close(); m_socket.close();
wxLogError(wxT("Connection to the APRS thread has failed")); wxLogError(wxT("Connection to the APRS thread has failed"));
startReconnectionTimer();
} }
delete[] p; delete[] p;
@ -135,6 +149,7 @@ void* CAPRSWriterThread::Entry()
m_connected = false; m_connected = false;
m_socket.close(); m_socket.close();
wxLogError(wxT("Error when reading from the APRS server")); wxLogError(wxT("Error when reading from the APRS server"));
startReconnectionTimer();
} }
if(length > 0 && line.GetChar(0) != '#'//check if we have something and if that something is an APRS frame if(length > 0 && line.GetChar(0) != '#'//check if we have something and if that something is an APRS frame
@ -201,6 +216,11 @@ void CAPRSWriterThread::stop()
Wait(); Wait();
} }
void CAPRSWriterThread::clock(unsigned int ms)
{
m_reconnectTimer.clock(ms);
}
bool CAPRSWriterThread::connect() bool CAPRSWriterThread::connect()
{ {
bool ret = m_socket.open(); bool ret = m_socket.open();
@ -248,3 +268,14 @@ bool CAPRSWriterThread::connect()
return true; return true;
} }
void CAPRSWriterThread::startReconnectionTimer()
{
// Clamp at a ten minutes reconnect time
m_tries++;
if (m_tries > 10U)
m_tries = 10U;
m_reconnectTimer.setTimeout(m_tries * 60U);
m_reconnectTimer.start();
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2010,2011,2012,2018 by Jonathan Naylor G4KLX * Copyright (C) 2010,2011,2012,2018,2020 by Jonathan Naylor G4KLX
* *
* 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
@ -21,6 +21,7 @@
#include "TCPReaderWriterClient.h" #include "TCPReaderWriterClient.h"
#include "RingBuffer.h" #include "RingBuffer.h"
#include "Timer.h"
#include <wx/wx.h> #include <wx/wx.h>
typedef void (*ReadAPRSFrameCallback)(const wxString&); typedef void (*ReadAPRSFrameCallback)(const wxString&);
@ -41,6 +42,8 @@ public:
virtual void stop(); virtual void stop();
void clock(unsigned int ms);
void setReadAPRSCallback(ReadAPRSFrameCallback cb); void setReadAPRSCallback(ReadAPRSFrameCallback cb);
private: private:
@ -51,11 +54,14 @@ private:
CRingBuffer<char*> m_queue; CRingBuffer<char*> m_queue;
bool m_exit; bool m_exit;
bool m_connected; bool m_connected;
CTimer m_reconnectTimer;
unsigned int m_tries;
ReadAPRSFrameCallback m_APRSReadCallback; ReadAPRSFrameCallback m_APRSReadCallback;
wxString m_filter; wxString m_filter;
wxString m_clientName; wxString m_clientName;
bool connect(); bool connect();
void startReconnectionTimer();
}; };
#endif #endif