#include <IdentityManager.h>
Definition at line 16 of file IdentityManager.h.
◆ all()
| const std::unordered_map< std::string, PeerIdentity > & IdentityManager::all |
( |
| ) |
const |
|
inline |
Definition at line 53 of file IdentityManager.h.
54 {
56 }
std::unordered_map< std::string, PeerIdentity > byUnique_
◆ bindPeer()
| void IdentityManager::bindPeer |
( |
const std::string & | peerId, |
|
|
const std::string & | uniqueId, |
|
|
const std::string & | username ) |
Definition at line 96 of file IdentityManager.cpp.
99{
100
102 {
103 const std::string& oldUid = it->second;
104 if (oldUid != uniqueId)
105 {
106
108 if (bit !=
byUnique_.end() && bit->second.peerId == peerId)
109 bit->second.peerId.clear();
110 }
112 }
113
114
116 if (!pi.peerId.empty() && pi.peerId != peerId)
117 {
119 }
120
121
123
124 pi.uniqueId = uniqueId;
125 pi.peerId = peerId;
126
127 if (!pi.username.empty() && pi.username != username)
128 {
129 pi.usernamesHistory.emplace_back(pi.username);
130 if (pi.usernamesHistory.size() > 10)
131 pi.usernamesHistory.erase(pi.usernamesHistory.begin());
132 }
133 pi.username = username;
134
136}
bool saveAddressBookToFile() const
std::unordered_map< std::string, std::string > peerToUnique_
◆ erasePeer()
| void IdentityManager::erasePeer |
( |
const std::string & | peerId | ) |
|
Definition at line 138 of file IdentityManager.cpp.
139{
141 {
142 const std::string uid = it->second;
145 if (bit !=
byUnique_.end() && bit->second.peerId == peerId)
146 bit->second.peerId.clear();
147 }
148}
◆ kBookFile()
| const char * IdentityManager::kBookFile |
( |
| ) |
|
|
staticprivate |
Definition at line 22 of file IdentityManager.cpp.
23{
25 return s_book.c_str();
26}
static fs::path getConfigPath()
◆ kMeFile()
| const char * IdentityManager::kMeFile |
( |
| ) |
|
|
staticprivate |
◆ loadAddressBookFromFile()
| bool IdentityManager::loadAddressBookFromFile |
( |
| ) |
|
◆ loadMyIdentityFromFile()
| bool IdentityManager::loadMyIdentityFromFile |
( |
| ) |
|
◆ myUniqueId()
| const std::string & IdentityManager::myUniqueId |
( |
| ) |
const |
|
inline |
◆ myUsername()
| const std::string & IdentityManager::myUsername |
( |
| ) |
const |
|
inline |
◆ peerForUnique()
| std::optional< std::string > IdentityManager::peerForUnique |
( |
const std::string & | uniqueId | ) |
const |
Definition at line 196 of file IdentityManager.cpp.
197{
198 if (
auto it =
byUnique_.find(uniqueId); it !=
byUnique_.end() && !it->second.peerId.empty())
199 return it->second.peerId;
200 return std::nullopt;
201}
◆ readBookFile()
| bool IdentityManager::readBookFile |
( |
| ) |
|
|
private |
Definition at line 313 of file IdentityManager.cpp.
314{
315 namespace fs = std::filesystem;
317 return true;
318
319 try
320 {
321 std::ifstream is(
kBookFile(), std::ios::binary);
322 is.seekg(0, std::ios::end);
323 auto sz = is.tellg();
324 is.seekg(0, std::ios::beg);
325 std::vector<uint8_t> buf(static_cast<size_t>(sz));
326 if (sz > 0)
327 is.read(reinterpret_cast<char*>(buf.data()), sz);
328
329 size_t off = 0;
331 return false;
333
335
336
337
338 return false;
339 }
340 catch (...)
341 {
342 return false;
343 }
344}
static constexpr const char * kBOOK_MAGIC
static const char * kBookFile()
static int deserializeInt(const std::vector< unsigned char > &buffer, size_t &offset)
static std::string deserializeString(const std::vector< unsigned char > &buffer, size_t &offset)
◆ readMeFile()
| bool IdentityManager::readMeFile |
( |
| ) |
|
|
private |
Definition at line 250 of file IdentityManager.cpp.
251{
252 namespace fs = std::filesystem;
254 return true;
255
256 try
257 {
258 std::ifstream is(
kMeFile(), std::ios::binary);
259 is.seekg(0, std::ios::end);
260 auto sz = is.tellg();
261 is.seekg(0, std::ios::beg);
262 std::vector<uint8_t> buf(static_cast<size_t>(sz));
263 if (sz > 0)
264 is.read(reinterpret_cast<char*>(buf.data()), sz);
265
266 size_t off = 0;
268 return false;
272 return true;
273 }
274 catch (...)
275 {
276 return false;
277 }
278}
static constexpr const char * kME_MAGIC
static const char * kMeFile()
◆ saveAddressBookToFile()
| bool IdentityManager::saveAddressBookToFile |
( |
| ) |
const |
◆ saveMyIdentityToFile()
| bool IdentityManager::saveMyIdentityToFile |
( |
| ) |
const |
◆ setMyIdentity()
| void IdentityManager::setMyIdentity |
( |
const std::string & | uniqueId, |
|
|
const std::string & | username ) |
Definition at line 41 of file IdentityManager.cpp.
42{
46
47
49 pi.uniqueId = uniqueId;
50 if (!pi.username.empty() && pi.username != username)
51 {
52 pi.usernamesHistory.emplace_back(pi.username);
53 if (pi.usernamesHistory.size() > 10)
54 pi.usernamesHistory.erase(pi.usernamesHistory.begin());
55 }
56 pi.username = username;
58}
bool saveMyIdentityToFile() const
◆ setUsernameForUnique()
| void IdentityManager::setUsernameForUnique |
( |
const std::string & | uniqueId, |
|
|
const std::string & | username ) |
Definition at line 151 of file IdentityManager.cpp.
152{
154 {
156 {
159 }
160 }
161
163 if (!pi.username.empty() && pi.username != username)
164 {
165 pi.usernamesHistory.emplace_back(pi.username);
166 if (pi.usernamesHistory.size() > 10)
167 pi.usernamesHistory.erase(pi.usernamesHistory.begin());
168 }
169 pi.uniqueId = uniqueId;
170 pi.username = username;
172}
◆ uniqueForPeer()
| std::optional< std::string > IdentityManager::uniqueForPeer |
( |
const std::string & | peerId | ) |
const |
◆ usernameForPeer()
| std::optional< std::string > IdentityManager::usernameForPeer |
( |
const std::string & | peerId | ) |
const |
Definition at line 219 of file IdentityManager.cpp.
220{
222 if (kv.first == peerId)
224 return std::nullopt;
225}
std::string usernameForUnique(const std::string &uniqueId) const
◆ usernameForUnique()
| std::string IdentityManager::usernameForUnique |
( |
const std::string & | uniqueId | ) |
const |
Definition at line 174 of file IdentityManager.cpp.
175{
177 if (it !=
byUnique_.end() && !it->second.username.empty())
178 return it->second.username;
179
182
183
184 if (uniqueId.size() > 8)
185 return uniqueId.substr(0, 8);
186 return uniqueId;
187}
◆ writeBookFile()
| bool IdentityManager::writeBookFile |
( |
| ) |
const |
|
private |
Definition at line 280 of file IdentityManager.cpp.
281{
282 try
283 {
284 std::vector<uint8_t> buf;
287
288
291 {
295
296
298 for (const auto& old : pi.usernamesHistory)
300 }
301
302 std::ofstream os(
kBookFile(), std::ios::binary | std::ios::trunc);
303 os.write(reinterpret_cast<const char*>(buf.data()),
304 static_cast<std::streamsize>(buf.size()));
305 return true;
306 }
307 catch (...)
308 {
309 return false;
310 }
311}
static constexpr const int kBOOK_VER
static void serializeString(std::vector< unsigned char > &buffer, const std::string &str)
static void serializeInt(std::vector< unsigned char > &buffer, int value)
◆ writeMeFile()
| bool IdentityManager::writeMeFile |
( |
| ) |
const |
|
private |
Definition at line 229 of file IdentityManager.cpp.
230{
231 try
232 {
233 std::vector<uint8_t> buf;
238
239 std::ofstream os(
kMeFile(), std::ios::binary | std::ios::trunc);
240 os.write(reinterpret_cast<const char*>(buf.data()),
241 static_cast<std::streamsize>(buf.size()));
242 return true;
243 }
244 catch (...)
245 {
246 return false;
247 }
248}
static constexpr const int kME_VER
◆ byUnique_
| std::unordered_map<std::string, PeerIdentity> IdentityManager::byUnique_ |
|
private |
◆ myUniqueId_
| std::string IdentityManager::myUniqueId_ |
|
private |
◆ myUsername_
| std::string IdentityManager::myUsername_ |
|
private |
◆ peerToUnique_
| std::unordered_map<std::string, std::string> IdentityManager::peerToUnique_ |
|
private |
The documentation for this class was generated from the following files: