From 1a8ab1457244d15b19ddcc94958f645f5ec2abc7 Mon Sep 17 00:00:00 2001 From: vnugent Date: Thu, 22 Jun 2023 21:16:28 -0400 Subject: Save checkpoint --- .../src/ActiveServer.cs | 2 +- .../src/BrokerRegistrationRequest.cs | 116 -------- .../src/CacheClientConfiguration.cs | 106 +++---- .../src/CacheListServerRequest.cs | 82 ------ .../src/CacheNodeConfiguration.cs | 14 +- .../src/FBMDataCacheExtensions.cs | 317 +++++++-------------- .../src/ICacheAuthManager.cs | 76 +++++ .../src/ICacheDiscoveryErrorHandler.cs | 41 +++ .../src/ICacheNodeAdvertisment.cs | 50 ++++ .../src/ICachePeerAdvertisment.cs | 50 ---- .../src/INodeDiscoveryCollection.cs | 62 ++++ .../src/INodeDiscoveryEnumerator.cs | 42 +++ .../src/NodeDiscoveryCollection.cs | 117 ++++++++ 13 files changed, 545 insertions(+), 530 deletions(-) delete mode 100644 lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs delete mode 100644 lib/VNLib.Data.Caching.Extensions/src/CacheListServerRequest.cs create mode 100644 lib/VNLib.Data.Caching.Extensions/src/ICacheAuthManager.cs create mode 100644 lib/VNLib.Data.Caching.Extensions/src/ICacheDiscoveryErrorHandler.cs create mode 100644 lib/VNLib.Data.Caching.Extensions/src/ICacheNodeAdvertisment.cs delete mode 100644 lib/VNLib.Data.Caching.Extensions/src/ICachePeerAdvertisment.cs create mode 100644 lib/VNLib.Data.Caching.Extensions/src/INodeDiscoveryCollection.cs create mode 100644 lib/VNLib.Data.Caching.Extensions/src/INodeDiscoveryEnumerator.cs create mode 100644 lib/VNLib.Data.Caching.Extensions/src/NodeDiscoveryCollection.cs (limited to 'lib/VNLib.Data.Caching.Extensions/src') diff --git a/lib/VNLib.Data.Caching.Extensions/src/ActiveServer.cs b/lib/VNLib.Data.Caching.Extensions/src/ActiveServer.cs index 2d02491..3020376 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/ActiveServer.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/ActiveServer.cs @@ -27,7 +27,7 @@ using System.Text.Json.Serialization; namespace VNLib.Data.Caching.Extensions { - public class ActiveServer : ICachePeerAdvertisment + public class ActiveServer : ICacheNodeAdvertisment { [JsonPropertyName("address")] public string? HostName { get; set; } diff --git a/lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs b/lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs deleted file mode 100644 index 9ec559a..0000000 --- a/lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs +++ /dev/null @@ -1,116 +0,0 @@ -/* -* Copyright (c) 2023 Vaughn Nugent -* -* Library: VNLib -* Package: VNLib.Data.Caching.Extensions -* File: BrokerRegistrationRequest.cs -* -* BrokerRegistrationRequest.cs is part of VNLib.Data.Caching.Extensions which is part of the larger -* VNLib collection of libraries and utilities. -* -* VNLib.Data.Caching.Extensions 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. -* -* VNLib.Data.Caching.Extensions 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/. -*/ - -using System; -using System.Collections.Generic; - -using VNLib.Utils; -using VNLib.Hashing.IdentityUtility; - - -namespace VNLib.Data.Caching.Extensions -{ - /// - /// A broker registration request message in a fluent api - /// format. This message may be disposed when no longer in use - /// - public sealed class BrokerRegistrationRequest : VnDisposeable - { - private bool ownsKey; - private ReadOnlyJsonWebKey? SigningKey; - - /// - /// The cache server node id - /// - public string? NodeId { get; private set; } - /// - /// The broker server's address - /// - public Uri? BrokerAddress { get; private set; } - /// - /// The security token used by the broker server to - /// authenticate during heartbeat connections - /// - public string? HeartbeatToken { get; private set; } - /// - /// The address for remote clients to use to - /// connect to this server - /// - public string? RegistrationAddress { get; private set; } - - /// - /// Recovers the private key from the supplied certificate - /// - /// The private key used to sign messages - /// A value that indicates if the current instance owns the key - /// - /// - public BrokerRegistrationRequest WithSigningKey(ReadOnlyJsonWebKey jwk, bool ownsKey) - { - this.ownsKey = ownsKey; - SigningKey = jwk ?? throw new ArgumentNullException(nameof(jwk)); - return this; - } - - public BrokerRegistrationRequest WithBroker(Uri brokerUri) - { - BrokerAddress = brokerUri; - return this; - } - - public BrokerRegistrationRequest WithRegistrationAddress(string address) - { - RegistrationAddress = address; - return this; - } - - public BrokerRegistrationRequest WithHeartbeatToken(string token) - { - HeartbeatToken = token; - return this; - } - - public BrokerRegistrationRequest WithNodeId(string nodeId) - { - NodeId = nodeId; - return this; - } - - internal void SignJwt(JsonWebToken jwt) - { - jwt.SignFromJwk(SigningKey); - } - - internal IReadOnlyDictionary JsonHeader => SigningKey!.JwtHeader; - - /// - protected override void Free() - { - if (ownsKey) - { - SigningKey?.Dispose(); - } - } - } -} diff --git a/lib/VNLib.Data.Caching.Extensions/src/CacheClientConfiguration.cs b/lib/VNLib.Data.Caching.Extensions/src/CacheClientConfiguration.cs index 05e4928..9229c89 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/CacheClientConfiguration.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/CacheClientConfiguration.cs @@ -3,9 +3,9 @@ * * Library: VNLib * Package: VNLib.Data.Caching.Extensions -* File: ClientCacheConfiguration.cs +* File: CacheClientConfiguration.cs * -* ClientCacheConfiguration.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* CacheClientConfiguration.cs is part of VNLib.Data.Caching.Extensions which is part of the larger * VNLib collection of libraries and utilities. * * VNLib.Data.Caching.Extensions is free software: you can redistribute it and/or modify @@ -22,69 +22,47 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ -using System; +using System.Linq; using System.Collections.Generic; -using System.Security.Cryptography; - -using VNLib.Hashing; -using VNLib.Hashing.IdentityUtility; namespace VNLib.Data.Caching.Extensions { - public interface ICacheJwtManager - { - IReadOnlyDictionary GetJwtHeader(); - - void SignJwt(JsonWebToken jwt); - - bool VerifyCache(JsonWebToken jwt); - - bool VerifyBroker(JsonWebToken jwt); - } - /// /// A fluent api configuration object for configuring a /// to connect to cache servers. /// - public class CacheClientConfiguration : ICacheJwtManager, ICacheListServerRequest + public class CacheClientConfiguration { - public ReadOnlyJsonWebKey? SigningKey { get; private set; } - public ReadOnlyJsonWebKey? VerificationKey { get; private set; } - public ReadOnlyJsonWebKey? BrokerVerificationKey { get; private set; } - - public Uri? DiscoveryEndpoint { get; private set; } - public bool UseTls { get; private set; } - internal ICachePeerAdvertisment[]? CacheServers { get; set; } + /// + /// Stores available cache servers to be used for discovery, and connections + /// + public INodeDiscoveryCollection NodeCollection { get; } = new NodeDiscoveryCollection(); /// - /// Imports the private key used to sign messages + /// The authentication manager to use for signing and verifying messages to and from the cache servers /// - /// The with a private key loaded - /// Chainable fluent object - /// - /// - public CacheClientConfiguration WithSigningKey(ReadOnlyJsonWebKey jwk) - { - SigningKey = jwk ?? throw new ArgumentNullException(nameof(jwk)); - return this; - } + public ICacheAuthManager AuthManager { get; private set; } /// - /// Imports the public key used to verify messages from the remote server + /// The error handler to use for handling errors that occur during the discovery process /// - /// The public key only used for message verification - /// Chainable fluent object - /// - /// - public CacheClientConfiguration WithVerificationKey(ReadOnlyJsonWebKey jwk) - { - VerificationKey = jwk ?? throw new ArgumentNullException(nameof(jwk)); - return this; - } + public ICacheDiscoveryErrorHandler? ErrorHandler { get; private set; } - public CacheClientConfiguration WithBrokerVerificationKey(ReadOnlyJsonWebKey jwk) + /// + /// Specifies if all connections should use TLS + /// + public bool UseTls { get; private set; } + + internal ICacheNodeAdvertisment[]? InitialPeers { get; set; } + + /// + /// Specifies the JWT authentication manager to use for signing and verifying JWTs + /// + /// The authentication manager + /// Chainable fluent object + public CacheClientConfiguration WithAuthenticator(ICacheAuthManager manager) { - BrokerVerificationKey = jwk ?? throw new ArgumentNullException(nameof(jwk)); + AuthManager = manager; return this; } @@ -92,7 +70,6 @@ namespace VNLib.Data.Caching.Extensions /// Specifies if all connections should be using TLS /// /// A value that indicates if connections should use TLS - /// Chainable fluent object public CacheClientConfiguration WithTls(bool useTls) { UseTls = useTls; @@ -100,28 +77,25 @@ namespace VNLib.Data.Caching.Extensions } /// - /// Specifies the broker address to discover cache nodes from + /// Specifies the initial cache peers to connect to /// - /// The address of the server broker + /// The collection of servers to discover peers from and connect to /// Chainable fluent object - /// - public CacheClientConfiguration WithBroker(Uri brokerAddress) + public CacheClientConfiguration WithInitialPeers(IEnumerable peers) { - DiscoveryEndpoint = brokerAddress ?? throw new ArgumentNullException(nameof(brokerAddress)); + InitialPeers = peers.ToArray(); return this; } - - /// - public void SignJwt(JsonWebToken jwt) => jwt.SignFromJwk(SigningKey!); - - /// - public bool VerifyCache(JsonWebToken jwt) => jwt.VerifyFromJwk(VerificationKey!); - - /// - public bool VerifyBroker(JsonWebToken jwt) => jwt.VerifyFromJwk(BrokerVerificationKey!); - - /// - public IReadOnlyDictionary GetJwtHeader() => SigningKey!.JwtHeader; + /// + /// Specifies the error handler to use for handling errors that occur during the discovery process + /// + /// The error handler to use during a discovery + /// Chainable fluent object + public CacheClientConfiguration WithErrorHandler(ICacheDiscoveryErrorHandler handler) + { + ErrorHandler = handler; + return this; + } } } diff --git a/lib/VNLib.Data.Caching.Extensions/src/CacheListServerRequest.cs b/lib/VNLib.Data.Caching.Extensions/src/CacheListServerRequest.cs deleted file mode 100644 index 76d4ad8..0000000 --- a/lib/VNLib.Data.Caching.Extensions/src/CacheListServerRequest.cs +++ /dev/null @@ -1,82 +0,0 @@ -/* -* Copyright (c) 2023 Vaughn Nugent -* -* Library: VNLib -* Package: VNLib.Data.Caching.Extensions -* File: ListServerRequest.cs -* -* ListServerRequest.cs is part of VNLib.Data.Caching.Extensions which is part of the larger -* VNLib collection of libraries and utilities. -* -* VNLib.Data.Caching.Extensions 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. -* -* VNLib.Data.Caching.Extensions 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/. -*/ - -using System; -using System.Collections.Generic; - -using VNLib.Utils; -using VNLib.Hashing.IdentityUtility; - -namespace VNLib.Data.Caching.Extensions -{ - public interface ICacheListServerRequest : ICacheJwtManager - { - Uri DiscoveryEndpoint { get; } - } - - /// - /// A request container for a ListServer request - /// - public sealed class CacheListServerRequest : ICacheListServerRequest - { - private readonly ICacheJwtManager _manager; - - - /// - /// The address of the broker server to connect to - /// - public Uri DiscoveryEndpoint { get; private set; } - - public CacheListServerRequest(ICacheJwtManager keyManager, Uri? brokerAddress = null) - { - _manager = keyManager; - DiscoveryEndpoint = brokerAddress!; - } - - - /// - /// Sets the broker address for the request - /// - /// The broker server's address to connect to - /// A fluent chainable value - /// - public CacheListServerRequest WithDiscoveryEndpoint(Uri brokerAddr) - { - DiscoveryEndpoint = brokerAddr ?? throw new ArgumentNullException(nameof(brokerAddr)); - return this; - } - - /// - public void SignJwt(JsonWebToken jwt) => _manager.SignJwt(jwt); - - /// - public bool VerifyCache(JsonWebToken jwt) => _manager.VerifyCache(jwt); - - /// - public bool VerifyBroker(JsonWebToken jwt) => _manager.VerifyBroker(jwt); - - /// - public IReadOnlyDictionary GetJwtHeader() => _manager.GetJwtHeader(); - } -} diff --git a/lib/VNLib.Data.Caching.Extensions/src/CacheNodeConfiguration.cs b/lib/VNLib.Data.Caching.Extensions/src/CacheNodeConfiguration.cs index 21a99e1..29a763c 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/CacheNodeConfiguration.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/CacheNodeConfiguration.cs @@ -24,11 +24,13 @@ using System; - namespace VNLib.Data.Caching.Extensions { - public class CacheNodeConfiguration: CacheClientConfiguration, ICachePeerAdvertisment + /// + /// A cache configuration for cache servers (nodes) + /// + public class CacheNodeConfiguration: CacheClientConfiguration, ICacheNodeAdvertisment { /// /// The address for clients to connect to @@ -56,9 +58,13 @@ namespace VNLib.Data.Caching.Extensions return this; } - public CacheNodeConfiguration EnableAdvertisment(bool enable, Uri? discoveryEndpoint) + /// + /// Enables or disables the advertisement of this node to other nodes + /// + /// The absolute endpoint clients will use to connect to + public CacheNodeConfiguration EnableAdvertisment(Uri? discoveryEndpoint) { - BroadcastAdverisment = enable; + BroadcastAdverisment = discoveryEndpoint != null; DiscoveryEndpoint = discoveryEndpoint; return this; } diff --git a/lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs b/lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs index 9efe16a..634b6de 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs @@ -139,91 +139,114 @@ namespace VNLib.Data.Caching.Extensions client.Config.DebugLog?.Debug("{debug}: {data}", "[CACHE]", message); } + /// - /// Creats a new from an existing + /// Discovers ALL possible cache nodes itteritivley from the current collection of initial peers. + /// This will make connections to all discoverable servers /// - /// The prepared client configuration - /// The new - public static CacheListServerRequest GetListMessage(this CacheClientConfiguration conf) + /// + /// A token to cancel the operation + /// + /// + public static async Task DiscoverNodesAsync(this CacheClientConfiguration config, CancellationToken cancellation) { - return new(conf, conf.DiscoveryEndpoint); + //Make sure at least one node defined + if(config?.InitialPeers == null || config.InitialPeers.Length == 0) + { + throw new ArgumentException("There must be at least one cache server defined in the client configuration"); + } + + //Get the discovery enumerator with the initial peers + INodeDiscoveryEnumerator enumerator = config.NodeCollection.BeginDiscovery(config.InitialPeers); + + //Start the discovery process + await DiscoverNodesAsync(enumerator, config.AuthManager, config.ErrorHandler, cancellation); + + //Commit nodes + config.NodeCollection.CompleteDiscovery(enumerator); } - /// - /// Discovers peer nodes from a given initial peer and returns a list of discovered nodes. If the config - /// is for a cache peer node, the current peer is removed from the list of discovered nodes. - /// - /// - /// The initial peer to discover nodes from - /// A token to cancel the discovery operation - /// The collection of discovered nodes - /// - public static async Task DiscoverClusterNodesAsync( - this CacheClientConfiguration cacheConfig, - ICachePeerAdvertisment initialPeer, - CancellationToken cancellation + private static async Task DiscoverNodesAsync( + INodeDiscoveryEnumerator enumerator, + ICacheAuthManager auth, + ICacheDiscoveryErrorHandler? errHandler, + CancellationToken cancellation ) { - _ = initialPeer?.DiscoveryEndpoint ?? throw new ArgumentException("Advertisment does not expose an advertisment endpoint"); - - //Create list request - CacheListServerRequest request = cacheConfig.GetListMessage(); + //Loop through servers + while (enumerator.MoveNext()) + { + //Make sure the node has a discovery endpoint + if (enumerator.Current.DiscoveryEndpoint == null) + { + //Skip this node + continue; + } - //Override with the initial peer's discovery endpoint - request.WithDiscoveryEndpoint(initialPeer.DiscoveryEndpoint); + /* + * We are allowed to save nodes that do not have a discovery endpoint, but we cannot discover nodes from them + * we can only use them as cache + */ - //Get the list of servers - ICachePeerAdvertisment[]? servers = await ListServersAsync(request, cancellation); + //add a random delay to avoid spamming the server + await Task.Delay((int)Random.Shared.NextInt64(50, 500), cancellation); - if (servers == null) - { - return null; - } - - if(cacheConfig is CacheNodeConfiguration cnc) - { - //Filter out the current node - return servers.Where(s => !cnc.NodeId.Equals(s.NodeId, StringComparison.OrdinalIgnoreCase)).ToArray(); + try + { + //Discover nodes from the current node + ICacheNodeAdvertisment[]? nodes = await GetCacheNodesAsync(enumerator.Current, auth, cancellation); + + if (nodes != null) + { + //Add nodes to the collection + enumerator.OnPeerDiscoveryComplete(nodes); + } + } + //Catch exceptions when an error handler is defined + catch(Exception ex) when (errHandler != null) + { + //Handle the error + errHandler.OnDiscoveryError(enumerator.Current, ex); + } } - else - { - //Do not filter - return servers; - } } /// /// Contacts the cache broker to get a list of active servers to connect to /// - /// The request message used to connecto the broker server + /// An advertisment of a server to discover other nodes from /// A token to cancel the operationS + /// The authentication manager /// The list of active servers /// + /// /// - public static async Task ListServersAsync(ICacheListServerRequest request, CancellationToken cancellationToken = default) + public static async Task GetCacheNodesAsync(ICacheNodeAdvertisment advert, ICacheAuthManager auth, CancellationToken cancellationToken = default) { - _ = request ?? throw new ArgumentNullException(nameof(request)); + _ = advert ?? throw new ArgumentNullException(nameof(advert)); + _ = auth ?? throw new ArgumentNullException(nameof(auth)); + _ = advert.DiscoveryEndpoint ?? throw new ArgumentException("Advertisment does not expose an advertisment endpoint"); string jwtBody; //Build request jwt using (JsonWebToken requestJwt = new()) { - requestJwt.WriteHeader(request.GetJwtHeader()); + requestJwt.WriteHeader(auth.GetJwtHeader()); requestJwt.InitPayloadClaim() .AddClaim("iat", DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) .AddClaim("nonce", RandomHash.GetRandomBase32(16)) .CommitClaims(); //sign the jwt - request.SignJwt(requestJwt); + auth.SignJwt(requestJwt); //Compile the jwt jwtBody = requestJwt.Compile(); } //New list request - RestRequest listRequest = new(request.DiscoveryEndpoint, Method.Post); + RestRequest listRequest = new(advert.DiscoveryEndpoint, Method.Post); //Add the jwt as a string to the request body listRequest.AddStringBody(jwtBody, DataFormat.None); @@ -248,86 +271,17 @@ namespace VNLib.Data.Caching.Extensions //Response is jwt using JsonWebToken responseJwt = JsonWebToken.ParseRaw(data); - + //Verify the jwt - if (!request.VerifyBroker(responseJwt)) + if (!auth.VerifyJwt(responseJwt)) { throw new SecurityException("Failed to verify the broker's challenge, cannot continue"); } - + using JsonDocument doc = responseJwt.GetPayload(); return doc.RootElement.GetProperty("peers").Deserialize(); } - /// - /// Registers the current node with the broker - /// - /// A task that completes when the regitration has been made successfully - /// - public static async Task RegisterWithBrokerAsync(this CacheNodeConfiguration config, string authToken) - { - //Recover the certificate - ReadOnlyJsonWebKey cacheCert = config?.SigningKey ?? throw new ArgumentException(nameof(config.SigningKey)); - - //init broker request - using BrokerRegistrationRequest request = new(); - - request.WithBroker(config.DiscoveryEndpoint!) - .WithRegistrationAddress(config.ConnectEndpoint!.ToString()) - .WithNodeId(config.NodeId!) - .WithSigningKey(cacheCert, true) - .WithHeartbeatToken(authToken); - - - //Send the request - await RegisterWithBrokerAsync(request); - } - - /// - /// Registers the current server as active with the specified broker - /// - /// The registration request - public static async Task RegisterWithBrokerAsync(BrokerRegistrationRequest registration) - { - _ = registration ?? throw new ArgumentNullException(nameof(registration)); - _ = registration.HeartbeatToken ?? throw new ArgumentException("Missing required heartbeat access token"); - _ = registration.NodeId ?? throw new ArgumentException("Missing required cache server NodeId"); - _ = registration.BrokerAddress ?? throw new ArgumentException("Broker server address has not been configured"); - _ = registration.RegistrationAddress ?? throw new ArgumentException("Missing required registration address", nameof(registration)); - - string requestData; - //Create the jwt for signed registration message - using (JsonWebToken jwt = new()) - { - //Shared jwt header - jwt.WriteHeader(registration.JsonHeader); - //build jwt claim - jwt.InitPayloadClaim() - .AddClaim("address", registration.RegistrationAddress) - .AddClaim("sub", registration.NodeId) - .AddClaim("token", registration.HeartbeatToken) - .CommitClaims(); - - //Sign the jwt - registration.SignJwt(jwt); - //Compile and save - requestData = jwt.Compile(); - } - - //Create reg request message - RestRequest regRequest = new(registration.BrokerAddress); - regRequest.AddStringBody(requestData, DataFormat.None); - regRequest.AddHeader("Content-Type", "text/plain"); - - //Rent client - using ClientContract cc = ClientPool.Lease(); - - //Exec the regitration request - RestResponse response = await cc.Resource.ExecutePutAsync(regRequest); - response.ThrowIfError(); - } - - /// /// Allows for configuration of an /// for a connection to a cache server @@ -359,32 +313,7 @@ namespace VNLib.Data.Caching.Extensions ClientCacheConfig.AddOrUpdate(client, nodeConfig); return nodeConfig; } - - /// - /// Discovers cache nodes in the broker configured for the current client. - /// - /// - /// A token to cancel the discovery - /// A task the resolves the list of active servers on the broker server - public static Task DiscoverCacheNodesAsync(this FBMClientWorkerBase client, CancellationToken token = default) - { - return client.Client.DiscoverCacheNodesAsync(token); - } - - /// - /// Discovers cache nodes in the broker configured for the current client. - /// - /// - /// A token to cancel the discovery - /// A task the resolves the list of active servers on the broker server - public static async Task DiscoverCacheNodesAsync(this FBMClient client, CancellationToken token = default) - { - //Get the stored client config - CacheClientConfiguration conf = ClientCacheConfig.GetOrCreateValue(client); - - //List servers async - return conf.CacheServers = await ListServersAsync(conf, token); - } + /// /// Waits for the client to disconnect from the server while observing @@ -428,15 +357,18 @@ namespace VNLib.Data.Caching.Extensions /// /// /// - public static async Task ConnectToRandomCacheAsync(this FBMClient client, CancellationToken cancellation = default) + public static async Task ConnectToRandomCacheAsync(this FBMClient client, CancellationToken cancellation = default) { //Get stored config CacheClientConfiguration conf = ClientCacheConfig.GetOrCreateValue(client); - //Select random - ICachePeerAdvertisment? randomServer = conf.CacheServers?.SelectRandom() - ?? throw new ArgumentException("No servers detected, cannot connect"); + //Get all available nodes, or at least the initial peers + ICacheNodeAdvertisment[]? adverts = conf.NodeCollection.GetAllNodes() ?? conf.InitialPeers ?? throw new ArgumentException("No cache nodes discovered, cannot connect"); + //Select random node from all available nodes + ICacheNodeAdvertisment randomServer = adverts.SelectRandom(); + + //Connect to the random server await ConnectToCacheAsync(client, randomServer, cancellation); //Return the random server we connected to @@ -456,13 +388,14 @@ namespace VNLib.Data.Caching.Extensions /// /// /// - public static Task ConnectToCacheAsync(this FBMClient client, ICachePeerAdvertisment server, CancellationToken token = default) + public static Task ConnectToCacheAsync(this FBMClient client, ICacheNodeAdvertisment server, CancellationToken token = default) { _ = client ?? throw new ArgumentNullException(nameof(client)); _ = server ?? throw new ArgumentNullException(nameof(server)); //Get stored config CacheClientConfiguration conf = ClientCacheConfig.GetOrCreateValue(client); + //Connect to server (no server id because client not replication server) return ConnectToCacheAsync(client, conf, server, token); } @@ -481,7 +414,7 @@ namespace VNLib.Data.Caching.Extensions /// /// /// - public static Task ConnectToCacheAsync(this FBMClient client, ICachePeerAdvertisment server, CacheClientConfiguration explicitConfig, CancellationToken token = default) + public static Task ConnectToCacheAsync(this FBMClient client, ICacheNodeAdvertisment server, CacheClientConfiguration explicitConfig, CancellationToken token = default) { _ = client ?? throw new ArgumentNullException(nameof(client)); _ = server ?? throw new ArgumentNullException(nameof(server)); @@ -494,7 +427,7 @@ namespace VNLib.Data.Caching.Extensions private static async Task ConnectToCacheAsync( FBMClient client, CacheClientConfiguration config, - ICachePeerAdvertisment server, + ICacheNodeAdvertisment server, CancellationToken token = default ) { @@ -513,7 +446,7 @@ namespace VNLib.Data.Caching.Extensions //Init jwt for connecting to server using (JsonWebToken jwt = new()) { - jwt.WriteHeader(config.GetJwtHeader()); + jwt.WriteHeader(config.AuthManager.GetJwtHeader()); //Init claim JwtPayload claim = jwt.InitPayloadClaim(); @@ -532,7 +465,7 @@ namespace VNLib.Data.Caching.Extensions claim.CommitClaims(); //Sign jwt - config.SignJwt(jwt); + config.AuthManager.SignJwt(jwt); //Compile to string jwtMessage = jwt.Compile(); @@ -576,7 +509,7 @@ namespace VNLib.Data.Caching.Extensions using (JsonWebToken jwt = JsonWebToken.Parse(authToken)) { //Verify the jwt - if (!config.VerifyCache(jwt)) + if (!config.AuthManager.VerifyJwt(jwt)) { throw new SecurityException("Failed to verify the cache server's negotiation message, cannot continue"); } @@ -591,7 +524,7 @@ namespace VNLib.Data.Caching.Extensions client.ClientSocket.Headers[HttpRequestHeader.Authorization] = authToken; //Compute the signature of the upgrade token - client.ClientSocket.Headers[X_UPGRADE_SIG_HEADER] = GetBase64UpgradeSingature(authToken, config.SigningKey!); + client.ClientSocket.Headers[X_UPGRADE_SIG_HEADER] = config.AuthManager.GetBase64UpgradeSingature(authToken); //Check to see if adversize self is enabled if (cnc?.BroadcastAdverisment == true) @@ -660,38 +593,16 @@ namespace VNLib.Data.Caching.Extensions * compute a signature of the upgrade token and send it to the server to prove we hold the private key. */ - private static string GetBase64UpgradeSingature(string? token, ReadOnlyJsonWebKey key) + private static string GetBase64UpgradeSingature(this ICacheAuthManager man, string? token) { - //try to get the ecdsa key first - using ECDsa? ec = key.GetECDsaPrivateKey(); - - if(ec != null) - { - //Compute hash of the token - byte[] hash = ManagedHash.ComputeHash(token, HashAlg.SHA256); - - //Sign the hash - byte[] sig = ec.SignHash(hash, DSASignatureFormat.IeeeP1363FixedFieldConcatenation); - - //Return the base64 string - return Convert.ToBase64String(sig); - } - - //Check rsa next - using RSA? rsa = key.GetRSAPrivateKey(); - if(rsa != null) - { - //Compute hash of the token - byte[] hash = ManagedHash.ComputeHash(token, HashAlg.SHA256); + //Compute hash of the token + byte[] hash = ManagedHash.ComputeHash(token, HashAlg.SHA256); - //Sign the hash - byte[] sig = rsa.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); + //Sign the hash + byte[] sig = man.SignMessageHash(hash, HashAlg.SHA256); - //Return the base64 string - return Convert.ToBase64String(sig); - } - - throw new CryptographicException("Cache JKW does not export a supported private key for upgrade challenges"); + //Return the base64 string + return Convert.ToBase64String(sig); } /// @@ -704,21 +615,21 @@ namespace VNLib.Data.Caching.Extensions /// public static bool VerifyUpgradeToken(this CacheClientConfiguration nodeConfig, string signature, string token) { - return VerifyUpgradeToken(signature, token, nodeConfig.VerificationKey); + return VerifyUpgradeToken(nodeConfig.AuthManager, signature, token); } /// /// Verifies the signed auth token against the given verification key /// + /// /// The base64 signature of the token /// The raw token to compute the hash of - /// The key used to verify the singature with /// True if the singature matches, false otherwise /// /// - public static bool VerifyUpgradeToken(string signature, string token, ReadOnlyJsonWebKey verifcationKey) + public static bool VerifyUpgradeToken(this ICacheAuthManager man, string signature, string token) { - _ = verifcationKey ?? throw new ArgumentNullException(nameof(verifcationKey)); + _ = man ?? throw new ArgumentNullException(nameof(man)); //get the hash of the token byte[] hash = ManagedHash.ComputeHash(token, HashAlg.SHA256); @@ -726,23 +637,7 @@ namespace VNLib.Data.Caching.Extensions //decode the signature byte[] sig = Convert.FromBase64String(signature); - //try to get the ecdsa key first - using ECDsa? ec = verifcationKey.GetECDsaPublicKey(); - if(ec != null) - { - //Verify the signature - return ec.VerifyHash(hash, sig, DSASignatureFormat.IeeeP1363FixedFieldConcatenation); - } - - //Check rsa next - using RSA? rsa = verifcationKey.GetRSAPublicKey(); - if(rsa != null) - { - //Verify the signature - return rsa.VerifyHash(hash, sig, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); - } - - throw new CryptographicException("Cache JKW does not export a supported public key for upgrade challenges"); + return man.VerifyMessageHash(hash, HashAlg.SHA256, sig); } private static string GetAdvertismentHeader(CacheNodeConfiguration nodeConfiguration) @@ -756,7 +651,7 @@ namespace VNLib.Data.Caching.Extensions using JsonWebToken jwt = new(); //Get the jwt header - jwt.WriteHeader(nodeConfiguration.GetJwtHeader()); + jwt.WriteHeader(nodeConfiguration.AuthManager.GetJwtHeader()); jwt.InitPayloadClaim() .AddClaim("nonce", RandomHash.GetRandomBase32(16)) @@ -768,7 +663,7 @@ namespace VNLib.Data.Caching.Extensions .CommitClaims(); //Sign message - nodeConfiguration.SignJwt(jwt); + nodeConfiguration.AuthManager.SignJwt(jwt); return jwt.Compile(); } @@ -780,12 +675,12 @@ namespace VNLib.Data.Caching.Extensions /// The advertisment message to verify /// The advertisment message if successfully verified, or null otherwise /// - public static ICachePeerAdvertisment? VerifyPeerAdvertisment(this ICacheJwtManager config, string message) + public static ICacheNodeAdvertisment? VerifyPeerAdvertisment(this ICacheAuthManager config, string message) { using JsonWebToken jwt = JsonWebToken.Parse(message); //Verify the signature - if (!config.VerifyCache(jwt)) + if (!config.VerifyJwt(jwt)) { return null; } @@ -800,7 +695,7 @@ namespace VNLib.Data.Caching.Extensions /// /// /// A server selected at random - public static ICachePeerAdvertisment SelectRandom(this ICollection servers) + public static ICacheNodeAdvertisment SelectRandom(this ICollection servers) { //select random server int randServer = RandomNumberGenerator.GetInt32(0, servers.Count); @@ -808,7 +703,7 @@ namespace VNLib.Data.Caching.Extensions } - private class Advertisment : ICachePeerAdvertisment + private class Advertisment : ICacheNodeAdvertisment { [JsonIgnore] public Uri? ConnectEndpoint { get; set; } diff --git a/lib/VNLib.Data.Caching.Extensions/src/ICacheAuthManager.cs b/lib/VNLib.Data.Caching.Extensions/src/ICacheAuthManager.cs new file mode 100644 index 0000000..e3ab868 --- /dev/null +++ b/lib/VNLib.Data.Caching.Extensions/src/ICacheAuthManager.cs @@ -0,0 +1,76 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Data.Caching.Extensions +* File: ClientCacheConfiguration.cs +* +* ClientCacheConfiguration.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Data.Caching.Extensions 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. +* +* VNLib.Data.Caching.Extensions 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/. +*/ + +using System; +using System.Collections.Generic; + +using VNLib.Hashing; +using VNLib.Hashing.IdentityUtility; + +namespace VNLib.Data.Caching.Extensions +{ + /// + /// Provides authentication services for cache clients and + /// servers. + /// + public interface ICacheAuthManager + { + /// + /// Gets the JWT header to use for signing messages with the + /// given key + /// + /// The JWT header collection + IReadOnlyDictionary GetJwtHeader(); + + /// + /// Signs the given JWT + /// + /// The message to sign + void SignJwt(JsonWebToken jwt); + + /// + /// Verifies the given JWT + /// + /// The message to verify authenticity + /// True of the JWT could be verified, false otherwise + bool VerifyJwt(JsonWebToken jwt); + + /// + /// Signs the given message hash + /// + /// The message hash to sign + /// The algorithm used to sign the message hash + /// The signature of the hash + byte[] SignMessageHash(byte[] hash, HashAlg alg); + + /// + /// Verifies the given message hash against the signature. + /// + /// The message hash to compare + /// The algorithm used to produce the message hash + /// The message signature to verify the message against + /// True of the signature could be verified + bool VerifyMessageHash(ReadOnlySpan hash, HashAlg alg, ReadOnlySpan signature); + } +} diff --git a/lib/VNLib.Data.Caching.Extensions/src/ICacheDiscoveryErrorHandler.cs b/lib/VNLib.Data.Caching.Extensions/src/ICacheDiscoveryErrorHandler.cs new file mode 100644 index 0000000..3493d48 --- /dev/null +++ b/lib/VNLib.Data.Caching.Extensions/src/ICacheDiscoveryErrorHandler.cs @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Data.Caching.Extensions +* File: ClientCacheConfiguration.cs +* +* ClientCacheConfiguration.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Data.Caching.Extensions 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. +* +* VNLib.Data.Caching.Extensions 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/. +*/ + +using System; + +namespace VNLib.Data.Caching.Extensions +{ + /// + /// Represents an type that will handle errors that occur during the discovery process + /// + public interface ICacheDiscoveryErrorHandler + { + /// + /// Invoked when an error occurs during the discovery process + /// + /// The node that the error occured on + /// The exception that caused the invocation + void OnDiscoveryError(ICacheNodeAdvertisment errorNode, Exception ex); + } +} diff --git a/lib/VNLib.Data.Caching.Extensions/src/ICacheNodeAdvertisment.cs b/lib/VNLib.Data.Caching.Extensions/src/ICacheNodeAdvertisment.cs new file mode 100644 index 0000000..fc29955 --- /dev/null +++ b/lib/VNLib.Data.Caching.Extensions/src/ICacheNodeAdvertisment.cs @@ -0,0 +1,50 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Data.Caching.Extensions +* File: ICacheNodeAdvertisment.cs +* +* ICacheNodeAdvertisment.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Data.Caching.Extensions 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. +* +* VNLib.Data.Caching.Extensions 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/. +*/ + +using System; + + +namespace VNLib.Data.Caching.Extensions +{ + /// + /// Represents a node that can be advertised to clients + /// + public interface ICacheNodeAdvertisment + { + /// + /// The endpoint for clients to connect to to access the cache + /// + Uri ConnectEndpoint { get; } + + /// + /// Gets the address for clients to connect to to discover other discovertable nodes + /// + Uri? DiscoveryEndpoint { get; } + + /// + /// Gets the unique identifier for this node + /// + string NodeId { get; } + } +} diff --git a/lib/VNLib.Data.Caching.Extensions/src/ICachePeerAdvertisment.cs b/lib/VNLib.Data.Caching.Extensions/src/ICachePeerAdvertisment.cs deleted file mode 100644 index acf883e..0000000 --- a/lib/VNLib.Data.Caching.Extensions/src/ICachePeerAdvertisment.cs +++ /dev/null @@ -1,50 +0,0 @@ -/* -* Copyright (c) 2023 Vaughn Nugent -* -* Library: VNLib -* Package: VNLib.Data.Caching.Extensions -* File: ICachePeerAdvertisment.cs -* -* ICachePeerAdvertisment.cs is part of VNLib.Data.Caching.Extensions which is part of the larger -* VNLib collection of libraries and utilities. -* -* VNLib.Data.Caching.Extensions 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. -* -* VNLib.Data.Caching.Extensions 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/. -*/ - -using System; - - -namespace VNLib.Data.Caching.Extensions -{ - /// - /// Represents a node that can be advertised to clients - /// - public interface ICachePeerAdvertisment - { - /// - /// The endpoint for clients to connect to to access the cache - /// - Uri ConnectEndpoint { get; } - - /// - /// Gets the address for clients to connect to to discover other discovertable nodes - /// - Uri? DiscoveryEndpoint { get; } - - /// - /// Gets the unique identifier for this node - /// - string NodeId { get; } - } -} diff --git a/lib/VNLib.Data.Caching.Extensions/src/INodeDiscoveryCollection.cs b/lib/VNLib.Data.Caching.Extensions/src/INodeDiscoveryCollection.cs new file mode 100644 index 0000000..9adebdc --- /dev/null +++ b/lib/VNLib.Data.Caching.Extensions/src/INodeDiscoveryCollection.cs @@ -0,0 +1,62 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Data.Caching.Extensions +* File: INodeDiscoveryCollection.cs +* +* INodeDiscoveryCollection.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Data.Caching.Extensions 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. +* +* VNLib.Data.Caching.Extensions 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/. +*/ + + +using System.Collections.Generic; + +namespace VNLib.Data.Caching.Extensions +{ + /// + /// Represents a collection of discovered nodes + /// +#pragma warning disable CA1711 // Identifiers should not have incorrect suffix + public interface INodeDiscoveryCollection +#pragma warning restore CA1711 // Identifiers should not have incorrect suffix + { + /// + /// Begins a new discovery and gets an enumerator for the discovery process + /// + /// An enumerator that simplifies discovery of unique nodes + INodeDiscoveryEnumerator BeginDiscovery(); + + /// + /// Begins a new discovery and gets an enumerator for the discovery process + /// + /// An initial collection of peers to add to the enumeration + /// An enumerator that simplifies discovery of unique nodes + INodeDiscoveryEnumerator BeginDiscovery(IEnumerable initialPeers); + + /// + /// Gets a snapshot of all discovered nodes in the current collection. + /// + /// The current collection of notes + ICacheNodeAdvertisment[] GetAllNodes(); + + /// + /// Completes a discovery process and updates the collection with the results + /// + /// The enumerator used to collect discovered nodes + void CompleteDiscovery(INodeDiscoveryEnumerator enumerator); + } +} diff --git a/lib/VNLib.Data.Caching.Extensions/src/INodeDiscoveryEnumerator.cs b/lib/VNLib.Data.Caching.Extensions/src/INodeDiscoveryEnumerator.cs new file mode 100644 index 0000000..f6d5f40 --- /dev/null +++ b/lib/VNLib.Data.Caching.Extensions/src/INodeDiscoveryEnumerator.cs @@ -0,0 +1,42 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Data.Caching.Extensions +* File: INodeDiscoveryEnumerator.cs +* +* INodeDiscoveryEnumerator.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Data.Caching.Extensions 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. +* +* VNLib.Data.Caching.Extensions 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/. +*/ + + +using System.Collections.Generic; + + +namespace VNLib.Data.Caching.Extensions +{ + /// + /// A custom enumerator for the node discovery process + /// + public interface INodeDiscoveryEnumerator : IEnumerator + { + /// + /// Adds the specified peer to the collection of discovered peers + /// + /// The peer collection + void OnPeerDiscoveryComplete(IEnumerable discoveredPeers); + } +} diff --git a/lib/VNLib.Data.Caching.Extensions/src/NodeDiscoveryCollection.cs b/lib/VNLib.Data.Caching.Extensions/src/NodeDiscoveryCollection.cs new file mode 100644 index 0000000..305f5de --- /dev/null +++ b/lib/VNLib.Data.Caching.Extensions/src/NodeDiscoveryCollection.cs @@ -0,0 +1,117 @@ +/* +* Copyright (c) 2023 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Data.Caching.Extensions +* File: INodeDiscoveryCollection.cs +* +* INodeDiscoveryCollection.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Data.Caching.Extensions 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. +* +* VNLib.Data.Caching.Extensions 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/. +*/ + +using System; +using System.Linq; +using System.Collections.Generic; +using System.Collections; + +namespace VNLib.Data.Caching.Extensions +{ + /// + /// Represents a collection of available cache nodes from a discovery process + /// + public sealed class NodeDiscoveryCollection : INodeDiscoveryCollection + { + private LinkedList _peers; + + /// + /// Initializes a new empty + /// + public NodeDiscoveryCollection() + { + _peers = new(); + } + + /// + public INodeDiscoveryEnumerator BeginDiscovery() + { + return new NodeEnumerator(new()); + } + + /// + public INodeDiscoveryEnumerator BeginDiscovery(IEnumerable initialPeers) + { + //Init new enumerator with the initial peers + return new NodeEnumerator(new(initialPeers)); + } + + /// + public void CompleteDiscovery(INodeDiscoveryEnumerator enumerator) + { + _ = enumerator ?? throw new ArgumentNullException(nameof(enumerator)); + + //Capture all nodes from the enumerator and store them as our current peers + _peers = (enumerator as NodeEnumerator)!.Peers; + } + + /// + public ICacheNodeAdvertisment[] GetAllNodes() + { + //Capture all current peers + return _peers.ToArray(); + } + + private sealed record class NodeEnumerator(LinkedList Peers) : INodeDiscoveryEnumerator + { + //Keep track of the current node in the collection so we can move down the list + private LinkedListNode? _currentNode = Peers.First; + + public ICacheNodeAdvertisment Current => _currentNode?.Value; + object IEnumerator.Current => _currentNode?.Value; + + + /// + public bool MoveNext() + { + //Move to the next peer in the collection + _currentNode = _currentNode?.Next; + + return _currentNode?.Value != null; + } + + /// + public void OnPeerDiscoveryComplete(IEnumerable discoveredPeers) + { + //Get only the peers from the discovery that are not already in the collection + IEnumerable newPeers = discoveredPeers.Except(Peers); + + //Add them to the end of the collection + foreach (ICacheNodeAdvertisment ad in newPeers) + { + Peers.AddLast(ad); + } + } + + public void Reset() + { + //Go to the first node + _currentNode = Peers.First; + } + + public void Dispose() + { } + } + } +} -- cgit