private static void SubscribeWithWildcard(string topic, byte qosLevel, MqttConnection connection)
        {
            var topicReplaced = topic.Replace(PLUS_WILDCARD, PLUS_WILDCARD_REPLACE)
                                .Replace(SHARP_WILDCARD, SHARP_WILDCARD_REPLACE);

            var subscriptionsForTopic = WildcardSubscriptions.GetOrAdd(topicReplaced, new List <MqttSubscription>());

            lock (subscriptionsForTopic)
            {
                if (!AlreadySubscribed(connection.ClientId, subscriptionsForTopic))
                {
                    var subscription = new MqttSubscription()
                    {
                        ClientId   = connection.ClientId,
                        Topic      = topicReplaced,
                        QosLevel   = qosLevel,
                        Connection = connection
                    };

                    subscriptionsForTopic.Add(subscription);

                    connection.Subscriptions.TryAdd(topicReplaced, subscription);
                }
            }
        }
        private void ClientsForSessionThread()
        {
            while (isRunning)
            {
                try
                {
                    string clientId = this.clientsForSession.Take();

                    MqttBrokerSession session = MqttSessionManager.GetSession(clientId);

                    MqttMsgPublish outgoingMsg;
                    while (session.OutgoingMessages.TryDequeue(out outgoingMsg))
                    {
                        var query = from s in session.Subscriptions
                                    where (new Regex(s.Topic)).IsMatch(outgoingMsg.Topic)
                                    // check for topics based also on wildcard with regex
                                    select s;

                        MqttSubscription subscription = query.FirstOrDefault();

                        if (subscription != null)
                        {
                            var qosLevel = (subscription.QosLevel < outgoingMsg.QosLevel) ? subscription.QosLevel : outgoingMsg.QosLevel;
                            MqttMessageToClientConnectionManager.Publish(subscription.ClientConnection, outgoingMsg.Topic, outgoingMsg.Message, qosLevel, outgoingMsg.Retain);
                        }
                    }
                }
                catch (Exception exception)
                {
                    logger.LogException(this, exception);
                }
            }
        }
        CheckSubscriptionsResult CheckSubscriptions(string topic, MqttQualityOfServiceLevel applicationMessageQoSLevel, string senderClientId)
        {
            ulong topicHashMask; // not needed
            bool  hasWildcard;   // not needed

            MqttSubscription.CalculateTopicHash(topic, out var topicHash, out topicHashMask, out hasWildcard);
            return(_subscriptionsManager.CheckSubscriptions(topic, topicHash, applicationMessageQoSLevel, senderClientId));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Publish retained message for a topic to a client
        /// </summary>
        /// <param name="topic">Topic to search for a retained message</param>
        /// <param name="clientId">Client Id to send retained message</param>
        public static void PublishRetaind(string topic, MqttClientConnection clientConnection)
        {
            MqttSubscription subscription = MqttSubscriberManager.GetSubscription(topic, clientConnection);

            // add subscription to list of subscribers for receiving retained messages
            if (subscription != null)
            {
                SubscribersForRetained.Add(subscription);
            }
        }
        private static void SubscribeWithoutWildcard(string topic, byte qosLevel, MqttConnection connection)
        {
            var subscriptionsForTopic = NonWildcardSubscriptions.GetOrAdd(topic, new List <MqttSubscription>());

            lock (subscriptionsForTopic)
            {
                if (!AlreadySubscribed(connection.ClientId, subscriptionsForTopic))
                {
                    var subscription = new MqttSubscription()
                    {
                        ClientId   = connection.ClientId,
                        Topic      = topic,
                        QosLevel   = qosLevel,
                        Connection = connection
                    };

                    subscriptionsForTopic.Add(subscription);

                    connection.Subscriptions.TryAdd(topic, subscription);
                }
            }
        }
Exemplo n.º 6
0
        static void CompareAndAssert(string topic, string filter, MqttTopicFilterCompareResult expectedResult)
        {
            Assert.AreEqual(expectedResult, MqttTopicFilterComparer.Compare(topic, filter));

            ulong topicHash;
            ulong topicHashMask;
            bool  topicHasWildcard;

            MqttSubscription.CalculateTopicHash(topic, out topicHash, out topicHashMask, out topicHasWildcard);


            ulong filterTopicHash;
            ulong filterTopicHashMask;
            bool  filterTopicHasWildcard;

            MqttSubscription.CalculateTopicHash(filter, out filterTopicHash, out filterTopicHashMask, out filterTopicHasWildcard);

            if (expectedResult == MqttTopicFilterCompareResult.IsMatch)
            {
                // If it matches then the hash evaluation should also indicate a match
                Assert.IsTrue((topicHash & filterTopicHashMask) == filterTopicHash, "Incorrect topic hash (is equal)");
            }
        }