-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathbridgeinfodb.cpp
More file actions
98 lines (71 loc) · 2.32 KB
/
bridgeinfodb.cpp
File metadata and controls
98 lines (71 loc) · 2.32 KB
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
#include "bridgeinfodb.h"
using std::unordered_map;
BridgeInfoForSerializing::BridgeInfoForSerializing(const BridgeConfig &bridge) :
prefix(bridge.clientidPrefix),
clientId(bridge.getClientid()),
client_group_share_name("") // In this context (constructing from BridgeConfig) we don't save this. It's done at an earlier stage.
{
}
std::list<BridgeInfoForSerializing> BridgeInfoForSerializing::getBridgeInfosForSerializing(const unordered_map<std::string, BridgeConfig> &input)
{
std::list<BridgeInfoForSerializing> result;
for (auto &pair : input)
{
const BridgeConfig &bridge = pair.second;
result.emplace_back(bridge);
}
return result;
}
BridgeInfoDb::BridgeInfoDb(const std::string &filePath) : PersistenceFile(filePath)
{
}
void BridgeInfoDb::openWrite()
{
PersistenceFile::openWrite(MAGIC_STRING_BRIDGEINFO_FILE_V2);
}
void BridgeInfoDb::openRead()
{
const std::string current_magic_string(MAGIC_STRING_BRIDGEINFO_FILE_V2);
PersistenceFile::openRead(current_magic_string);
if (detectedVersionString == MAGIC_STRING_BRIDGEINFO_FILE_V1)
readVersion = ReadVersion::v1;
else if (detectedVersionString == current_magic_string)
readVersion = ReadVersion::v2;
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);
writeString(b.client_group_share_name);
}
}
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);
if (readVersion >= ReadVersion::v2)
r.client_group_share_name = readString(eofFound);
result.push_back(std::move(r));
}
}
return result;
}