diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-08-02 08:13:48 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-08-02 08:13:48 -0500 |
| commit | a2ce1945f76f4a6f54483436cae7d3539a91be38 (patch) | |
| tree | 32d33a2aa61e1b3214e395c86e8e3e76b21fdebc /src | |
| parent | ad8b3dabef05aa9c7edba5fbb22e071baa3e95e1 (diff) | |
| download | slskd-a2ce1945f76f4a6f54483436cae7d3539a91be38.tar.gz | |
rename stuff
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application/Service.cs | 31 | ||||
| -rw-r--r-- | src/slskd/Application/ServiceState.cs (renamed from src/slskd/Application/State.cs) | 6 | ||||
| -rw-r--r-- | src/slskd/Application/Shares/ISharedFileCache.cs (renamed from src/slskd/Application/Shares/ISharesService.cs) | 20 | ||||
| -rw-r--r-- | src/slskd/Application/Shares/SharedFileCache.cs (renamed from src/slskd/Application/Shares/SharesService.cs) | 35 | ||||
| -rw-r--r-- | src/slskd/Application/Shares/SharedFileCacheState.cs (renamed from src/slskd/Application/Shares/SharesState.cs) | 26 | ||||
| -rw-r--r-- | src/slskd/Startup.cs | 7 |
6 files changed, 80 insertions, 45 deletions
diff --git a/src/slskd/Application/Service.cs b/src/slskd/Application/Service.cs index 1ee8ea20..3d019dd3 100644 --- a/src/slskd/Application/Service.cs +++ b/src/slskd/Application/Service.cs @@ -19,7 +19,6 @@ namespace slskd { using System; using System.Collections.Concurrent; - using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; @@ -44,18 +43,16 @@ namespace slskd { private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes - private (int Directories, int Files) sharedCounts = (0, 0); - public Service( OptionsAtStartup optionsAtStartup, IOptionsMonitor<Options> optionsMonitor, - IStateMonitor<State> stateMonitor, - IStateMonitor<SharesState> sharesStateMonitor, + IStateMonitor<ServiceState> stateMonitor, + IStateMonitor<SharedFileCacheState> sharedFileCacheStateMonitor, ITransferTracker transferTracker, IBrowseTracker browseTracker, IConversationTracker conversationTracker, IRoomTracker roomTracker, - ISharesService sharesService, + ISharedFileCache sharesService, IPushbulletService pushbulletService) { OptionsAtStartup = optionsAtStartup; @@ -68,8 +65,8 @@ namespace slskd StateMonitor = stateMonitor; StateMonitor.OnChange(state => StateMonitor_OnChange(state)); - SharesStateMonitor = sharesStateMonitor; - SharesStateMonitor.OnChange(state => SharesStateMonitor_OnChange(state)); + SharedFileCacheStateMonitor = sharedFileCacheStateMonitor; + SharedFileCacheStateMonitor.OnChange(state => SharedFileCacheStateMonitor_OnChange(state)); TransferTracker = transferTracker; BrowseTracker = browseTracker; @@ -154,9 +151,9 @@ namespace slskd private OptionsAtStartup OptionsAtStartup { get; set; } private Options PreviousOptions { get; set; } private IRoomTracker RoomTracker { get; set; } - private IStateMonitor<State> StateMonitor { get; set; } - private IStateMonitor<SharesState> SharesStateMonitor { get; set; } - private ISharesService SharesService { get; set; } + private IStateMonitor<ServiceState> StateMonitor { get; set; } + private IStateMonitor<SharedFileCacheState> SharedFileCacheStateMonitor { get; set; } + private ISharedFileCache SharesService { get; set; } private ITransferTracker TransferTracker { get; set; } private IPushbulletService Pushbullet { get; } private ReaderWriterLockSlim OptionsSyncRoot { get; } = new ReaderWriterLockSlim(); @@ -289,13 +286,13 @@ namespace slskd // OR if the call to reconfigure the client requires a reconnect if ((Client.State.HasFlag(SoulseekClientStates.Connected) && pendingReconnect) || soulseekRequiresReconnect) { - StateMonitor.Set(state => state with { PendingReconnect = true }); + StateMonitor.SetValue(state => state with { PendingReconnect = true }); Logger.Information("One or more updated Soulseek options requires the client to be disconnected, then reconnected to the network to take effect."); } if (pendingRestart) { - StateMonitor.Set(state => state with { PendingRestart = true }); + StateMonitor.SetValue(state => state with { PendingRestart = true }); Logger.Information("One or more updated options requires an application restart to take effect."); } @@ -312,12 +309,12 @@ namespace slskd } } - private void StateMonitor_OnChange((State Previous, State Current) state) + private void StateMonitor_OnChange((ServiceState Previous, ServiceState Current) state) { Logger.Debug("State changed from {Previous} to {Current}", state.Previous.ToJson(), state.Current.ToJson()); } - private void SharesStateMonitor_OnChange((SharesState Previous, SharesState Current) state) + private void SharedFileCacheStateMonitor_OnChange((SharedFileCacheState Previous, SharedFileCacheState Current) state) { if (state.Previous.Ready && !state.Current.Ready) { @@ -400,9 +397,9 @@ namespace slskd private async void Client_Disconnected(object sender, SoulseekClientDisconnectedEventArgs args) { - if (StateMonitor.Current.PendingReconnect) + if (StateMonitor.CurrentValue.PendingReconnect) { - StateMonitor.Set(state => state with { PendingReconnect = false }); + StateMonitor.SetValue(state => state with { PendingReconnect = false }); } if (args.Exception is ObjectDisposedException || args.Exception is ApplicationShutdownException) diff --git a/src/slskd/Application/State.cs b/src/slskd/Application/ServiceState.cs index 83b801f3..67671947 100644 --- a/src/slskd/Application/State.cs +++ b/src/slskd/Application/ServiceState.cs @@ -1,4 +1,4 @@ -// <copyright file="State.cs" company="slskd Team"> +// <copyright file="ServiceState.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -18,9 +18,9 @@ namespace slskd { /// <summary> - /// Application state. + /// Application service state. /// </summary> - public record State() + public record ServiceState() { public bool PendingReconnect { get; init; } public bool PendingRestart { get; init; } diff --git a/src/slskd/Application/Shares/ISharesService.cs b/src/slskd/Application/Shares/ISharedFileCache.cs index 78fe3e12..22667a7a 100644 --- a/src/slskd/Application/Shares/ISharesService.cs +++ b/src/slskd/Application/Shares/ISharedFileCache.cs @@ -1,4 +1,4 @@ -// <copyright file="ISharesService.cs" company="slskd Team"> +// <copyright file="ISharedFileCache.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -21,12 +21,28 @@ namespace slskd.Shares using System.Threading.Tasks; using Soulseek; - public interface ISharesService + /// <summary> + /// Shared file cache. + /// </summary> + public interface ISharedFileCache { + /// <summary> + /// Returns the contents of the cache. + /// </summary> + /// <returns>The contents of the cache.</returns> IEnumerable<Directory> Browse(); + /// <summary> + /// Drops and re-creates the cache from disk. + /// </summary> + /// <returns>The operation context.</returns> Task FillAsync(); + /// <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> Task<IEnumerable<File>> SearchAsync(SearchQuery query); } }
\ No newline at end of file diff --git a/src/slskd/Application/Shares/SharesService.cs b/src/slskd/Application/Shares/SharedFileCache.cs index f5fbb88d..efc43a17 100644 --- a/src/slskd/Application/Shares/SharesService.cs +++ b/src/slskd/Application/Shares/SharedFileCache.cs @@ -1,4 +1,4 @@ -// <copyright file="SharesService.cs" company="slskd Team"> +// <copyright file="SharedFileCache.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -31,38 +31,37 @@ namespace slskd.Shares using Soulseek; /// <summary> - /// Caches shared files. + /// Shared file cache. /// </summary> - public class SharesService : ISharesService + public class SharedFileCache : ISharedFileCache { /// <summary> - /// Initializes a new instance of the <see cref="SharesService"/> class. + /// Initializes a new instance of the <see cref="SharedFileCache"/> class. /// </summary> /// <param name="stateMonitor"></param> /// <param name="optionsMonitor"></param> - public SharesService( - IStateMonitor<SharesState> stateMonitor, + public SharedFileCache( + IStateMonitor<SharedFileCacheState> stateMonitor, IOptionsMonitor<Options> optionsMonitor) { StateMonitor = stateMonitor; OptionsMonitor = optionsMonitor; - - State = new SharesState() { Ready = false, FillProgress = 0 }; } - public IEnumerable<string> Shares => OptionsMonitor.CurrentValue.Directories.Shared; - public SharesState State { get; } - private HashSet<string> Directories { get; set; } private Dictionary<string, Soulseek.File> Files { get; set; } private IOptionsMonitor<Options> OptionsMonitor { get; set; } private SqliteConnection SQLite { get; set; } - private IStateMonitor<SharesState> StateMonitor { get; set; } + private IStateMonitor<SharedFileCacheState> StateMonitor { get; set; } private ReaderWriterLockSlim SyncRoot { get; } = new ReaderWriterLockSlim(); + /// <summary> + /// Returns the contents of the cache. + /// </summary> + /// <returns>The contents of the cache.</returns> public IEnumerable<Soulseek.Directory> Browse() { - if (!State.Ready) + if (!StateMonitor.CurrentValue.Ready) { return Enumerable.Empty<Soulseek.Directory>(); } @@ -117,11 +116,11 @@ namespace slskd.Shares try { - StateMonitor.Set(state => state with { Ready = false, FillProgress = 0 }); + StateMonitor.SetValue(state => state with { Ready = false, FillProgress = 0 }); CreateTable(); - var directories = Shares + var directories = OptionsMonitor.CurrentValue.Directories.Shared .SelectMany(share => System.IO.Directory.GetDirectories(share, "*", SearchOption.AllDirectories)) .ToHashSet(); @@ -150,7 +149,7 @@ namespace slskd.Shares } current++; - StateMonitor.Set(state => state with { Ready = false, FillProgress = current / (double)total, Directories = current, Files = files.Count }); + StateMonitor.SetValue(state => state with { Ready = false, FillProgress = current / (double)total, Directories = current, Files = files.Count }); } // potentially optimize with multi-valued insert https://stackoverflow.com/questions/16055566/insert-multiple-rows-in-sqlite @@ -162,7 +161,7 @@ namespace slskd.Shares Directories = directories; Files = files; - StateMonitor.Set(state => state with { Ready = true, FillProgress = 1, Directories = directories.Count, Files = files.Count }); + StateMonitor.SetValue(state => state with { Ready = true, FillProgress = 1, Directories = directories.Count, Files = files.Count }); } finally { @@ -179,7 +178,7 @@ namespace slskd.Shares /// <returns></returns> public async Task<IEnumerable<Soulseek.File>> SearchAsync(SearchQuery query) { - if (!State.Ready) + if (!StateMonitor.CurrentValue.Ready) { return Enumerable.Empty<Soulseek.File>(); } diff --git a/src/slskd/Application/Shares/SharesState.cs b/src/slskd/Application/Shares/SharedFileCacheState.cs index bea4b308..4bda9d7b 100644 --- a/src/slskd/Application/Shares/SharesState.cs +++ b/src/slskd/Application/Shares/SharedFileCacheState.cs @@ -1,4 +1,4 @@ -// <copyright file="SharesState.cs" company="slskd Team"> +// <copyright file="SharedFileCacheState.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -17,14 +17,36 @@ namespace slskd.Shares { + using System; + /// <summary> /// Share cache state. /// </summary> - public record SharesState() + public record SharedFileCacheState() { + /// <summary> + /// Gets a value indicating whether the cache is ready to be used. + /// </summary> public bool Ready { get; init; } = true; + + /// <summary> + /// Gets the current fill progress. + /// </summary> public double FillProgress { get; init; } = 1; + + /// <summary> + /// Gets the number of cached directories. + /// </summary> public int Directories { get; init; } + + /// <summary> + /// Gets the number of cached files. + /// </summary> public int Files { get; init; } + + /// <summary> + /// Gets the UTC timestamp of the last fill. + /// </summary> + public DateTime? LastFilled { get; init; } } } diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index d30d717e..c15e9f8a 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -108,8 +108,8 @@ namespace slskd // add IStateMonitor instances to DI, to track application state in an observable way. // imilar to IOptionsMonitor, but state is managed by the application itself. // Usage is roughly the same. - services.AddSingleton<IStateMonitor<State>, StateMonitor<State>>(); - services.AddSingleton<IStateMonitor<SharesState>, StateMonitor<SharesState>>(); + services.AddSingleton<IStateMonitor<ServiceState>, StateMonitor<ServiceState>>(); + services.AddSingleton<IStateMonitor<SharedFileCacheState>, StateMonitor<SharedFileCacheState>>(); services.AddCors(options => options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); @@ -209,7 +209,8 @@ namespace slskd services.AddSingleton<IConversationTracker, ConversationTracker>(); services.AddSingleton<IRoomTracker, RoomTracker>(_ => new RoomTracker(messageLimit: 250)); - services.AddSingleton<ISharesService, SharesService>(); + services.AddSingleton<ISharedFileCache, SharedFileCache>(); + services.AddSingleton<ISearchService, SearchService>(); services.AddSingleton<IPeerService, PeerService>(); services.AddSingleton<IManagementService, ManagementService>(); |
