コード例 #1
0
        /// <summary>
        /// Performs a synchronous connect to the message broker with an optional username and password
        /// for the purposes of authentication.
        /// </summary>
        /// <param name="username">Optionally the username to authenticate as.</param>
        /// <param name="password">Optionally the password to authenticate with.</param>
        public ConnectionState Connect(string username = null, string password = null)
        {
            Log.Debug(m => m("Initiating connection to broker '{0}', port '{1}' using Client Identifier '{2}'",
                             server, port, clientIdentifier));

            if (username != null)
            {
                Log.Info(m => m("Authenticating with username '{0}' and password '{0}'", username, password));
                if (username.Trim().Length > Constants.RecommendedMaxUsernamePasswordLength)
                {
                    Log.Warn(m => m("Username length ({0}) exceeds the max recommended in the MQTT spec. ", username.Trim().Length));
                }
                if (password != null && password.Trim().Length > Constants.RecommendedMaxUsernamePasswordLength)
                {
                    Log.Warn(m => m("Password length ({0}) exceeds the max recommended in the MQTT spec. ", password.Trim().Length));
                }
            }

            var connectMessage = GetConnectMessage(username, password);

            connectionHandler = new SynchronousMqttConnectionHandler();

            // TODO: Get Get timeout from config or ctor or elsewhere.
            keepAlive            = new MqttConnectionKeepAlive(connectionHandler, 30);
            publishingManager    = new PublishingManager(connectionHandler);
            subscriptionsManager = new SubscriptionsManager(connectionHandler, publishingManager);
            messageLogger        = new MessageLogger(connectionHandler);

            return(connectionHandler.Connect(this.server, this.port, connectMessage));
        }
コード例 #2
0
        public void CtorRegistersForAllSentMessages()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to register and save the callback for later.
            chMock.Setup((x) => x.RegisterForAllSentMessages(It.IsAny<Func<MqttMessage, bool>>()));

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 60);

            chMock.VerifyAll();
            ka.Dispose();
        }
コード例 #3
0
        public void DisposeUnRegistersForPingResponse()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to register and save the callback for later.
            chMock.Setup((x) => x.UnRegisterForMessage(MqttMessageType.PingResponse, It.IsAny<Func<MqttMessage, bool>>()));

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 60);
            ka.Dispose();

            chMock.VerifyAll();
        }
コード例 #4
0
        /// <summary>
        /// Performs a synchronous connect to the message broker.
        /// </summary>
        public ConnectionState Connect()
        {
            MqttConnectMessage connectMessage = GetConnectMessage();

            connectionHandler = new SynchronousMqttConnectionHandler();

            // TODO: Get Get timeout from config or ctor or elsewhere.
            keepAlive            = new MqttConnectionKeepAlive(connectionHandler, 30);
            subscriptionsManager = new SubscriptionsManager(connectionHandler);
            messageLogger        = new MessageLogger(connectionHandler);
            publishingManager    = new PublishingManager(connectionHandler, HandlePublishMessage);

            return(connectionHandler.Connect(this.server, this.port, connectMessage));
        }
コード例 #5
0
        public void PingRequestFromBrokerCausesPingResponse()
        {
            Func<MqttMessage, bool> pingReqCallback = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup((x) => x.SendMessage(It.IsAny<MqttPingResponseMessage>()));
            chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.PingRequest, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pingReqCallback = cb);

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 1);
            pingReqCallback(new MqttPingRequestMessage());
            
            ka.Dispose();

            // verify that we did actually send a ping request.
            chMock.VerifyAll();
        }
コード例 #6
0
        public void PingReceiveDoesNotStopPingRequest()
        {
            Func<MqttMessage, bool> pingRespCallback = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to Send

            chMock.Setup((x) => x.SendMessage(It.IsAny<MqttPingRequestMessage>()));
            chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.PingResponse, It.IsAny<Func<MqttMessage, bool>>()))
                .Callback((MqttMessageType mt, Func<MqttMessage, bool> cb) => pingRespCallback = cb);

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 1);
            System.Threading.Thread.Sleep(800);
            pingRespCallback(new MqttPingResponseMessage()); // fake that we received a ping response from the broker.
            System.Threading.Thread.Sleep(300);

            ka.Dispose();

            // verify that we did actually send a ping request.
            chMock.VerifyAll();
        }
コード例 #7
0
        public void SendPingRequestDoesNotOccurWhenOtherMessageSent()
        {
            Func<MqttMessage, bool> sendCallback = null;

            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to Send

            chMock.Setup((x) => x.SendMessage(It.IsAny<MqttPingRequestMessage>()));
            chMock.Setup((x) => x.RegisterForAllSentMessages(It.IsAny < Func<MqttMessage, bool>>()))
                .Callback((Func<MqttMessage, bool> regSendCallback) => sendCallback = regSendCallback);

            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 1);
            System.Threading.Thread.Sleep(800);
            sendCallback(new MqttSubscribeAckMessage()); // fake that something was sent
            System.Threading.Thread.Sleep(300);

            // 1.3s should have passed, and we should not have had a send operation occur because we faked
            // a send operation to the callback.
            // Ie. SendMessage (first expectation we set on the mock) should not have been called.

            ka.Dispose();

            // verify it wasn't called.
            chMock.Verify((x) => x.SendMessage(It.IsAny<MqttPingRequestMessage>()), Times.Never());
        }
コード例 #8
0
        public void SendInactivityCausesPingRequest()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            // mock the call to Send
            chMock.Setup((x) => x.SendMessage(It.IsAny<MqttPingRequestMessage>()));

            // initiate the keepalive connection, then sleep for a few ms, to allow the timer to execute
            MqttConnectionKeepAlive ka = new MqttConnectionKeepAlive(chMock.Object, 0);
            System.Threading.Thread.Sleep(1000);

            chMock.VerifyAll();
            ka.Dispose();
        }