1
0
Fork 0

Hopefully fixed the reflector.

ycs232-kbc
Jonathan Naylor 8 years ago
parent 1927af2fcf
commit fcbd984c02

@ -24,17 +24,12 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
const unsigned int BUFFER_LENGTH = 200U;
CNetwork::CNetwork(unsigned int port, const std::string& name, const std::string& description, bool debug) : CNetwork::CNetwork(unsigned int port, const std::string& name, const std::string& description, bool debug) :
m_socket(port), m_socket(port),
m_name(name), m_name(name),
m_description(description), m_description(description),
m_address(),
m_port(0U),
m_callsign(), m_callsign(),
m_debug(debug), m_debug(debug),
m_buffer(1000U, "YSF Network"),
m_status(NULL) m_status(NULL)
{ {
m_name.resize(16U, ' '); m_name.resize(16U, ' ');
@ -91,68 +86,27 @@ bool CNetwork::writePoll(const in_addr& address, unsigned int port)
return m_socket.write(buffer, 14U, address, port); return m_socket.write(buffer, 14U, address, port);
} }
void CNetwork::clock(unsigned int ms) unsigned int CNetwork::readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port)
{ {
unsigned char buffer[BUFFER_LENGTH]; assert(data != NULL);
assert(length > 0U);
in_addr address;
unsigned int port; int len = m_socket.read(data, length, address, port);
int length = m_socket.read(buffer, BUFFER_LENGTH, address, port); if (len <= 0)
if (length <= 0) return 0U;
return;
// Handle incoming polls
if (::memcmp(buffer, "YSFP", 4U) == 0) {
m_callsign = std::string((char*)(buffer + 4U), YSF_CALLSIGN_LENGTH);
m_address = address;
m_port = port;
return;
}
// Handle incoming status requests // Handle incoming status requests
if (::memcmp(buffer, "YSFS", 4U) == 0) { if (::memcmp(data, "YSFS", 4U) == 0) {
m_socket.write(m_status, 42U, address, port); m_socket.write(m_status, 42U, address, port);
return; return 0U;
} }
if (m_debug) if (m_debug)
CUtils::dump(1U, "YSF Network Data Received", buffer, length); CUtils::dump(1U, "YSF Network Data Received", data, len);
unsigned char len = length;
m_buffer.addData(&len, 1U);
m_buffer.addData(buffer, length);
}
unsigned int CNetwork::readData(unsigned char* data)
{
assert(data != NULL);
if (m_buffer.isEmpty())
return 0U;
unsigned char len = 0U;
m_buffer.getData(&len, 1U);
m_buffer.getData(data, len);
return len; return len;
} }
bool CNetwork::readPoll(std::string& callsign, in_addr& address, unsigned int& port)
{
if (m_port == 0U)
return false;
callsign = m_callsign;
address = m_address;
port = m_port;
m_port = 0U;
return true;
}
void CNetwork::setCount(unsigned int count) void CNetwork::setCount(unsigned int count)
{ {
if (count > 999U) if (count > 999U)

@ -20,7 +20,6 @@
#define Network_H #define Network_H
#include "YSFDefines.h" #include "YSFDefines.h"
#include "RingBuffer.h"
#include "UDPSocket.h" #include "UDPSocket.h"
#include "Timer.h" #include "Timer.h"
@ -37,24 +36,18 @@ public:
bool writeData(const unsigned char* data, const in_addr& address, unsigned int port); bool writeData(const unsigned char* data, const in_addr& address, unsigned int port);
bool writePoll(const in_addr& address, unsigned int port); bool writePoll(const in_addr& address, unsigned int port);
unsigned int readData(unsigned char* data); unsigned int readData(unsigned char* data, unsigned int length, in_addr& address, unsigned int& port);
bool readPoll(std::string& callsign, in_addr& address, unsigned int& port);
void close(); void close();
void clock(unsigned int ms);
void setCount(unsigned int count); void setCount(unsigned int count);
private: private:
CUDPSocket m_socket; CUDPSocket m_socket;
std::string m_name; std::string m_name;
std::string m_description; std::string m_description;
in_addr m_address;
unsigned int m_port;
std::string m_callsign; std::string m_callsign;
bool m_debug; bool m_debug;
CRingBuffer<unsigned char> m_buffer;
unsigned char* m_status; unsigned char* m_status;
}; };

@ -1,147 +0,0 @@
/*
* Copyright (C) 2006-2009,2012,2013,2015,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 RingBuffer_H
#define RingBuffer_H
#include <cstdio>
#include <cassert>
#include <cstring>
template<class T> class CRingBuffer {
public:
CRingBuffer(unsigned int length, const char* name) :
m_length(length),
m_name(name),
m_buffer(NULL),
m_iPtr(0U),
m_oPtr(0U)
{
assert(length > 0U);
assert(name != NULL);
m_buffer = new T[length];
::memset(m_buffer, 0x00, m_length * sizeof(T));
}
~CRingBuffer()
{
delete[] m_buffer;
}
bool addData(const T* buffer, unsigned int nSamples)
{
if (nSamples >= freeSpace()) {
::fprintf(stderr, "**** Overflow in %s ring buffer, %u >= %u\n", m_name, nSamples, freeSpace());
return false;
}
for (unsigned int i = 0U; i < nSamples; i++) {
m_buffer[m_iPtr++] = buffer[i];
if (m_iPtr == m_length)
m_iPtr = 0U;
}
return true;
}
bool getData(T* buffer, unsigned int nSamples)
{
if (dataSize() < nSamples) {
::fprintf(stderr, "**** Underflow in %s ring buffer, %u < %u\n", m_name, dataSize(), nSamples);
return false;
}
for (unsigned int i = 0U; i < nSamples; i++) {
buffer[i] = m_buffer[m_oPtr++];
if (m_oPtr == m_length)
m_oPtr = 0U;
}
return true;
}
bool peek(T* buffer, unsigned int nSamples)
{
if (dataSize() < nSamples) {
::fprintf(stderr, "**** Underflow peek in %s ring buffer, %u < %u\n", m_name, dataSize(), nSamples);
return false;
}
unsigned int ptr = m_oPtr;
for (unsigned int i = 0U; i < nSamples; i++) {
buffer[i] = m_buffer[ptr++];
if (ptr == m_length)
ptr = 0U;
}
return true;
}
void clear()
{
m_iPtr = 0U;
m_oPtr = 0U;
::memset(m_buffer, 0x00, m_length * sizeof(T));
}
unsigned int freeSpace() const
{
if (m_oPtr == m_iPtr)
return m_length;
if (m_oPtr > m_iPtr)
return m_oPtr - m_iPtr;
return (m_length + m_oPtr) - m_iPtr;
}
unsigned int dataSize() const
{
return m_length - freeSpace();
}
bool hasSpace(unsigned int length) const
{
return freeSpace() > length;
}
bool hasData() const
{
return m_oPtr != m_iPtr;
}
bool isEmpty() const
{
return m_oPtr == m_iPtr;
}
private:
unsigned int m_length;
const char* m_name;
T* m_buffer;
unsigned int m_iPtr;
unsigned int m_oPtr;
};
#endif

@ -186,38 +186,28 @@ void CYSFReflector::run()
for (;;) { for (;;) {
unsigned char buffer[200U]; unsigned char buffer[200U];
// Refresh/add repeaters based on their polls
in_addr address; in_addr address;
unsigned int port; unsigned int port;
std::string callsign;
bool ret = network.readPoll(callsign, address, port);
if (ret) {
CYSFRepeater* rpt = findRepeater(callsign, address, port);
if (rpt == NULL) {
LogMessage("Adding %s", callsign.c_str());
rpt = new CYSFRepeater;
rpt->m_timer.start();
rpt->m_callsign = callsign;
rpt->m_address = address;
rpt->m_port = port;
m_repeaters.push_back(rpt);
network.setCount(m_repeaters.size());
} else {
rpt->m_timer.start();
rpt->m_callsign = callsign;
rpt->m_address = address;
rpt->m_port = port;
}
}
unsigned int len = network.readData(buffer); unsigned int len = network.readData(buffer, 200U, address, port);
if (len > 0U) { if (len > 0U) {
if (::memcmp(buffer + 0U, "YSFU", 4U) == 0) { if (::memcmp(buffer, "YSFP", 4U) == 0) {
CYSFRepeater* rpt = findRepeater(address, port);
if (rpt == NULL) {
rpt = new CYSFRepeater;
rpt->m_callsign = std::string((char*)(buffer + 4U), 10U);
rpt->m_address = address;
rpt->m_port = port;
m_repeaters.push_back(rpt);
network.setCount(m_repeaters.size());
LogMessage("Adding %s", rpt->m_callsign.c_str());
}
rpt->m_timer.start();
} else if (::memcmp(buffer + 0U, "YSFU", 4U) == 0) {
std::string callsign = std::string((char*)(buffer + 4U), 10U); std::string callsign = std::string((char*)(buffer + 4U), 10U);
CYSFRepeater* rpt = findRepeater(callsign); CYSFRepeater* rpt = findRepeater(address, port);
if (rpt != NULL) { if (rpt != NULL) {
LogMessage("Removing %s (unlinked)", callsign.c_str()); LogMessage("Removing %s (unlinked)", rpt->m_callsign.c_str());
std::vector<CYSFRepeater*>::iterator it = std::find(m_repeaters.begin(), m_repeaters.end(), rpt); std::vector<CYSFRepeater*>::iterator it = std::find(m_repeaters.begin(), m_repeaters.end(), rpt);
if (it != m_repeaters.end()) if (it != m_repeaters.end())
m_repeaters.erase(it); m_repeaters.erase(it);
@ -262,7 +252,7 @@ void CYSFReflector::run()
watchdogTimer.start(); watchdogTimer.start();
for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) { for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
if ((*it)->m_callsign != callsign) if ((*it)->m_address.s_addr != address.s_addr && (*it)->m_port != port)
network.writeData(buffer, (*it)->m_address, (*it)->m_port); network.writeData(buffer, (*it)->m_address, (*it)->m_port);
} }
@ -277,8 +267,6 @@ void CYSFReflector::run()
unsigned int ms = stopWatch.elapsed(); unsigned int ms = stopWatch.elapsed();
stopWatch.start(); stopWatch.start();
network.clock(ms);
pollTimer.clock(ms); pollTimer.clock(ms);
if (pollTimer.hasExpired()) { if (pollTimer.hasExpired()) {
for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it)
@ -325,20 +313,10 @@ void CYSFReflector::run()
::LogFinalise(); ::LogFinalise();
} }
CYSFRepeater* CYSFReflector::findRepeater(const std::string& callsign, const in_addr& address, unsigned int port) const CYSFRepeater* CYSFReflector::findRepeater(const in_addr& address, unsigned int port) const
{
for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
if ((*it)->m_callsign == callsign || (address.s_addr == (*it)->m_address.s_addr && (*it)->m_port == port))
return *it;
}
return NULL;
}
CYSFRepeater* CYSFReflector::findRepeater(const std::string& callsign) const
{ {
for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) { for (std::vector<CYSFRepeater*>::const_iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
if ((*it)->m_callsign == callsign) if (address.s_addr == (*it)->m_address.s_addr && (*it)->m_port == port)
return *it; return *it;
} }

@ -66,8 +66,7 @@ private:
CConf m_conf; CConf m_conf;
std::vector<CYSFRepeater*> m_repeaters; std::vector<CYSFRepeater*> m_repeaters;
CYSFRepeater* findRepeater(const std::string& callsign, const in_addr& address, unsigned int port) const; CYSFRepeater* findRepeater(const in_addr& address, unsigned int port) const;
CYSFRepeater* findRepeater(const std::string& callsign) const;
void dumpRepeaters() const; void dumpRepeaters() const;
}; };

@ -149,7 +149,6 @@
<ClInclude Include="Conf.h" /> <ClInclude Include="Conf.h" />
<ClInclude Include="Log.h" /> <ClInclude Include="Log.h" />
<ClInclude Include="Network.h" /> <ClInclude Include="Network.h" />
<ClInclude Include="RingBuffer.h" />
<ClInclude Include="StopWatch.h" /> <ClInclude Include="StopWatch.h" />
<ClInclude Include="Timer.h" /> <ClInclude Include="Timer.h" />
<ClInclude Include="UDPSocket.h" /> <ClInclude Include="UDPSocket.h" />

@ -29,9 +29,6 @@
<ClInclude Include="YSFReflector.h"> <ClInclude Include="YSFReflector.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="RingBuffer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="StopWatch.h"> <ClInclude Include="StopWatch.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>

Loading…
Cancel
Save