示例#1
0
        public async ValueTask <IEnumerable <EndPointAddress> > GetEndPointsAsync(ReadOnlyMemory <char> prefix, CancellationToken cancellation)
        {
            if (prefix.Span.IsEmptyOrWhiteSpace())
            {
                throw new ArgumentException("The argument must not be empty, not consist of whitespace only.", nameof(prefix));
            }

            var normalizedPrefix = NormalizePrefix(prefix);

            // It is not possible to register an end-point address for the root path.
            if (normalizedPrefix.IsEmpty || normalizedPrefix.Span[0] == '_')
            {
                return(Enumerable.Empty <EndPointAddress>());
            }

            var path  = GetPrefixPath(normalizedPrefix, normalize: false);
            var entry = await _coordinationManager.GetOrCreateAsync(path, _emptyPayload, EntryCreationModes.Default, cancellation);

            Assert(entry != null);

            var result       = new List <EndPointAddress>(capacity: entry.Children.Count);
            var childEntries = (await entry.GetChildrenEntriesAsync(cancellation)).OrderBy(p => p.CreationTime).ToList();

            foreach (var childEntry in childEntries)
            {
                var endPoint = ReadModulePrefixEntry(childEntry);
                result.Add(endPoint);
            }

            return(result);
        }
示例#2
0
        public async Task AddRouteAsync(EndPointAddress endPoint, RouteRegistration routeRegistration, CancellationToken cancellation)
        {
            if (endPoint == default)
            {
                throw new ArgumentDefaultException(nameof(endPoint));
            }

            if (routeRegistration == default)
            {
                throw new ArgumentDefaultException(nameof(routeRegistration));
            }

            var session           = (await _coordinationManager.GetSessionAsync(cancellation)).ToString();
            var entryCreationMode = EntryCreationModes.Default;

            if (routeRegistration.RegistrationOptions.IncludesFlag(RouteRegistrationOptions.Transient))
            {
                entryCreationMode |= EntryCreationModes.Ephemeral;
            }

            var reversePath = GetReversePath(session, endPoint, routeRegistration.Route);

            using (var stream = new MemoryStream(capacity: 4))
            {
                using (var writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true))
                {
                    writer.Write((int)routeRegistration.RegistrationOptions);
                }
                var payload = stream.ToArray();
                await _coordinationManager.CreateAsync(reversePath, payload, entryCreationMode, cancellation);
            }

            var path = GetPath(routeRegistration.Route, endPoint, session);

            using (var stream = new MemoryStream(capacity: 4 + 4 + endPoint.Utf8EncodedValue.Length))
            {
                using (var writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true))
                {
                    writer.Write((int)routeRegistration.RegistrationOptions);
                    writer.Write(endPoint);
                }
                var payload = stream.ToArray();
                // TODO: What to do if the entry already existed but with different options?
                await _coordinationManager.GetOrCreateAsync(path, payload, entryCreationMode, cancellation);
            }
        }
示例#3
0
        public async Task MapEndPointAsync(EndPointAddress endPoint, TAddress address, CancellationToken cancellation)
        {
            if (endPoint == default)
            {
                throw new ArgumentDefaultException(nameof(endPoint));
            }

            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (address.Equals(default(TAddress)))
            {
                throw new ArgumentDefaultException(nameof(address));
            }

            var session = (await _coordinationManager.GetSessionAsync(cancellation)).ToString();
            var path    = GetPath(endPoint, session);

            await _coordinationManager.GetOrCreateAsync(path, _addressConversion.SerializeAddress(address), EntryCreationModes.Ephemeral, cancellation);
        }