// // 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; 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 Soulseek client. public ManagementService( Microsoft.Extensions.Options.IOptionsMonitor optionsMonitor, ISoulseekClient soulseekClient) { Options = optionsMonitor.CurrentValue; Client = soulseekClient; } private ISoulseekClient Client { get; } private Options Options { get; } /// /// 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)); /// /// 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); /// /// Gets the current state of the connection to the Soulseek server. /// /// The current server state. public ServerState GetServerState() => new ServerState() { Address = Client.Address, IPEndPoint = Client.IPEndPoint, State = Client.State, Username = Client.Username, }; } }