-
Notifications
You must be signed in to change notification settings - Fork 23
/
bridgeinfodb.cpp
93 lines (66 loc) · 1.99 KB
/
bridgeinfodb.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
#include "bridgeinfodb.h"
BridgeInfoForSerializing::BridgeInfoForSerializing(const std::shared_ptr<BridgeConfig> bridge) :
prefix(bridge->clientidPrefix),
clientId(bridge->getClientid())
{
}
std::list<BridgeInfoForSerializing> BridgeInfoForSerializing::getBridgeInfosForSerializing(const std::unordered_map<std::string, std::shared_ptr<BridgeConfig>> &input)
{
std::list<BridgeInfoForSerializing> result;
for (auto &pair : input)
{
const std::shared_ptr<BridgeConfig> &bridge = pair.second;
if (!bridge)
continue;
result.emplace_back(bridge);
}
return result;
}
BridgeInfoDb::BridgeInfoDb(const std::string &filePath) : PersistenceFile(filePath)
{
}
void BridgeInfoDb::openWrite()
{
PersistenceFile::openWrite(MAGIC_STRING_BRIDGEINFO_FILE_V1);
}
void BridgeInfoDb::openRead()
{
const std::string current_magic_string(MAGIC_STRING_BRIDGEINFO_FILE_V1);
PersistenceFile::openRead(current_magic_string);
if (detectedVersionString == current_magic_string)
readVersion = ReadVersion::v1;
else
throw std::runtime_error("Unknown file version.");
}
void BridgeInfoDb::saveInfo(const std::list<BridgeInfoForSerializing> &bridgeInfos)
{
if (!f)
return;
writeUint32(bridgeInfos.size());
for (const BridgeInfoForSerializing &b : bridgeInfos)
{
writeString(b.prefix);
writeString(b.clientId);
}
}
std::list<BridgeInfoForSerializing> BridgeInfoDb::readInfo()
{
std::list<BridgeInfoForSerializing> result;
if (!f)
return result;
while (!feof(f))
{
bool eofFound = false;
uint32_t number_of_bridges = readUint32(eofFound);
if (eofFound)
continue;
for (uint32_t i = 0; i < number_of_bridges; i++)
{
BridgeInfoForSerializing r;
r.prefix = readString(eofFound);
r.clientId = readString(eofFound);
result.push_back(std::move(r));
}
}
return result;
}