Skip to content

Commit

Permalink
getting user names for emoij asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 16, 2024
1 parent d066f22 commit 6094411
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 8 deletions.
18 changes: 16 additions & 2 deletions source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ _BrowserWindow::_BrowserWindow(
{
_downloadCache = std::make_shared<_DownloadCache>();
_refreshProcessor = _TaskProcessor::createTaskProcessor(_persisterController);
_emojiProcessor = _TaskProcessor::createTaskProcessor(_persisterController);

auto& settings = GlobalSettings::get();
_currentWorkspace.resourceType = settings.getInt("windows.browser.resource type", _currentWorkspace.resourceType);
Expand Down Expand Up @@ -1226,6 +1227,7 @@ void _BrowserWindow::processActivated()
void _BrowserWindow::processPendingRequestIds()
{
_refreshProcessor->process();
_emojiProcessor->process();
}

void _BrowserWindow::createTreeTOs(Workspace& workspace)
Expand Down Expand Up @@ -1451,8 +1453,20 @@ std::string _BrowserWindow::getUserNamesToEmojiType(std::string const& resourceI
if (findResult != _userNamesByEmojiTypeBySimIdCache.end()) {
userNames = findResult->second;
} else {
NetworkService::getUserNamesForResourceAndEmojiType(userNames, resourceId, emojiType);
_userNamesByEmojiTypeBySimIdCache.emplace(std::make_pair(resourceId, emojiType), userNames);
if (!_emojiProcessor->pendingTasks()) {
_emojiProcessor->executeTask(
[&](auto const& senderId) {
return _persisterController->scheduleGetUserNamesForEmoji(
SenderInfo{.senderId = senderId, .wishResultData = true, .wishErrorInfo = false},
GetUserNamesForEmojiRequestData{.resourceId = resourceId, .emojiType = emojiType});
},
[&](auto const& requestId) {
auto data = _persisterController->fetchGetUserNamesForEmojiData(requestId);
_userNamesByEmojiTypeBySimIdCache.emplace(std::make_pair(data.resourceId, data.emojiType), data.userNames);
},
[](auto const& errors) { MessageDialog::get().information("Error", errors); });
}
return "Loading...";
}

return boost::algorithm::join(userNames, ", ");
Expand Down
1 change: 1 addition & 0 deletions source/Gui/BrowserWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class _BrowserWindow : public _AlienWindow
void popTextColor();

TaskProcessor _refreshProcessor;
TaskProcessor _emojiProcessor;

bool _activateEmojiPopup = false;
bool _showAllEmojis = false;
Expand Down
16 changes: 13 additions & 3 deletions source/PersisterImpl/PersisterControllerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,18 @@ ReplaceNetworkResourceResultData _PersisterControllerImpl::fetchReplaceNetworkRe
return fetchData<_ReplaceNetworkResourceRequestResult, ReplaceNetworkResourceResultData>(id);
}

PersisterRequestId _PersisterControllerImpl::generateNewJobId()
PersisterRequestId _PersisterControllerImpl::scheduleGetUserNamesForEmoji(SenderInfo const& senderInfo, GetUserNamesForEmojiRequestData const& data)
{
++_latestJobId;
return PersisterRequestId{std::to_string(_latestJobId)};
return scheduleRequest<_GetUserNamesForEmojiRequest>(senderInfo, data);
}

GetUserNamesForEmojiResultData _PersisterControllerImpl::fetchGetUserNamesForEmojiData(PersisterRequestId const& id)
{
return fetchData<_GetUserNamesForEmojiRequestResult, GetUserNamesForEmojiResultData>(id);
}

PersisterRequestId _PersisterControllerImpl::generateNewRequestId()
{
++_latestRequestId;
return PersisterRequestId{std::to_string(_latestRequestId)};
}
9 changes: 6 additions & 3 deletions source/PersisterImpl/PersisterControllerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class _PersisterControllerImpl : public _PersisterController
PersisterRequestId scheduleReplaceNetworkResource(SenderInfo const& senderInfo, ReplaceNetworkResourceRequestData const& data) override;
ReplaceNetworkResourceResultData fetchReplaceNetworkResourcesData(PersisterRequestId const& id) override;

PersisterRequestId scheduleGetUserNamesForEmoji(SenderInfo const& senderInfo, GetUserNamesForEmojiRequestData const& data) override;
GetUserNamesForEmojiResultData fetchGetUserNamesForEmojiData(PersisterRequestId const& id) override;

private:
static auto constexpr MaxWorkerThreads = 4;

Expand All @@ -52,11 +55,11 @@ class _PersisterControllerImpl : public _PersisterController
template <typename RequestResult, typename ResultData>
ResultData fetchData(PersisterRequestId const& id);

PersisterRequestId generateNewJobId();
PersisterRequestId generateNewRequestId();

PersisterWorker _worker;
std::thread* _thread[MaxWorkerThreads];
int _latestJobId = 0;
int _latestRequestId = 0;
};


Expand All @@ -67,7 +70,7 @@ class _PersisterControllerImpl : public _PersisterController
template <typename Request, typename RequestData>
PersisterRequestId _PersisterControllerImpl::scheduleRequest(SenderInfo const& senderInfo, RequestData const& data)
{
auto requestId = generateNewJobId();
auto requestId = generateNewRequestId();
auto request = std::make_shared<Request>(requestId, senderInfo, data);

_worker->addRequest(request);
Expand Down
4 changes: 4 additions & 0 deletions source/PersisterImpl/PersisterRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "PersisterInterface/DownloadNetworkResourceRequestData.h"
#include "PersisterInterface/GetNetworkResourcesRequestData.h"
#include "PersisterInterface/GetUserNamesForEmojiRequestData.h"
#include "PersisterInterface/LoginRequestData.h"
#include "PersisterInterface/ReadSimulationRequestData.h"
#include "PersisterInterface/PersisterRequestId.h"
Expand Down Expand Up @@ -71,3 +72,6 @@ using UploadNetworkResourceRequest = std::shared_ptr<_UploadNetworkResourceReque

using _ReplaceNetworkResourceRequest = _ConcreteRequest<ReplaceNetworkResourceRequestData>;
using ReplaceNetworkResourceRequest = std::shared_ptr<_ReplaceNetworkResourceRequest>;

using _GetUserNamesForEmojiRequest = _ConcreteRequest<GetUserNamesForEmojiRequestData>;
using GetUserNamesForEmojiRequest = std::shared_ptr<_GetUserNamesForEmojiRequest>;
1 change: 1 addition & 0 deletions source/PersisterImpl/PersisterRequestResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ using _GetNetworkResourcesRequestResult = _ConcreteRequestResult<GetNetworkResou
using _DownloadNetworkResourceRequestResult = _ConcreteRequestResult<DownloadNetworkResourceResultData>;
using _UploadNetworkResourceRequestResult = _ConcreteRequestResult<UploadNetworkResourceResultData>;
using _ReplaceNetworkResourceRequestResult = _ConcreteRequestResult<ReplaceNetworkResourceResultData>;
using _GetUserNamesForEmojiRequestResult = _ConcreteRequestResult<GetUserNamesForEmojiResultData>;
20 changes: 20 additions & 0 deletions source/PersisterImpl/PersisterWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ void _PersisterWorker::processRequests(std::unique_lock<std::mutex>& lock)
if (auto const& concreteRequest = std::dynamic_pointer_cast<_ReplaceNetworkResourceRequest>(request)) {
processingResult = processRequest(lock, concreteRequest);
}
if (auto const& concreteRequest = std::dynamic_pointer_cast<_GetUserNamesForEmojiRequest>(request)) {
processingResult = processRequest(lock, concreteRequest);
}
auto inProgressJobsIter = std::ranges::find_if(
_inProgressRequests, [&](PersisterRequest const& otherRequest) { return otherRequest->getRequestId() == request->getRequestId(); });
_inProgressRequests.erase(inProgressJobsIter);
Expand Down Expand Up @@ -513,3 +516,20 @@ _PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest
}
return std::make_shared<_ReplaceNetworkResourceRequestResult>(request->getRequestId(), ReplaceNetworkResourceResultData{});
}

_PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest(std::unique_lock<std::mutex>& lock, GetUserNamesForEmojiRequest const& request)
{
UnlockGuard unlockGuard(lock);

auto const& requestData = request->getData();

GetUserNamesForEmojiResultData resultData;
resultData.resourceId = requestData.resourceId;
resultData.emojiType = requestData.emojiType;
if (!NetworkService::getUserNamesForResourceAndEmojiType(resultData.userNames, requestData.resourceId, requestData.emojiType)) {
return std::make_shared<_PersisterRequestError>(
request->getRequestId(), request->getSenderInfo().senderId, PersisterErrorInfo{"Could not load user names."});
}

return std::make_shared<_GetUserNamesForEmojiRequestResult>(request->getRequestId(), resultData);
}
1 change: 1 addition & 0 deletions source/PersisterImpl/PersisterWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class _PersisterWorker
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, DownloadNetworkResourceRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, UploadNetworkResourceRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, ReplaceNetworkResourceRequest const& request);
PersisterRequestResultOrError processRequest(std::unique_lock<std::mutex>& lock, GetUserNamesForEmojiRequest const& request);

SimulationController _simController;

Expand Down
2 changes: 2 additions & 0 deletions source/PersisterInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ add_library(PersisterInterface
DownloadNetworkResourceResultData.h
GetNetworkResourcesRequestData.h
GetNetworkResourcesResultData.h
GetUserNamesForEmojiRequestData.h
GetUserNamesForEmojiResultData.h
LoginRequestData.h
LoginResultData.h
PersisterController.cpp
Expand Down
9 changes: 9 additions & 0 deletions source/PersisterInterface/GetUserNamesForEmojiRequestData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string>

struct GetUserNamesForEmojiRequestData
{
std::string resourceId;
int emojiType = 0;
};
11 changes: 11 additions & 0 deletions source/PersisterInterface/GetUserNamesForEmojiResultData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <string>
#include <set>

struct GetUserNamesForEmojiResultData
{
std::string resourceId;
int emojiType = 0;
std::set<std::string> userNames;
};
6 changes: 6 additions & 0 deletions source/PersisterInterface/PersisterController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "DownloadNetworkResourceResultData.h"
#include "GetNetworkResourcesRequestData.h"
#include "GetNetworkResourcesResultData.h"
#include "GetUserNamesForEmojiRequestData.h"
#include "GetUserNamesForEmojiResultData.h"
#include "LoginRequestData.h"
#include "LoginResultData.h"
#include "ReadSimulationResultData.h"
Expand All @@ -31,6 +33,7 @@ class _PersisterController
public:
virtual ~_PersisterController() = default;

//lifecycle control
virtual void init(SimulationController const& simController) = 0;
virtual void shutdown() = 0;
virtual void restart() = 0;
Expand Down Expand Up @@ -62,4 +65,7 @@ class _PersisterController

virtual PersisterRequestId scheduleReplaceNetworkResource(SenderInfo const& senderInfo, ReplaceNetworkResourceRequestData const& data) = 0;
virtual ReplaceNetworkResourceResultData fetchReplaceNetworkResourcesData(PersisterRequestId const& id) = 0;

virtual PersisterRequestId scheduleGetUserNamesForEmoji(SenderInfo const& senderInfo, GetUserNamesForEmojiRequestData const& data) = 0;
virtual GetUserNamesForEmojiResultData fetchGetUserNamesForEmojiData(PersisterRequestId const& id) = 0;
};

0 comments on commit 6094411

Please sign in to comment.