Esempio n. 1
0
        void Client_MqttMsgSubscribeReceived(object sender, MqttMsgSubscribeEventArgs e)
        {
            MqttClient client = (MqttClient)sender;

            for (int i = 0; i < e.Topics.Length; i++)
            {
                // TODO : business logic to grant QoS levels based on some conditions ?
                //        now the broker granted the QoS levels requested by client

                // subscribe client for each topic and QoS level requested
                this.subscriberManager.Subscribe(e.Topics[i], e.QoSLevels[i], client);
            }

            try
            {
                // send SUBACK message to the client
                client.Suback(e.MessageId, e.QoSLevels);

                for (int i = 0; i < e.Topics.Length; i++)
                {
                    // publish retained message on the current subscription
                    this.publisherManager.PublishRetaind(e.Topics[i], client.ClientId);
                }
            }
            catch (MqttCommunicationException)
            {
                this.CloseClient(client);
            }
        }
Esempio n. 2
0
        void Client_MqttMsgSubscribeReceived(object sender, MqttMsgSubscribeEventArgs e)
        {
            MqttClient client = (MqttClient)sender;

            // Since a client may subscribe to multiple topics, we must check each one
            var topicAuth = new Dictionary <string, bool>();

            for (int i = 0; i < e.Topics.Length; i++)
            {
                // Authenticate this client & topic.
                var verified = this.uacManager.PubSubAuthentication(false, client.ClientId, e.Topics[i]);
                topicAuth[e.Topics[i]] = verified;
                if (verified)
                {
                    // TODO : business logic to grant QoS levels based on some conditions ?
                    //        now the broker granted the QoS levels requested by client

                    // subscribe client for each topic and QoS level requested
                    this.subscriberManager.Subscribe(e.Topics[i], e.QoSLevels[i], client);
                }
            }

            bool closeClient = false;

            try
            {
                // If any have been allowed, send the suback
                if (topicAuth.Any(x => x.Value))
                {
                    // send SUBACK message to the client
                    client.Suback(e.MessageId, e.QoSLevels);

                    for (int i = 0; i < e.Topics.Length; i++)
                    {
                        // Send any retained message to the subscriber if verified
                        var verified = topicAuth[e.Topics[i]];
                        if (verified)
                        {
                            // publish retained message on the current subscription
                            this.publisherManager.PublishRetaind(e.Topics[i], client.ClientId);
                        }
                    }
                }
                else
                {
                    closeClient = true;
                }
            }
            catch (MqttCommunicationException)
            {
                closeClient = true;
            }

            if (closeClient)
            {
                this.CloseClient(client);
            }
        }
Esempio n. 3
0
        void Client_MqttMsgSubscribeReceived(object sender, MqttMsgSubscribeEventArgs e)
        {
            MqttClient client = (MqttClient)sender;
            //client.Subscribe(e.Topics, e.QoSLevels);

            int i = 0;

            //for (int i = 0; i < e.Topics.Length; i++)
            //{
            commBroker = new CommBroker();
            commBroker.connect(e.Topics[i]);
            if (!commBroker.connected)
            {
                throw new Exception("Unable to connect the broker for subscribing message");
            }
            commBroker.disable_heartbeat = true;
            MqttCommon.LogMessageToFile("Subscribed to:" + e.Topics[i].Replace("/", ".").Replace("+", "*").ToString());
            MqttCommon.LogMessageToFile("Topics Subscribed :" + e.Topics.Length.ToString());
            Subscription subscription = commBroker.new_subscription(e.Topics[i].Replace("/", ".").Replace("+", "*"), string.Empty, (message, context) =>
            {
                client.Publish(message.subject.Replace(".", "/"), Convert.FromBase64String(message.data.ToString()));   // System.Text.Encoding.UTF8.GetBytes(message.data.ToString()));
                Console.WriteLine("Message Received");
                MqttCommon.LogMessageToFile("Message Received");
                Console.WriteLine("From Topic:" + message.subject.Replace(".", "/") + ". Data:" + message.data.ToString());
                MqttCommon.LogMessageToFile("From Topic:" + message.subject.Replace(".", "/") + ". Data:" + message.data.ToString());
            }, null, commBroker, string.Empty);

            // TODO : business logic to grant QoS levels based on some conditions ?
            //        now the broker granted the QoS levels requested by client

            // subscribe client for each topic and QoS level requested
            //this.subscriberManager.Subscribe(e.Topics[i].Replace("/", ".").Replace("+", "*"), e.QoSLevels[i], client);
            //}

            try
            {
                // send SUBACK message to the client
                client.Suback(e.MessageId, e.QoSLevels);

                //for (int i = 0; i < e.Topics.Length; i++)
                //{
                //    // publish retained message on the current subscription
                //    //this.publisherManager.PublishRetaind(e.Topics[i], client.ClientId);
                //}
            }
            catch (MqttCommunicationException)
            {
                this.CloseClient(client);
            }
        }
Esempio n. 4
0
 public virtual void OnSubscribe(MqttClient client, MqttMsgSubscribeEventArgs message)
 {
     Composable.GetExport <IXLogger>().Verbose("Mqtt Subscribe {@m}", message);
     return;
 }