Skip to content

Commit

Permalink
asynchronous replacing
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 15, 2024
1 parent 7aa3655 commit d066f22
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 88 deletions.
60 changes: 10 additions & 50 deletions source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,6 @@ void _BrowserWindow::onDownloadResource(BrowserLeaf const& leaf)
printOverlayMessage("Downloading ...");
++leaf.rawTO->numDownloads;


NetworkTransferController::get().onDownload(DownloadNetworkResourceRequestData{
.resourceId = leaf.rawTO->id,
.resourceName = leaf.leafName,
Expand All @@ -1274,57 +1273,18 @@ void _BrowserWindow::onReplaceResource(BrowserLeaf const& leaf)
auto func = [&] {
printOverlayMessage("Replacing ...");

delayedExecution([=, this] {
std::string mainData;
std::string settings;
std::string statistics;
IntVector2D worldSize;
int numObjects = 0;

DeserializedSimulation deserializedSim;
if (leaf.rawTO->resourceType == NetworkResourceType_Simulation) {
deserializedSim = SerializationHelperService::getDeserializedSerialization(_simController);

SerializedSimulation serializedSim;
if (!SerializerService::serializeSimulationToStrings(serializedSim, deserializedSim)) {
MessageDialog::get().information("Replace simulation", "The simulation could not be serialized for replacing.");
return;
}
mainData = serializedSim.mainData;
settings = serializedSim.auxiliaryData;
statistics = serializedSim.statistics;
worldSize = {deserializedSim.auxiliaryData.generalSettings.worldSizeX, deserializedSim.auxiliaryData.generalSettings.worldSizeY};
numObjects = deserializedSim.mainData.getNumberOfCellAndParticles();
auto data = [&]() -> std::variant<ReplaceNetworkResourceRequestData::SimulationData, ReplaceNetworkResourceRequestData::GenomeData> {
if (_currentWorkspace.resourceType == NetworkResourceType_Simulation) {
return ReplaceNetworkResourceRequestData::SimulationData{.zoom = Viewport::getZoomFactor(), .center = Viewport::getCenterInWorldPos()};
} else {
auto genome = _genomeEditorWindow.lock()->getCurrentGenome();
if (genome.cells.empty()) {
showMessage("Replace genome", "The is no valid genome in the genome editor selected.");
return;
}
auto genomeData = GenomeDescriptionService::convertDescriptionToBytes(genome);
numObjects = GenomeDescriptionService::getNumNodesRecursively(genomeData, true);

if (!SerializerService::serializeGenomeToString(mainData, genomeData)) {
showMessage("Replace genome", "The genome could not be serialized for replacing.");
return;
}
}

if (!NetworkService::replaceResource(leaf.rawTO->id, worldSize, numObjects, mainData, settings, statistics)) {
std::string type = leaf.rawTO->resourceType == NetworkResourceType_Simulation ? "simulation" : "genome";
showMessage(
"Error",
"Failed to replace " + type
+ ".\n\n"
"Possible reasons:\n\n" ICON_FA_CHEVRON_RIGHT " The server is not reachable.\n\n" ICON_FA_CHEVRON_RIGHT
" The total size of your uploads exceeds the allowed storage limit.");
return;
return ReplaceNetworkResourceRequestData::GenomeData{.description = _genomeEditorWindow.lock()->getCurrentGenome()};
}
if (leaf.rawTO->resourceType == NetworkResourceType_Simulation) {
getSimulationCache()->insertOrAssign(leaf.rawTO->id, deserializedSim);
}
onRefresh();
});
}();
NetworkTransferController::get().onReplace(ReplaceNetworkResourceRequestData{
.resourceId = leaf.rawTO->id,
.workspaceType = leaf.rawTO->workspaceType,
.downloadCache = getSimulationCache(),
.data = data});
};
MessageDialog::get().yesNo("Delete", "Do you really want to replace the content of the selected item?", func);
}
Expand Down
16 changes: 16 additions & 0 deletions source/Gui/NetworkTransferController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void NetworkTransferController::init(
_browserWindow = browserWindow;
_downloadProcessor = _TaskProcessor::createTaskProcessor(_persisterController);
_uploadProcessor = _TaskProcessor::createTaskProcessor(_persisterController);
_replaceProcessor = _TaskProcessor::createTaskProcessor(_persisterController);
}

void NetworkTransferController::onDownload(DownloadNetworkResourceRequestData const& requestData)
Expand Down Expand Up @@ -103,8 +104,23 @@ void NetworkTransferController::onUpload(UploadNetworkResourceRequestData const&
[](auto const& errors) { MessageDialog::get().information("Error", errors); });
}

void NetworkTransferController::onReplace(ReplaceNetworkResourceRequestData const& requestData)
{
_replaceProcessor->executeTask(
[&](auto const& senderId) {
return _persisterController->scheduleReplaceNetworkResource(
SenderInfo{.senderId = senderId, .wishResultData = true, .wishErrorInfo = true}, requestData);
},
[&](auto const& requestId) {
_persisterController->fetchReplaceNetworkResourcesData(requestId);
_browserWindow->onRefresh();
},
[](auto const& errors) { MessageDialog::get().information("Error", errors); });
}

void NetworkTransferController::process()
{
_downloadProcessor->process();
_uploadProcessor->process();
_replaceProcessor->process();
}
3 changes: 3 additions & 0 deletions source/Gui/NetworkTransferController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "PersisterInterface/PersisterRequestId.h"
#include "PersisterInterface/DownloadNetworkResourceRequestData.h"
#include "PersisterInterface/UploadNetworkResourceRequestData.h"
#include "PersisterInterface/ReplaceNetworkResourceRequestData.h"

#include "Definitions.h"

Expand All @@ -24,6 +25,7 @@ class NetworkTransferController

void onDownload(DownloadNetworkResourceRequestData const& requestData);
void onUpload(UploadNetworkResourceRequestData const& requestData);
void onReplace(ReplaceNetworkResourceRequestData const& requestData);

void process();

Expand All @@ -36,4 +38,5 @@ class NetworkTransferController

TaskProcessor _downloadProcessor;
TaskProcessor _uploadProcessor;
TaskProcessor _replaceProcessor;
};
20 changes: 15 additions & 5 deletions source/PersisterImpl/PersisterControllerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ PersisterErrorInfo _PersisterControllerImpl::fetchError(PersisterRequestId const

PersisterRequestId _PersisterControllerImpl::scheduleSaveSimulationToFile(SenderInfo const& senderInfo, SaveSimulationRequestData const& data)
{
return scheduleRequest<_SaveToFileRequest>(senderInfo, data);
return scheduleRequest<_SaveSimulationRequest>(senderInfo, data);
}

SavedSimulationResultData _PersisterControllerImpl::fetchSavedSimulationData(PersisterRequestId const& id)
SaveSimulationResultData _PersisterControllerImpl::fetchSavedSimulationData(PersisterRequestId const& id)
{
return fetchData<_SaveToFileRequestResult, SavedSimulationResultData>(id);
return fetchData<_SaveSimulationRequestResult, SaveSimulationResultData>(id);
}

PersisterRequestId _PersisterControllerImpl::scheduleReadSimulationFromFile(SenderInfo const& senderInfo, ReadSimulationRequestData const& data)
{
return scheduleRequest<_ReadFromFileRequest>(senderInfo, data);
return scheduleRequest<_ReadSimulationRequest>(senderInfo, data);
}

ReadSimulationResultData _PersisterControllerImpl::fetchReadSimulationData(PersisterRequestId const& id)
{
return fetchData<_ReadFromFileRequestResult, ReadSimulationResultData>(id);
return fetchData<_ReadSimulationRequestResult, ReadSimulationResultData>(id);
}

PersisterRequestId _PersisterControllerImpl::scheduleLogin(SenderInfo const& senderInfo, LoginRequestData const& data)
Expand Down Expand Up @@ -116,6 +116,16 @@ UploadNetworkResourceResultData _PersisterControllerImpl::fetchUploadNetworkReso
return fetchData<_UploadNetworkResourceRequestResult, UploadNetworkResourceResultData>(id);
}

PersisterRequestId _PersisterControllerImpl::scheduleReplaceNetworkResource(SenderInfo const& senderInfo, ReplaceNetworkResourceRequestData const& data)
{
return scheduleRequest<_ReplaceNetworkResourceRequest>(senderInfo, data);
}

ReplaceNetworkResourceResultData _PersisterControllerImpl::fetchReplaceNetworkResourcesData(PersisterRequestId const& id)
{
return fetchData<_ReplaceNetworkResourceRequestResult, ReplaceNetworkResourceResultData>(id);
}

PersisterRequestId _PersisterControllerImpl::generateNewJobId()
{
++_latestJobId;
Expand Down
5 changes: 4 additions & 1 deletion source/PersisterImpl/PersisterControllerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class _PersisterControllerImpl : public _PersisterController
PersisterErrorInfo fetchError(PersisterRequestId const& id) override;

PersisterRequestId scheduleSaveSimulationToFile(SenderInfo const& senderInfo, SaveSimulationRequestData const& data) override;
SavedSimulationResultData fetchSavedSimulationData(PersisterRequestId const& id) override;
SaveSimulationResultData fetchSavedSimulationData(PersisterRequestId const& id) override;

PersisterRequestId scheduleReadSimulationFromFile(SenderInfo const& senderInfo, ReadSimulationRequestData const& data) override;
ReadSimulationResultData fetchReadSimulationData(PersisterRequestId const& id) override;
Expand All @@ -40,6 +40,9 @@ class _PersisterControllerImpl : public _PersisterController
PersisterRequestId scheduleUploadNetworkResource(SenderInfo const& senderInfo, UploadNetworkResourceRequestData const& data) override;
UploadNetworkResourceResultData fetchUploadNetworkResourcesData(PersisterRequestId const& id) override;

PersisterRequestId scheduleReplaceNetworkResource(SenderInfo const& senderInfo, ReplaceNetworkResourceRequestData const& data) override;
ReplaceNetworkResourceResultData fetchReplaceNetworkResourcesData(PersisterRequestId const& id) override;

private:
static auto constexpr MaxWorkerThreads = 4;

Expand Down
12 changes: 8 additions & 4 deletions source/PersisterImpl/PersisterRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "PersisterInterface/LoginRequestData.h"
#include "PersisterInterface/ReadSimulationRequestData.h"
#include "PersisterInterface/PersisterRequestId.h"
#include "PersisterInterface/ReplaceNetworkResourceRequestData.h"
#include "PersisterInterface/SenderInfo.h"
#include "PersisterInterface/SaveSimulationRequestData.h"
#include "PersisterInterface/UploadNetworkResourceRequestData.h"
Expand Down Expand Up @@ -50,11 +51,11 @@ class _ConcreteRequest : public _PersisterRequest
template<typename Data_t>
using ConcreteRequest = std::shared_ptr<_ConcreteRequest<Data_t>>;

using _SaveToFileRequest = _ConcreteRequest<SaveSimulationRequestData>;
using SaveToFileRequest = std::shared_ptr<_SaveToFileRequest>;
using _SaveSimulationRequest = _ConcreteRequest<SaveSimulationRequestData>;
using SaveSimulationRequest = std::shared_ptr<_SaveSimulationRequest>;

using _ReadFromFileRequest = _ConcreteRequest<ReadSimulationRequestData>;
using ReadFromFileRequest = std::shared_ptr<_ReadFromFileRequest>;
using _ReadSimulationRequest = _ConcreteRequest<ReadSimulationRequestData>;
using ReadSimulationRequest = std::shared_ptr<_ReadSimulationRequest>;

using _LoginRequest = _ConcreteRequest<LoginRequestData>;
using LoginRequest = std::shared_ptr<_LoginRequest>;
Expand All @@ -67,3 +68,6 @@ using DownloadNetworkResourceRequest = std::shared_ptr<_DownloadNetworkResourceR

using _UploadNetworkResourceRequest = _ConcreteRequest<UploadNetworkResourceRequestData>;
using UploadNetworkResourceRequest = std::shared_ptr<_UploadNetworkResourceRequest>;

using _ReplaceNetworkResourceRequest = _ConcreteRequest<ReplaceNetworkResourceRequestData>;
using ReplaceNetworkResourceRequest = std::shared_ptr<_ReplaceNetworkResourceRequest>;
18 changes: 4 additions & 14 deletions source/PersisterImpl/PersisterRequestResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "PersisterInterface/ReadSimulationResultData.h"
#include "PersisterInterface/PersisterRequestId.h"
#include "PersisterInterface/SavedSimulationResultData.h"
#include "PersisterInterface/SaveSimulationResultData.h"

class _PersisterRequestResult
{
Expand Down Expand Up @@ -38,20 +38,10 @@ using PersisterRequestResult = std::shared_ptr<_PersisterRequestResult>;
template <typename Data_t>
using ConcreteRequestResult = std::shared_ptr<_ConcreteRequestResult<Data_t>>;

using _SaveToFileRequestResult = _ConcreteRequestResult<SavedSimulationResultData>;
using SaveToFileRequestResult = std::shared_ptr<_SaveToFileRequestResult>;

using _ReadFromFileRequestResult = _ConcreteRequestResult<ReadSimulationResultData>;
using ReadFromFileRequestResult = std::shared_ptr<_ReadFromFileRequestResult>;

using _SaveSimulationRequestResult = _ConcreteRequestResult<SaveSimulationResultData>;
using _ReadSimulationRequestResult = _ConcreteRequestResult<ReadSimulationResultData>;
using _LoginRequestResult = _ConcreteRequestResult<LoginResultData>;
using LoginRequestResult = std::shared_ptr<_LoginRequestResult>;

using _GetNetworkResourcesRequestResult = _ConcreteRequestResult<GetNetworkResourcesResultData>;
using GetNetworkResourcesRequestResult = std::shared_ptr<_GetNetworkResourcesRequestResult>;

using _DownloadNetworkResourceRequestResult = _ConcreteRequestResult<DownloadNetworkResourceResultData>;
using DownloadNetworkResourceRequestResult = std::shared_ptr<_DownloadNetworkResourceRequestResult>;

using _UploadNetworkResourceRequestResult = _ConcreteRequestResult<UploadNetworkResourceResultData>;
using UploadNetworkResourceRequestResult = std::shared_ptr<_UploadNetworkResourceRequestResult>;
using _ReplaceNetworkResourceRequestResult = _ConcreteRequestResult<ReplaceNetworkResourceResultData>;
Loading

0 comments on commit d066f22

Please sign in to comment.