diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 144 |
1 files changed, 110 insertions, 34 deletions
diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 68b3c629..103a96fc 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -19,67 +19,143 @@ using Microsoft.Extensions.Options; namespace slskd.Shares { - using System; using System.Collections.Generic; using System.Linq; + using System.Threading; using System.Threading.Tasks; using Soulseek; - public interface IShareService - { - IReadOnlyList<Share> Shares { get; } - - Task<IEnumerable<Soulseek.Directory>> BrowseAsync(); - - Task<Directory> ListAsync(string directory); - - Task<IEnumerable<File>> SearchAsync(SearchQuery query); - - Task StartScanAsync(); - - Task<string> ResolveAsync(string remoteFilename); - - public IStateMonitor<ShareState> StateMonitor { get; } - } - + /// <summary> + /// Provides control and interactions with configured shares and shared files. + /// </summary> public class ShareService : IShareService { + /// <summary> + /// Initializes a new instance of the <see cref="ShareService"/> class. + /// </summary> + /// <param name="optionsMonitor"></param> + /// <param name="sharedFileCache"></param> public ShareService( IOptionsMonitor<Options> optionsMonitor, - IShareCache sharesCache = null) + ISharedFileCache sharedFileCache = null) { OptionsMonitor = optionsMonitor; - - // todo: when options change, fill SharesList with configured shares - // and set ScanPending = true - - Cache = sharesCache ?? new ShareCache(OptionsMonitor); - Cache.StateMonitor.OnChange(cacheState - => State.SetValue(state - => state with { Cache = cacheState.Current })); + OptionsMonitor.OnChange(Configure); + + Cache = sharedFileCache ?? new SharedFileCache(OptionsMonitor); + Cache.StateMonitor.OnChange(cacheState => + { + var (previous, current) = cacheState; + + State.SetValue(state => state with + { + ScanPending = current.Faulted || ((previous.Filling && !current.Filling) ? false : state.ScanPending), + Scanning = current.Filling, + ScanProgress = current.FillProgress, + Directories = current.Directories, + Files = current.Files, + }); + }); } + /// <summary> + /// Gets the list of configured shares. + /// </summary> public IReadOnlyList<Share> Shares => SharesList.AsReadOnly(); + + /// <summary> + /// Gets the state monitor for the service. + /// </summary> public IStateMonitor<ShareState> StateMonitor => State; - private IShareCache Cache { get; } + private ISharedFileCache Cache { get; } + private string LastOptionsHash { get; set; } private IOptionsMonitor<Options> OptionsMonitor { get; } private List<Share> SharesList { get; set; } = new List<Share>(); private IManagedState<ShareState> State { get; } = new ManagedState<ShareState>(); + private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); - public Task<IEnumerable<Soulseek.Directory>> BrowseAsync() + /// <summary> + /// Returns the entire contents of the share. + /// </summary> + /// <returns>The entire contents of the share.</returns> + public Task<IEnumerable<Directory>> BrowseAsync() => Task.FromResult(Cache.Browse()); - public Task<Soulseek.Directory> ListAsync(string directory) + /// <summary> + /// Returns the contents of the specified <paramref name="directory"/>. + /// </summary> + /// <param name="directory">The directory for which the contents are to be listed.</param> + /// <returns>The contents of the directory.</returns> + public Task<Directory> ListAsync(string directory) => Task.FromResult(Cache.List(directory)); - public Task<IEnumerable<Soulseek.File>> SearchAsync(SearchQuery query) + /// <summary> + /// Resolves the local filename of the specified <paramref name="remoteFilename"/>, if the mask is associated with a + /// configured share. + /// </summary> + /// <param name="remoteFilename">The fully qualified filename to resolve.</param> + /// <returns>The resolved local filename.</returns> + /// <exception cref="NotFoundException"> + /// Thrown when the specified remote filename can not be associated with a configured share. + /// </exception> + public Task<string> ResolveAsync(string remoteFilename) + { + var resolvedFilename = Cache.Resolve(remoteFilename); + + if (resolvedFilename == remoteFilename) + { + throw new NotFoundException($"The requested filename '{remoteFilename}' could not be resolved to a configured share."); + } + + return Task.FromResult(resolvedFilename); + } + + /// <summary> + /// Searches the cache for the specified <paramref name="query"/> and returns the matching files. + /// </summary> + /// <param name="query">The query for which to search.</param> + /// <returns>The matching files.</returns> + public Task<IEnumerable<File>> SearchAsync(SearchQuery query) => Cache.SearchAsync(query); + /// <summary> + /// Starts a scan of the configured shares. + /// </summary> + /// <returns>The operation context.</returns> + /// <exception cref="ShareScanInProgressException">Thrown when a scan is already in progress.</exception> public Task StartScanAsync() - => Cache.FillAsync(); + => Cache.FillAsync(Shares); - public Task<string> ResolveAsync(string remoteFilename) - => Task.FromResult(Cache.Resolve(remoteFilename)); + private void Configure(Options options) + { + SyncRoot.Wait(); + + try + { + var optionsHash = Compute.Sha1Hash(string.Join(';', options.Directories.Shared)); + + if (optionsHash == LastOptionsHash) + { + return; + } + + var shares = options.Directories.Shared + .Select(share => share.TrimEnd('/', '\\')) + .ToHashSet() // remove duplicates + .Select(share => new Share(share)) // convert to Shares + .OrderByDescending(share => share.LocalPath.Length) // process subdirectories first. this allows them to be aliased separately from their parent + .ToList(); + + SharesList = shares; + State.SetValue(state => state with { ScanPending = true }); + + LastOptionsHash = optionsHash; + } + finally + { + SyncRoot.Release(); + } + } } }
\ No newline at end of file |
