Exemple #1
0
        public void TestTopicTree()
        {
            //因为树节点的测试类似,树从根开始,仅测试不同部分
            //测试无节点时的树根
            TopicTree <int> topicTree = new TopicTree <int>();

            Assert.Equal(".", topicTree.rootNode.Nodevalue);
            List <int> matchlist = topicTree.CollectMatches("#");

            Assert.Empty(matchlist);

            topicTree.Add(new Topic("work"), 20);

            topicTree.Add(new Topic("sport/ball/football"), 5);
            topicTree.Add(new Topic("sport/ball/longrun"), 6);
            topicTree.Add(new Topic("sport/run/longrun"), 8);
            topicTree.Add(new Topic("sport/run/shortrun"), 10);
            matchlist = topicTree.CollectMatches("#");
            Assert.Equal(5, matchlist.Count);
            matchlist = topicTree.CollectMatches("sport/#");
            Assert.Equal(4, matchlist.Count);
            matchlist = topicTree.CollectMatches("sport/+/longrun");
            Assert.Equal(2, matchlist.Count);

            topicTree.RemoveAll();
            matchlist = topicTree.CollectMatches("#");
            Assert.Empty(matchlist);
        }
Exemple #2
0
        protected void OnPublishArrived(MqttPublishMessage m)
        {
            bool accepted = false;

            if (PublishArrived != null)
            {
                PublishArrivedArgs e = new PublishArrivedArgs(m.Topic, m.Payload, m.Retained, m.QualityOfService);
                try
                {
                    accepted |= PublishArrived(this, e);
                }
                catch (Exception ex)
                {
                    MqttLib.Logger.Log.Write(LogLevel.ERROR, "MqttLib: Uncaught exception from user delegate: " + ex.ToString());
                }
            }

            if (topicTree != null)
            {
                PublishArrivedArgs            e           = new PublishArrivedArgs(m.Topic, m.Payload, m.Retained, m.QualityOfService);
                List <PublishArrivedDelegate> subscribers = topicTree.CollectMatches(new Topic(m.Topic));
                foreach (PublishArrivedDelegate pad in subscribers)
                {
                    try
                    {
                        accepted |= pad(this, e);
                    }
                    catch (Exception ex)
                    {
                        MqttLib.Logger.Log.Write(LogLevel.ERROR, "MqttLib: Uncaught exception from user delegate: " + ex.ToString());
                    }
                }
            }

            if (m.QualityOfService > QoS.BestEfforts)
            {
                qosManager.PublishAccepted(m.MessageID, accepted);
            }
        }