First untested version with user storage

This commit is contained in:
Martin Bauer
2021-05-16 13:45:00 +02:00
parent 06fdda1d93
commit 5984a233af
14 changed files with 174 additions and 49 deletions

View File

@@ -7,13 +7,13 @@ template <class ForwardIterator, class T>
ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T &val)
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first, last);
int count, step;
count = last - first;
while (count > 0)
{
it = first;
step = count / 2;
advance(it, step);
it += step;
if (*it < val)
{
first = ++it;
@@ -30,11 +30,11 @@ static String userFileName(const String &userName)
return userDir + userName;
}
bool User::load(const String &name)
bool User::load(const String &stringId)
{
name_ = name;
assert(name_.length() > 0);
const auto fileName = userFileName(name_);
stringId_ = stringId;
assert(stringId_.length() > 0);
const auto fileName = userFileName(stringId_);
if (!portablefs::exists(fileName.c_str()))
return false;
@@ -43,7 +43,7 @@ bool User::load(const String &name)
size_t sessionsInFile;
file.read((uint8_t *)&sessionsInFile, sizeof(numSessions_));
init(name, sessionsInFile * 2);
init(stringId, sessionsInFile * 2);
size_t expectedSize = sizeof(SessionIdType) * numSessions_;
auto bytesRead = file.read((uint8_t *)sessionIds_, expectedSize);
@@ -52,14 +52,14 @@ bool User::load(const String &name)
assert(expectedSize == bytesRead);
}
void User::init(const String &name, size_t sessionAllocateSize)
void User::init(const String &stringId, size_t sessionAllocateSize)
{
if (sessionIds_ != nullptr)
{
free(sessionIds_);
sessionIds_ = nullptr;
}
name_ = name;
stringId_ = stringId;
numSessionsAllocated_ = sessionAllocateSize;
sessionIds_ = (SessionIdType *)ps_malloc(sizeof(SessionIdType) * numSessionsAllocated_);
numSessions_ = 0;
@@ -70,7 +70,7 @@ void User::save()
if (!portablefs::exists(userDir.c_str()))
portablefs::mkdir(userDir.c_str());
auto file = portablefs::open(userFileName(name_).c_str(), "w");
auto file = portablefs::open(userFileName(stringId_).c_str(), "w");
file.write((uint8_t *)&numSessions_, sizeof(numSessions_));
file.write((uint8_t *)sessionIds_, sizeof(SessionIdType) * numSessions_);
}
@@ -86,9 +86,9 @@ void User::freeResources()
void User::remove()
{
portablefs::remove(userFileName(name_).c_str());
portablefs::remove(userFileName(stringId_).c_str());
freeResources();
name_ = "";
stringId_ = "";
numSessions_ = 0;
numSessionsAllocated_ = 0;
}
@@ -134,11 +134,11 @@ bool User::removeSession(SessionIdType sessionIdToRemove)
// --------------------------------------------------------------------------------------------------------------------
User *UserStorage::getUserInfo(const String &userName)
User *UserStorage::getUserInfo(const String &stringId)
{
// index 0 is the unassigned user
for (size_t i = 1; i < numUsers_; ++i)
if (users_[i].name() == userName)
if (users_[i].stringId() == stringId)
return &users_[i];
return nullptr;
}
@@ -148,20 +148,23 @@ User *UserStorage::getUnassignedUser()
return &users_[0];
}
User *UserStorage::addNewUser(const String &userName)
User *UserStorage::addNewUser(const String &stringId)
{
if (numUsers_ >= MAX_USERS)
return nullptr;
if(stringId.length() >= USER_STRING_ID_MAX_LEN)
return nullptr;
auto userIdx = numUsers_;
numUsers_++;
assert(numUsers_ < MAX_USERS);
users_[userIdx].init(userName);
users_[userIdx].init(stringId);
}
bool UserStorage::deleteUser(const String &userName)
bool UserStorage::deleteUser(const String &stringId)
{
User *userPtr = getUserInfo(userName);
User *userPtr = getUserInfo(stringId);
if (userPtr == nullptr)
return false;

View File

@@ -5,13 +5,14 @@
using SessionIdType = uint32_t;
constexpr size_t MAX_USERS = 64;
constexpr size_t INITIAL_SESSIONS_PER_USER = 128;
constexpr size_t USER_STRING_ID_MAX_LEN = 63;
struct User
{
public:
User() : numSessions_(0), numSessionsAllocated_(0), sessionIds_(nullptr) {}
void init(const String &name, size_t sessionAllocateSize = INITIAL_SESSIONS_PER_USER);
void init(const String &stringId_, size_t sessionAllocateSize = INITIAL_SESSIONS_PER_USER);
void freeResources();
bool load(const String &name);
@@ -24,19 +25,19 @@ public:
bool hasSession(SessionIdType sessionId) const;
const String &name() const { return name_; }
const String &stringId() const { return stringId_; }
// session access
SessionIdType *sessionBegin() { return sessionIds_; }
SessionIdType *sessionEnd() { return sessionIds_ + numSessions_; }
size_t numSessions() const { return numSessions_; }
const SessionIdType *sessionBegin() const { return sessionIds_; }
const SessionIdType *sessionEnd() const { return sessionIds_ + numSessions_; }
private:
void growSessionArrayIfNecessary();
String name_;
String stringId_;
size_t numSessions_;
size_t numSessionsAllocated_;
SessionIdType *sessionIds_;
@@ -48,20 +49,29 @@ public:
UserStorage()
: numUsers_(0)
{
}
void init()
{
numUsers_ = 0;
fillFromFileSystem();
}
User *getUserInfo(const String &userName);
User *getUserInfo(const String &stringId);
User *getUnassignedUser();
User *addNewUser(const String &userName);
bool deleteUser(const String &userName);
User *addNewUser(const String &stringId);
bool deleteUser(const String &stringId);
User *begin() { return &users_[0]; }
User *beginWithoutUnassigned() { return &users_[1]; }
User *end() { return &users_[numUsers_]; }
const User *begin() const { return &users_[0]; }
const User *end() const { return &users_[numUsers_]; }
size_t numUsers() const { return numUsers_; }
private:
void fillFromFileSystem();