parent
815165141c
commit
7759575bbb
@ -1,279 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2014,2016,2020 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "APRSWriterThread.h"
|
||||
#include "Utils.h"
|
||||
#include "Log.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cassert>
|
||||
|
||||
// #define DUMP_TX
|
||||
|
||||
const unsigned int CALLSIGN_LENGTH = 8U;
|
||||
|
||||
const unsigned int APRS_TIMEOUT = 10U;
|
||||
|
||||
CAPRSWriterThread::CAPRSWriterThread(const std::string& callsign, const std::string& password, const std::string& address, unsigned int port) :
|
||||
CThread(),
|
||||
m_username(callsign),
|
||||
m_password(password),
|
||||
m_socket(address, port),
|
||||
m_queue(20U, "APRS Queue"),
|
||||
m_exit(false),
|
||||
m_connected(false),
|
||||
m_reconnectTimer(1000U),
|
||||
m_tries(1U),
|
||||
m_APRSReadCallback(NULL),
|
||||
m_filter(),
|
||||
m_clientName("YSFGateway")
|
||||
{
|
||||
assert(!callsign.empty());
|
||||
assert(!password.empty());
|
||||
assert(!address.empty());
|
||||
assert(port > 0U);
|
||||
|
||||
m_username.resize(CALLSIGN_LENGTH, ' ');
|
||||
m_username.erase(std::find_if(m_username.rbegin(), m_username.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), m_username.end());
|
||||
std::transform(m_username.begin(), m_username.end(), m_username.begin(), ::toupper);
|
||||
}
|
||||
|
||||
CAPRSWriterThread::CAPRSWriterThread(const std::string& callsign, const std::string& password, const std::string& address, unsigned int port, const std::string& filter, const std::string& clientName) :
|
||||
CThread(),
|
||||
m_username(callsign),
|
||||
m_password(password),
|
||||
m_socket(address, port),
|
||||
m_queue(20U, "APRS Queue"),
|
||||
m_exit(false),
|
||||
m_connected(false),
|
||||
m_reconnectTimer(1000U),
|
||||
m_tries(1U),
|
||||
m_APRSReadCallback(NULL),
|
||||
m_filter(filter),
|
||||
m_clientName(clientName)
|
||||
{
|
||||
assert(!callsign.empty());
|
||||
assert(!password.empty());
|
||||
assert(!address.empty());
|
||||
assert(port > 0U);
|
||||
|
||||
m_username.resize(CALLSIGN_LENGTH, ' ');
|
||||
m_username.erase(std::find_if(m_username.rbegin(), m_username.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), m_username.end());
|
||||
std::transform(m_username.begin(), m_username.end(), m_username.begin(), ::toupper);
|
||||
}
|
||||
|
||||
CAPRSWriterThread::~CAPRSWriterThread()
|
||||
{
|
||||
m_username.clear();
|
||||
}
|
||||
|
||||
bool CAPRSWriterThread::start()
|
||||
{
|
||||
run();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CAPRSWriterThread::entry()
|
||||
{
|
||||
LogMessage("Starting the APRS Writer thread");
|
||||
|
||||
m_connected = connect();
|
||||
if (!m_connected) {
|
||||
LogError("Connect attempt to the APRS server has failed");
|
||||
startReconnectionTimer();
|
||||
}
|
||||
|
||||
try {
|
||||
while (!m_exit) {
|
||||
if (!m_connected) {
|
||||
if (m_reconnectTimer.isRunning() && m_reconnectTimer.hasExpired()) {
|
||||
m_reconnectTimer.stop();
|
||||
|
||||
m_connected = connect();
|
||||
if (!m_connected) {
|
||||
LogError("Reconnect attempt to the APRS server has failed");
|
||||
startReconnectionTimer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_connected) {
|
||||
m_tries = 0U;
|
||||
|
||||
if (!m_queue.isEmpty()){
|
||||
char* p = NULL;
|
||||
m_queue.getData(&p, 1U);
|
||||
|
||||
LogMessage("APRS ==> %s", p);
|
||||
|
||||
::strcat(p, "\r\n");
|
||||
|
||||
bool ret = m_socket.write((unsigned char*)p, (unsigned int)::strlen(p));
|
||||
if (!ret) {
|
||||
m_connected = false;
|
||||
m_socket.close();
|
||||
LogError("Connection to the APRS thread has failed");
|
||||
startReconnectionTimer();
|
||||
}
|
||||
|
||||
delete[] p;
|
||||
}
|
||||
{
|
||||
std::string line;
|
||||
int length = m_socket.readLine(line, APRS_TIMEOUT);
|
||||
|
||||
if (length < 0) {
|
||||
m_connected = false;
|
||||
m_socket.close();
|
||||
LogError("Error when reading from the APRS server");
|
||||
startReconnectionTimer();
|
||||
}
|
||||
|
||||
if(length > 0 && line.at(0U) != '#'//check if we have something and if that something is an APRS frame
|
||||
&& m_APRSReadCallback != NULL)//do we have someone wanting an APRS Frame?
|
||||
{
|
||||
//wxLogMessage(wxT("Received APRS Frame : ") + line);
|
||||
m_APRSReadCallback(std::string(line));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (m_connected)
|
||||
m_socket.close();
|
||||
|
||||
while (!m_queue.isEmpty()) {
|
||||
char* p = NULL;
|
||||
m_queue.getData(&p, 1U);
|
||||
delete[] p;
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
LogError("Exception raised in the APRS Writer thread - \"%s\"", e.what());
|
||||
}
|
||||
catch (...) {
|
||||
LogError("Unknown exception raised in the APRS Writer thread");
|
||||
}
|
||||
|
||||
LogMessage("Stopping the APRS Writer thread");
|
||||
}
|
||||
|
||||
void CAPRSWriterThread::setReadAPRSCallback(ReadAPRSFrameCallback cb)
|
||||
{
|
||||
m_APRSReadCallback = cb;
|
||||
}
|
||||
|
||||
void CAPRSWriterThread::write(const char* data)
|
||||
{
|
||||
assert(data != NULL);
|
||||
|
||||
if (!m_connected)
|
||||
return;
|
||||
|
||||
unsigned int len = (unsigned int)::strlen(data);
|
||||
|
||||
char* p = new char[len + 5U];
|
||||
::strcpy(p, data);
|
||||
|
||||
m_queue.addData(&p, 1U);
|
||||
}
|
||||
|
||||
bool CAPRSWriterThread::isConnected() const
|
||||
{
|
||||
return m_connected;
|
||||
}
|
||||
|
||||
void CAPRSWriterThread::stop()
|
||||
{
|
||||
m_exit = true;
|
||||
|
||||
wait();
|
||||
}
|
||||
|
||||
void CAPRSWriterThread::clock(unsigned int ms)
|
||||
{
|
||||
m_reconnectTimer.clock(ms);
|
||||
}
|
||||
|
||||
bool CAPRSWriterThread::connect()
|
||||
{
|
||||
bool ret = m_socket.open();
|
||||
if (!ret)
|
||||
return false;
|
||||
|
||||
//wait for lgin banner
|
||||
int length;
|
||||
std::string serverResponse;
|
||||
length = m_socket.readLine(serverResponse, APRS_TIMEOUT);
|
||||
if (length == 0) {
|
||||
LogError("No reply from the APRS server after %u seconds", APRS_TIMEOUT);
|
||||
m_socket.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
LogMessage("Received login banner : %s", serverResponse.c_str());
|
||||
|
||||
std::string filter(m_filter);
|
||||
if (filter.length() > 0)
|
||||
filter.insert(0U, " filter ");
|
||||
|
||||
char connectString[200U];
|
||||
::sprintf(connectString, "user %s pass %s vers %s%s\n", m_username.c_str(), m_password.c_str(), (m_clientName.length() ? m_clientName : "YSFGateway").c_str(), filter.c_str());
|
||||
|
||||
ret = m_socket.writeLine(std::string(connectString));
|
||||
if (!ret) {
|
||||
m_socket.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
length = m_socket.readLine(serverResponse, APRS_TIMEOUT);
|
||||
if (length == 0) {
|
||||
LogError("No reply from the APRS server after %u seconds", APRS_TIMEOUT);
|
||||
m_socket.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (length < 0) {
|
||||
LogError("Error when reading from the APRS server");
|
||||
m_socket.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
LogMessage("Response from APRS server: %s", serverResponse.c_str());
|
||||
|
||||
LogMessage("Connected to the APRS server");
|
||||
|
||||
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();
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010,2011,2012,2016,2020 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef APRSWriterThread_H
|
||||
#define APRSWriterThread_H
|
||||
|
||||
#include "TCPSocket.h"
|
||||
#include "RingBuffer.h"
|
||||
#include "Timer.h"
|
||||
#include "Thread.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
typedef void (*ReadAPRSFrameCallback)(const std::string&);
|
||||
|
||||
class CAPRSWriterThread : public CThread {
|
||||
public:
|
||||
CAPRSWriterThread(const std::string& callsign, const std::string& password, const std::string& address, unsigned int port);
|
||||
CAPRSWriterThread(const std::string& callsign, const std::string& password, const std::string& address, unsigned int port, const std::string& filter, const std::string& clientName);
|
||||
virtual ~CAPRSWriterThread();
|
||||
|
||||
virtual bool start();
|
||||
|
||||
virtual bool isConnected() const;
|
||||
|
||||
virtual void write(const char* data);
|
||||
|
||||
virtual void entry();
|
||||
|
||||
virtual void stop();
|
||||
|
||||
void setReadAPRSCallback(ReadAPRSFrameCallback cb);
|
||||
|
||||
void clock(unsigned int ms);
|
||||
|
||||
private:
|
||||
std::string m_username;
|
||||
std::string m_password;
|
||||
CTCPSocket m_socket;
|
||||
CRingBuffer<char*> m_queue;
|
||||
bool m_exit;
|
||||
bool m_connected;
|
||||
CTimer m_reconnectTimer;
|
||||
unsigned int m_tries;
|
||||
ReadAPRSFrameCallback m_APRSReadCallback;
|
||||
std::string m_filter;
|
||||
std::string m_clientName;
|
||||
|
||||
bool connect();
|
||||
void startReconnectionTimer();
|
||||
};
|
||||
|
||||
#endif
|
@ -1,232 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2013,2016 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "TCPSocket.h"
|
||||
#include "UDPSocket.h"
|
||||
#include "Log.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
typedef int ssize_t;
|
||||
#else
|
||||
#include <cerrno>
|
||||
#endif
|
||||
|
||||
CTCPSocket::CTCPSocket(const std::string& address, unsigned int port) :
|
||||
m_address(address),
|
||||
m_port(port),
|
||||
m_fd(-1)
|
||||
{
|
||||
assert(!address.empty());
|
||||
assert(port > 0U);
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
WSAData data;
|
||||
int wsaRet = ::WSAStartup(MAKEWORD(2, 2), &data);
|
||||
if (wsaRet != 0)
|
||||
LogError("Error from WSAStartup");
|
||||
#endif
|
||||
}
|
||||
|
||||
CTCPSocket::~CTCPSocket()
|
||||
{
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
::WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool CTCPSocket::open()
|
||||
{
|
||||
if (m_fd != -1)
|
||||
return true;
|
||||
|
||||
if (m_address.empty() || m_port == 0U)
|
||||
return false;
|
||||
|
||||
m_fd = ::socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (m_fd < 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Cannot create the TCP client socket, err=%d", ::GetLastError());
|
||||
#else
|
||||
LogError("Cannot create the TCP client socket, err=%d", errno);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
struct sockaddr_in addr;
|
||||
::memset(&addr, 0x00, sizeof(struct sockaddr_in));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(m_port);
|
||||
addr.sin_addr = CUDPSocket::lookup(m_address);
|
||||
|
||||
if (addr.sin_addr.s_addr == INADDR_NONE) {
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (::connect(m_fd, (sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Cannot connect the TCP client socket, err=%d", ::GetLastError());
|
||||
#else
|
||||
LogError("Cannot connect the TCP client socket, err=%d", errno);
|
||||
#endif
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
int noDelay = 1;
|
||||
if (::setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&noDelay, sizeof(noDelay)) == -1) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Cannot set the TCP client socket option, err=%d", ::GetLastError());
|
||||
#else
|
||||
LogError("Cannot set the TCP client socket option, err=%d", errno);
|
||||
#endif
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int CTCPSocket::read(unsigned char* buffer, unsigned int length, unsigned int secs, unsigned int msecs)
|
||||
{
|
||||
assert(buffer != NULL);
|
||||
assert(length > 0U);
|
||||
assert(m_fd != -1);
|
||||
|
||||
// Check that the recv() won't block
|
||||
fd_set readFds;
|
||||
FD_ZERO(&readFds);
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
FD_SET((unsigned int)m_fd, &readFds);
|
||||
#else
|
||||
FD_SET(m_fd, &readFds);
|
||||
#endif
|
||||
|
||||
// Return after timeout
|
||||
timeval tv;
|
||||
tv.tv_sec = secs;
|
||||
tv.tv_usec = msecs * 1000;
|
||||
|
||||
int ret = ::select(m_fd + 1, &readFds, NULL, NULL, &tv);
|
||||
if (ret < 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Error returned from TCP client select, err=%d", ::GetLastError());
|
||||
#else
|
||||
LogError("Error returned from TCP client select, err=%d", errno);
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (!FD_ISSET((unsigned int)m_fd, &readFds))
|
||||
return 0;
|
||||
#else
|
||||
if (!FD_ISSET(m_fd, &readFds))
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
ssize_t len = ::recv(m_fd, (char*)buffer, length, 0);
|
||||
if (len == 0) {
|
||||
return -2;
|
||||
} else if (len < 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Error returned from recv, err=%d", ::GetLastError());
|
||||
#else
|
||||
LogError("Error returned from recv, err=%d", errno);
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int CTCPSocket::readLine(std::string& line, unsigned int secs)
|
||||
{
|
||||
// Maybe there is a better way to do this like reading blocks, pushing them for later calls
|
||||
// Nevermind, we'll read one char at a time for the time being.
|
||||
int resultCode;
|
||||
int len = 0;
|
||||
|
||||
line.clear();
|
||||
|
||||
char c[2U];
|
||||
c[1U] = 0x00U;
|
||||
|
||||
do
|
||||
{
|
||||
resultCode = read((unsigned char*)c, 1U, secs);
|
||||
if (resultCode == 1){
|
||||
line.append(c);
|
||||
len++;
|
||||
}
|
||||
} while (c[0U] != '\n' && resultCode == 1);
|
||||
|
||||
return resultCode <= 0 ? resultCode : len;
|
||||
}
|
||||
|
||||
bool CTCPSocket::write(const unsigned char* buffer, unsigned int length)
|
||||
{
|
||||
assert(buffer != NULL);
|
||||
assert(length > 0U);
|
||||
assert(m_fd != -1);
|
||||
|
||||
ssize_t ret = ::send(m_fd, (char *)buffer, length, 0);
|
||||
if (ret != ssize_t(length)) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
LogError("Error returned from send, err=%d", ::GetLastError());
|
||||
#else
|
||||
LogError("Error returned from send, err=%d", errno);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CTCPSocket::writeLine(const std::string& line)
|
||||
{
|
||||
std::string lineCopy(line);
|
||||
if (lineCopy.length() > 0 && lineCopy.at(lineCopy.length() - 1) != '\n')
|
||||
lineCopy.append("\n");
|
||||
|
||||
//stupidly write one char after the other
|
||||
size_t len = lineCopy.length();
|
||||
bool result = true;
|
||||
for (size_t i = 0U; i < len && result; i++){
|
||||
unsigned char c = lineCopy.at(i);
|
||||
result = write(&c , 1);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void CTCPSocket::close()
|
||||
{
|
||||
if (m_fd != -1) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
::closesocket(m_fd);
|
||||
#else
|
||||
::close(m_fd);
|
||||
#endif
|
||||
m_fd = -1;
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010,2011,2012,2013,2016 by Jonathan Naylor G4KLX
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef TCPSocket_H
|
||||
#define TCPSocket_H
|
||||
|
||||
#if !defined(_WIN32) && !defined(_WIN64)
|
||||
#include <netdb.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#else
|
||||
#include <winsock.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
|
||||
class CTCPSocket {
|
||||
public:
|
||||
CTCPSocket(const std::string& address, unsigned int port);
|
||||
~CTCPSocket();
|
||||
|
||||
bool open();
|
||||
|
||||
int read(unsigned char* buffer, unsigned int length, unsigned int secs, unsigned int msecs = 0U);
|
||||
int readLine(std::string& line, unsigned int secs);
|
||||
bool write(const unsigned char* buffer, unsigned int length);
|
||||
bool writeLine(const std::string& line);
|
||||
|
||||
void close();
|
||||
|
||||
private:
|
||||
std::string m_address;
|
||||
unsigned short m_port;
|
||||
int m_fd;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in new issue