Beispiel #1
0
        public void MultipleQos()
        {
            // Message Specs________________
            // <90><03><00><02><00> <01><02>
            var sampleMessage = new[]
            {
                (byte)0x90,
                (byte)0x05,
                (byte)0x00,
                (byte)0x02,
                (byte)0x00,
                (byte)0x01,
                (byte)0x02,
            };

            MqttMessage baseMessage = MqttMessage.CreateFrom(sampleMessage);

            Console.WriteLine(baseMessage.ToString());

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

            Assert.Equal <int>(3, message.Payload.QosGrants.Count);
            Assert.Equal <MqttQos>(MqttQos.AtMostOnce, message.Payload.QosGrants[0]);
            Assert.Equal <MqttQos>(MqttQos.AtLeastOnce, message.Payload.QosGrants[1]);
            Assert.Equal <MqttQos>(MqttQos.ExactlyOnce, message.Payload.QosGrants[2]);
        }
Beispiel #2
0
        public void SingleTopic()
        {
            // Message Specs________________
            // <A2><08><00><03><00><04>fred (Unsubscribe to topic fred)
            var sampleMessage = new[]
            {
                (byte)0xA2,
                (byte)0x08,
                (byte)0x00,
                (byte)0x03,
                (byte)0x00,
                (byte)0x04,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
            };

            MqttMessage baseMessage = MqttMessage.CreateFrom(sampleMessage);

            Console.WriteLine(baseMessage.ToString());

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

            Assert.Equal <int>(1, message.Payload.Subscriptions.Count);
            Assert.True(message.Payload.Subscriptions.Contains("fred"));
        }
Beispiel #3
0
        public void ValidPayload()
        {
            // Message Specs________________
            // <40><02><00><04> (Pub Release for Message ID 4)
            var sampleMessage = new[]
            {
                (byte)0x60,
                (byte)0x02,
                (byte)0x0,
                (byte)0x4,
            };

            MqttMessage baseMessage = MqttMessage.CreateFrom(sampleMessage);

            Console.WriteLine(baseMessage.ToString());

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

            // validate the message deserialization
            Assert.Equal <MqttMessageType>(MqttMessageType.PublishRelease, message.Header.MessageType);
            Assert.Equal <int>(2, message.Header.MessageSize);

            // make sure the publish message length matches the expectred size.
            Assert.Equal <int>(4, message.VariableHeader.MessageIdentifier);
        }
Beispiel #4
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"]);
        }
Beispiel #5
0
        public void UnacceptableProtocolVersion()
        {
            // Our test deserialization message, with the following properties. Note this message is not
            // yet a real MQTT message, because not everything is implemented, but it must be modified
            // and ammeneded as work progresses
            //
            // Message Specs________________
            // <20><02><00><00>
            var sampleMessage = new[]
            {
                (byte)0x20,
                (byte)0x02,
                (byte)0x0,
                (byte)0x1,
            };

            MqttMessage baseMessage = MqttMessage.CreateFrom(sampleMessage);

            Console.WriteLine(baseMessage.ToString());

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

            // validate the message deserialization
            Assert.Equal <bool>(false, message.Header.Duplicate);
            Assert.Equal <bool>(false, message.Header.Retain);
            Assert.Equal <MqttQos>(MqttQos.AtMostOnce, message.Header.Qos);
            Assert.Equal <MqttMessageType>(MqttMessageType.ConnectAck, message.Header.MessageType);
            Assert.Equal <int>(2, message.Header.MessageSize);

            // validate the variable header
            //Assert.Equal<int>(30, message.VariableHeader.KeepAlive);
            Assert.Equal <MqttConnectReturnCode>(MqttConnectReturnCode.UnacceptedProtocolVersion, message.VariableHeader.ReturnCode);
        }
Beispiel #6
0
        public void MessageReceived(byte[] message)
        {
            MqttMessage incoming = MqttMessage.CreateFrom(message);

            switch (incoming.Header.MessageType)
            {
            }
        }
Beispiel #7
0
        public void Payload_InvalidClientIdenfierLength()
        {
            // Our test deserialization message, with the following properties. Note this message is not
            // yet a real MQTT message, because not everything is implemented, but it must be modified
            // and ammeneded as work progresses
            //
            // Message Specs________________
            // <10><15><00><06>MQIsdp<03><02><00><1E><00><07>andy111andy111andy111andy111
            var sampleMessage = new[]
            {
                (byte)0x10,
                (byte)0x15,
                (byte)0x0,
                (byte)0x6,
                (byte)'M',
                (byte)'Q',
                (byte)'I',
                (byte)'s',
                (byte)'d',
                (byte)'p',
                (byte)0x3,
                (byte)0x2,
                (byte)0x0,
                (byte)0x1E,
                (byte)0x0,
                (byte)0x1C,
                (byte)'a',
                (byte)'n',
                (byte)'d',
                (byte)'y',
                (byte)'1',
                (byte)'1',
                (byte)'1',
                (byte)'a',
                (byte)'n',
                (byte)'d',
                (byte)'y',
                (byte)'1',
                (byte)'1',
                (byte)'1',
                (byte)'a',
                (byte)'n',
                (byte)'d',
                (byte)'y',
                (byte)'1',
                (byte)'1',
                (byte)'1',
                (byte)'a',
                (byte)'n',
                (byte)'d',
                (byte)'y',
                (byte)'1',
                (byte)'1',
                (byte)'1'
            };

            Assert.Throws <ClientIdentifierException>(() => MqttMessage.CreateFrom(sampleMessage));
        }
Beispiel #8
0
        public void Deserialize_Message_MessageType_UnsubscribeAck_ValidPayload()
        {
            // Message Specs________________
            // <B0><02><00><04> (Subscribe ack for message id 4)
            var sampleMessage = new[]
            {
                (byte)0xFF,
                (byte)0x02,
                (byte)0x0,
                (byte)0x4,
            };

            Assert.Throws <InvalidMessageException>(() => MqttMessage.CreateFrom(sampleMessage));
        }
        public void SuccessfullResponseCausesConnectionStateConnected()
        {
            // register a method to process the Connect message and respond with a ConnectAck message
            broker.SetMessageHandler((messageArrived) =>
            {
                MqttConnectMessage connect = (MqttConnectMessage)MqttMessage.CreateFrom(messageArrived);
                MqttConnectAckMessage ack  = new MqttConnectAckMessage().WithReturnCode(MqttConnectReturnCode.ConnectionAccepted);
                broker.SendMessage(ack);
            });

            var ch = new SynchronousMqttConnectionHandler();

            Assert.Equal <ConnectionState>(ConnectionState.Connected,
                                           ch.Connect(mockBrokerAddress, mockBrokerPort, new MqttConnectMessage().WithClientIdentifier(testClientId)));
        }
Beispiel #10
0
        void PubSubWebSocketServer_NewDataReceived(WebSocketSession session, byte[] value)
        {
            MqttMessage incoming = MqttMessage.CreateFrom(value);

            switch (incoming.Header.MessageType)
            {
            case MqttMessageType.Publish:
                PublishReceived(session, (MqttPublishMessage)incoming);
                break;

            case MqttMessageType.Subscribe:
                SubscribeReceived(session, (MqttSubscribeMessage)incoming);
                break;

            case MqttMessageType.Unsubscribe:
                RemoveSubscriptions(session, ((MqttUnsubscribeMessage)incoming).Payload.Subscriptions);
                var unsubAck = new MqttUnsubscribeAckMessage().WithMessageIdentifier(((MqttPublishReceivedMessage)incoming).VariableHeader.MessageIdentifier);
                using (var messageStream = new MemoryStream())
                {
                    unsubAck.WriteTo(messageStream);
                    ArraySegment <byte> bytes = new ArraySegment <byte>(messageStream.ToArray());
                    session.Send(bytes);
                }
                break;

            case MqttMessageType.PublishAck:
                //string messageId = session.PublishAcknowledged(((MqttPublishAckMessage)incoming).VariableHeader.MessageIdentifier);
                //if (messageId != null)
                //    storageProvider.ReleaseMessage(messageId);
                break;

            case MqttMessageType.PublishReceived:
                //messageId = session.PublishReceived(((MqttPublishReceivedMessage)incoming).VariableHeader.MessageIdentifier);
                //if (messageId != null)
                //    storageProvider.ReleaseMessage(messageId);
                break;

            case MqttMessageType.PublishRelease:
                //session.RemoveQoS2(((MqttPublishReleaseMessage)incoming).VariableHeader.MessageIdentifier);
                //var pubComp = new MqttPublishCompleteMessage().WithMessageIdentifier(((MqttPublishReleaseMessage)incoming).VariableHeader.MessageIdentifier);
                //session.Write(pubComp);
                break;

            case MqttMessageType.PublishComplete:
                //session.PublishCompleted(((MqttPublishCompleteMessage)incoming).VariableHeader.MessageIdentifier);
                break;
            }
        }
Beispiel #11
0
        public void BasicDeserialization()
        {
            // Message Specs________________
            // <C0><00>
            var sampleMessage = new[]
            {
                (byte)0xC0,
                (byte)0x00,
            };

            MqttMessage baseMessage = MqttMessage.CreateFrom(sampleMessage);

            Console.WriteLine(baseMessage.ToString());

            // check that the message was correctly identified as a connect message.
            Assert.IsType <MqttPingRequestMessage>(baseMessage);
        }
Beispiel #12
0
        public void MessageReceived(byte[] message)
        {
            try
            {
                MqttMessage incoming = MqttMessage.CreateFrom(message);
                switch (incoming.Header.MessageType)
                {
                case MqttMessageType.SubscribeAck:
                    break;

                default:
                    break;
                }
            }
            catch (InvalidMessageException ex)
            {
            }
        }
Beispiel #13
0
 public virtual void Route(byte[] messageData, T session)
 {
     try
     {
         MqttMessage msg = MqttMessage.CreateFrom(messageData);
     }
     catch (TargetInvocationException ex)
     {
         var inner = ex.InnerException as HttpException;
         session.Write(inner != null ? new ErrorMessage(inner.GetHttpCode(), inner.Message) : new ErrorMessage((ex.InnerException ?? ex).Message));
     }
     catch (HttpException ex)
     {
         session.Write(new ErrorMessage(ex.Message));
     }
     catch (Exception ex)
     {
         session.Write(new ErrorMessage(ex.Message));
     }
 }
Beispiel #14
0
        public void ValidPayload()
        {
            // Message Specs________________
            // <30><0C><00><04>fredhello!
            var sampleMessage = new[]
            {
                (byte)0x30,
                (byte)0x0C,
                (byte)0x0,
                (byte)0x4,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
                // message payload is here
                (byte)'h',
                (byte)'e',
                (byte)'l',
                (byte)'l',
                (byte)'o',
                (byte)'!',
            };

            MqttMessage baseMessage = MqttMessage.CreateFrom(sampleMessage);

            Console.WriteLine(baseMessage.ToString());

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

            // validate the message deserialization
            Assert.Equal <bool>(false, message.Header.Duplicate);
            Assert.Equal <bool>(false, message.Header.Retain);
            Assert.Equal <MqttQos>(MqttQos.AtMostOnce, message.Header.Qos);
            Assert.Equal <MqttMessageType>(MqttMessageType.Publish, message.Header.MessageType);
            Assert.Equal <int>(12, message.Header.MessageSize);

            // make sure the publish message length matches the expectred size.
            Assert.Equal <int>(6, message.Payload.Message.Count);
        }
Beispiel #15
0
        public void PayloadTooShort()
        {
            // Message Specs________________
            // <30><0C><00><04>fredhello!
            var sampleMessage = new[]
            {
                (byte)0x30,
                (byte)0x0C,
                (byte)0x0,
                (byte)0x4,
                (byte)'f',
                (byte)'r',
                (byte)'e',
                (byte)'d',
                // message payload is here
                (byte)'h',
                (byte)'e',
                (byte)'l',
                (byte)'l',
                (byte)'o',
            };

            Assert.Throws <InvalidPayloadSizeException>(() => MqttMessage.CreateFrom(sampleMessage));
        }
Beispiel #16
0
        public void BasicDeserialization()
        {
            // Our test deserialization message, with the following properties. Note this message is not
            // yet a real MQTT message, because not everything is implemented, but it must be modified
            // and ammeneded as work progresses
            //
            // Message Specs________________
            // <10><15><00><06>MQIsdp<03><02><00><1E><00><07>andy111
            var sampleMessage = new[]
            {
                (byte)0x10,
                (byte)0x1B,
                (byte)0x0,
                (byte)0x6,
                (byte)'M',
                (byte)'Q',
                (byte)'I',
                (byte)'s',
                (byte)'d',
                (byte)'p',
                (byte)0x3,
                (byte)0x2E,
                (byte)0x0,
                (byte)0x1E,
                (byte)0x0,
                (byte)0x7,
                (byte)'a',
                (byte)'n',
                (byte)'d',
                (byte)'y',
                (byte)'1',
                (byte)'1',
                (byte)'1',
                (byte)0x00,
                (byte)0x01,
                (byte)'m',
                (byte)0x00,
                (byte)0x01,
                (byte)'a'
            };

            MqttMessage baseMessage = MqttMessage.CreateFrom(sampleMessage);

            Console.WriteLine(baseMessage.ToString());

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

            // validate the message deserialization
            Assert.Equal <bool>(false, message.Header.Duplicate);
            Assert.Equal <bool>(false, message.Header.Retain);
            Assert.Equal <MqttQos>(MqttQos.AtMostOnce, message.Header.Qos);
            Assert.Equal <MqttMessageType>(MqttMessageType.Connect, message.Header.MessageType);
            Assert.Equal <int>(27, message.Header.MessageSize);

            // validate the variable header
            Assert.Equal <string>("MQIsdp", message.VariableHeader.ProtocolName);
            Assert.Equal <int>(30, message.VariableHeader.KeepAlive);
            Assert.Equal <int>(3, message.VariableHeader.ProtocolVersion);
            Assert.True(message.VariableHeader.ConnectFlags.CleanStart);
            Assert.True(message.VariableHeader.ConnectFlags.WillFlag);
            Assert.True(message.VariableHeader.ConnectFlags.WillRetain);
            Assert.Equal <MqttQos>(MqttQos.AtLeastOnce, message.VariableHeader.ConnectFlags.WillQos);

            // payload tests
            Assert.Equal <string>("andy111", message.Payload.ClientIdentifier);
            Assert.Equal <string>("m", message.Payload.WillTopic);
            Assert.Equal <string>("a", message.Payload.WillMessage);
        }
Beispiel #17
0
        public virtual void Route(byte[] messageData, T session)
        {
            try
            {
                MqttMessage incoming = MqttMessage.CreateFrom(messageData);
                switch (incoming.Header.MessageType)
                {
                case MqttMessageType.Publish:
                    PublishReceived(session, (MqttPublishMessage)incoming);
                    break;

                case MqttMessageType.Subscribe:
                    SubscribeReceived(session, (MqttSubscribeMessage)incoming);
                    break;

                case MqttMessageType.Unsubscribe:
                    RemoveSubscriptions(session, ((MqttUnsubscribeMessage)incoming).Payload.Subscriptions);
                    var unsubAck = new MqttUnsubscribeAckMessage().WithMessageIdentifier(((MqttPublishReceivedMessage)incoming).VariableHeader.MessageIdentifier);
                    session.Write(unsubAck);
                    break;

                case MqttMessageType.PublishAck:
                    string messageId = session.PublishAcknowledged(((MqttPublishAckMessage)incoming).VariableHeader.MessageIdentifier);
                    if (messageId != null)
                    {
                        storageProvider.ReleaseMessage(messageId);
                    }
                    break;

                case MqttMessageType.PublishReceived:
                    messageId = session.PublishReceived(((MqttPublishReceivedMessage)incoming).VariableHeader.MessageIdentifier);
                    if (messageId != null)
                    {
                        storageProvider.ReleaseMessage(messageId);
                    }
                    break;

                case MqttMessageType.PublishRelease:
                    session.RemoveQoS2(((MqttPublishReleaseMessage)incoming).VariableHeader.MessageIdentifier);
                    var pubComp = new MqttPublishCompleteMessage().WithMessageIdentifier(((MqttPublishReleaseMessage)incoming).VariableHeader.MessageIdentifier);
                    session.Write(pubComp);
                    break;

                case MqttMessageType.PublishComplete:
                    session.PublishCompleted(((MqttPublishCompleteMessage)incoming).VariableHeader.MessageIdentifier);
                    break;
                }
            }
            catch (TargetInvocationException ex)
            {
                var inner = ex.InnerException as HttpException;
                session.Write(inner != null ? new ErrorMessage(inner.GetHttpCode(), inner.Message) : new ErrorMessage((ex.InnerException ?? ex).Message));
            }
            catch (HttpException ex)
            {
                session.Write(new ErrorMessage(ex.Message));
            }
            catch (Exception ex)
            {
                session.Write(new ErrorMessage(ex.Message));
            }
        }