-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathReflectors.cpp
160 lines (132 loc) · 4.05 KB
/
Reflectors.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (C) 2016,2018,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 "Reflectors.h"
#include "M17Defines.h"
#include "Log.h"
#include <algorithm>
#include <functional>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <cctype>
CReflectors::CReflectors(const std::string& hostsFile1, const std::string& hostsFile2, unsigned int reloadTime) :
m_hostsFile1(hostsFile1),
m_hostsFile2(hostsFile2),
m_reflectors(),
m_timer(1000U, reloadTime * 60U)
{
if (reloadTime > 0U)
m_timer.start();
}
CReflectors::~CReflectors()
{
for (std::vector<CM17Reflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it)
delete *it;
m_reflectors.clear();
}
bool CReflectors::load()
{
// Clear out the old reflector list
for (std::vector<CM17Reflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it)
delete *it;
m_reflectors.clear();
FILE* fp = ::fopen(m_hostsFile1.c_str(), "rt");
if (fp != NULL) {
char buffer[100U];
while (::fgets(buffer, 100U, fp) != NULL) {
if (buffer[0U] == '#')
continue;
char* p1 = ::strtok(buffer, " \t\r\n");
char* p2 = ::strtok(NULL, " \t\r\n");
char* p3 = ::strtok(NULL, " \t\r\n");
if (p1 != NULL && p2 != NULL && p3 != NULL) {
std::string name = std::string(p1);
name.resize(M17_CALLSIGN_LENGTH - 2U, ' ');
std::string host = std::string(p2);
unsigned int port= (unsigned int)::atoi(p3);
sockaddr_storage addr;
unsigned int addrLen;
if (CUDPSocket::lookup(host, port, addr, addrLen) == 0) {
CM17Reflector* refl = new CM17Reflector;
refl->m_name = name;
refl->m_addr = addr;
refl->m_addrLen = addrLen;
m_reflectors.push_back(refl);
} else {
LogWarning("Unable to resolve the address of %s", host.c_str());
}
}
}
::fclose(fp);
}
fp = ::fopen(m_hostsFile2.c_str(), "rt");
if (fp != NULL) {
char buffer[100U];
while (::fgets(buffer, 100U, fp) != NULL) {
if (buffer[0U] == '#')
continue;
char* p1 = ::strtok(buffer, " \t\r\n");
char* p2 = ::strtok(NULL, " \t\r\n");
char* p3 = ::strtok(NULL, " \t\r\n");
if (p1 != NULL && p2 != NULL && p3 != NULL) {
// Don't allow duplicate reflector ids from the secondary hosts file.
std::string name = std::string(p1);
name.resize(M17_CALLSIGN_LENGTH - 2U, ' ');
if (find(name) == NULL) {
std::string host = std::string(p2);
unsigned int port = (unsigned int)::atoi(p3);
sockaddr_storage addr;
unsigned int addrLen;
if (CUDPSocket::lookup(host, port, addr, addrLen) == 0) {
CM17Reflector* refl = new CM17Reflector;
refl->m_name = name;
refl->m_addr = addr;
refl->m_addrLen = addrLen;
m_reflectors.push_back(refl);
} else {
LogWarning("Unable to resolve the address of %s", host.c_str());
}
}
}
}
::fclose(fp);
}
size_t size = m_reflectors.size();
LogInfo("Loaded %u M17 reflectors", size);
if (size == 0U)
return false;
return true;
}
CM17Reflector* CReflectors::find(const std::string& name)
{
std::string nm = name;
nm.resize(7U);
for (std::vector<CM17Reflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it) {
if (nm == (*it)->m_name)
return *it;
}
return NULL;
}
void CReflectors::clock(unsigned int ms)
{
m_timer.clock(ms);
if (m_timer.isRunning() && m_timer.hasExpired()) {
load();
m_timer.start();
}
}