-
Notifications
You must be signed in to change notification settings - Fork 91
/
Reflectors.cpp
106 lines (87 loc) · 2.63 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
/*
* Copyright (C) 2016,2017 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 "Log.h"
#include <algorithm>
#include <functional>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <cctype>
CReflectors::CReflectors(const std::string& hostsFile, unsigned int reloadTime) :
m_hostsFile(hostsFile),
m_reflectors(),
m_timer(1000U, reloadTime * 60U)
{
if (reloadTime > 0U)
m_timer.start();
}
CReflectors::~CReflectors()
{
for (std::vector<CReflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it)
delete *it;
m_reflectors.clear();
}
bool CReflectors::load()
{
for (std::vector<CReflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it)
delete *it;
m_reflectors.clear();
FILE* fp = ::fopen(m_hostsFile.c_str(), "rt");
if (fp != NULL) {
char buffer[100U];
while (::fgets(buffer, 100U, fp) != NULL) {
if (buffer[0U] == '#')
continue;
char* p1 = ::strtok(buffer, ";\r\n");
char* p2 = ::strtok(NULL, ";\r\n");
char* p3 = ::strtok(NULL, "\r\n");
if (p1 != NULL && p2 != NULL && p3 != NULL) {
CReflector* refl = new CReflector;
refl->m_id = std::string(p1);
refl->m_address = std::string(p2);
refl->m_startup = (unsigned int)::atoi(p3);
m_reflectors.push_back(refl);
}
}
::fclose(fp);
}
size_t size = m_reflectors.size();
LogInfo("Loaded %u XLX reflectors", size);
size = m_reflectors.size();
if (size == 0U)
return false;
return true;
}
CReflector* CReflectors::find(const std::string &id)
{
for (std::vector<CReflector*>::iterator it = m_reflectors.begin(); it != m_reflectors.end(); ++it) {
if (id == (*it)->m_id)
return *it;
}
LogMessage("Trying to find non existent XLX reflector with an id of %s", id.c_str());
return NULL;
}
void CReflectors::clock(unsigned int ms)
{
m_timer.clock(ms);
if (m_timer.isRunning() && m_timer.hasExpired()) {
load();
m_timer.start();
}
}