From acd293a58a8efe20f98d8702d4823fae5ef81316 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 10 Sep 2021 18:21:23 -0500 Subject: remove I/ManagementService --- src/slskd/Application/Application.cs | 21 +++++ src/slskd/Application/IApplication.cs | 4 + .../API/Controllers/ApplicationController.cs | 14 +-- .../API/Controllers/ConfigurationController.cs | 13 ++- .../Management/API/Controllers/ServerController.cs | 19 ++-- .../Application/Management/IManagementService.cs | 62 ------------- .../Application/Management/ManagementService.cs | 102 --------------------- src/slskd/Startup.cs | 1 - 8 files changed, 52 insertions(+), 184 deletions(-) delete mode 100644 src/slskd/Application/Management/IManagementService.cs delete mode 100644 src/slskd/Application/Management/ManagementService.cs (limited to 'src') diff --git a/src/slskd/Application/Application.cs b/src/slskd/Application/Application.cs index 6b95c779..fb521fdb 100644 --- a/src/slskd/Application/Application.cs +++ b/src/slskd/Application/Application.cs @@ -244,6 +244,27 @@ namespace slskd } } + /// + /// Connects the Soulseek client to the server using the configured username and password. + /// + /// The operation context. + public Task ConnectAsync() + => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); + + /// + /// Disconnects the Soulseek client from the server. + /// + /// An optional message containing the reason for the disconnect. + /// An optional Exception to associate with the disconnect. + public void Disconnect(string message = null, Exception exception = null) + => Client.Disconnect(message, exception ?? new IntentionalDisconnectException(message)); + + /// + /// Re-scans shared directories. + /// + /// The operation context. + public Task RescanSharesAsync() => SharedFileCache.FillAsync(); + private async Task OptionsMonitor_OnChange(Options newOptions) { // this code is known to fire more than once per update. i'm not sure diff --git a/src/slskd/Application/IApplication.cs b/src/slskd/Application/IApplication.cs index 45cf0a10..db23b766 100644 --- a/src/slskd/Application/IApplication.cs +++ b/src/slskd/Application/IApplication.cs @@ -17,11 +17,15 @@ namespace slskd { + using System; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; public interface IApplication : IHostedService { public Task CheckVersionAsync(); + public Task ConnectAsync(); + public void Disconnect(string message = null, Exception exception = null); + public Task RescanSharesAsync(); } } diff --git a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs index b3d43300..8115b3fc 100644 --- a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs +++ b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs @@ -34,15 +34,18 @@ namespace slskd.Management.API [Consumes("application/json")] public class ApplicationController : ControllerBase { - public ApplicationController(IManagementService managementService, IHostApplicationLifetime lifetime, IApplication application) + public ApplicationController( + IHostApplicationLifetime lifetime, + IApplication application, + IStateMonitor applicationStateMonitor) { - Management = managementService; Lifetime = lifetime; Application = application; + ApplicationStateMonitor = applicationStateMonitor; } private IApplication Application { get; } - private IManagementService Management { get; } + private IStateMonitor ApplicationStateMonitor { get; } private IHostApplicationLifetime Lifetime { get; } /// @@ -53,7 +56,7 @@ namespace slskd.Management.API [Authorize] public IActionResult State() { - return Ok(Management.ApplicationState); + return Ok(ApplicationStateMonitor.CurrentValue); } /// @@ -106,8 +109,7 @@ namespace slskd.Management.API await Application.CheckVersionAsync(); } - var state = Management.ApplicationState; - return Ok(state.Version); + return Ok(ApplicationStateMonitor.CurrentValue.Version); } } } diff --git a/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs b/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs index f2b90718..b8c81f84 100644 --- a/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs +++ b/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs @@ -34,15 +34,18 @@ namespace slskd.Management.API public class ConfigurationController : ControllerBase { public ConfigurationController( + IApplication application, IOptionsSnapshot optionsShapshot, - IManagementService managementService) + IStateMonitor applicationStateMonitor) { + Application = application; OptionsShapshot = optionsShapshot; - ManagementService = managementService; + ApplicationStateMonitor = applicationStateMonitor; } - private IManagementService ManagementService { get; } + private IApplication Application { get; } private IOptionsSnapshot OptionsShapshot { get; } + private IStateMonitor ApplicationStateMonitor { get; } [HttpGet] [Route("")] @@ -58,12 +61,12 @@ namespace slskd.Management.API [Authorize] public IActionResult RescanSharesAsync() { - if (ManagementService.SharedFileCacheState.Filling) + if (ApplicationStateMonitor.CurrentValue.SharedFileCache.Filling) { return Conflict("A share scan is already in progress."); } - _ = ManagementService.RescanSharesAsync(); + _ = Application.RescanSharesAsync(); return Ok(); } diff --git a/src/slskd/Application/Management/API/Controllers/ServerController.cs b/src/slskd/Application/Management/API/Controllers/ServerController.cs index fc47f475..127b59c0 100644 --- a/src/slskd/Application/Management/API/Controllers/ServerController.cs +++ b/src/slskd/Application/Management/API/Controllers/ServerController.cs @@ -32,12 +32,15 @@ namespace slskd.Management.API public class ServerController : ControllerBase { public ServerController( - IManagementService managementService) + IApplication application, + IStateMonitor applicationStateMonitor) { - Management = managementService; + Application = application; + ApplicationStateMonitor = applicationStateMonitor; } - private IManagementService Management { get; } + private IApplication Application { get; } + private IStateMonitor ApplicationStateMonitor { get; } /// /// Connects the client. @@ -50,9 +53,9 @@ namespace slskd.Management.API [ProducesResponseType(403)] public async Task Connect() { - if (!Management.ServerState.IsConnected) + if (!ApplicationStateMonitor.CurrentValue.Server.IsConnected) { - await Management.ConnectServerAsync(); + await Application.ConnectAsync(); } return Ok(); @@ -70,9 +73,9 @@ namespace slskd.Management.API [ProducesResponseType(403)] public IActionResult Disconnect([FromBody] string message) { - if (Management.ServerState.IsConnected) + if (ApplicationStateMonitor.CurrentValue.Server.IsConnected) { - Management.DisconnectServer(message); + Application.Disconnect(message); } return NoContent(); @@ -90,7 +93,7 @@ namespace slskd.Management.API [ProducesResponseType(403)] public IActionResult Get() { - return Ok(Management.ServerState); + return Ok(ApplicationStateMonitor.CurrentValue.Server); } } } \ No newline at end of file diff --git a/src/slskd/Application/Management/IManagementService.cs b/src/slskd/Application/Management/IManagementService.cs deleted file mode 100644 index 9a06d961..00000000 --- a/src/slskd/Application/Management/IManagementService.cs +++ /dev/null @@ -1,62 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Management -{ - using System; - using System.Threading.Tasks; - - /// - /// Application and Soulseek client management. - /// - public interface IManagementService - { - /// - /// Gets the current state of the slskd service. - /// - ApplicationState ApplicationState { get; } - - /// - /// Gets the current state of the connection to the Soulseek server. - /// - ServerState ServerState { get; } - - /// - /// Gets the current state of the shared file cache. - /// - SharedFileCacheState SharedFileCacheState { get; } - - /// - /// Connects the Soulseek client to the server using the configured username and password. - /// - /// The operation context. - Task ConnectServerAsync(); - - /// - /// Disconnects the Soulseek client from the server. - /// - /// An optional message containing the reason for the disconnect. - /// An optional Exception to associate with the disconnect. - void DisconnectServer(string message = null, Exception exception = null); - - /// - /// Re-scans shared directories. - /// - /// The operation context. - Task RescanSharesAsync(); - } -} \ No newline at end of file diff --git a/src/slskd/Application/Management/ManagementService.cs b/src/slskd/Application/Management/ManagementService.cs deleted file mode 100644 index 4319c9e3..00000000 --- a/src/slskd/Application/Management/ManagementService.cs +++ /dev/null @@ -1,102 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -using Microsoft.Extensions.Options; - -namespace slskd.Management -{ - using System; - using System.Threading.Tasks; - using Serilog; - using Soulseek; - - /// - /// Application and Soulseek client management. - /// - public class ManagementService : IManagementService - { - /// - /// Initializes a new instance of the class. - /// - /// The options monitor used to derive application options. - /// The state monitor for application service state. - /// The Soulseek client. - /// The shared file cache. - public ManagementService( - IOptionsMonitor optionsMonitor, - IStateMonitor applicationStateMonitor, - ISoulseekClient soulseekClient, - ISharedFileCache sharedFileCache) - { - OptionsMonitor = optionsMonitor; - ApplicationStateMonitor = applicationStateMonitor; - Client = soulseekClient; - SharedFileCache = sharedFileCache; - } - - private ILogger Log { get; } = Serilog.Log.ForContext(); - - /// - /// Gets the current state of the slskd service. - /// - public ApplicationState ApplicationState => ApplicationStateMonitor.CurrentValue; - - /// - /// Gets the current state of the connection to the Soulseek server. - /// - public ServerState ServerState => - new ServerState() - { - Address = Client.Address, - IPEndPoint = Client.IPEndPoint, - State = Client.State, - Username = Client.Username, - }; - - /// - /// Gets the current state of the connection to the Soulseek server. - /// - public SharedFileCacheState SharedFileCacheState => SharedFileCache.State.CurrentValue; - - private ISoulseekClient Client { get; } - private Options Options => OptionsMonitor.CurrentValue; - private IOptionsMonitor OptionsMonitor { get; } - private IStateMonitor ApplicationStateMonitor { get; } - private ISharedFileCache SharedFileCache { get; } - - /// - /// Connects the Soulseek client to the server using the configured username and password. - /// - /// The operation context. - public Task ConnectServerAsync() - => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); - - /// - /// Disconnects the Soulseek client from the server. - /// - /// An optional message containing the reason for the disconnect. - /// An optional Exception to associate with the disconnect. - public void DisconnectServer(string message = null, Exception exception = null) - => Client.Disconnect(message, exception ?? new IntentionalDisconnectException(message)); - - /// - /// Re-scans shared directories. - /// - /// The operation context. - public Task RescanSharesAsync() => SharedFileCache.FillAsync(); - } -} \ No newline at end of file diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 5c83fed3..81896eb2 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -211,7 +211,6 @@ namespace slskd services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); -- cgit v1.2.3