Server termination and refactoring message dialogs.

This commit is contained in:
Burak Kaan Köse
2024-08-21 22:42:52 +02:00
parent bab3272970
commit f627226da9
31 changed files with 548 additions and 388 deletions

View File

@@ -21,6 +21,7 @@ namespace Wino.Server.Core
nameof(ProtocolAuthorizationCallbackReceived) => App.Current.Services.GetService<ProtocolAuthActivationHandler>(),
nameof(SynchronizationExistenceCheckRequest) => App.Current.Services.GetService<SyncExistenceHandler>(),
nameof(ServerTerminationModeChanged) => App.Current.Services.GetService<ServerTerminationModeHandler>(),
nameof(TerminateServerRequested) => App.Current.Services.GetService<TerminateServerRequestHandler>(),
_ => throw new Exception($"Server handler for {typeName} is not registered."),
};
}
@@ -36,6 +37,7 @@ namespace Wino.Server.Core
serviceCollection.AddTransient<ProtocolAuthActivationHandler>();
serviceCollection.AddTransient<SyncExistenceHandler>();
serviceCollection.AddTransient<ServerTerminationModeHandler>();
serviceCollection.AddTransient<TerminateServerRequestHandler>();
}
}
}

View File

@@ -0,0 +1,26 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using Wino.Core.Domain.Models.Server;
using Wino.Messaging.Server;
using Wino.Server.Core;
namespace Wino.Server.MessageHandlers
{
public class TerminateServerRequestHandler : ServerMessageHandler<TerminateServerRequested, bool>
{
public override WinoServerResponse<bool> FailureDefaultResponse(Exception ex) => WinoServerResponse<bool>.CreateErrorResponse(ex.Message);
protected override Task<WinoServerResponse<bool>> HandleAsync(TerminateServerRequested message, CancellationToken cancellationToken = default)
{
// This handler is only doing the logging right now.
// Client will always expect success response.
// Server will be terminated in the server context once the client gets the response.
Log.Information("Terminate server is requested by client. Killing server.");
return Task.FromResult(WinoServerResponse<bool>.CreateSuccessResponse(true));
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using CommunityToolkit.Mvvm.Messaging;
using Serilog;
using Windows.ApplicationModel;
@@ -308,12 +309,28 @@ namespace Wino.Server
case nameof(ServerTerminationModeChanged):
await ExecuteServerMessageSafeAsync(args, JsonSerializer.Deserialize<ServerTerminationModeChanged>(messageJson, _jsonSerializerOptions));
break;
case nameof(TerminateServerRequested):
await ExecuteServerMessageSafeAsync(args, JsonSerializer.Deserialize<TerminateServerRequested>(messageJson, _jsonSerializerOptions));
KillServer();
break;
default:
Debug.WriteLine($"Missing handler for {typeName} in the server. Check ServerContext.cs - HandleServerMessageAsync.");
break;
}
}
private void KillServer()
{
DisposeConnection();
Application.Current.Dispatcher.Invoke(() =>
{
Application.Current.Shutdown();
});
}
/// <summary>
/// Executes ServerMessage coming from the UWP.
/// These requests are awaited and expected to return a response.