Handle DPlsu the same way has DCS and Dextra

master
Geoffrey Merck 4 years ago
parent 659e455d85
commit 5a46526bf2

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2012,2013,2015 by Jonathan Naylor G4KLX * Copyright (C) 2012,2013 by Jonathan Naylor G4KLX
* Copyright (c) 2017-2018 by Thomas A. Early
* Copyright (c) 2021 by Geoffrey Merck F4FXL / KC3FRA * Copyright (c) 2021 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
@ -20,122 +21,107 @@
#include <cassert> #include <cassert>
#include "DPlusProtocolHandlerPool.h" #include "DPlusProtocolHandlerPool.h"
#include "Log.h" #include "Utils.h"
CDPlusProtocolHandlerPool::CDPlusProtocolHandlerPool(unsigned int n, unsigned int port, const std::string& addr) : CDPlusProtocolHandlerPool::CDPlusProtocolHandlerPool(const unsigned int port, const std::string &addr) :
m_pool(NULL), m_basePort(port),
m_n(n), m_address(addr)
m_index(0U)
{ {
assert(port > 0U); assert(port > 0U);
assert(n > 0U); m_index = m_pool.end();
printf("DExtra UDP port base = %u\n", port);
m_pool = new CDPlusProtocolHandlerEntry[n];
for (unsigned int i = 0U; i < n; i++) {
m_pool[i].m_handler = new CDPlusProtocolHandler(port + i, addr);
m_pool[i].m_port = port + i;
m_pool[i].m_inUse = false;
}
wxLogMessage("Allocated UDP ports %u-%u to D-Plus", port, port + n - 1U);
} }
CDPlusProtocolHandlerPool::~CDPlusProtocolHandlerPool() CDPlusProtocolHandlerPool::~CDPlusProtocolHandlerPool()
{ {
for (unsigned int i = 0U; i < m_n; i++) while (m_pool.end() != m_pool.begin()) {
delete m_pool[i].m_handler; auto it = m_pool.begin();
delete it->second;
delete[] m_pool; m_pool.erase(it);
}
} }
bool CDPlusProtocolHandlerPool::open() CDPlusProtocolHandler* CDPlusProtocolHandlerPool::getIncomingHandler()
{ {
for (unsigned int i = 0U; i < m_n; i++) { return getHandler(m_basePort);
bool ret = m_pool[i].m_handler->open(); }
if (!ret)
return false;
}
return true; CDPlusProtocolHandler* CDPlusProtocolHandlerPool::getHandler()
{
return getHandler(m_basePort + 1U);
} }
CDPlusProtocolHandler* CDPlusProtocolHandlerPool::getHandler(unsigned int port) CDPlusProtocolHandler* CDPlusProtocolHandlerPool::getHandler(unsigned int port)
{ {
if (port == 0U) { while (m_pool.end() != m_pool.find(port))
for (unsigned int i = 0U; i < m_n; i++) { port++; // find an unused port
if (!m_pool[i].m_inUse) {
m_pool[i].m_inUse = true; CDPlusProtocolHandler *proto = new CDPlusProtocolHandler(port, m_address);
return m_pool[i].m_handler; if (proto) {
} if (proto->open()) {
} m_pool[port] = proto;
} else { printf("New CDPlusProtocolHandler now on UDP port %u.\n", port);
for (unsigned int i = 0U; i < m_n; i++) { } else {
if (m_pool[i].m_port == port) { delete proto;
m_pool[i].m_inUse = true; proto = NULL;
return m_pool[i].m_handler; printf("ERROR: Can't open new DPlus UDP port %u!\n", port);
}
} }
} } else
printf("ERROR: Can't allocate new CDPlusProtocolHandler at port %u\n", port);
wxLogError("Cannot find a free D-Plus port in the pool"); return proto;
return NULL;
} }
void CDPlusProtocolHandlerPool::release(CDPlusProtocolHandler* handler) void CDPlusProtocolHandlerPool::release(CDPlusProtocolHandler *handler)
{ {
assert(handler != NULL); assert(handler != NULL);
for (auto it=m_pool.begin(); it!=m_pool.end(); it++) {
for (unsigned int i = 0U; i < m_n; i++) { if (it->second == handler) {
if (m_pool[i].m_handler == handler && m_pool[i].m_inUse) { it->second->close();
m_pool[i].m_inUse = false; delete it->second;
printf("Releasing CDPlusProtocolHandler on port %u.\n", it->first);
m_pool.erase(it);
return; return;
} }
} }
// we should never get here!
wxLogError("Trying to release an unused D-Plus port"); printf("ERROR: could not find CDPlusProtocolHander (port=%u) to release!\n", handler->getPort());
} }
DPLUS_TYPE CDPlusProtocolHandlerPool::read() DPLUS_TYPE CDPlusProtocolHandlerPool::read()
{ {
while (m_index < m_n) { if (m_index == m_pool.end())
if (m_pool[m_index].m_inUse) { m_index = m_pool.begin();
DPLUS_TYPE type = m_pool[m_index].m_handler->read(); while (m_index != m_pool.end()) {
if (type != DP_NONE) DPLUS_TYPE type = m_index->second->read();
return type; if (type != DP_NONE)
} return type;
m_index++; m_index++;
} }
m_index = 0U;
return DP_NONE; return DP_NONE;
} }
CHeaderData* CDPlusProtocolHandlerPool::readHeader() CHeaderData *CDPlusProtocolHandlerPool::readHeader()
{ {
return m_pool[m_index].m_handler->readHeader(); return m_index->second->readHeader();
} }
CAMBEData* CDPlusProtocolHandlerPool::readAMBE() CAMBEData *CDPlusProtocolHandlerPool::readAMBE()
{ {
return m_pool[m_index].m_handler->readAMBE(); return m_index->second->readAMBE();
} }
CPollData* CDPlusProtocolHandlerPool::readPoll() CPollData *CDPlusProtocolHandlerPool::readPoll()
{ {
return m_pool[m_index].m_handler->readPoll(); return m_index->second->readPoll();
} }
CConnectData* CDPlusProtocolHandlerPool::readConnect() CConnectData *CDPlusProtocolHandlerPool::readConnect()
{ {
return m_pool[m_index].m_handler->readConnect(); return m_index->second->readConnect();
} }
void CDPlusProtocolHandlerPool::close() void CDPlusProtocolHandlerPool::close()
{ {
for (unsigned int i = 0U; i < m_n; i++) for (auto it=m_pool.begin(); it!=m_pool.end(); it++)
m_pool[i].m_handler->close(); it->second->close();
} }

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2012,2013,2015 by Jonathan Naylor G4KLX * Copyright (C) 2012,2013 by Jonathan Naylor G4KLX
* Copyright (c) 2021 by Geoffrey Merck F4FXL / KC3FRA * Copyright (c) 2017-2018 by Thomas A. Early
* *
* 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
@ -17,29 +17,23 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#ifndef DPlusProtocolHandlerPool_H #pragma once
#define DPlusProtocolHandlerPool_H
#include "DPlusProtocolHandler.h" #include <string>
#include <map>
class CDPlusProtocolHandlerEntry { #include "DPlusProtocolHandler.h"
public:
CDPlusProtocolHandler* m_handler;
unsigned int m_port;
bool m_inUse;
};
class CDPlusProtocolHandlerPool { class CDPlusProtocolHandlerPool {
public: public:
CDPlusProtocolHandlerPool(unsigned int n, unsigned int port, const std::string& addr = ""); CDPlusProtocolHandlerPool(const unsigned int port, const std::string &addr = std::string(""));
~CDPlusProtocolHandlerPool(); ~CDPlusProtocolHandlerPool();
bool open(); CDPlusProtocolHandler *getHandler();
CDPlusProtocolHandler *getIncomingHandler();
void release(CDPlusProtocolHandler *handler);
CDPlusProtocolHandler* getHandler(unsigned int port = 0U); DPLUS_TYPE read();
void release(CDPlusProtocolHandler* handler);
DPLUS_TYPE read();
CHeaderData* readHeader(); CHeaderData* readHeader();
CAMBEData* readAMBE(); CAMBEData* readAMBE();
CPollData* readPoll(); CPollData* readPoll();
@ -48,9 +42,10 @@ public:
void close(); void close();
private: private:
CDPlusProtocolHandlerEntry* m_pool; CDPlusProtocolHandler *getHandler(unsigned int port);
unsigned int m_n;
unsigned int m_index;
};
#endif std::map<unsigned int, CDPlusProtocolHandler *> m_pool;
std::map<unsigned int, CDPlusProtocolHandler *>::iterator m_index;
unsigned int m_basePort;
std::string m_address;
};

Loading…
Cancel
Save

Powered by TurnKey Linux.