Skip to content

Commit

Permalink
make NetworkService a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 16, 2024
1 parent 2797168 commit 43baa12
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 95 deletions.
4 changes: 2 additions & 2 deletions source/Gui/ActivateUserDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ void _ActivateUserDialog::processIntern()

void _ActivateUserDialog::onActivateUser()
{
auto result = NetworkService::activateUser(_userName, _password, _userInfo, _confirmationCode);
auto result = NetworkService::get().activateUser(_userName, _password, _userInfo, _confirmationCode);
if (result) {
LoginErrorCode errorCode;
result |= NetworkService::login(errorCode, _userName, _password, _userInfo);
result |= NetworkService::get().login(errorCode, _userName, _password, _userInfo);
}
if (!result) {
MessageDialog::get().information("Error", "An error occurred on the server. Your entered code may be incorrect.\nPlease try to register again.");
Expand Down
30 changes: 15 additions & 15 deletions source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void _BrowserWindow::refreshIntern(bool withRetry)

for (auto& [workspaceId, workspace] : _workspaces) {
workspace.rawTOs.clear();
auto userName = NetworkService::getLoggedInUserName().value_or("");
auto userName = NetworkService::get().getLoggedInUserName().value_or("");
for (auto const& rawTO : data.resourceTOs) {
if (rawTO->resourceType == workspaceId.resourceType) {
//public user items should also be visible in private workspace
Expand Down Expand Up @@ -250,7 +250,7 @@ void _BrowserWindow::processToolbar()

//login button
ImGui::SameLine();
ImGui::BeginDisabled(NetworkService::getLoggedInUserName().has_value());
ImGui::BeginDisabled(NetworkService::get().getLoggedInUserName().has_value());
if (AlienImGui::ToolbarButton(ICON_FA_SIGN_IN_ALT)) {
if (auto loginDialog = _loginDialog.lock()) {
loginDialog->open();
Expand All @@ -261,10 +261,10 @@ void _BrowserWindow::processToolbar()

//logout button
ImGui::SameLine();
ImGui::BeginDisabled(!NetworkService::getLoggedInUserName());
ImGui::BeginDisabled(!NetworkService::get().getLoggedInUserName());
if (AlienImGui::ToolbarButton(ICON_FA_SIGN_OUT_ALT)) {
if (auto loginDialog = _loginDialog.lock()) {
NetworkService::logout();
NetworkService::get().logout();
onRefresh();
}
}
Expand Down Expand Up @@ -402,7 +402,7 @@ void _BrowserWindow::processWorkspaceSelectionAndFilter()

ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
auto userName = NetworkService::getLoggedInUserName();
auto userName = NetworkService::get().getLoggedInUserName();
auto privateWorkspaceString = userName.has_value() ? *userName + "'s private workspace" : "Private workspace (need to login)";
auto workspaceType_reordered = 2 - _currentWorkspace.workspaceType; //change the order for display
if (AlienImGui::Switcher(
Expand Down Expand Up @@ -464,7 +464,7 @@ void _BrowserWindow::processUserList()
AlienImGui::Group("Simulators");
if (ImGui::BeginTable("Browser", 5, flags, ImVec2(0, 0), 0.0f)) {
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_PreferSortDescending | ImGuiTableColumnFlags_WidthFixed, scale(90.0f));
auto isLoggedIn = NetworkService::getLoggedInUserName().has_value();
auto isLoggedIn = NetworkService::get().getLoggedInUserName().has_value();
ImGui::TableSetupColumn(
isLoggedIn ? "GPU model" : "GPU (visible if logged in)",
ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed,
Expand All @@ -488,7 +488,7 @@ void _BrowserWindow::processUserList()
ImGui::TableNextRow(0, scale(RowHeight));

ImGui::TableNextColumn();
auto isBoldFont = isLoggedIn && *NetworkService::getLoggedInUserName() == item->userName;
auto isBoldFont = isLoggedIn && *NetworkService::get().getLoggedInUserName() == item->userName;

if (item->online) {
AlienImGui::OnlineSymbol();
Expand Down Expand Up @@ -551,13 +551,13 @@ void _BrowserWindow::processStatus()
statusText += std::to_string(_userTOs.size()) + " simulators found";

statusText += std::string(" " ICON_FA_INFO_CIRCLE " ");
if (auto userName = NetworkService::getLoggedInUserName()) {
statusText += "Logged in as " + *userName + " @ " + NetworkService::getServerAddress(); // + ": ";
if (auto userName = NetworkService::get().getLoggedInUserName()) {
statusText += "Logged in as " + *userName + " @ " + NetworkService::get().getServerAddress(); // + ": ";
} else {
statusText += "Not logged in to " + NetworkService::getServerAddress(); // + ": ";
statusText += "Not logged in to " + NetworkService::get().getServerAddress(); // + ": ";
}

if (!NetworkService::getLoggedInUserName()) {
if (!NetworkService::get().getLoggedInUserName()) {
statusText += std::string(" " ICON_FA_INFO_CIRCLE " ");
statusText += "In order to share and upvote simulations you need to log in.";
}
Expand Down Expand Up @@ -1329,7 +1329,7 @@ void _BrowserWindow::onMoveResource(NetworkResourceTreeTO const& treeTO)
//apply changes to server
delayedExecution([rawTOs = rawTOs, this] {
for (auto const& rawTO : rawTOs) {
if (!NetworkService::moveResource(rawTO->id, rawTO->workspaceType)) {
if (!NetworkService::get().moveResource(rawTO->id, rawTO->workspaceType)) {
MessageDialog::get().information("Error", "Failed to move item.");
refreshIntern(true);
return;
Expand Down Expand Up @@ -1370,7 +1370,7 @@ void _BrowserWindow::onToggleLike(NetworkResourceTreeTO const& to, int emojiType
{
CHECK(to->isLeaf());
auto& leaf = to->getLeaf();
if (NetworkService::getLoggedInUserName()) {
if (NetworkService::get().getLoggedInUserName()) {

//remove existing like
auto findResult = _ownEmojiTypeBySimId.find(leaf.rawTO->id);
Expand All @@ -1396,7 +1396,7 @@ void _BrowserWindow::onToggleLike(NetworkResourceTreeTO const& to, int emojiType
}

_userNamesByEmojiTypeBySimIdCache.erase(std::make_pair(leaf.rawTO->id, emojiType)); //invalidate cache entry
NetworkService::toggleReactToResource(leaf.rawTO->id, emojiType);
NetworkService::get().toggleReactToResource(leaf.rawTO->id, emojiType);
} else {
_loginDialog.lock()->open();
}
Expand Down Expand Up @@ -1431,7 +1431,7 @@ bool _BrowserWindow::isOwner(NetworkResourceTreeTO const& treeTO) const
auto const& workspace = _workspaces.at(_currentWorkspace);

auto rawTOs = NetworkResourceService::getMatchingRawTOs(treeTO, workspace.rawTOs);
auto userName = NetworkService::getLoggedInUserName().value_or("");
auto userName = NetworkService::get().getLoggedInUserName().value_or("");
return std::ranges::all_of(rawTOs, [&](NetworkResourceRawTO const& rawTO) { return rawTO->userName == userName; });
}

Expand Down
2 changes: 1 addition & 1 deletion source/Gui/CreateUserDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void _CreateUserDialog::processIntern()

void _CreateUserDialog::onCreateUser()
{
if (NetworkService::createUser(_userName, _password, _email)) {
if (NetworkService::get().createUser(_userName, _password, _email)) {
_activateUserDialog->open(_userName, _password, _userInfo);
} else {
MessageDialog::get().information(
Expand Down
8 changes: 4 additions & 4 deletions source/Gui/DeleteUserDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _DeleteUserDialog::_DeleteUserDialog(BrowserWindow const& browserWindow)
void _DeleteUserDialog::processIntern()
{
AlienImGui::Text(
"Warning: All the data of the user '" + *NetworkService::getLoggedInUserName()
"Warning: All the data of the user '" + *NetworkService::get().getLoggedInUserName()
+ "' will be deleted on the server side.\nThese include the likes, the simulations and the account data.");
AlienImGui::Separator();

Expand All @@ -28,7 +28,7 @@ void _DeleteUserDialog::processIntern()
ImGui::BeginDisabled(_reenteredPassword.empty());
if (AlienImGui::Button("Delete")) {
close();
if (_reenteredPassword == *NetworkService::getPassword()) {
if (_reenteredPassword == *NetworkService::get().getPassword()) {
onDelete();
} else {
MessageDialog::get().information("Error", "The password does not match.");
Expand All @@ -47,8 +47,8 @@ void _DeleteUserDialog::processIntern()

void _DeleteUserDialog::onDelete()
{
auto userName = *NetworkService::getLoggedInUserName();
if (NetworkService::deleteUser()) {
auto userName = *NetworkService::get().getLoggedInUserName();
if (NetworkService::get().deleteUser()) {
_browserWindow->onRefresh();
MessageDialog::get().information("Information", "The user '" + userName + "' has been deleted.\nYou are logged out.");
} else {
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/EditSimulationDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void _EditSimulationDialog::processForLeaf()
if (AlienImGui::Button("OK")) {
if (ValidationService::isStringValidForDatabase(_newName) && ValidationService::isStringValidForDatabase(_newDescription)) {
delayedExecution([rawTO = rawTO, resourceTypeString = resourceTypeString, this] {
if (!NetworkService::editResource(rawTO->id, _newName, _newDescription)) {
if (!NetworkService::get().editResource(rawTO->id, _newName, _newDescription)) {
showMessage("Error", "Failed to edit " + resourceTypeString + ".");
}
_browserWindow->onRefresh();
Expand Down Expand Up @@ -110,7 +110,7 @@ void _EditSimulationDialog::processForFolder()
for (auto const& rawTO : _rawTOs) {
auto nameWithoutOldFolder = rawTO->resourceName.substr(_origFolderName.size() + 1);
auto newName = NetworkResourceService::concatenateFolderName({_newName, nameWithoutOldFolder}, false);
if (!NetworkService::editResource(rawTO->id, newName, rawTO->description)) {
if (!NetworkService::get().editResource(rawTO->id, newName, rawTO->description)) {
showMessage("Error", "Failed to change folder name.");
break;
}
Expand Down
26 changes: 13 additions & 13 deletions source/Gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ _MainWindow::_MainWindow(SimulationFacade const& simulationFacade, PersisterFaca

//init services
StyleRepository::get().init();
NetworkService::init();
NetworkService::get().init();

//init controllers, windows and dialogs
Viewport::get().init(_simulationFacade);
Expand Down Expand Up @@ -251,7 +251,7 @@ void _MainWindow::shutdown()

_persisterFacade->shutdown();
_simulationFacade->closeSimulation();
NetworkService::shutdown();
NetworkService::get().shutdown();
}

char const* _MainWindow::initGlfwAndReturnGlslVersion()
Expand Down Expand Up @@ -409,30 +409,30 @@ void _MainWindow::processMenubar()
_browserWindow->setOn(!_browserWindow->isOn());
}
ImGui::Separator();
ImGui::BeginDisabled((bool)NetworkService::getLoggedInUserName());
ImGui::BeginDisabled((bool)NetworkService::get().getLoggedInUserName());
if (ImGui::MenuItem("Login", "ALT+L")) {
_loginDialog->open();
}
ImGui::EndDisabled();
ImGui::BeginDisabled(!NetworkService::getLoggedInUserName());
ImGui::BeginDisabled(!NetworkService::get().getLoggedInUserName());
if (ImGui::MenuItem("Logout", "ALT+T")) {
NetworkService::logout();
NetworkService::get().logout();
_browserWindow->onRefresh();
}
ImGui::EndDisabled();
ImGui::BeginDisabled(!NetworkService::getLoggedInUserName());
ImGui::BeginDisabled(!NetworkService::get().getLoggedInUserName());
if (ImGui::MenuItem("Upload simulation", "ALT+D")) {
_uploadSimulationDialog->open(NetworkResourceType_Simulation);
}
ImGui::EndDisabled();
ImGui::BeginDisabled(!NetworkService::getLoggedInUserName());
ImGui::BeginDisabled(!NetworkService::get().getLoggedInUserName());
if (ImGui::MenuItem("Upload genome", "ALT+Q")) {
_uploadSimulationDialog->open(NetworkResourceType_Genome);
}
ImGui::EndDisabled();

ImGui::Separator();
ImGui::BeginDisabled(!NetworkService::getLoggedInUserName());
ImGui::BeginDisabled(!NetworkService::get().getLoggedInUserName());
if (ImGui::MenuItem("Delete user", "ALT+J")) {
_deleteUserDialog->open();
}
Expand Down Expand Up @@ -602,20 +602,20 @@ void _MainWindow::processMenubar()
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_W)) {
_browserWindow->setOn(!_browserWindow->isOn());
}
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_L) && !NetworkService::getLoggedInUserName()) {
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_L) && !NetworkService::get().getLoggedInUserName()) {
_loginDialog->open();
}
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_T)) {
NetworkService::logout();
NetworkService::get().logout();
_browserWindow->onRefresh();
}
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_D) && NetworkService::getLoggedInUserName()) {
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_D) && NetworkService::get().getLoggedInUserName()) {
_uploadSimulationDialog->open(NetworkResourceType_Simulation);
}
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_Q) && NetworkService::getLoggedInUserName()) {
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_Q) && NetworkService::get().getLoggedInUserName()) {
_uploadSimulationDialog->open(NetworkResourceType_Genome);
}
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_J) && NetworkService::getLoggedInUserName()) {
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_J) && NetworkService::get().getLoggedInUserName()) {
_deleteUserDialog->open();
}

Expand Down
4 changes: 2 additions & 2 deletions source/Gui/NetworkSettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ void _NetworkSettingsDialog::processIntern()

void _NetworkSettingsDialog::openIntern()
{
_origServerAddress = NetworkService::getServerAddress();
_origServerAddress = NetworkService::get().getServerAddress();
_serverAddress = _origServerAddress;
}

void _NetworkSettingsDialog::onChangeSettings()
{
NetworkService::setServerAddress(_serverAddress);
NetworkService::get().setServerAddress(_serverAddress);
_browserWindow->onRefresh();
}
4 changes: 2 additions & 2 deletions source/Gui/NewPasswordDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ void _NewPasswordDialog::processIntern()

void _NewPasswordDialog::onNewPassword()
{
auto result = NetworkService::setNewPassword(_userName, _newPassword, _confirmationCode);
auto result = NetworkService::get().setNewPassword(_userName, _newPassword, _confirmationCode);
if (result) {
LoginErrorCode errorCode;
result |= NetworkService::login(errorCode, _userName, _newPassword, _userInfo);
result |= NetworkService::get().login(errorCode, _userName, _newPassword, _userInfo);
}
if (!result) {
MessageDialog::get().information("Error", "An error occurred on the server. Your entered code may be incorrect.\nPlease try to reset the password again.");
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/ResetPasswordDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void _ResetPasswordDialog::processIntern()

void _ResetPasswordDialog::onResetPassword()
{
if (NetworkService::resetPassword(_userName, _email)) {
if (NetworkService::get().resetPassword(_userName, _email)) {
_newPasswordDialog->open(_userName, _userInfo);
} else {
MessageDialog::get().information(
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/UploadSimulationDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ _UploadSimulationDialog::~_UploadSimulationDialog()

void _UploadSimulationDialog::open(NetworkResourceType resourceType, std::string const& folder)
{
if (NetworkService::getLoggedInUserName()) {
if (NetworkService::get().getLoggedInUserName()) {
changeTitle("Upload " + BrowserDataTypeToLowerString.at(resourceType));
_resourceType = resourceType;
_folder = folder;
Expand Down
6 changes: 0 additions & 6 deletions source/Network/NetworkService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ namespace
}
}

std::string NetworkService::_serverAddress;
std::optional<std::string> NetworkService::_loggedInUserName;
std::optional<std::string> NetworkService::_password;
std::optional<std::chrono::steady_clock::time_point> NetworkService::_lastRefreshTime;
Cache<std::string, NetworkService::ResourceData, 20> NetworkService::_downloadCache;

void NetworkService::init()
{
_serverAddress = GlobalSettings::get().getString("settings.server", "alien-project.org");
Expand Down
Loading

0 comments on commit 43baa12

Please sign in to comment.