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)); }
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))); }
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]); }
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)); }
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"]); }
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); }
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"]); }
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); }
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) }
public static byte[] GenerateSubscribeCommand(string topic) { using (MemoryStream stream = new MemoryStream()) { MqttSubscribeMessage data = new MqttSubscribeMessage().ToTopic(topic); data.WriteTo(stream); return(stream.ToArray()); } }
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); } }
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 }