-
Notifications
You must be signed in to change notification settings - Fork 1
/
DExtraProtocolHandlerPool.cpp
116 lines (104 loc) · 3.09 KB
/
DExtraProtocolHandlerPool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (C) 2012,2013 by Jonathan Naylor G4KLX
* Copyright (c) 2017-2018 by Thomas A. Early
*
* 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 <cassert>
#include "DExtraProtocolHandlerPool.h"
#include "Utils.h"
CDExtraProtocolHandlerPool::CDExtraProtocolHandlerPool(const unsigned int port, const std::string &addr) :
m_basePort(port),
m_address(addr)
{
assert(port > 0U);
m_index = m_pool.end();
printf("DExtra UDP port base = %u\n", port);
}
CDExtraProtocolHandlerPool::~CDExtraProtocolHandlerPool()
{
while (m_pool.end() != m_pool.begin()) {
auto it = m_pool.begin();
delete it->second;
m_pool.erase(it);
}
}
CDExtraProtocolHandler* CDExtraProtocolHandlerPool::getHandler()
{
unsigned int port = m_basePort;
while (m_pool.end() != m_pool.find(port))
port++; // find an unused port
CDExtraProtocolHandler *proto = new CDExtraProtocolHandler(port, m_address);
if (proto) {
if (proto->open()) {
m_pool[port] = proto;
printf("New CDExtraProtocolHandler now on UDP port %u.\n", port);
} else {
delete proto;
proto = NULL;
printf("ERROR: Can't open new DExtra UDP port %u!\n", port);
}
} else
printf("ERROR: Can't allocate new CDExtraProtocolHandler at port %u\n", port);
return proto;
}
void CDExtraProtocolHandlerPool::release(CDExtraProtocolHandler *handler)
{
assert(handler != NULL);
for (auto it=m_pool.begin(); it!=m_pool.end(); it++) {
if (it->second == handler) {
it->second->close();
delete it->second;
printf("Releasing CDExtraProtocolHandler on port %u.\n", it->first);
m_pool.erase(it);
return;
}
}
// we should never get here!
printf("ERROR: could not find CDExtraProtocolHander (port=%u) to release!\n", handler->getPort());
}
DEXTRA_TYPE CDExtraProtocolHandlerPool::read()
{
if (m_index == m_pool.end())
m_index = m_pool.begin();
while (m_index != m_pool.end()) {
DEXTRA_TYPE type = m_index->second->read();
if (type != DE_NONE)
return type;
m_index++;
}
return DE_NONE;
}
CHeaderData *CDExtraProtocolHandlerPool::newHeader()
{
return m_index->second->newHeader();
}
CAMBEData *CDExtraProtocolHandlerPool::newAMBE()
{
return m_index->second->newAMBE();
}
CPollData *CDExtraProtocolHandlerPool::newPoll()
{
return m_index->second->newPoll();
}
CConnectData *CDExtraProtocolHandlerPool::newConnect()
{
return m_index->second->newConnect();
}
void CDExtraProtocolHandlerPool::close()
{
for (auto it=m_pool.begin(); it!=m_pool.end(); it++)
it->second->close();
}