Skip to content

Commit

Permalink
asynchronous upload
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 14, 2024
1 parent b8ea5c4 commit f2291e1
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 142 deletions.
72 changes: 7 additions & 65 deletions source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "GenomeEditorWindow.h"
#include "HelpStrings.h"
#include "LoginController.h"
#include "NetworkTransferController.h"
#include "PersisterInterface/TaskProcessor.h"

namespace
Expand Down Expand Up @@ -74,7 +75,6 @@ _BrowserWindow::_BrowserWindow(
{
_downloadCache = std::make_shared<_DownloadCache>();
_refreshProcessor = _TaskProcessor::createTaskProcessor(_persisterController);
_downloadProcessor = _TaskProcessor::createTaskProcessor(_persisterController);

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

void _BrowserWindow::createTreeTOs(Workspace& workspace)
Expand Down Expand Up @@ -1261,70 +1260,13 @@ void _BrowserWindow::onDownloadResource(BrowserLeaf const& leaf)
printOverlayMessage("Downloading ...");
++leaf.rawTO->numDownloads;

_downloadProcessor->executeTask(
[&](auto const& senderId) {
return _persisterController->scheduleDownloadNetworkResource(
SenderInfo{.senderId = senderId, .wishResultData = true, .wishErrorInfo = true},
DownloadNetworkResourceRequestData{
.resourceId = leaf.rawTO->id,
.resourceName = leaf.leafName,
.resourceVersion = leaf.rawTO->version,
.resourceType = _currentWorkspace.resourceType,
.downloadCache = _downloadCache});
},
[&](auto const& requestId) {
auto data = _persisterController->fetchDownloadNetworkResourcesData(requestId);

if (data.resourceType == NetworkResourceType_Simulation) {
_persisterController->shutdown();
_simController->closeSimulation();
std::optional<std::string> errorMessage;
auto const& deserializedSimulation = std::get<DeserializedSimulation>(data.resourceData);
try {
_simController->newSimulation(
leaf.leafName,
deserializedSimulation.auxiliaryData.timestep,
deserializedSimulation.auxiliaryData.generalSettings,
deserializedSimulation.auxiliaryData.simulationParameters);
_simController->setRealTime(deserializedSimulation.auxiliaryData.realTime);
_simController->setClusteredSimulationData(deserializedSimulation.mainData);
_simController->setStatisticsHistory(deserializedSimulation.statistics);
} catch (CudaMemoryAllocationException const& exception) {
errorMessage = exception.what();
} catch (...) {
errorMessage = "Failed to load simulation.";
}
if (errorMessage) {
showMessage("Error", *errorMessage);
_simController->closeSimulation();
_simController->newSimulation(
leaf.leafName,
deserializedSimulation.auxiliaryData.timestep,
deserializedSimulation.auxiliaryData.generalSettings,
deserializedSimulation.auxiliaryData.simulationParameters);
}
_persisterController->restart();

Viewport::setCenterInWorldPos(deserializedSimulation.auxiliaryData.center);
Viewport::setZoomFactor(deserializedSimulation.auxiliaryData.zoom);
_temporalControlWindow->onSnapshot();
} else {
_editorController->setOn(true);
_editorController->getGenomeEditorWindow()->openTab(std::get<GenomeDescription>(data.resourceData));
}
if (VersionChecker::isVersionNewer(data.resourceVersion)) {
std::string dataTypeString = data.resourceType == NetworkResourceType_Simulation ? "simulation" : "genome";
MessageDialog::get().information(
"Warning",
"The download was successful but the " + dataTypeString
+ " was generated using a more recent\n"
"version of ALIEN. Consequently, the "
+ dataTypeString
+ "might not function as expected.\n"
"Please visit\n\nhttps://github.com/chrxh/alien\n\nto obtain the latest version.");
}
},
[](auto const& errors) { MessageDialog::get().information("Error", errors); });
NetworkTransferController::get().onDownload(DownloadNetworkResourceRequestData{
.resourceId = leaf.rawTO->id,
.resourceName = leaf.leafName,
.resourceVersion = leaf.rawTO->version,
.resourceType = _currentWorkspace.resourceType,
.downloadCache = _downloadCache});
}

void _BrowserWindow::onReplaceResource(BrowserLeaf const& leaf)
Expand Down
1 change: 0 additions & 1 deletion source/Gui/BrowserWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class _BrowserWindow : public _AlienWindow
void popTextColor();

TaskProcessor _refreshProcessor;
TaskProcessor _downloadProcessor;

bool _activateEmojiPopup = false;
bool _showAllEmojis = false;
Expand Down
2 changes: 2 additions & 0 deletions source/Gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ PUBLIC
MultiplierWindow.h
NetworkSettingsDialog.cpp
NetworkSettingsDialog.h
NetworkTransferController.cpp
NetworkTransferController.h
NewSimulationDialog.cpp
NewSimulationDialog.h
NewPasswordDialog.cpp
Expand Down
6 changes: 0 additions & 6 deletions source/Gui/FileTransferController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ namespace
auto constexpr FileTransferSenderId = "FileTransfer";
}

FileTransferController& FileTransferController::get()
{
static FileTransferController instance;
return instance;
}

void FileTransferController::init(
PersisterController const& persisterController,
SimulationController const& simController,
Expand Down
5 changes: 3 additions & 2 deletions source/Gui/FileTransferController.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
#include "PersisterInterface/PersisterRequestId.h"

#include "Definitions.h"
#include "Base/Singleton.h"

class FileTransferController
{
public:
static FileTransferController& get();
MAKE_SINGLETON(FileTransferController);

public:
void init(PersisterController const& persisterController, SimulationController const& simController, TemporalControlWindow const& temporalControlWindow);

void onOpenSimulation();
Expand Down
3 changes: 3 additions & 0 deletions source/Gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
#include "AutosaveWindow.h"
#include "FileTransferController.h"
#include "LoginController.h"
#include "NetworkTransferController.h"

namespace
{
Expand Down Expand Up @@ -164,6 +165,7 @@ _MainWindow::_MainWindow(SimulationController const& simController, PersisterCon
_autosaveWindow = std::make_shared<_AutosaveWindow>(_simController, _persisterController);
OverlayMessageController::get().init(_persisterController);
FileTransferController::get().init(_persisterController, _simController, _temporalControlWindow);
NetworkTransferController::get().init(_simController, _persisterController, _temporalControlWindow, _editorController, _browserWindow);
LoginController::get().init(_simController, _persisterController, _activateUserDialog, _browserWindow);

//cyclic references
Expand Down Expand Up @@ -763,6 +765,7 @@ void _MainWindow::processControllers()
OverlayMessageController::get().process();
DelayedExecutionController::get().process();
FileTransferController::get().process();
NetworkTransferController::get().process();
LoginController::get().process();
}

Expand Down
110 changes: 110 additions & 0 deletions source/Gui/NetworkTransferController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "NetworkTransferController.h"

#include "Base/VersionChecker.h"
#include "EngineInterface/SimulationController.h"
#include "PersisterInterface/TaskProcessor.h"

#include "MessageDialog.h"
#include "TemporalControlWindow.h"
#include "Viewport.h"
#include "EditorController.h"
#include "GenomeEditorWindow.h"
#include "BrowserWindow.h"

void NetworkTransferController::init(
SimulationController const& simController,
PersisterController const& persisterController,
TemporalControlWindow const& temporalControlWindow,
EditorController const& editorController,
BrowserWindow const& browserWindow)
{
_simController = simController;
_persisterController = persisterController;
_temporalControlWindow = temporalControlWindow;
_editorController = editorController;
_browserWindow = browserWindow;
_downloadProcessor = _TaskProcessor::createTaskProcessor(_persisterController);
_uploadProcessor = _TaskProcessor::createTaskProcessor(_persisterController);
}

void NetworkTransferController::onDownload(DownloadNetworkResourceRequestData const& requestData)
{
_downloadProcessor->executeTask(
[&](auto const& senderId) {
return _persisterController->scheduleDownloadNetworkResource(
SenderInfo{.senderId = senderId, .wishResultData = true, .wishErrorInfo = true}, requestData);
},
[&](auto const& requestId) {
auto data = _persisterController->fetchDownloadNetworkResourcesData(requestId);

if (data.resourceType == NetworkResourceType_Simulation) {
_persisterController->shutdown();
_simController->closeSimulation();
std::optional<std::string> errorMessage;
auto const& deserializedSimulation = std::get<DeserializedSimulation>(data.resourceData);
try {
_simController->newSimulation(
requestData.resourceName,
deserializedSimulation.auxiliaryData.timestep,
deserializedSimulation.auxiliaryData.generalSettings,
deserializedSimulation.auxiliaryData.simulationParameters);
_simController->setRealTime(deserializedSimulation.auxiliaryData.realTime);
_simController->setClusteredSimulationData(deserializedSimulation.mainData);
_simController->setStatisticsHistory(deserializedSimulation.statistics);
} catch (CudaMemoryAllocationException const& exception) {
errorMessage = exception.what();
} catch (...) {
errorMessage = "Failed to load simulation.";
}
if (errorMessage) {
showMessage("Error", *errorMessage);
_simController->closeSimulation();
_simController->newSimulation(
requestData.resourceName,
deserializedSimulation.auxiliaryData.timestep,
deserializedSimulation.auxiliaryData.generalSettings,
deserializedSimulation.auxiliaryData.simulationParameters);
}
_persisterController->restart();

Viewport::setCenterInWorldPos(deserializedSimulation.auxiliaryData.center);
Viewport::setZoomFactor(deserializedSimulation.auxiliaryData.zoom);
_temporalControlWindow->onSnapshot();
} else {
_editorController->setOn(true);
_editorController->getGenomeEditorWindow()->openTab(std::get<GenomeDescription>(data.resourceData));
}
if (VersionChecker::isVersionNewer(data.resourceVersion)) {
std::string dataTypeString = data.resourceType == NetworkResourceType_Simulation ? "simulation" : "genome";
MessageDialog::get().information(
"Warning",
"The download was successful but the " + dataTypeString
+ " was generated using a more recent\n"
"version of ALIEN. Consequently, the "
+ dataTypeString
+ "might not function as expected.\n"
"Please visit\n\nhttps://github.com/chrxh/alien\n\nto obtain the latest version.");
}
},
[](auto const& errors) { MessageDialog::get().information("Error", errors); });
}

void NetworkTransferController::onUpload(UploadNetworkResourceRequestData const& requestData)
{
_uploadProcessor->executeTask(
[&](auto const& senderId) {
return _persisterController->scheduleUploadNetworkResource(
SenderInfo{.senderId = senderId, .wishResultData = true, .wishErrorInfo = true}, requestData);
},
[&](auto const& requestId) {
_persisterController->fetchUploadNetworkResourcesData(requestId);
_browserWindow->onRefresh();
},
[](auto const& errors) { MessageDialog::get().information("Error", errors); });
}

void NetworkTransferController::process()
{
_downloadProcessor->process();
_uploadProcessor->process();
}
39 changes: 39 additions & 0 deletions source/Gui/NetworkTransferController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "Base/Singleton.h"

#include "EngineInterface/Definitions.h"
#include "PersisterInterface/Definitions.h"
#include "PersisterInterface/PersisterRequestId.h"
#include "PersisterInterface/DownloadNetworkResourceRequestData.h"
#include "PersisterInterface/UploadNetworkResourceRequestData.h"

#include "Definitions.h"

class NetworkTransferController
{
MAKE_SINGLETON(NetworkTransferController);

public:
void init(
SimulationController const& simController,
PersisterController const& persisterController,
TemporalControlWindow const& temporalControlWindow,
EditorController const& editorController,
BrowserWindow const& browserWindow);

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

void process();

private:
SimulationController _simController;
PersisterController _persisterController;
TemporalControlWindow _temporalControlWindow;
EditorController _editorController;
BrowserWindow _browserWindow;

TaskProcessor _downloadProcessor;
TaskProcessor _uploadProcessor;
};
60 changes: 8 additions & 52 deletions source/Gui/UploadSimulationDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "GenomeEditorWindow.h"
#include "HelpStrings.h"
#include "LoginDialog.h"
#include "NetworkTransferController.h"
#include "SerializationHelperService.h"

namespace
Expand Down Expand Up @@ -143,56 +144,11 @@ void _UploadSimulationDialog::onUpload()
{
printOverlayMessage("Uploading ...");

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

DeserializedSimulation deserializedSim;
if (_resourceType == NetworkResourceType_Simulation) {
deserializedSim = SerializationHelperService::getDeserializedSerialization(_simController);

SerializedSimulation serializedSim;
if (!SerializerService::serializeSimulationToStrings(serializedSim, deserializedSim)) {
MessageDialog::get().information(
"Upload simulation", "The simulation could not be serialized for uploading.");
return;
}
mainData = serializedSim.mainData;
settings = serializedSim.auxiliaryData;
statistics = serializedSim.statistics;
size = {deserializedSim.auxiliaryData.generalSettings.worldSizeX, deserializedSim.auxiliaryData.generalSettings.worldSizeY};
numObjects = deserializedSim.mainData.getNumberOfCellAndParticles();
} else {
auto genome = _genomeEditorWindow->getCurrentGenome();
if (genome.cells.empty()) {
showMessage("Upload 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("Upload genome", "The genome could not be serialized for uploading.");
return;
}
}

std::string resourceId;
auto workspaceType = _share ? WorkspaceType_Public : WorkspaceType_Private;
if (!NetworkService::uploadResource(
resourceId, _folder + _resourceName, _resourceDescription, size, numObjects, mainData, settings, statistics, _resourceType, workspaceType)) {
showMessage(
"Error",
"Failed to upload " + BrowserDataTypeToLowerString.at(_resourceType)
+ ".\n\nPossible 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;
}
if (_resourceType == NetworkResourceType_Simulation) {
_browserWindow->getSimulationCache()->insertOrAssign(resourceId, deserializedSim);
}
_browserWindow->onRefresh();
});
auto workspaceType = _share ? WorkspaceType_Public : WorkspaceType_Private;
NetworkTransferController::get().onUpload(UploadNetworkResourceRequestData{
.folderName = _folder,
.resourceWithoutFolderName = _resourceName,
.resourceDescription = _resourceDescription,
.workspaceType = workspaceType,
.downloadCache = _browserWindow->getSimulationCache()});

Check failure on line 153 in source/Gui/UploadSimulationDialog.cpp

View workflow job for this annotation

GitHub Actions / build

use of deleted function ‘std::variant<_Types>::variant() [with _Types = {UploadNetworkResourceRequestData::SimulationData, UploadNetworkResourceRequestData::GenomeData}]’
}
Loading

0 comments on commit f2291e1

Please sign in to comment.