From a1b481cfbb0483e54266f2dfeed80ceb070d0f63 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 30 May 2022 16:11:35 -0500 Subject: docs --- src/slskd/Shares/ShareService.cs | 144 ++++++++++++++++++++++++++++++--------- 1 file 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 Shares { get; } - - Task> BrowseAsync(); - - Task ListAsync(string directory); - - Task> SearchAsync(SearchQuery query); - - Task StartScanAsync(); - - Task ResolveAsync(string remoteFilename); - - public IStateMonitor StateMonitor { get; } - } - + /// + /// Provides control and interactions with configured shares and shared files. + /// public class ShareService : IShareService { + /// + /// Initializes a new instance of the class. + /// + /// + /// public ShareService( IOptionsMonitor 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, + }); + }); } + /// + /// Gets the list of configured shares. + /// public IReadOnlyList Shares => SharesList.AsReadOnly(); + + /// + /// Gets the state monitor for the service. + /// public IStateMonitor StateMonitor => State; - private IShareCache Cache { get; } + private ISharedFileCache Cache { get; } + private string LastOptionsHash { get; set; } private IOptionsMonitor OptionsMonitor { get; } private List SharesList { get; set; } = new List(); private IManagedState State { get; } = new ManagedState(); + private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); - public Task> BrowseAsync() + /// + /// Returns the entire contents of the share. + /// + /// The entire contents of the share. + public Task> BrowseAsync() => Task.FromResult(Cache.Browse()); - public Task ListAsync(string directory) + /// + /// Returns the contents of the specified . + /// + /// The directory for which the contents are to be listed. + /// The contents of the directory. + public Task ListAsync(string directory) => Task.FromResult(Cache.List(directory)); - public Task> SearchAsync(SearchQuery query) + /// + /// Resolves the local filename of the specified , if the mask is associated with a + /// configured share. + /// + /// The fully qualified filename to resolve. + /// The resolved local filename. + /// + /// Thrown when the specified remote filename can not be associated with a configured share. + /// + public Task 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); + } + + /// + /// Searches the cache for the specified and returns the matching files. + /// + /// The query for which to search. + /// The matching files. + public Task> SearchAsync(SearchQuery query) => Cache.SearchAsync(query); + /// + /// Starts a scan of the configured shares. + /// + /// The operation context. + /// Thrown when a scan is already in progress. public Task StartScanAsync() - => Cache.FillAsync(); + => Cache.FillAsync(Shares); - public Task 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 -- cgit v1.2.3