diff options
Diffstat (limited to 'src')
8 files changed, 52 insertions, 184 deletions
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 } } + /// <summary> + /// Connects the Soulseek client to the server using the configured username and password. + /// </summary> + /// <returns>The operation context.</returns> + public Task ConnectAsync() + => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); + + /// <summary> + /// Disconnects the Soulseek client from the server. + /// </summary> + /// <param name="message">An optional message containing the reason for the disconnect.</param> + /// <param name="exception">An optional Exception to associate with the disconnect.</param> + public void Disconnect(string message = null, Exception exception = null) + => Client.Disconnect(message, exception ?? new IntentionalDisconnectException(message)); + + /// <summary> + /// Re-scans shared directories. + /// </summary> + /// <returns>The operation context.</returns> + 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<ApplicationState> applicationStateMonitor) { - Management = managementService; Lifetime = lifetime; Application = application; + ApplicationStateMonitor = applicationStateMonitor; } private IApplication Application { get; } - private IManagementService Management { get; } + private IStateMonitor<ApplicationState> ApplicationStateMonitor { get; } private IHostApplicationLifetime Lifetime { get; } /// <summary> @@ -53,7 +56,7 @@ namespace slskd.Management.API [Authorize] public IActionResult State() { - return Ok(Management.ApplicationState); + return Ok(ApplicationStateMonitor.CurrentValue); } /// <summary> @@ -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<Options> optionsShapshot, - IManagementService managementService) + IStateMonitor<ApplicationState> applicationStateMonitor) { + Application = application; OptionsShapshot = optionsShapshot; - ManagementService = managementService; + ApplicationStateMonitor = applicationStateMonitor; } - private IManagementService ManagementService { get; } + private IApplication Application { get; } private IOptionsSnapshot<Options> OptionsShapshot { get; } + private IStateMonitor<ApplicationState> 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<ApplicationState> applicationStateMonitor) { - Management = managementService; + Application = application; + ApplicationStateMonitor = applicationStateMonitor; } - private IManagementService Management { get; } + private IApplication Application { get; } + private IStateMonitor<ApplicationState> ApplicationStateMonitor { get; } /// <summary> /// Connects the client. @@ -50,9 +53,9 @@ namespace slskd.Management.API [ProducesResponseType(403)] public async Task<IActionResult> 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 file="IManagementService.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Management -{ - using System; - using System.Threading.Tasks; - - /// <summary> - /// Application and Soulseek client management. - /// </summary> - public interface IManagementService - { - /// <summary> - /// Gets the current state of the slskd service. - /// </summary> - ApplicationState ApplicationState { get; } - - /// <summary> - /// Gets the current state of the connection to the Soulseek server. - /// </summary> - ServerState ServerState { get; } - - /// <summary> - /// Gets the current state of the shared file cache. - /// </summary> - SharedFileCacheState SharedFileCacheState { get; } - - /// <summary> - /// Connects the Soulseek client to the server using the configured username and password. - /// </summary> - /// <returns>The operation context.</returns> - Task ConnectServerAsync(); - - /// <summary> - /// Disconnects the Soulseek client from the server. - /// </summary> - /// <param name="message">An optional message containing the reason for the disconnect.</param> - /// <param name="exception">An optional Exception to associate with the disconnect.</param> - void DisconnectServer(string message = null, Exception exception = null); - - /// <summary> - /// Re-scans shared directories. - /// </summary> - /// <returns>The operation context.</returns> - 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 file="ManagementService.cs" company="slskd Team"> -// 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/. -// </copyright> - -using Microsoft.Extensions.Options; - -namespace slskd.Management -{ - using System; - using System.Threading.Tasks; - using Serilog; - using Soulseek; - - /// <summary> - /// Application and Soulseek client management. - /// </summary> - public class ManagementService : IManagementService - { - /// <summary> - /// Initializes a new instance of the <see cref="ManagementService"/> class. - /// </summary> - /// <param name="optionsMonitor">The options monitor used to derive application options.</param> - /// <param name="applicationStateMonitor">The state monitor for application service state.</param> - /// <param name="soulseekClient">The Soulseek client.</param> - /// <param name="sharedFileCache">The shared file cache.</param> - public ManagementService( - IOptionsMonitor<Options> optionsMonitor, - IStateMonitor<ApplicationState> applicationStateMonitor, - ISoulseekClient soulseekClient, - ISharedFileCache sharedFileCache) - { - OptionsMonitor = optionsMonitor; - ApplicationStateMonitor = applicationStateMonitor; - Client = soulseekClient; - SharedFileCache = sharedFileCache; - } - - private ILogger Log { get; } = Serilog.Log.ForContext<ManagementService>(); - - /// <summary> - /// Gets the current state of the slskd service. - /// </summary> - public ApplicationState ApplicationState => ApplicationStateMonitor.CurrentValue; - - /// <summary> - /// Gets the current state of the connection to the Soulseek server. - /// </summary> - public ServerState ServerState => - new ServerState() - { - Address = Client.Address, - IPEndPoint = Client.IPEndPoint, - State = Client.State, - Username = Client.Username, - }; - - /// <summary> - /// Gets the current state of the connection to the Soulseek server. - /// </summary> - public SharedFileCacheState SharedFileCacheState => SharedFileCache.State.CurrentValue; - - private ISoulseekClient Client { get; } - private Options Options => OptionsMonitor.CurrentValue; - private IOptionsMonitor<Options> OptionsMonitor { get; } - private IStateMonitor<ApplicationState> ApplicationStateMonitor { get; } - private ISharedFileCache SharedFileCache { get; } - - /// <summary> - /// Connects the Soulseek client to the server using the configured username and password. - /// </summary> - /// <returns>The operation context.</returns> - public Task ConnectServerAsync() - => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password); - - /// <summary> - /// Disconnects the Soulseek client from the server. - /// </summary> - /// <param name="message">An optional message containing the reason for the disconnect.</param> - /// <param name="exception">An optional Exception to associate with the disconnect.</param> - public void DisconnectServer(string message = null, Exception exception = null) - => Client.Disconnect(message, exception ?? new IntentionalDisconnectException(message)); - - /// <summary> - /// Re-scans shared directories. - /// </summary> - /// <returns>The operation context.</returns> - 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<ISearchService, SearchService>(); services.AddSingleton<IPeerService, PeerService>(); - services.AddSingleton<IManagementService, ManagementService>(); services.AddSingleton<IFTPClientFactory, FTPClientFactory>(); services.AddSingleton<IFTPService, FTPService>(); |
