25 return s_book.c_str();
49 pi.uniqueId = uniqueId;
50 if (!pi.username.empty() && pi.username != username)
52 pi.usernamesHistory.emplace_back(pi.username);
53 if (pi.usernamesHistory.size() > 10)
54 pi.usernamesHistory.erase(pi.usernamesHistory.begin());
56 pi.username = username;
97 const std::string& uniqueId,
98 const std::string& username)
103 const std::string& oldUid = it->second;
104 if (oldUid != uniqueId)
108 if (bit !=
byUnique_.end() && bit->second.peerId == peerId)
109 bit->second.peerId.clear();
116 if (!pi.peerId.empty() && pi.peerId != peerId)
124 pi.uniqueId = uniqueId;
127 if (!pi.username.empty() && pi.username != username)
129 pi.usernamesHistory.emplace_back(pi.username);
130 if (pi.usernamesHistory.size() > 10)
131 pi.usernamesHistory.erase(pi.usernamesHistory.begin());
133 pi.username = username;
142 const std::string uid = it->second;
145 if (bit !=
byUnique_.end() && bit->second.peerId == peerId)
146 bit->second.peerId.clear();
163 if (!pi.username.empty() && pi.username != username)
165 pi.usernamesHistory.emplace_back(pi.username);
166 if (pi.usernamesHistory.size() > 10)
167 pi.usernamesHistory.erase(pi.usernamesHistory.begin());
169 pi.uniqueId = uniqueId;
170 pi.username = username;
177 if (it !=
byUnique_.end() && !it->second.username.empty())
178 return it->second.username;
184 if (uniqueId.size() > 8)
185 return uniqueId.substr(0, 8);
198 if (
auto it =
byUnique_.find(uniqueId); it !=
byUnique_.end() && !it->second.peerId.empty())
199 return it->second.peerId;
222 if (kv.first == peerId)
233 std::vector<uint8_t> buf;
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()));
252 namespace fs = std::filesystem;
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));
264 is.read(
reinterpret_cast<char*
>(buf.data()), sz);
284 std::vector<uint8_t> buf;
298 for (
const auto& old : pi.usernamesHistory)
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()));
315 namespace fs = std::filesystem;
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));
327 is.read(
reinterpret_cast<char*
>(buf.data()), sz);
static constexpr const int kME_VER
static constexpr const char * kBOOK_MAGIC
static constexpr const char * kME_MAGIC
static constexpr const int kBOOK_VER
void setMyIdentity(const std::string &uniqueId, const std::string &username)
bool saveMyIdentityToFile() const
bool saveAddressBookToFile() const
std::optional< std::string > peerForUnique(const std::string &uniqueId) const
void bindPeer(const std::string &peerId, const std::string &uniqueId, const std::string &username)
void setUsernameForUnique(const std::string &uniqueId, const std::string &username)
std::optional< std::string > uniqueForPeer(const std::string &peerId) const
void erasePeer(const std::string &peerId)
std::string usernameForUnique(const std::string &uniqueId) const
static const char * kMeFile()
bool writeBookFile() const
std::optional< std::string > usernameForPeer(const std::string &peerId) const
bool loadAddressBookFromFile()
std::unordered_map< std::string, PeerIdentity > byUnique_
static const char * kBookFile()
std::unordered_map< std::string, std::string > peerToUnique_
bool loadMyIdentityFromFile()
static fs::path getConfigPath()
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)
static void serializeString(std::vector< unsigned char > &buffer, const std::string &str)
static void serializeInt(std::vector< unsigned char > &buffer, int value)