public void when_getting_client_flow_from_valid_packet_type_then_succeeds(MqttPacketType packetType, Type flowType)
        {
            ClientProtocolFlowProvider flowProvider = new ClientProtocolFlowProvider(Mock.Of <IMqttTopicEvaluator>(), Mock.Of <IRepositoryProvider>(), new MqttConfiguration());

            IProtocolFlow flow = flowProvider.GetFlow(packetType);

            flowType.Should().Be(flow.GetType());
        }
Exemple #2
0
        IFormatter GetFormatter(MqttPacketType packetType, Type type)
        {
            var genericType   = typeof(EmptyPacketFormatter <>);
            var formatterType = genericType.MakeGenericType(type);
            var formatter     = Activator.CreateInstance(formatterType, packetType) as IFormatter;

            return(formatter);
        }
        public void when_getting_server_flow_from_valid_packet_type_then_succeeds(MqttPacketType packetType, Type flowType)
        {
            IMqttAuthenticationProvider authenticationProvider = Mock.Of <IMqttAuthenticationProvider>(p => p.Authenticate(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()) == true);
            ServerProtocolFlowProvider  flowProvider           = new ServerProtocolFlowProvider(authenticationProvider, Mock.Of <IConnectionProvider>(), Mock.Of <IMqttTopicEvaluator>(),
                                                                                                Mock.Of <IRepositoryProvider>(), Mock.Of <IPacketIdProvider>(), Mock.Of <ISubject <MqttUndeliveredMessage> >(), new MqttConfiguration());

            IProtocolFlow flow = flowProvider.GetFlow(packetType);

            flowType.Should().Be(flow.GetType());
        }
Exemple #4
0
        public void when_reading_invalid_empty_packet_then_fails(string packetPath, MqttPacketType packetType, Type type)
        {
            packetPath = Path.Combine(Environment.CurrentDirectory, packetPath);

            var formatter = GetFormatter(packetType, type);
            var packet    = Packet.ReadAllBytes(packetPath);

            var ex = Assert.Throws <AggregateException> (() => formatter.FormatAsync(packet).Wait());

            Assert.True(ex.InnerException is MqttException);
        }
Exemple #5
0
        public async Task when_writing_empty_packet_then_succeeds(string packetPath, MqttPacketType packetType, Type type)
        {
            packetPath = Path.Combine(Environment.CurrentDirectory, packetPath);

            byte[]     expectedPacket = Packet.ReadAllBytes(packetPath);
            IFormatter formatter      = GetFormatter(packetType, type);
            IPacket    packet         = Activator.CreateInstance(type) as IPacket;

            byte[] result = await formatter.FormatAsync(packet);

            expectedPacket.Should().BeEquivalentTo(result);
        }
Exemple #6
0
        public async Task when_reading_empty_packet_then_succeeds(string packetPath, MqttPacketType packetType, Type type)
        {
            packetPath = Path.Combine(Environment.CurrentDirectory, packetPath);

            IFormatter formatter = GetFormatter(packetType, type);

            byte[] packet = Packet.ReadAllBytes(packetPath);

            IPacket result = await formatter.FormatAsync(packet);

            Assert.NotNull(result);
        }
Exemple #7
0
 protected override bool IsValidPacketType(MqttPacketType packetType)
 {
     return(packetType == MqttPacketType.ConnectAck ||
            packetType == MqttPacketType.SubscribeAck ||
            packetType == MqttPacketType.UnsubscribeAck ||
            packetType == MqttPacketType.Publish ||
            packetType == MqttPacketType.PublishAck ||
            packetType == MqttPacketType.PublishComplete ||
            packetType == MqttPacketType.PublishReceived ||
            packetType == MqttPacketType.PublishRelease ||
            packetType == MqttPacketType.PingResponse);
 }
Exemple #8
0
        public async Task when_reading_empty_packet_then_succeeds(string packetPath, MqttPacketType packetType, Type type)
        {
            packetPath = Path.Combine(Environment.CurrentDirectory, packetPath);

            var formatter = GetFormatter(packetType, type);
            var packet    = Packet.ReadAllBytes(packetPath);

            var result = await formatter.FormatAsync(packet)
                         .ConfigureAwait(continueOnCapturedContext: false);

            Assert.NotNull(result);
        }
        public async Task <IPacket> GetPacketAsync(byte[] bytes)
        {
            MqttPacketType packetType = (MqttPacketType)bytes.Byte(0).Bits(4);

            if (!formatters.TryGetValue(packetType, out IFormatter formatter))
            {
                throw new MqttException(ClientProperties.PacketManager_PacketUnknown);
            }

            IPacket packet = await formatter.FormatAsync(bytes);

            return(packet);
        }
Exemple #10
0
        public async Task when_writing_empty_packet_then_succeeds(string packetPath, MqttPacketType packetType, Type type)
        {
            packetPath = Path.Combine(Environment.CurrentDirectory, packetPath);

            var expectedPacket = Packet.ReadAllBytes(packetPath);
            var formatter      = GetFormatter(packetType, type);
            var packet         = Activator.CreateInstance(type) as IPacket;

            var result = await formatter.FormatAsync(packet)
                         .ConfigureAwait(continueOnCapturedContext: false);

            Assert.Equal(expectedPacket, result);
        }
        public async Task <IPacket> FormatAsync(byte[] bytes)
        {
            MqttPacketType actualType = (MqttPacketType)bytes.Byte(0).Bits(4);

            if (PacketType != actualType)
            {
                throw new MqttException(ClientProperties.Formatter_InvalidPacket(typeof(T).Name));
            }

            T packet = await Task.Run(() => Read( bytes ));

            return(packet);
        }
        byte[] GetFixedHeader( MqttPacketType packetType, byte[] remainingLength )
        {
            // MQTT 2.2.2: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/csprd02/mqtt-v3.1.1-csprd02.html#_Toc385349758
            // The flags for PUBREL are different than for the other flow packets.
            int flags = packetType == MqttPacketType.PublishRelease ? 0x02 : 0x00;
            int type = Convert.ToInt32( packetType ) << 4;
            byte fixedHeaderByte1 = Convert.ToByte( flags | type );
            byte[] fixedHeader = new byte[1 + remainingLength.Length];

            fixedHeader[0] = fixedHeaderByte1;
            remainingLength.CopyTo( fixedHeader, 1 );

            return fixedHeader;
        }
        public IProtocolFlow GetFlow(MqttPacketType packetType)
        {
            if (!IsValidPacketType(packetType))
            {
                throw new MqttException(ClientProperties.ProtocolFlowProvider_InvalidPacketType(packetType));
            }

            ProtocolFlowType flowType = packetType.ToFlowType();


            if (!GetFlows().TryGetValue(flowType, out IProtocolFlow flow))
            {
                throw new MqttException(ClientProperties.ProtocolFlowProvider_UnknownPacketType(packetType));
            }

            return(flow);
        }
        internal static ProtocolFlowType ToFlowType(this MqttPacketType packetType)
        {
            var flowType = default(ProtocolFlowType);

            switch (packetType)
            {
            case MqttPacketType.Connect:
            case MqttPacketType.ConnectAck:
                flowType = ProtocolFlowType.Connect;
                break;

            case MqttPacketType.Publish:
            case MqttPacketType.PublishRelease:
                flowType = ProtocolFlowType.PublishReceiver;
                break;

            case MqttPacketType.PublishAck:
            case MqttPacketType.PublishReceived:
            case MqttPacketType.PublishComplete:
                flowType = ProtocolFlowType.PublishSender;
                break;

            case MqttPacketType.Subscribe:
            case MqttPacketType.SubscribeAck:
                flowType = ProtocolFlowType.Subscribe;
                break;

            case MqttPacketType.Unsubscribe:
            case MqttPacketType.UnsubscribeAck:
                flowType = ProtocolFlowType.Unsubscribe;
                break;

            case MqttPacketType.PingRequest:
            case MqttPacketType.PingResponse:
                flowType = ProtocolFlowType.Ping;
                break;

            case MqttPacketType.Disconnect:
                flowType = ProtocolFlowType.Disconnect;
                break;
            }

            return(flowType);
        }
        public IProtocolFlow GetFlow(MqttPacketType packetType)
        {
            if (!IsValidPacketType(packetType))
            {
                var error = string.Format(Properties.Resources.ProtocolFlowProvider_InvalidPacketType, packetType);

                throw new MqttException(error);
            }

            var flow     = default(IProtocolFlow);
            var flowType = packetType.ToFlowType();

            if (!GetFlows().TryGetValue(flowType, out flow))
            {
                var error = string.Format(Properties.Resources.ProtocolFlowProvider_UnknownPacketType, packetType);

                throw new MqttException(error);
            }

            return(flow);
        }
Exemple #16
0
        private static MqttPacket Acquire(MqttPacketType type, IClient client)
        {
            switch (type)
            {
            case MqttPacketType.Connect: return(MqttConnectPacket.Acquire());

            case MqttPacketType.Subscribe: return(MqttSubscribePacket.Acquire());

            case MqttPacketType.Unsubscribe: return(MqttUnsubscribePacket.Acquire());

            case MqttPacketType.PingReq: return(MqttPingReqPacket.Acquire());

            case MqttPacketType.Disconnect: return(MqttDisconnectPacket.Acquire());

            case MqttPacketType.Publish: return(MqttPublishPacket.Acquire());

            case MqttPacketType.PubAck: return(MqttPubackPacket.Acquire());

            default:
                Service.Logger.Log("Unknown MQTT Type: " + type);
                return(null);
            }
        }
Exemple #17
0
 public static string ServerPacketListener_DispatchingMessage(MqttPacketType packetType, string flowName, string clientId) => $"Server - Dispatching {packetType} message to flow {flowName} for client {clientId}";
 public static string ProtocolFlowProvider_UnknownPacketType(MqttPacketType packetType) => $"An error occured while trying to get a Flow Type based on Packet Type {packetType}";
Exemple #19
0
        protected void RemovePendingAcknowledgement(string clientId, ushort packetId, MqttPacketType type)
        {
            var session = sessionRepository.Get(s => s.ClientId == clientId);

            if (session == null)
            {
                throw new MqttException(string.Format(Properties.Resources.SessionRepository_ClientSessionNotFound, clientId));
            }

            var pendingAcknowledgement = session
                                         .GetPendingAcknowledgements()
                                         .FirstOrDefault(u => u.Type == type && u.PacketId == packetId);

            session.RemovePendingAcknowledgement(pendingAcknowledgement);

            sessionRepository.Update(session);
        }
Exemple #20
0
 public EmptyPacketFormatter(MqttPacketType packetType)
 {
     this.packetType = packetType;
 }
 public static string ClientPacketListener_DispatchingMessage(string clientId, MqttPacketType packetType, string flowTypeName) => $"Client {clientId} - Dispatching {packetType} message to flow {flowTypeName}";
 public static string ClientPacketListener_FirstPacketReceived(string clientId, MqttPacketType packetType) => $"Client {clientId} - First packet from Server has been received. Type: {packetType}";
 public static string PublishFlow_RetryingQoSFlow(MqttPacketType type, string clientId) => $"The ack for message {type} has not been received. Re sending message for client {clientId}";
 public static string ProtocolFlowProvider_InvalidPacketType(MqttPacketType packetType) => $"The packet type {packetType} cannot be handled by this flow provider";
 protected abstract bool IsValidPacketType(MqttPacketType packetType);
 public FlowPacketFormatter( MqttPacketType packetType, Func<ushort, T> packetFactory )
 {
     _packetType = packetType;
     _packetFactory = packetFactory;
 }
        public async Task when_managing_packet_from_source_then_succeeds(string packetPath, string jsonPath, Type packetType, MqttPacketType type)
        {
            jsonPath = Path.Combine(Environment.CurrentDirectory, jsonPath);

            IPacket packet = Packet.ReadPacket(jsonPath, packetType) as IPacket;

            packetPath = Path.Combine(Environment.CurrentDirectory, packetPath);

            byte[]            bytes     = Packet.ReadAllBytes(packetPath);
            Mock <IFormatter> formatter = new Mock <IFormatter>();

            formatter.Setup(f => f.PacketType).Returns(type);

            formatter
            .Setup(f => f.FormatAsync(It.Is <IPacket>(p => Convert.ChangeType(p, packetType) == packet)))
            .Returns(Task.FromResult(bytes));

            PacketManager packetManager = new PacketManager(formatter.Object);

            byte[] result = await packetManager.GetBytesAsync(packet);

            bytes.Should().BeEquivalentTo(result);
        }
        protected void RemovePendingAcknowledgement(string clientId, ushort packetId, MqttPacketType type)
        {
            ClientSession session = sessionRepository.Read(clientId);

            if (session == null)
            {
                throw new MqttException(ClientProperties.SessionRepository_ClientSessionNotFound(clientId));
            }

            PendingAcknowledgement pendingAcknowledgement = session
                                                            .GetPendingAcknowledgements()
                                                            .FirstOrDefault(u => u.Type == type && u.PacketId == packetId);

            session.RemovePendingAcknowledgement(pendingAcknowledgement);

            sessionRepository.Update(session);
        }
        public async Task when_managing_packet_bytes_then_succeeds(string packetPath, string jsonPath, Type packetType, MqttPacketType type)
        {
            packetPath = Path.Combine(Environment.CurrentDirectory, packetPath);

            byte[] bytes = Packet.ReadAllBytes(packetPath);

            jsonPath = Path.Combine(Environment.CurrentDirectory, jsonPath);

            object            packet    = Packet.ReadPacket(jsonPath, packetType);
            Mock <IFormatter> formatter = new Mock <IFormatter>();

            formatter.Setup(f => f.PacketType).Returns(type);

            formatter
            .Setup(f => f.FormatAsync(It.Is <byte[]>(b => b.ToList().SequenceEqual(bytes))))
            .Returns(Task.FromResult <IPacket>((IPacket)packet));

            PacketManager packetManager = new PacketManager(formatter.Object);
            IPacket       result        = await packetManager.GetPacketAsync(bytes);

            packet.Should().Be(result);
        }
Exemple #30
0
        public async Task when_managing_packet_from_source_then_succeeds(string packetPath, string jsonPath, Type packetType, MqttPacketType type)
        {
            jsonPath = Path.Combine(Environment.CurrentDirectory, jsonPath);

            var packet = Packet.ReadPacket(jsonPath, packetType) as IPacket;

            packetPath = Path.Combine(Environment.CurrentDirectory, packetPath);

            var bytes     = Packet.ReadAllBytes(packetPath);
            var formatter = new Mock <IFormatter> ();

            formatter.Setup(f => f.PacketType).Returns(type);

            formatter
            .Setup(f => f.FormatAsync(It.Is <IPacket> (p => Convert.ChangeType(p, packetType) == packet)))
            .Returns(Task.FromResult(bytes));

            var packetManager = new PacketManager(formatter.Object);
            var result        = await packetManager.GetBytesAsync(packet)
                                .ConfigureAwait(continueOnCapturedContext: false);

            Assert.Equal(bytes, result);
        }