RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
IdentityManager Class Reference

#include <IdentityManager.h>

Collaboration diagram for IdentityManager:

Public Member Functions

bool loadMyIdentityFromFile ()
 
bool saveMyIdentityToFile () const
 
void setMyIdentity (const std::string &uniqueId, const std::string &username)
 
const std::string & myUniqueId () const
 
const std::string & myUsername () const
 
bool loadAddressBookFromFile ()
 
bool saveAddressBookToFile () const
 
void bindPeer (const std::string &peerId, const std::string &uniqueId, const std::string &username)
 
std::string usernameForUnique (const std::string &uniqueId) const
 
std::optional< std::string > uniqueForPeer (const std::string &peerId) const
 
std::optional< std::string > peerForUnique (const std::string &uniqueId) const
 
std::optional< std::string > usernameForPeer (const std::string &peerId) const
 
void setUsernameForUnique (const std::string &uniqueId, const std::string &username)
 
const std::unordered_map< std::string, PeerIdentity > & all () const
 
void erasePeer (const std::string &peerId)
 

Private Member Functions

bool writeMeFile () const
 
bool readMeFile ()
 
bool writeBookFile () const
 
bool readBookFile ()
 

Static Private Member Functions

static const char * kMeFile ()
 
static const char * kBookFile ()
 

Private Attributes

std::string myUniqueId_
 
std::string myUsername_
 
std::unordered_map< std::string, PeerIdentitybyUnique_
 
std::unordered_map< std::string, std::string > peerToUnique_
 

Detailed Description

Definition at line 16 of file IdentityManager.h.

Member Function Documentation

◆ all()

const std::unordered_map< std::string, PeerIdentity > & IdentityManager::all ( ) const
inline

Definition at line 53 of file IdentityManager.h.

54 {
55 return byUnique_;
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 // 1) If this peerId was previously mapped to some unique, clear that first.
101 if (auto it = peerToUnique_.find(peerId); it != peerToUnique_.end())
102 {
103 const std::string& oldUid = it->second;
104 if (oldUid != uniqueId)
105 {
106 // Unlink old mapping (the old unique keeps its record, but without this peerId)
107 auto bit = byUnique_.find(oldUid);
108 if (bit != byUnique_.end() && bit->second.peerId == peerId)
109 bit->second.peerId.clear();
110 }
111 peerToUnique_.erase(it);
112 }
113
114 // 2) If this uniqueId already had a different peerId, remove that stale reverse mapping.
115 auto& pi = byUnique_[uniqueId];
116 if (!pi.peerId.empty() && pi.peerId != peerId)
117 {
118 peerToUnique_.erase(pi.peerId); // remove old peerId -> uniqueId
119 }
120
121 // 3) Set the fresh links (one-to-one at a time)
122 peerToUnique_[peerId] = uniqueId;
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
135 (void)saveAddressBookToFile();
136}
bool saveAddressBookToFile() const
std::unordered_map< std::string, std::string > peerToUnique_
Here is the call graph for this function:

◆ erasePeer()

void IdentityManager::erasePeer ( const std::string & peerId)

Definition at line 138 of file IdentityManager.cpp.

139{
140 if (auto it = peerToUnique_.find(peerId); it != peerToUnique_.end())
141 {
142 const std::string uid = it->second;
143 peerToUnique_.erase(it);
144 auto bit = byUnique_.find(uid);
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{
24 static const std::string s_book = (PathManager::getConfigPath() / "identity_book.runic").string();
25 return s_book.c_str();
26}
static fs::path getConfigPath()
Definition PathManager.h:86
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kMeFile()

const char * IdentityManager::kMeFile ( )
staticprivate

Definition at line 17 of file IdentityManager.cpp.

18{
19 static const std::string s_me = (PathManager::getConfigPath() / "identity_me.runic").string();
20 return s_me.c_str();
21}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadAddressBookFromFile()

bool IdentityManager::loadAddressBookFromFile ( )

Definition at line 62 of file IdentityManager.cpp.

63{
64 byUnique_.clear();
65 peerToUnique_.clear();
66 return readBookFile();
67}
Here is the call graph for this function:

◆ loadMyIdentityFromFile()

bool IdentityManager::loadMyIdentityFromFile ( )

Definition at line 29 of file IdentityManager.cpp.

30{
31 myUniqueId_.clear();
32 myUsername_.clear();
33 return readMeFile();
34}
std::string myUniqueId_
std::string myUsername_
Here is the call graph for this function:

◆ myUniqueId()

const std::string & IdentityManager::myUniqueId ( ) const
inline

Definition at line 25 of file IdentityManager.h.

26 {
27 return myUniqueId_;
28 }

◆ myUsername()

const std::string & IdentityManager::myUsername ( ) const
inline

Definition at line 29 of file IdentityManager.h.

30 {
31 return myUsername_;
32 }

◆ 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;
316 if (!fs::exists(kBookFile()))
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;
332 (void)Serializer::deserializeInt(buf, off); // ver
333
334 int n = Serializer::deserializeInt(buf, off);
335 // NOTE: const method; we need to cast away const to fill the map or
336 // make this non-const. Easiest: make readBookFile() non-const (done above).
337 // So keep this implementation but ensure header declares it non-const.
338 return false; // (safety: this should never compile with const)
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)
Definition Serializer.h:367
static std::string deserializeString(const std::vector< unsigned char > &buffer, size_t &offset)
Definition Serializer.h:388
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readMeFile()

bool IdentityManager::readMeFile ( )
private

Definition at line 250 of file IdentityManager.cpp.

251{
252 namespace fs = std::filesystem;
253 if (!fs::exists(kMeFile()))
254 return true; // first run is fine
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;
269 (void)Serializer::deserializeInt(buf, off); // ver
272 return true;
273 }
274 catch (...)
275 {
276 return false;
277 }
278}
static constexpr const char * kME_MAGIC
static const char * kMeFile()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ saveAddressBookToFile()

bool IdentityManager::saveAddressBookToFile ( ) const

Definition at line 69 of file IdentityManager.cpp.

70{
71 return writeBookFile();
72}
bool writeBookFile() const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ saveMyIdentityToFile()

bool IdentityManager::saveMyIdentityToFile ( ) const

Definition at line 36 of file IdentityManager.cpp.

37{
38 return writeMeFile();
39}
bool writeMeFile() const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setMyIdentity()

void IdentityManager::setMyIdentity ( const std::string & uniqueId,
const std::string & username )

Definition at line 41 of file IdentityManager.cpp.

42{
43 myUniqueId_ = uniqueId;
44 myUsername_ = username;
46
47 // also mirror into address book
48 auto& pi = byUnique_[uniqueId];
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) // cap
54 pi.usernamesHistory.erase(pi.usernamesHistory.begin());
55 }
56 pi.username = username;
58}
bool saveMyIdentityToFile() const
Here is the call graph for this function:

◆ setUsernameForUnique()

void IdentityManager::setUsernameForUnique ( const std::string & uniqueId,
const std::string & username )

Definition at line 151 of file IdentityManager.cpp.

152{
153 if (uniqueId == myUniqueId_)
154 {
155 if (myUsername_ != username)
156 {
157 myUsername_ = username;
158 (void)saveMyIdentityToFile();
159 }
160 }
161
162 auto& pi = byUnique_[uniqueId];
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;
171 (void)saveAddressBookToFile();
172}
Here is the call graph for this function:

◆ uniqueForPeer()

std::optional< std::string > IdentityManager::uniqueForPeer ( const std::string & peerId) const

Definition at line 189 of file IdentityManager.cpp.

190{
191 if (auto it = peerToUnique_.find(peerId); it != peerToUnique_.end())
192 return it->second;
193 return std::nullopt;
194}

◆ usernameForPeer()

std::optional< std::string > IdentityManager::usernameForPeer ( const std::string & peerId) const

Definition at line 219 of file IdentityManager.cpp.

220{
221 for (const auto& kv : peerToUnique_)
222 if (kv.first == peerId)
223 return usernameForUnique(kv.second);
224 return std::nullopt;
225}
std::string usernameForUnique(const std::string &uniqueId) const
Here is the call graph for this function:

◆ usernameForUnique()

std::string IdentityManager::usernameForUnique ( const std::string & uniqueId) const

Definition at line 174 of file IdentityManager.cpp.

175{
176 auto it = byUnique_.find(uniqueId);
177 if (it != byUnique_.end() && !it->second.username.empty())
178 return it->second.username;
179
180 if (uniqueId == myUniqueId_ && !myUsername_.empty())
181 return myUsername_;
182
183 // fallback: show truncated uniqueId if unknown
184 if (uniqueId.size() > 8)
185 return uniqueId.substr(0, 8);
186 return uniqueId;
187}
Here is the caller graph for this function:

◆ 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 // count
289 Serializer::serializeInt(buf, static_cast<int>(byUnique_.size()));
290 for (const auto& [uid, pi] : byUnique_)
291 {
292 Serializer::serializeString(buf, pi.uniqueId);
293 Serializer::serializeString(buf, pi.username);
294 Serializer::serializeString(buf, pi.peerId);
295
296 // history
297 Serializer::serializeInt(buf, static_cast<int>(pi.usernamesHistory.size()));
298 for (const auto& old : pi.usernamesHistory)
299 Serializer::serializeString(buf, old);
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)
Definition Serializer.h:318
static void serializeInt(std::vector< unsigned char > &buffer, int value)
Definition Serializer.h:302
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ byUnique_

std::unordered_map<std::string, PeerIdentity> IdentityManager::byUnique_
private

Definition at line 66 of file IdentityManager.h.

◆ myUniqueId_

std::string IdentityManager::myUniqueId_
private

Definition at line 62 of file IdentityManager.h.

◆ myUsername_

std::string IdentityManager::myUsername_
private

Definition at line 63 of file IdentityManager.h.

◆ peerToUnique_

std::unordered_map<std::string, std::string> IdentityManager::peerToUnique_
private

Definition at line 69 of file IdentityManager.h.


The documentation for this class was generated from the following files: