Пример #1
0
        public void InitMqttConnetMessageTemp()
        {
            MqttConnectMessage mqttConnectMessage = new MqttConnectMessage("test", "sa", "123456", 255, false);

            Assert.Equal("test", mqttConnectMessage.ClientID);
            Assert.Equal("sa", mqttConnectMessage.UserName);
            Assert.Equal("123456", mqttConnectMessage.Password);
            Assert.Equal(255, mqttConnectMessage.KeepAlive);
            Assert.False(mqttConnectMessage.CleanSession);
        }
Пример #2
0
        public async Task HandleClientConnectedAsync(MqttServerClientConnectedEventArgs eventArgs)
        {
            var message = new MqttConnectMessage()
            {
                ClientId = eventArgs.ClientId
            };

            if (string.Equals(message.ClientId, _configuration.GetSection("MQTT").GetSection("MqttClientEnactorName").Value) == false)
            {
                await _endpoint.Request <HttpStatusCode>(message).ConfigureAwait(false);
            }
        }
        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)));
        }
Пример #4
0
        public void BasicSerialization()
        {
            MqttConnectMessage msg = new MqttConnectMessage()
                                     .WithClientIdentifier("mark")
                                     .KeepAliveFor(30)
                                     .StartClean();

            Console.WriteLine(msg);

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

            Assert.Equal <byte>(0x10, mb[0]);
            // VH will = 12, Msg = 6
            Assert.Equal <byte>(18, mb[1]);
        }
Пример #5
0
 private void DoConnect(MqttConnectMessage conmsg)
 {
     try
     {
         manager.Connect();
         manager.SendMessage(conmsg);
         manager.WaitForResponse();
         TimerCallback callback = new TimerCallback(tmrCallback);
         // TODO: Set Keep Alive interval and keepAlive time as property of client
         int keepAliveInterval = 1000 * _keepAlive;
         keepAliveTimer = new Timer(callback, null, keepAliveInterval, keepAliveInterval);
     }
     catch (Exception e)
     {
         throw new MqttBrokerUnavailableException("Unable to connect to the broker", e);
     }
 }
Пример #6
0
        public void InitMqttConnetMessage(string clientID, string userID, string password, ushort keepAlive,
                                          bool cleanSession)
        {
            MqttConnectMessage mqttConnectMessage = new MqttConnectMessage(clientID, userID, password, keepAlive, cleanSession);

            Assert.Equal(clientID, mqttConnectMessage.ClientID);
            Assert.Equal(userID, mqttConnectMessage.UserName);
            Assert.Equal(password, mqttConnectMessage.Password);
            Assert.Equal(keepAlive, mqttConnectMessage.KeepAlive);
            Assert.Equal(cleanSession, mqttConnectMessage.CleanSession);

            //获取消息类型
            Assert.Equal(MessageType.CONNECT, mqttConnectMessage.MsgType);
            //用户名位与密码位
            Assert.Equal(userID.Trim().Length > 0?true:false, mqttConnectMessage.ContainUsername);
            Assert.Equal(password.Trim().Length > 0?true:false, mqttConnectMessage.ContainPassword);
        }
Пример #7
0
        public void InitMqttConnetMessage2(string clientID, string username, string password, ushort keepAlive,
                                           string willTopic, byte[] willPayload,
                                           QoS willQos, bool willRetained, bool cleanStart)
        {
            MqttConnectMessage mqttConnectMessage = new MqttConnectMessage(clientID, username, password, keepAlive,
                                                                           willTopic, willPayload, willQos, willRetained, cleanStart);

            Assert.Equal(clientID, mqttConnectMessage.ClientID);
            Assert.Equal(username, mqttConnectMessage.UserName);
            Assert.Equal(password, mqttConnectMessage.Password);
            Assert.Equal(keepAlive, mqttConnectMessage.KeepAlive);
            Assert.Equal(cleanStart, mqttConnectMessage.CleanSession);

            Assert.Equal(MessageType.CONNECT, mqttConnectMessage.MsgType);
            Assert.Equal(username.Trim().Length > 0 ? true : false, mqttConnectMessage.ContainUsername);
            Assert.Equal(password.Trim().Length > 0 ? true : false, mqttConnectMessage.ContainPassword);

            Assert.Equal(willQos, mqttConnectMessage.QualityOfService);
            Assert.Equal(willRetained, mqttConnectMessage.Retained);
            Assert.Equal(willTopic, mqttConnectMessage.WillTopic);
            Assert.Equal(willPayload, mqttConnectMessage.WillPayload);
        }
Пример #8
0
        public void WithWillSet()
        {
            MqttConnectMessage msg = new MqttConnectMessage()
                                     .WithProtocolName("MQIsdp")
                                     .WithProtocolVersion(3)
                                     .WithClientIdentifier("mark")
                                     .KeepAliveFor(30)
                                     .StartClean()
                                     .Will()
                                     .WithWillQos(MqttQos.AtLeastOnce)
                                     .WithWillRetain()
                                     .WithWillTopic("willTopic")
                                     .WithWillMessage("willMessage");

            Console.WriteLine(msg);

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

            Assert.Equal <byte>(0x10, mb[0]);
            // VH will = 12, Msg = 6
            Assert.Equal <byte>(42, mb[1]);
        }
Пример #9
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);
        }
Пример #10
0
 protected string GenerateClientUid(MqttConnectMessage msg)
 {
     return(GenerateClientUid(msg.ClientId, msg.UserName));
 }