/// <summary>
        /// Initializes a new instance of the <see cref="CommunicationEntryPoint"/> class.
        /// </summary>
        /// <param name="endpointStorage">The collection that stores information about all known endpoints.</param>
        /// <param name="commands">The collection that stores information about all the known remote commands.</param>
        /// <param name="notifications">The collection that stores information about all the known remote notifications.</param>
        /// <param name="verifyConnectionTo">The function that is used to verify connections to remote endpoints.</param>
        /// <param name="configuration">The object that stores the configuration for the application.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="endpointStorage"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="commands"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="notifications"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="verifyConnectionTo"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="configuration"/> is <see langword="null" />.
        /// </exception>
        public CommunicationEntryPoint(
            IStoreInformationAboutEndpoints endpointStorage,
            ISendCommandsToRemoteEndpoints commands,
            INotifyOfRemoteEndpointEvents notifications,
            VerifyEndpointConnectionStatus verifyConnectionTo,
            IConfiguration configuration)
        {
            {
                Lokad.Enforce.Argument(() => endpointStorage);
                Lokad.Enforce.Argument(() => commands);
                Lokad.Enforce.Argument(() => notifications);
                Lokad.Enforce.Argument(() => verifyConnectionTo);
            }

            m_MessageSendTimeout = configuration.HasValueFor(CommunicationConfigurationKeys.WaitForResponseTimeoutInMilliSeconds)
                ? TimeSpan.FromMilliseconds(configuration.Value <int>(CommunicationConfigurationKeys.WaitForResponseTimeoutInMilliSeconds))
                : TimeSpan.FromMilliseconds(CommunicationConstants.DefaultWaitForResponseTimeoutInMilliSeconds);

            m_EndpointStorage = endpointStorage;
            m_EndpointStorage.OnEndpointConnected    += HandleOnEndpointConnected;
            m_EndpointStorage.OnEndpointDisconnected += HandleOnEndpointDisconnected;

            m_Commands = commands;
            m_Commands.OnEndpointConnected    += HandleOnEndpointConnected;
            m_Commands.OnEndpointDisconnected += HandleOnEndpointDisconnected;

            m_Notifications = notifications;
            m_Notifications.OnEndpointConnected    += HandleOnEndpointConnected;
            m_Notifications.OnEndpointDisconnected += HandleOnEndpointDisconnected;

            m_VerifyConnectionTo = verifyConnectionTo;
        }
Exemplo n.º 2
0
        public void IsConnectionActiveWithIdAndUnresponsiveConnection()
        {
            var endpoint     = EndpointIdExtensions.CreateEndpointIdForCurrentProcess();
            var address      = new Uri("http://localhost/discovery");
            var endpointInfo = new EndpointInformation(
                endpoint,
                new DiscoveryInformation(address),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/messages"),
                    new Uri("http://localhost/data")));
            var endpoints = new Mock <IStoreInformationAboutEndpoints>();
            {
                endpoints.Setup(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo))
                .Returns(true)
                .Verifiable();
            }

            var commands      = new Mock <ISendCommandsToRemoteEndpoints>();
            var notifications = new Mock <INotifyOfRemoteEndpointEvents>();
            var configuration = new Mock <IConfiguration>();
            {
                configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>()))
                .Returns(false);
            }

            var source = new CancellationTokenSource();

            source.Cancel();
            VerifyEndpointConnectionStatus func =
                (id, timeout) =>
            {
                return(Task.Factory.StartNew(
                           () =>
                {
                },
                           source.Token,
                           TaskCreationOptions.None,
                           new CurrentThreadTaskScheduler()));
            };

            var entryPoint = new CommunicationEntryPoint(
                endpoints.Object,
                commands.Object,
                notifications.Object,
                func,
                configuration.Object);

            endpoints.Raise(e => e.OnEndpointConnected     += null, new EndpointEventArgs(endpoint));
            commands.Raise(c => c.OnEndpointConnected      += null, new EndpointEventArgs(endpoint));
            notifications.Raise(n => n.OnEndpointConnected += null, new EndpointEventArgs(endpoint));

            Assert.IsFalse(entryPoint.IsConnectionActive(endpoint));
        }
Exemplo n.º 3
0
        public void OnEndpointDisconnectedForAnUnknownEndpoint()
        {
            var endpoint      = EndpointIdExtensions.CreateEndpointIdForCurrentProcess();
            var address       = new Uri("http://localhost/discovery");
            var endpoints     = new Mock <IStoreInformationAboutEndpoints>();
            var commands      = new Mock <ISendCommandsToRemoteEndpoints>();
            var notifications = new Mock <INotifyOfRemoteEndpointEvents>();
            var configuration = new Mock <IConfiguration>();
            {
                configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>()))
                .Returns(false);
            }

            VerifyEndpointConnectionStatus func = (id, timeout) => null;

            var entryPoint = new CommunicationEntryPoint(
                endpoints.Object,
                commands.Object,
                notifications.Object,
                func,
                configuration.Object);

            EndpointId eventEndpoint = null;

            entryPoint.OnEndpointDisconnected +=
                (s, e) =>
            {
                eventEndpoint = e.Endpoint;
            };

            endpoints.Raise(e => e.OnEndpointDisconnected += null, new EndpointEventArgs(endpoint));
            Assert.IsNull(eventEndpoint);
            Assert.IsFalse(entryPoint.KnownEndpoints().Any());
            Assert.IsNull(entryPoint.FromUri(address));

            commands.Raise(c => c.OnEndpointDisconnected += null, new EndpointEventArgs(endpoint));
            Assert.IsNull(eventEndpoint);
            Assert.IsFalse(entryPoint.KnownEndpoints().Any());
            Assert.IsNull(entryPoint.FromUri(address));

            notifications.Raise(n => n.OnEndpointDisconnected += null, new EndpointEventArgs(endpoint));
            Assert.IsNull(eventEndpoint);
            Assert.IsFalse(entryPoint.KnownEndpoints().Any());
            Assert.IsNull(entryPoint.FromUri(address));
        }
Exemplo n.º 4
0
        private static void RegisterConnectionVerificationFunctions(ContainerBuilder builder)
        {
            builder.Register(
                c =>
            {
                var layer = c.Resolve <IProtocolLayer>();
                VerifyEndpointConnectionStatus func = (id, timeout) => layer.VerifyConnectionIsActive(id, timeout);
                return(func);
            })
            .As <VerifyEndpointConnectionStatus>()
            .SingleInstance();

            builder.Register(
                c =>
            {
                var layer = c.Resolve <IProtocolLayer>();
                VerifyEndpointConnectionStatusWithCustomData func = layer.VerifyConnectionIsActive;
                return(func);
            })
            .As <VerifyEndpointConnectionStatusWithCustomData>()
            .SingleInstance();
        }
Exemplo n.º 5
0
        public void IsConnectionActiveWithUrlAndUnknownConnection()
        {
            var endpoint     = EndpointIdExtensions.CreateEndpointIdForCurrentProcess();
            var address      = new Uri("http://localhost/discovery");
            var endpointInfo = new EndpointInformation(
                endpoint,
                new DiscoveryInformation(address),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/messages"),
                    new Uri("http://localhost/data")));
            var endpoints = new Mock <IStoreInformationAboutEndpoints>();
            {
                endpoints.Setup(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo))
                .Returns(true)
                .Verifiable();
            }

            var commands      = new Mock <ISendCommandsToRemoteEndpoints>();
            var notifications = new Mock <INotifyOfRemoteEndpointEvents>();
            var configuration = new Mock <IConfiguration>();
            {
                configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>()))
                .Returns(false);
            }

            VerifyEndpointConnectionStatus func = (id, timeout) => null;

            var entryPoint = new CommunicationEntryPoint(
                endpoints.Object,
                commands.Object,
                notifications.Object,
                func,
                configuration.Object);

            Assert.IsFalse(entryPoint.IsConnectionActive(new Uri("http://localhost/invalid")));
        }
Exemplo n.º 6
0
        public void OnEndpointDisconnected()
        {
            var endpoint     = EndpointIdExtensions.CreateEndpointIdForCurrentProcess();
            var address      = new Uri("http://localhost/discovery");
            var endpointInfo = new EndpointInformation(
                endpoint,
                new DiscoveryInformation(address),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/messages"),
                    new Uri("http://localhost/data")));
            var endpoints = new Mock <IStoreInformationAboutEndpoints>();
            {
                endpoints.Setup(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo))
                .Returns(true)
                .Verifiable();
            }

            var commands      = new Mock <ISendCommandsToRemoteEndpoints>();
            var notifications = new Mock <INotifyOfRemoteEndpointEvents>();
            var configuration = new Mock <IConfiguration>();
            {
                configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>()))
                .Returns(false);
            }

            VerifyEndpointConnectionStatus func = (id, timeout) => null;

            var entryPoint = new CommunicationEntryPoint(
                endpoints.Object,
                commands.Object,
                notifications.Object,
                func,
                configuration.Object);

            EndpointId eventEndpoint = null;

            entryPoint.OnEndpointConnected +=
                (s, e) =>
            {
                eventEndpoint = e.Endpoint;
            };

            entryPoint.OnEndpointDisconnected +=
                (s, e) =>
            {
                eventEndpoint = e.Endpoint;
            };

            endpoints.Raise(e => e.OnEndpointConnected += null, new EndpointEventArgs(endpoint));
            Assert.IsNull(eventEndpoint);
            endpoints.Verify(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo), Times.Never());
            Assert.IsFalse(entryPoint.KnownEndpoints().Any());
            Assert.IsNull(entryPoint.FromUri(address));

            commands.Raise(c => c.OnEndpointConnected += null, new EndpointEventArgs(endpoint));
            Assert.IsNull(eventEndpoint);
            endpoints.Verify(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo), Times.Never());
            Assert.IsFalse(entryPoint.KnownEndpoints().Any());
            Assert.IsNull(entryPoint.FromUri(address));

            notifications.Raise(n => n.OnEndpointConnected += null, new EndpointEventArgs(endpoint));
            Assert.AreSame(endpoint, eventEndpoint);
            endpoints.Verify(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo), Times.Once());
            Assert.IsTrue(entryPoint.KnownEndpoints().Any());
            Assert.AreSame(endpoint, entryPoint.FromUri(address));

            eventEndpoint = null;
            endpoints.Raise(e => e.OnEndpointDisconnected += null, new EndpointEventArgs(endpoint));
            Assert.AreSame(endpoint, eventEndpoint);
            endpoints.Verify(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo), Times.Once());
            Assert.IsFalse(entryPoint.KnownEndpoints().Any());
            Assert.IsNull(entryPoint.FromUri(address));

            eventEndpoint = null;
            commands.Raise(c => c.OnEndpointDisconnected += null, new EndpointEventArgs(endpoint));
            Assert.IsNull(eventEndpoint);
            endpoints.Verify(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo), Times.Once());
            Assert.IsFalse(entryPoint.KnownEndpoints().Any());
            Assert.IsNull(entryPoint.FromUri(address));

            notifications.Raise(n => n.OnEndpointDisconnected += null, new EndpointEventArgs(endpoint));
            Assert.IsNull(eventEndpoint);
            endpoints.Verify(e => e.TryGetConnectionFor(It.IsAny <EndpointId>(), out endpointInfo), Times.Once());
            Assert.IsFalse(entryPoint.KnownEndpoints().Any());
            Assert.IsNull(entryPoint.FromUri(address));
        }