예제 #1
0
        /// <inheritdoc cref="IMqttRepositoryGrain" />
        /// <summary>
        ///     Connects a broker to the grain.
        /// </summary>
        /// <param name="brokerConnectionSettings">The broker connection settings.</param>
        /// <param name="brokerId">The broker identifier.</param>
        /// <exception cref="ArgumentNullException">Thrown when the broker connection settings or the broker identifier is <c>null</c>.</exception>
        /// <seealso cref="IMqttRepositoryGrain" />
        public Task ConnectBroker(IBrokerConnectionSettings brokerConnectionSettings, Guid brokerId)
        {
            if (brokerConnectionSettings == null)
            {
#pragma warning disable IDE0016 // throw-Ausdruck verwenden
                throw new ArgumentNullException(nameof(brokerConnectionSettings));
#pragma warning restore IDE0016 // throw-Ausdruck verwenden
            }

            if (brokerId == null)
            {
                throw new ArgumentNullException(nameof(brokerId));
            }

            // Save connect to the database
            var eventLog = new EventLog
            {
                EventType    = EventType.BrokerConnect,
                EventDetails = $"New broker connected: BrokerId = {brokerId}."
            };

            this.eventLogQueue.Enqueue(eventLog);

            // Add to dictionary
            this.brokers[brokerId] = brokerConnectionSettings;

            return(Task.CompletedTask);
        }
예제 #2
0
        /// <inheritdoc cref="IMqttRepositoryGrain" />
        /// <summary>
        ///     Connects a broker to the grain.
        /// </summary>
        /// <param name="brokerConnectionSettings">The broker connection settings.</param>
        /// <param name="brokerId">The broker identifier.</param>
        /// <seealso cref="IMqttRepositoryGrain" />
        public async void ConnectBroker(IBrokerConnectionSettings brokerConnectionSettings, Guid brokerId)
        {
            try
            {
                // Save connect to the database
                var eventLog = new EventLog
                {
                    EventType    = EventType.BrokerConnect,
                    EventDetails = $"New broker connected: BrokerId = {brokerId}."
                };

                await this.eventLogRepository.InsertEventLog(eventLog);

                // Add to dictionary
                this.brokers[brokerId] = brokerConnectionSettings;
            }
            catch (Exception ex)
            {
                this.logger.Error("An error occured: {ex}.", ex);
            }
        }
예제 #3
0
        /// <summary>
        ///     Publishes a message to a remote broker that hasn't initially sent the message to the cluster.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="brokerConnectionSettings">The broker connection settings.</param>
        /// <returns>A <see cref="Task" /> representing asynchronous operation.</returns>
        private static async Task PublishMessageToBroker(MqttApplicationMessageInterceptorContext context, IBrokerConnectionSettings brokerConnectionSettings)
        {
            if (context.ApplicationMessage == null)
            {
                return;
            }

            // Create a new MQTT client
            var factory        = new MqttFactory();
            var mqttClient     = factory.CreateMqttClient();
            var optionsBuilder = new MqttClientOptionsBuilder().WithClientId(brokerConnectionSettings.ClientId).WithTcpServer(brokerConnectionSettings.HostName, brokerConnectionSettings.Port)
                                 .WithCredentials(brokerConnectionSettings.UserName, brokerConnectionSettings.Password).WithCleanSession(brokerConnectionSettings.UseCleanSession);

            if (brokerConnectionSettings.UseTls)
            {
                optionsBuilder.WithTls();
            }

            var options = optionsBuilder.Build();

            // Deserialize payload
            var payloadString = context.ApplicationMessage?.Payload == null ? string.Empty : Encoding.UTF8.GetString(context.ApplicationMessage.Payload);

            // Connect the MQTT client
            await mqttClient.ConnectAsync(options, CancellationToken.None);

            // Send the message
            var message = new MqttApplicationMessageBuilder().WithTopic(context.ApplicationMessage.Topic).WithPayload(payloadString).WithQualityOfServiceLevel(context.ApplicationMessage.QualityOfServiceLevel)
                          .WithRetainFlag(context.ApplicationMessage.Retain).Build();

            await mqttClient.PublishAsync(message, CancellationToken.None);

            await mqttClient.DisconnectAsync(null, CancellationToken.None);
        }