예제 #1
0
        public void AcknowledgedSubscriptionRequestCreatesActiveSubscription()
        {
            Func <MqttMessage, bool> theCallback = null;
            MqttSubscribeMessage     subMsg      = null;
            var pubMock = new Mock <IPublishingManager>();

            var chMock = new Mock <IMqttConnectionHandler>();

            // mock the call to register and save the callback for later.
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb);
            // mock the call to Send(), which should occur when the subscription manager tries to subscribe
            chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>()))
            .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg);

            const string  topic = "testtopic";
            const MqttQos qos   = MqttQos.AtMostOnce;

            // run and verify the mocks were called.
            var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object);

            subs.RegisterSubscription <string, AsciiPayloadConverter>(topic, qos);
            chMock.VerifyAll();

            // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos
            Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys);
            Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]);

            // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived.
            theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(1).AddQosGrant(MqttQos.AtMostOnce));

            Assert.Equal(SubscriptionStatus.Active, subs.GetSubscriptionsStatus(topic));
        }
예제 #2
0
        public void SubscriptionAckForNonPendingSubscriptionThrowsException()
        {
            Func <MqttMessage, bool> theCallback = null;
            MqttSubscribeMessage     subMsg      = null;
            var pubMock = new Mock <IPublishingManager>();

            var chMock = new Mock <IMqttConnectionHandler>();

            // mock the call to register and save the callback for later.
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb);
            // mock the call to Send(), which should occur when the subscription manager tries to subscribe
            chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>()))
            .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg);

            const string  topic = "testtopic";
            const MqttQos qos   = MqttQos.AtMostOnce;

            // run and verify the mocks were called.
            var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object);

            subs.RegisterSubscription <string, AsciiPayloadConverter>(topic, qos);
            chMock.VerifyAll();

            // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos
            Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys);
            Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]);

            // execute the callback with a bogus message identifier.
            Assert.Throws <ArgumentException>(() => theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(999).AddQosGrant(MqttQos.AtMostOnce)));
        }
예제 #3
0
        public void SubscriptionRequestInvokesSend()
        {
            MqttSubscribeMessage subMsg = null;
            var pubMock = new Mock <IPublishingManager>();

            var chMock = new Mock <IMqttConnectionHandler>();

            // mock the call to register and save the callback for later.
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >()));
            // mock the call to Send(), which should occur when the subscription manager tries to subscribe
            chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>()))
            .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg);

            const string  topic = "testtopic";
            const MqttQos qos   = MqttQos.AtMostOnce;

            // run and verify the mocks were called.
            var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object);

            subs.RegisterSubscription <string, AsciiPayloadConverter>(topic, qos);
            chMock.VerifyAll();

            // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos
            Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys);
            Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]);
        }
예제 #4
0
        public void MultipleSubscriptionsForAcknowledgedSubscriptionThrowsException()
        {
            Func <MqttMessage, bool> theCallback = null;
            MqttSubscribeMessage     subMsg      = null;

            var chMock = new Mock <IMqttConnectionHandler>();

            // mock the call to register and save the callback for later.
            chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >()))
            .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb);
            // mock the call to Send(), which should occur when the subscription manager tries to subscribe
            chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>()))
            .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg);

            string  topic = "testtopic";
            MqttQos qos   = MqttQos.AtMostOnce;

            // run and verify the mocks were called.
            Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object);
            short subid = subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos);

            chMock.VerifyAll();

            // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos
            Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys);
            Assert.Equal <MqttQos>(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]);

            // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived.
            theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(subid).AddQosGrant(MqttQos.AtMostOnce));

            Assert.Equal <SubscriptionStatus>(SubscriptionStatus.Active, subs.GetSubscriptionsStatus(topic));

            // NOW THE IMPORTANT PART - Try and subscribe agin to the same topic.
            Assert.Throws <ArgumentException>(() => subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos));
        }
예제 #5
0
        public void SingleTopic()
        {
            // Message Specs________________
            // <82><09><00><02><00><04>fred<00> (subscribe to topic fred at qos 0)
            var sampleMessage = new[]
            {
                (byte)0x82,
                (byte)0x09,
                (byte)0x00,
                (byte)0x02,
                (byte)0x00,
                (byte)0x04,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
                (byte)0x00,
            };

            MqttMessage baseMessage = MqttMessage.CreateFrom(sampleMessage);

            Console.WriteLine(baseMessage.ToString());

            // check that the message was correctly identified as a connect message.
            Assert.IsType <MqttSubscribeMessage>(baseMessage);
            MqttSubscribeMessage message = (MqttSubscribeMessage)baseMessage;

            Assert.Equal <int>(1, message.Payload.Subscriptions.Count);
            Assert.True(message.Payload.Subscriptions.ContainsKey("fred"));
            Assert.Equal <MqttQos>(MqttQos.AtMostOnce, message.Payload.Subscriptions["fred"]);
        }
예제 #6
0
파일: Api.cs 프로젝트: zhanghz/nmqtt
        public void ClearSubscriptionsClearsSubscriptions()
        {
            MqttSubscribeMessage msg = new MqttSubscribeMessage();

            msg.Payload.AddSubscription("A/Topic", MqttQos.AtMostOnce);
            msg.Payload.ClearSubscriptions();
            Assert.Equal <int>(0, msg.Payload.Subscriptions.Count);
        }
예제 #7
0
파일: Api.cs 프로젝트: zhanghz/nmqtt
        public void AddSubscriptionOverExistingSubscriptionUpdatesQos()
        {
            MqttSubscribeMessage msg = new MqttSubscribeMessage();

            msg.Payload.AddSubscription("A/Topic", MqttQos.AtMostOnce);
            msg.Payload.AddSubscription("A/Topic", MqttQos.AtLeastOnce);
            Assert.Equal <MqttQos>(MqttQos.AtLeastOnce, msg.Payload.Subscriptions["A/Topic"]);
        }
예제 #8
0
        private void SubscribeReceived(IMqttSession session, MqttSubscribeMessage subscribeMessage)
        {
            MqttSubscribeAckMessage subAck = new MqttSubscribeAckMessage().WithMessageIdentifier(subscribeMessage.VariableHeader.MessageIdentifier);

            AddSubscriptions(session, subscribeMessage.Payload.Subscriptions);
            session.Write(subAck);
            PublishMessages(session, storageProvider.GetRetained(subscribeMessage.Payload.Subscriptions), true);
        }
예제 #9
0
        public void MultiTopic()
        {
            // double topic Subscribe
            var expected = new[]
            {
                (byte)0x82,
                (byte)0x10,
                (byte)0x00,
                (byte)0x03,
                (byte)0x00,
                (byte)0x04,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
                (byte)0x01,
                (byte)0x00,
                (byte)0x04,
                (byte)'m',
                (byte)'a',
                (byte)'r',
                (byte)'k',
                (byte)0x02
            };

            MqttMessage msg = new MqttSubscribeMessage()
                              .ToTopic("fred")
                              .AtQos(MqttQos.AtLeastOnce)
                              .ToTopic("mark")
                              .AtQos(MqttQos.ExactlyOnce)
                              .WithMessageIdentifier(3)
                              .ExpectAcknowledgement();

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]);   // msg type of header
            Assert.Equal <byte>(expected[1], actual[1]);   // remaining length
            Assert.Equal <byte>(expected[2], actual[2]);   // Start of VH: MsgID Byte1
            Assert.Equal <byte>(expected[3], actual[3]);   // MsgID Byte 2
            Assert.Equal <byte>(expected[4], actual[4]);   // Topic Length B1
            Assert.Equal <byte>(expected[5], actual[5]);   // Topic Length B2
            Assert.Equal <byte>(expected[6], actual[6]);   // f
            Assert.Equal <byte>(expected[7], actual[7]);   // r
            Assert.Equal <byte>(expected[8], actual[8]);   // e
            Assert.Equal <byte>(expected[9], actual[9]);   // d
            Assert.Equal <byte>(expected[10], actual[10]); // Qos (LeastOnce)
            Assert.Equal <byte>(expected[11], actual[11]); // Topic Length B1
            Assert.Equal <byte>(expected[12], actual[12]); // Topic Length B2
            Assert.Equal <byte>(expected[13], actual[13]); // m
            Assert.Equal <byte>(expected[14], actual[14]); // a
            Assert.Equal <byte>(expected[15], actual[15]); // r
            Assert.Equal <byte>(expected[16], actual[16]); // k
            Assert.Equal <byte>(expected[17], actual[17]); // Qos (ExactlyOnce)
        }
예제 #10
0
 public static byte[] GenerateSubscribeCommand(string topic)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         MqttSubscribeMessage data = new MqttSubscribeMessage().ToTopic(topic);
         data.WriteTo(stream);
         return(stream.ToArray());
     }
 }
예제 #11
0
        private void SubscribeReceived(WebSocketSession session, MqttSubscribeMessage subscribeMessage)
        {
            MqttSubscribeAckMessage subAck = new MqttSubscribeAckMessage().WithMessageIdentifier(subscribeMessage.VariableHeader.MessageIdentifier);

            AddSubscriptions(session, subscribeMessage.Payload.Subscriptions);
            using (var messageStream = new MemoryStream())
            {
                subAck.WriteTo(messageStream);
                ArraySegment <byte> bytes = new ArraySegment <byte>(messageStream.ToArray());
                session.Send(bytes);
            }
        }
예제 #12
0
        public void SingleTopic()
        {
            // simple single topic Subscribe message
            var expected = new[]
            {
                (byte)0x8A,
                (byte)0x09,
                (byte)0x00,
                (byte)0x02,
                (byte)0x00,
                (byte)0x04,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
                (byte)0x01,
            };

            MqttMessage msg = new MqttSubscribeMessage()
                              .ToTopic("fred")
                              .AtQos(MqttQos.AtLeastOnce)
                              .WithMessageIdentifier(2)
                              .ExpectAcknowledgement()
                              .IsDuplicate();

            Console.WriteLine(msg);

            byte[] actual = MessageSerializationHelper.GetMessageBytes(msg);

            Assert.Equal <int>(expected.Length, actual.Length);
            Assert.Equal <byte>(expected[0], actual[0]);   // msg type of header
            Assert.Equal <byte>(expected[1], actual[1]);   // remaining length
            Assert.Equal <byte>(expected[2], actual[2]);   // Start of VH: MsgID Byte1
            Assert.Equal <byte>(expected[3], actual[3]);   // MsgID Byte 2
            Assert.Equal <byte>(expected[4], actual[4]);   // Topic Length B1
            Assert.Equal <byte>(expected[5], actual[5]);   // Topic Length B2
            Assert.Equal <byte>(expected[6], actual[6]);   // f
            Assert.Equal <byte>(expected[7], actual[7]);   // r
            Assert.Equal <byte>(expected[8], actual[8]);   // e
            Assert.Equal <byte>(expected[9], actual[9]);   // d
            Assert.Equal <byte>(expected[10], actual[10]); // d
        }