// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.Dapr.Common; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Threading; using global::Dapr.Client; using MUnique.OpenMU.Interfaces; /// /// A client to control a . /// /// internal class ManageableServerClient : IManageableServer { private readonly DaprClient _daprClient; private readonly string _targetAppId; private ServerState _serverState; private int _currentConnections; /// /// Initializes a new instance of the class. /// /// The dapr client. /// The server data. public ManageableServerClient(DaprClient daprClient, ServerStateData serverData) { this._daprClient = daprClient; this._targetAppId = serverData.AppId; this.Id = serverData.Id; this.Description = serverData.Description; this.ConfigurationId = serverData.ConfigurationId; this.CurrentConnections = serverData.CurrentConnections; this.MaximumConnections = serverData.MaximumConnections; this.ServerState = serverData.State; this.Type = serverData.Type; this.LastUpdate = DateTime.UtcNow; } /// public event PropertyChangedEventHandler? PropertyChanged; /// public int Id { get; } /// public Guid ConfigurationId { get; } /// public string Description { get; } /// public ServerType Type { get; } /// public int MaximumConnections { get; } /// /// Gets the timestamp of the last update. /// public DateTime LastUpdate { get; private set; } /// public ServerState ServerState { get => this._serverState; set { if (this._serverState == value) { return; } this._serverState = value; this.RaisePropertyChanged(); } } /// public int CurrentConnections { get => this._currentConnections; set { if (this._currentConnections == value) { return; } this._currentConnections = value; this.RaisePropertyChanged(); } } /// public Task StartAsync(CancellationToken cancellationToken) { return this._daprClient.InvokeMethodAsync(this._targetAppId, nameof(IManageableServer.StartAsync), cancellationToken); } /// public async ValueTask StartAsync() { await this.StartAsync(default).ConfigureAwait(false); } /// public Task StopAsync(CancellationToken cancellationToken) { return this._daprClient.InvokeMethodAsync(this._targetAppId, nameof(IManageableServer.ShutdownAsync), cancellationToken); } /// public async ValueTask ShutdownAsync() { await this.StopAsync(default).ConfigureAwait(false); } /// /// Updates the specified server data. /// /// The server data. public void Update(ServerStateData serverData) { this.ServerState = serverData.State; this.CurrentConnections = serverData.CurrentConnections; this.LastUpdate = DateTime.UtcNow; } private void RaisePropertyChanged([CallerMemberName] string propertyName = "") { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }