Made changes to new cache admin access.

pull/328/head
dahall 2022-07-25 15:25:47 -06:00
parent 2f98621983
commit da5f3d4ee8
4 changed files with 123 additions and 134 deletions

View File

@ -326,7 +326,7 @@ namespace Vanara.IO
get
{
var state = State;
if (state != BackgroundCopyJobState.Error && state != BackgroundCopyJobState.TransientError)
if (state is not BackgroundCopyJobState.Error and not BackgroundCopyJobState.TransientError)
return null;
var err = RunAction(() => m_ijob.GetError());
return err is null ? null : new BackgroundCopyException(err);
@ -638,7 +638,7 @@ namespace Vanara.IO
set => RunAction(() =>
{
var st = State;
if (st != BackgroundCopyJobState.Acknowledged && st != BackgroundCopyJobState.Cancelled)
if (st is not BackgroundCopyJobState.Acknowledged and not BackgroundCopyJobState.Cancelled)
m_ijob.SetNotifyFlags(value);
});
}
@ -855,7 +855,7 @@ namespace Vanara.IO
private void HandleCOMException(COMException cex)
{
if (State == BackgroundCopyJobState.Error || State == BackgroundCopyJobState.TransientError)
if (State is BackgroundCopyJobState.Error or BackgroundCopyJobState.TransientError)
{
OnError(m_ijob.GetError());
}

View File

@ -21,6 +21,10 @@ namespace Vanara.IO
/// <summary>Gets the list of currently queued jobs for all users.</summary>
public static BackgroundCopyJobCollection Jobs { get; } = new BackgroundCopyJobCollection();
/// <summary>Gets an object that manages the pool of peers from which you can download content.</summary>
/// <value>The peer cache administration.</value>
public static PeerCacheAdministration PeerCacheAdministration { get; } = new PeerCacheAdministration(ciMgr.Item);
/// <summary>Retrieves the running version of BITS.</summary>
public static Version Version
{

View File

@ -10,128 +10,6 @@ using static Vanara.PInvoke.BITS;
namespace Vanara.IO;
/// <summary>Use <c>PeerCacheAdministration</c> to manage the pool of peers from which you can download content.</summary>
public static class PeerCacheAdministration
{
internal static readonly ComReleaser<IBitsPeerCacheAdministration> ciCacheAdmin = ComReleaserFactory.Create((IBitsPeerCacheAdministration)BackgroundCopyManager.IMgr);
/// <summary>
/// Gets or sets the configuration flags that determine if the computer serves content to peers and can download content from peers.
/// </summary>
/// <value>Flags that determine if the computer serves content to peers and can download content from peers.</value>
public static PeerCaching ConfigurationFlags
{
get => (PeerCaching)ciCacheAdmin.Item.GetConfigurationFlags();
set => ciCacheAdmin.Item.SetConfigurationFlags((BG_ENABLE_PEERCACHING)value);
}
/// <summary>Gets or sets the maximum size of the cache.</summary>
/// <value>Maximum size of the cache, as a percentage of available hard disk drive space.</value>
public static uint MaximumCacheSize
{
get => ciCacheAdmin.Item.GetMaximumCacheSize();
set => ciCacheAdmin.Item.SetMaximumCacheSize(value);
}
/// <summary>Gets or sets the age by when files are removed from the cache.</summary>
/// <value>Age. If the last time that the file was accessed is older than this age, BITS removes the file from the cache.</value>
public static TimeSpan MaximumContentAge
{
get => TimeSpan.FromSeconds(ciCacheAdmin.Item.GetMaximumContentAge());
set => ciCacheAdmin.Item.SetMaximumContentAge((uint)value.TotalSeconds);
}
/// <summary>Gets a <see cref="CachePeers"/> instance that you use to enumerate the peers that can serve content.</summary>
/// <value>A <see cref="CachePeers"/> instance that you use to enumerate the peers that can serve content.</value>
public static CachePeers Peers { get; } = new CachePeers();
/// <summary>
/// Gets a <see cref="PeerCacheRecords"/> instance that you use to enumerate the records in the cache. The enumeration is a snapshot of
/// the records in the cache.
/// </summary>
/// <value>A <see cref="PeerCacheRecords"/> instance that you use to enumerate the records in the cache.</value>
public static PeerCacheRecords Records { get; } = new PeerCacheRecords();
/// <summary>Deletes all cache records and the file from the cache for the given URL.</summary>
/// <param name="url">
/// Null-terminated string that contains the URL of the file whose cache records and file you want to delete from the cache.
/// </param>
public static void DeleteUrl(string url) => ciCacheAdmin.Item.DeleteUrl(url);
/// <summary>Provides the ability to enumerate the list of peers that BITS has discovered.</summary>
public class CachePeers : IReadOnlyCollection<CachePeer>
{
/// <summary>Gets the number of elements in the collection.</summary>
public int Count => EnumPeers().Count();
/// <summary>Removes all peers from the list of peers that can serve content.</summary>
public static void Clear() => ciCacheAdmin.Item.ClearPeers();
/// <summary>Returns an enumerator that iterates through the collection.</summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<CachePeer> GetEnumerator()
{
ciCacheAdmin.Item.DiscoverPeers();
return EnumPeers().GetEnumerator();
}
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private IEnumerable<CachePeer> EnumPeers() => IEnumFromCom<IBitsPeer>.Create(ciCacheAdmin.Item.EnumPeers()).Select(i => new CachePeer(i));
}
/// <summary>Provides the ability to enumerate the records of the cache.</summary>
public class PeerCacheRecords : IReadOnlyList<PeerCacheRecord>
{
internal PeerCacheRecords() { }
/// <summary>Gets the number of elements in the collection.</summary>
public int Count => EnumRecords().Count();
/// <summary>Gets the <see cref="PeerCacheRecord"/> at the specified index.</summary>
/// <value>The <see cref="PeerCacheRecord"/>.</value>
/// <param name="index">The index.</param>
/// <returns>A <see cref="PeerCacheRecord"/> instance of the cache record.</returns>
public PeerCacheRecord this[int index] => EnumRecords().ElementAt(index);
/// <summary>Gets a record from the cache.</summary>
/// <param name="id">Identifier of the record to get from the cache.</param>
/// <returns>A <see cref="PeerCacheRecord"/> instance of the cache record.</returns>
public PeerCacheRecord this[Guid id] => new(ciCacheAdmin.Item.GetRecord(id));
/// <summary>Removes all the records and files from the cache.</summary>
public void Clear() => ciCacheAdmin.Item.ClearRecords();
/// <summary>Determines whether this collection contains the specified record.</summary>
/// <param name="item">The record.</param>
/// <returns><see langword="true"/> if the specified item is contained in the collection; otherwise, <see langword="false"/>.</returns>
public bool Contains(PeerCacheRecord item) => EnumRecords().Contains(item);
/// <summary>Returns an enumerator that iterates through the collection.</summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<PeerCacheRecord> GetEnumerator() => EnumRecords().GetEnumerator();
/// <summary>Searches for the specified record and returns the zero-based index of the first occurrence within the colection.</summary>
/// <param name="item">The record to locate in the collection. The value can be <see langword="null"/>.</param>
/// <returns>The zero-based index of the first occurrence of <paramref name="item"/> within the entire collection, if found; otherwise, -1.</returns>
public int IndexOf(PeerCacheRecord item) => EnumRecords().SelectMany((value, index) => value == item ? new[] { index } : Enumerable.Empty<int>()).DefaultIfEmpty(-1).First();
/// <summary>Deletes a record and file from the cache.</summary>
/// <param name="item">The record to delete from the cache.</param>
public bool Remove(PeerCacheRecord item) { try { ciCacheAdmin.Item.DeleteRecord(item.Id); return true; } catch { return false; } }
/// <summary>Removes the element at the specified index of the collection.</summary>
/// <param name="index">The zero-based index of the element to remove.</param>
public void RemoveAt(int index) => Remove(EnumRecords().ElementAt(index));
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private IEnumerable<PeerCacheRecord> EnumRecords() => IEnumFromCom<IBitsPeerCacheRecord>.Create(ciCacheAdmin.Item.EnumRecords()).Select(i => new PeerCacheRecord(i));
}
}
/// <summary>Provides information about a peer in the neighborhood.</summary>
public class CachePeer
{
@ -140,6 +18,7 @@ public class CachePeer
internal CachePeer(IBitsPeer peer) => ciPeer = ComReleaserFactory.Create(peer);
private CachePeer() => throw new NotImplementedException();
/// <summary>Determines whether the peer is authenticated.</summary>
/// <returns><see langword="true"/> if the peer is authenticated, otherwise, <see langword="false"/>.</returns>
public bool IsAuthenticated => ciPeer.Item.IsAuthenticated();
@ -153,6 +32,117 @@ public class CachePeer
public string Name => ciPeer.Item.GetPeerName();
}
/// <summary>Provides the ability to enumerate the list of peers that BITS has discovered.</summary>
public class CachePeers : IReadOnlyCollection<CachePeer>
{
private readonly IBitsPeerCacheAdministration iCacheAdmin;
internal CachePeers(IBitsPeerCacheAdministration admin) => iCacheAdmin = admin;
/// <summary>Gets the number of elements in the collection.</summary>
public int Count => EnumPeers().Count();
/// <summary>Removes all peers from the list of peers that can serve content.</summary>
public void Clear() => iCacheAdmin.ClearPeers();
/// <summary>Returns an enumerator that iterates through the collection.</summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<CachePeer> GetEnumerator()
{
iCacheAdmin.DiscoverPeers();
return EnumPeers().GetEnumerator();
}
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private IEnumerable<CachePeer> EnumPeers() => IEnumFromCom<IBitsPeer>.Create(iCacheAdmin.EnumPeers()).Select(i => new CachePeer(i));
}
/// <summary>Use <c>PeerCacheAdministration</c> to manage the pool of peers from which you can download content.</summary>
public class PeerCacheAdministration
{
internal readonly ComReleaser<IBitsPeerCacheAdministration> ciCacheAdmin;
private CachePeers peers;
private PeerCacheRecords recs;
internal PeerCacheAdministration(IBackgroundCopyManager mgr) => ciCacheAdmin = ComReleaserFactory.Create((IBitsPeerCacheAdministration)mgr);
/// <summary>
/// Gets or sets the configuration flags that determine if the computer serves content to peers and can download content from peers.
/// </summary>
/// <value>Flags that determine if the computer serves content to peers and can download content from peers.</value>
public PeerCaching ConfigurationFlags
{
get => (PeerCaching)ciCacheAdmin.Item.GetConfigurationFlags();
set => ciCacheAdmin.Item.SetConfigurationFlags((BG_ENABLE_PEERCACHING)value);
}
/// <summary>Gets or sets the maximum size of the cache.</summary>
/// <value>Maximum size of the cache, as a percentage of available hard disk drive space.</value>
public uint MaximumCacheSize
{
get => ciCacheAdmin.Item.GetMaximumCacheSize();
set => ciCacheAdmin.Item.SetMaximumCacheSize(value);
}
/// <summary>Gets or sets the age by when files are removed from the cache.</summary>
/// <value>Age. If the last time that the file was accessed is older than this age, BITS removes the file from the cache.</value>
public TimeSpan MaximumContentAge
{
get => TimeSpan.FromSeconds(ciCacheAdmin.Item.GetMaximumContentAge());
set => ciCacheAdmin.Item.SetMaximumContentAge((uint)value.TotalSeconds);
}
/// <summary>Gets a <see cref="CachePeers"/> instance that you use to enumerate the peers that can serve content.</summary>
/// <value>A <see cref="CachePeers"/> instance that you use to enumerate the peers that can serve content.</value>
public CachePeers Peers => peers ??= new CachePeers(ciCacheAdmin.Item);
/// <summary>
/// Gets a <see cref="PeerCacheRecords"/> instance that you use to enumerate the records in the cache. The enumeration is a snapshot of
/// the records in the cache.
/// </summary>
/// <value>A <see cref="PeerCacheRecords"/> instance that you use to enumerate the records in the cache.</value>
public PeerCacheRecords Records => recs ??= new PeerCacheRecords(ciCacheAdmin.Item);
/// <summary>Deletes all cache records and the file from the cache for the given URL.</summary>
/// <param name="url">
/// Null-terminated string that contains the URL of the file whose cache records and file you want to delete from the cache.
/// </param>
public void DeleteUrl(string url) => ciCacheAdmin.Item.DeleteUrl(url);
}
/// <summary>Provides the ability to enumerate the records of the cache.</summary>
public class PeerCacheRecords : IReadOnlyCollection<PeerCacheRecord>
{
private readonly IBitsPeerCacheAdministration iCacheAdmin;
internal PeerCacheRecords(IBitsPeerCacheAdministration admin) => iCacheAdmin = admin;
/// <summary>Gets the number of elements in the collection.</summary>
public int Count => EnumRecords().Count();
/// <summary>Gets a record from the cache.</summary>
/// <param name="id">Identifier of the record to get from the cache.</param>
/// <returns>A <see cref="PeerCacheRecord"/> instance of the cache record.</returns>
public PeerCacheRecord this[Guid id] => new(iCacheAdmin.GetRecord(id));
/// <summary>Removes all the records and files from the cache.</summary>
public void Clear() => iCacheAdmin.ClearRecords();
/// <summary>Returns an enumerator that iterates through the collection.</summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<PeerCacheRecord> GetEnumerator() => EnumRecords().GetEnumerator();
/// <summary>Deletes a record and file from the cache.</summary>
/// <param name="item">The record to delete from the cache.</param>
public bool Remove(PeerCacheRecord item) { try { iCacheAdmin.DeleteRecord(item.Id); return true; } catch { return false; } }
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private IEnumerable<PeerCacheRecord> EnumRecords() => IEnumFromCom<IBitsPeerCacheRecord>.Create(iCacheAdmin.EnumRecords()).Select(i => new PeerCacheRecord(i));
}
/// <summary>Provides information about a file in the BITS peer cache.</summary>
public class PeerCacheRecord
{
@ -161,6 +151,7 @@ public class PeerCacheRecord
internal PeerCacheRecord(IBitsPeerCacheRecord bitsPeerCacheRecord) => ciRecord = ComReleaserFactory.Create(bitsPeerCacheRecord);
private PeerCacheRecord() => throw new NotImplementedException();
/// <summary>Gets the date and time that the file was last modified on the server.</summary>
/// <value>Date and time that the file was last modified on the server.</value>
public DateTime FileModificationTime => ciRecord.Item.GetFileModificationTime().ToDateTime();

View File

@ -32,12 +32,9 @@ namespace Vanara.IO.Tests
var raiseException = false;
AutoResetEvent autoReset;
// Local method because of local vars.
void OnDownloadCompleted(object s, BackgroundCopyJobEventArgs e)
{
DownloadCompleted(e, autoReset, allFilesToDownload.Length, allFilesTotalSize);
}
void OnDownloadCompleted(object s, BackgroundCopyJobEventArgs e) => DownloadCompleted(e, autoReset, allFilesToDownload.Length, allFilesTotalSize);
// Create a download job.
@ -86,10 +83,7 @@ namespace Vanara.IO.Tests
// Better performance when event methods are defined seperately, preferably static.
private static void OnDownloadError(object s, BackgroundCopyJobEventArgs e)
{
throw e.Job.LastError;
}
private static void OnDownloadError(object s, BackgroundCopyJobEventArgs e) => throw e.Job.LastError;
private static void DownloadCompleted(BackgroundCopyJobEventArgs e, AutoResetEvent autoResetEvent, long totalFiles, long totalBytes)