From 8d0799dc956b6c5b1473e010cdf0e6566e10c72c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 15 Oct 2022 15:36:19 -0500 Subject: simplify Host --- src/slskd/Shares/Types/Host.cs | 18 ++---------------- src/slskd/Shares/Types/HostState.cs | 35 ----------------------------------- src/slskd/Shares/Types/HostType.cs | 35 ----------------------------------- 3 files changed, 2 insertions(+), 86 deletions(-) delete mode 100644 src/slskd/Shares/Types/HostState.cs delete mode 100644 src/slskd/Shares/Types/HostType.cs (limited to 'src/slskd/Shares') diff --git a/src/slskd/Shares/Types/Host.cs b/src/slskd/Shares/Types/Host.cs index 3c5c9f9f..50302480 100644 --- a/src/slskd/Shares/Types/Host.cs +++ b/src/slskd/Shares/Types/Host.cs @@ -29,36 +29,22 @@ namespace slskd.Shares /// Initializes a new instance of the class. /// /// The name of the host. - /// The type of the host. /// The collection of hosted shares. - /// The initial state of the host. - public Host(string name, HostType type, IEnumerable shares = null, HostState state = HostState.Offline) + public Host(string name, IEnumerable shares = null) { Name = name; - Type = type; Shares = shares ?? Enumerable.Empty(); - State = state; } /// /// Gets the name of the host. /// - /// Corresponds to the configured of the host. + /// Corresponds to the configured of the host, or "local" for the local host. public string Name { get; } /// /// Gets the collection of hosted shares. /// public IEnumerable Shares { get; init; } - - /// - /// Gets the host state. - /// - public HostState State { get; init; } - - /// - /// Gets the host type. - /// - public HostType Type { get; } } } \ No newline at end of file diff --git a/src/slskd/Shares/Types/HostState.cs b/src/slskd/Shares/Types/HostState.cs deleted file mode 100644 index b10074d3..00000000 --- a/src/slskd/Shares/Types/HostState.cs +++ /dev/null @@ -1,35 +0,0 @@ -// -// 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.Shares -{ - /// - /// The state of a share host. - /// - public enum HostState - { - /// - /// Offline; unable to service requests. - /// - Offline = 0, - - /// - /// Online; able to service requests. - /// - Online = 1, - } -} diff --git a/src/slskd/Shares/Types/HostType.cs b/src/slskd/Shares/Types/HostType.cs deleted file mode 100644 index 6a892dff..00000000 --- a/src/slskd/Shares/Types/HostType.cs +++ /dev/null @@ -1,35 +0,0 @@ -// -// 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.Shares -{ - /// - /// The type of host. - /// - public enum HostType - { - /// - /// Local. - /// - Local = 0, - - /// - /// Remote; shares are hosted by an Agent. - /// - Remote = 1, - } -} -- cgit v1.2.3 From 88e1aac548cdc6a8bd9cad51c288e61f935ea7cf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 15 Oct 2022 15:39:09 -0500 Subject: add some methods for managing agent hosts --- src/slskd/Shares/IShareService.cs | 20 +++++++++++++++++ src/slskd/Shares/ShareService.cs | 45 +++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 9 deletions(-) (limited to 'src/slskd/Shares') diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index b55db482..f0c474a1 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -31,6 +31,11 @@ namespace slskd.Shares /// IReadOnlyList Hosts { get; } + /// + /// Gets the local share host. + /// + Host LocalHost { get; } + /// /// Gets the state monitor for the service. /// @@ -55,6 +60,14 @@ namespace slskd.Shares /// The operation context. Task DumpAsync(string filename); + /// + /// Returns the share host with the specified . + /// + /// The name of the host. + /// The host, if found. + /// A value indicating whether the host was found. + bool TryGetHost(string name, out Host host); + /// /// Returns the contents of the specified . /// @@ -62,6 +75,13 @@ namespace slskd.Shares /// The contents of the directory. Task ListDirectoryAsync(string directory); + /// + /// Removes the share host with the specified . + /// + /// The name of the host. + /// A value indicating whether the host was removed. + bool TryRemoveHost(string name); + /// /// Resolves the local filename of the specified , if the mask is associated with a /// configured share. diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index cc35477e..03b0f2e1 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -51,8 +51,8 @@ namespace slskd.Shares ShareRepositoryFactory = shareRepositoryFactory; - var host = new Host(options.InstanceName, type: HostType.Local, state: HostState.Online); - var repository = ShareRepositoryFactory.CreateFromHost(options.InstanceName); + var host = new Host("local"); + var repository = ShareRepositoryFactory.CreateFromHost(host.Name); Local = (host, repository); @@ -93,6 +93,11 @@ namespace slskd.Shares /// public IReadOnlyList Hosts => HostDictionary.Values.Select(v => v.Host).Prepend(Local.Host).ToList().AsReadOnly(); + /// + /// Gets the local share host. + /// + public Host LocalHost => Local.Host; + /// /// Gets the state monitor for the service. /// @@ -124,7 +129,6 @@ namespace slskd.Shares updateValueFactory: (_, existing) => (host, existing.Repository)); AllRepositories = HostDictionary.Values - .Where(value => value.Host.State == HostState.Online) .Select(value => value.Repository) .Prepend(Local.Repository); } @@ -192,6 +196,32 @@ namespace slskd.Shares return Task.CompletedTask; } + /// + /// Returns the share host with the specified . + /// + /// The name of the host. + /// The host, if found. + /// A value indicating whether the host was found. + public bool TryGetHost(string name, out Host host) + { + if (HostDictionary.TryGetValue(name, out var record)) + { + host = record.Host; + return true; + } + + host = null; + return false; + } + + /// + /// Removes the share host with the specified . + /// + /// The name of the host. + /// A value indicating whether the host was removed. + public bool TryRemoveHost(string name) + => HostDictionary.TryRemove(name, out _); + /// /// Returns the contents of the specified . /// @@ -264,7 +294,7 @@ namespace slskd.Shares await Scanner.ScanAsync(Local.Host.Shares, OptionsMonitor.CurrentValue.Shares); Log.Debug("Backing up shared file cache database..."); - Local.Repository.BackupTo(ShareRepositoryFactory.CreateFromHostBackup(OptionsMonitor.CurrentValue.InstanceName)); + Local.Repository.BackupTo(ShareRepositoryFactory.CreateFromHostBackup(Local.Host.Name)); Log.Debug("Shared file cache database backup complete"); } @@ -311,7 +341,7 @@ namespace slskd.Shares { Log.Information("Share cache StorageMode is 'Memory'. Attempting to load from backup..."); - var backupRepository = ShareRepositoryFactory.CreateFromHostBackup(OptionsMonitor.CurrentValue.InstanceName); + var backupRepository = ShareRepositoryFactory.CreateFromHostBackup(Local.Host.Name); if (backupRepository.TryValidate(out _)) { @@ -339,7 +369,7 @@ namespace slskd.Shares { Log.Warning("Share cache is missing, corrupt, or is out of date. Attempting to load from backup..."); - var backupRepository = ShareRepositoryFactory.CreateFromHostBackup(OptionsMonitor.CurrentValue.InstanceName); + var backupRepository = ShareRepositoryFactory.CreateFromHostBackup(Local.Host.Name); if (backupRepository.TryValidate(out _)) { @@ -413,9 +443,6 @@ namespace slskd.Shares Local = (Local.Host with { Shares = shares }, Local.Repository); - // todo: initialize agent hosts - // todo: don't destroy or overwrite existing hosts! - LastOptionsHash = optionsHash; } finally -- cgit v1.2.3 From cdf397d340a1a4c2b4c5077adce997854a0787b2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 15 Oct 2022 15:46:53 -0500 Subject: fix bug in dump --- src/slskd/Shares/SqliteShareRepository.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/slskd/Shares') diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs index 9b65794e..0a71280d 100644 --- a/src/slskd/Shares/SqliteShareRepository.cs +++ b/src/slskd/Shares/SqliteShareRepository.cs @@ -160,10 +160,10 @@ namespace slskd.Shares public void DumpTo(string filename) { using var sourceConn = GetConnection(ConnectionString); - using var backupConn = GetConnection($"Data Source={filename}"); - sourceConn.BackupDatabase(backupConn); - Sqlite. + // very important! don't use pooling for the backup connection or the file will remain locked + using var backupConn = GetConnection($"Data Source={filename};Pooling=False"); + sourceConn.BackupDatabase(backupConn); } /// -- cgit v1.2.3