Ejemplo n.º 1
0
        /// <summary>
        /// Creates and adds a <see cref="IReceiver"/> to the cache that binds and accepts requests sent to the <see cref="IAddress"/>
        /// using a matching <see cref="IReceiverFactory{TReceiver}"/> registered with <see cref="AddFactory{TReceiver}(IReceiverFactory{TReceiver})"/>
        /// </summary>
        /// <typeparam name="TReceiver">Transport specific implementation of <see cref="IReceiver"/> to create</typeparam>
        /// <param name="address">The <see cref="IAddress"/> to which the <see cref="IReceiver"/> binds</param>
        public void AddReceiver <TReceiver>(IAddress address) where TReceiver : IReceiver
        {
            if (address is null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (receivers.ContainsKey(address))
            {
                throw new InvalidOperationException($"Cache already contains a {typeof(TReceiver).Name} for {address.ToString()}");
            }

            if (!factories.TryGetValue(typeof(TReceiver), out var factory))
            {
                throw MissingFactoryException.For <TReceiver, ReceiverCache>();
            }

            var receiver = factory.CreateReceiver(address);

            receivers.Add(address, receiver);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates and adds a <see cref="IPublisher"/> to the cache that binds and distributes <see cref="Package"/>s to <see cref="ISubscriber"/>s
        /// </summary>
        /// <typeparam name="TPublisher">Transport specific implementation of <see cref="IPublisher"/> to create</typeparam>
        /// <param name="address">The <see cref="IAddress"/> to bind to on which <see cref="ISubscriber"/>s can connect to receive updates</param>
        public void AddPublisher <TPublisher>(IAddress address) where TPublisher : IPublisher
        {
            if (address is null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (publishers.ContainsKey(address))
            {
                throw new InvalidOperationException(nameof(address));
            }

            if (!factories.TryGetValue(typeof(TPublisher), out var factory))
            {
                throw MissingFactoryException.For <TPublisher, PublisherCache>();
            }

            var publisher = factory.CreatePublisher(address);

            publishers.Add(address, publisher);
        }