diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-10-15 15:39:09 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-10-15 15:39:09 -0500 |
| commit | 88e1aac548cdc6a8bd9cad51c288e61f935ea7cf (patch) | |
| tree | e09f6eb95809b065f48e1c0b7038625fb6be0d33 /src | |
| parent | 8d0799dc956b6c5b1473e010cdf0e6566e10c72c (diff) | |
| download | slskd-88e1aac548cdc6a8bd9cad51c288e61f935ea7cf.tar.gz | |
add some methods for managing agent hosts
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Shares/IShareService.cs | 20 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 45 |
2 files changed, 56 insertions, 9 deletions
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 @@ -32,6 +32,11 @@ namespace slskd.Shares IReadOnlyList<Host> Hosts { get; } /// <summary> + /// Gets the local share host. + /// </summary> + Host LocalHost { get; } + + /// <summary> /// Gets the state monitor for the service. /// </summary> IStateMonitor<ShareState> StateMonitor { get; } @@ -56,6 +61,14 @@ namespace slskd.Shares Task DumpAsync(string filename); /// <summary> + /// Returns the share host with the specified <paramref name="name"/>. + /// </summary> + /// <param name="name">The name of the host.</param> + /// <param name="host">The host, if found.</param> + /// <returns>A value indicating whether the host was found.</returns> + bool TryGetHost(string name, out Host host); + + /// <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> @@ -63,6 +76,13 @@ namespace slskd.Shares Task<Directory> ListDirectoryAsync(string directory); /// <summary> + /// Removes the share host with the specified <paramref name="name"/>. + /// </summary> + /// <param name="name">The name of the host.</param> + /// <returns>A value indicating whether the host was removed.</returns> + bool TryRemoveHost(string name); + + /// <summary> /// Resolves the local filename of the specified <paramref name="remoteFilename"/>, if the mask is associated with a /// configured share. /// </summary> 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); @@ -94,6 +94,11 @@ namespace slskd.Shares public IReadOnlyList<Host> Hosts => HostDictionary.Values.Select(v => v.Host).Prepend(Local.Host).ToList().AsReadOnly(); /// <summary> + /// Gets the local share host. + /// </summary> + public Host LocalHost => Local.Host; + + /// <summary> /// Gets the state monitor for the service. /// </summary> public IStateMonitor<ShareState> StateMonitor { get; } @@ -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); } @@ -193,6 +197,32 @@ namespace slskd.Shares } /// <summary> + /// Returns the share host with the specified <paramref name="name"/>. + /// </summary> + /// <param name="name">The name of the host.</param> + /// <param name="host">The host, if found.</param> + /// <returns>A value indicating whether the host was found.</returns> + public bool TryGetHost(string name, out Host host) + { + if (HostDictionary.TryGetValue(name, out var record)) + { + host = record.Host; + return true; + } + + host = null; + return false; + } + + /// <summary> + /// Removes the share host with the specified <paramref name="name"/>. + /// </summary> + /// <param name="name">The name of the host.</param> + /// <returns>A value indicating whether the host was removed.</returns> + public bool TryRemoveHost(string name) + => HostDictionary.TryRemove(name, out _); + + /// <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> @@ -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 |
