//
// 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 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 serviceStateMonitor,
ISoulseekClient soulseekClient,
ISharedFileCache sharedFileCache)
{
OptionsMonitor = optionsMonitor;
ServiceStateMonitor = serviceStateMonitor;
Client = soulseekClient;
SharedFileCache = sharedFileCache;
}
///
/// Gets the current state of the slskd service.
///
public ApplicationState ApplicationState => ServiceStateMonitor.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 ServiceStateMonitor { 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();
}
}