示例#1
0
        /// <summary>
        /// Creates an MQTT Client
        /// </summary>
        /// <param name="configuration">
        /// The configuration used for creating the Client
        /// See <see cref="MqttConfiguration" /> for more details about the supported values
        /// </param>
        /// <returns>A new MQTT Client</returns>
        /// <exception cref="MqttClientException">MqttClientException</exception>
        public async Task <IMqttClient> CreateClientAsync(MqttConfiguration configuration)
        {
            try
            {
                //Adding this to not break backwards compatibility related to the method signature
                //Yielding at this point will cause the method to return immediately after it's called,
                //running the rest of the logic acynchronously
                await Task.Yield();

                MqttTopicEvaluator         topicEvaluator      = new MqttTopicEvaluator(configuration);
                IMqttChannelFactory        innerChannelFactory = _binding.GetChannelFactory(_hostAddress, configuration);
                PacketChannelFactory       channelFactory      = new PacketChannelFactory(innerChannelFactory, topicEvaluator, configuration);
                PacketIdProvider           packetIdProvider    = new PacketIdProvider();
                InMemoryRepositoryProvider repositoryProvider  = new InMemoryRepositoryProvider();
                ClientProtocolFlowProvider flowProvider        = new ClientProtocolFlowProvider(topicEvaluator, repositoryProvider, configuration);

                return(new MqttClientImpl(channelFactory, flowProvider, repositoryProvider, packetIdProvider, configuration));
            }
            catch (Exception ex)
            {
                _tracer.Error(ex, ClientProperties.Client_InitializeError);

                throw new MqttClientException(ClientProperties.Client_InitializeError, ex);
            }
        }
示例#2
0
        public async Task <IMqttConnectedClient> CreateClientAsync(MqttConfiguration configuration)
        {
            try
            {
                //Adding this to not break backwards compatibility related to the method signature
                //Yielding at this point will cause the method to return immediately after it's called,
                //running the rest of the logic acynchronously
                await Task.Yield();

                var binding             = new PrivateBinding(privateStreamListener, EndpointIdentifier.Client);
                var topicEvaluator      = new MqttTopicEvaluator(configuration);
                var innerChannelFactory = binding.GetChannelFactory(IPAddress.Loopback.ToString(), configuration);
                var channelFactory      = new PacketChannelFactory(innerChannelFactory, topicEvaluator, configuration);
                var packetIdProvider    = new PacketIdProvider();
                var repositoryProvider  = new InMemoryRepositoryProvider();
                var flowProvider        = new ClientProtocolFlowProvider(topicEvaluator, repositoryProvider, configuration);

                return(new MqttConnectedClient(channelFactory, flowProvider, repositoryProvider, packetIdProvider, configuration));
            }
            catch (Exception ex)
            {
                tracer.Error(ex, Properties.Resources.Client_InitializeError);

                throw new MqttClientException(Properties.Resources.Client_InitializeError, ex);
            }
        }
        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());
        }
        public void when_getting_explicit_client_flow_from_type_then_succeeds()
        {
            ClientProtocolFlowProvider flowProvider = new ClientProtocolFlowProvider(Mock.Of <IMqttTopicEvaluator>(), Mock.Of <IRepositoryProvider>(), new MqttConfiguration());

            ClientConnectFlow     connectFlow     = flowProvider.GetFlow <ClientConnectFlow>();
            PublishSenderFlow     senderFlow      = flowProvider.GetFlow <PublishSenderFlow>();
            PublishReceiverFlow   receiverFlow    = flowProvider.GetFlow <PublishReceiverFlow>();
            ClientSubscribeFlow   subscribeFlow   = flowProvider.GetFlow <ClientSubscribeFlow>();
            ClientUnsubscribeFlow unsubscribeFlow = flowProvider.GetFlow <ClientUnsubscribeFlow>();
            PingFlow disconnectFlow = flowProvider.GetFlow <PingFlow>();

            Assert.NotNull(connectFlow);
            Assert.NotNull(senderFlow);
            Assert.NotNull(receiverFlow);
            Assert.NotNull(subscribeFlow);
            Assert.NotNull(unsubscribeFlow);
            Assert.NotNull(disconnectFlow);
        }
示例#5
0
        /// <summary>
        /// Creates an MQTT Client
        /// </summary>
        /// <param name="configuration">
        /// The configuration used for creating the Client
        /// See <see cref="MqttConfiguration" /> for more details about the supported values
        /// </param>
        /// <returns>A new MQTT Client</returns>
        /// <exception cref="MqttClientException">MqttClientException</exception>
        public async Task <IMqttClient> CreateClientAsync(MqttConfiguration configuration)
        {
            try {
                var topicEvaluator      = new MqttTopicEvaluator(configuration);
                var innerChannelFactory = binding.GetChannelFactory(hostAddress, configuration);
                var channelFactory      = new PacketChannelFactory(innerChannelFactory, topicEvaluator, configuration);
                var packetIdProvider    = new PacketIdProvider();
                var repositoryProvider  = new InMemoryRepositoryProvider();
                var flowProvider        = new ClientProtocolFlowProvider(topicEvaluator, repositoryProvider, configuration);
                var packetChannel       = await channelFactory
                                          .CreateAsync()
                                          .ConfigureAwait(continueOnCapturedContext: false);

                return(new MqttClientImpl(packetChannel, flowProvider, repositoryProvider, packetIdProvider, configuration));
            } catch (Exception ex) {
                tracer.Error(ex, Properties.Resources.Client_InitializeError);

                throw new MqttClientException(Properties.Resources.Client_InitializeError, ex);
            }
        }