コード例 #1
0
ファイル: RouteManager.cs プロジェクト: developeramarish/AI4E
        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);
            }
        }
コード例 #2
0
        public async Task AddModuleAsync(ModuleIdentifier module, EndPointAddress endPoint, IEnumerable <ReadOnlyMemory <char> > prefixes, CancellationToken cancellation)
        {
            if (module == default)
            {
                throw new ArgumentDefaultException(nameof(module));
            }

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

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

            if (!prefixes.Any())
            {
                throw new ArgumentException("The collection must not be empty.", nameof(prefixes));
            }

            if (prefixes.Any(prefix => prefix.Span.IsEmptyOrWhiteSpace()))
            {
                throw new ArgumentException("The collection must not contain null entries or entries that are empty or contain whitespace only.", nameof(prefixes));
            }

            var prefixCollection = (prefixes as ICollection <ReadOnlyMemory <char> >) ?? prefixes.ToList();
            var session          = await _coordinationManager.GetSessionAsync(cancellation);

            var tasks = new List <Task>(capacity: prefixCollection.Count());

            foreach (var prefix in prefixCollection)
            {
                tasks.Add(WriteModulePrefixEntryAsync(prefix, endPoint, session, cancellation));
            }

            await Task.WhenAll(tasks);

            await WriteRunningModuleEntryAsync(module, endPoint, prefixCollection, session, cancellation);

            // TODO: When cancelled, alls completed operations should be reverted.
            // TODO: The RemoveModuleAsync alogrithm assumes that there are no prefix entries, if the running module entry is not present. We should reflect this assumtion here.
        }
コード例 #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);
        }