public int Add(SubscriptionItem item)
        {
            if (Contains(item))
            {
                throw new ArgumentException("An item with this topic name already exists.");
            }

            return _list.Add(item);
        }
        public int Add(SubscriptionItem item)
        {
            if (Contains(item))
            {
                throw new ArgumentException("An item with this topic name already exists.");
            }

            return(_list.Add(item));
        }
Exemplo n.º 3
0
 internal SubscriptionClient(MqttClientProtocol mqttClient, SubscriptionItem subscription, string clientUid)
 {
     _mqtt = mqttClient;
     _mqtt.SubscribeComplete += MqttOnSubscribeComplete;
     _mqtt.SendMessageComplete += MqttOnOperationComplete;
     _mqtt.ConnectComplete += MqttOnConnectComplete;
     _subscription = subscription;
     _clientUid = clientUid;
     _disposed = false;
 }
        public void OnMessageCallbackGetsCalledWithWildcardTopics1()
        {
            var are = new AutoResetEvent(false);
            var ip = "1.1.1.1";
            var port = 1883;
            var eventData = "Test data";
            string connectionKey = "123";
            var moqSocket = new MoqSocket();
            var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr = new MqttConnectMessageBuilder
            {
                ClientId = "UnitTest"
            };

            protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);

            var subscriptionItem = new SubscriptionItem
            {
                TopicName = "a/b/+",
                QualityOfService = QualityOfService.AtMostOnce
            };
            var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey);

            subClient.OnMessage(msg =>
            {
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.MessageType == MessageType.Publish);
                Assert.AreEqual(msg.Payload[0], 0x00);
                Assert.AreEqual(msg.Payload[1], 0x01);
                Assert.AreEqual(msg.Payload[2], 0x02);
                are.Set();
            });

            moqSocket.ReceiveMessage(new MqttPublishMessageBuilder
            {
                TopicName = "a/b/c",
                Payload = new byte[] { 0x00, 0x01, 0x02 }
            });

            if (!are.WaitOne(5000))
            {
                Assert.Fail("OnMessage callback not called.");
            }
        }
        public void OnMessageCallbackDoesNotGetCalledAfterClose()
        {
            var are = new AutoResetEvent(false);
            var ip = "1.1.1.1";
            var port = 1883;
            var eventData = "Test data";
            string connectionKey = "123";
            var moqSocket = new MoqSocket();
            var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr = new MqttConnectMessageBuilder
            {
                ClientId = "UnitTest"
            };

            protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);

            var subscriptionItem = new SubscriptionItem
            {
                TopicName = "a/b/c",
                QualityOfService = QualityOfService.AtMostOnce
            };
            var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey);

            subClient.OnMessage(msg =>
            {
                Assert.Fail("OnMessage callback was called after Close call.");
                are.Set();
            });
            
            subClient.Close();

            moqSocket.ReceiveMessage(new MqttPublishMessageBuilder
            {
                TopicName = "a/b/c",
                Payload = new byte[] { 0x00, 0x01, 0x02 }
            });

            are.WaitOne(3000);
        }
        public void IsClosedCorrectlyReflectsStateOfObject()
        {
            var ip = "1.1.1.1";
            var port = 1883;
            var eventData = "Test data";
            string connectionKey = "123";
            var moqSocket = new MoqSocket();
            var protocol = new MqttClientProtocol(new LogCompositor(), moqSocket);
            var bldr = new MqttConnectMessageBuilder
            {
                ClientId = "UnitTest"
            };

            protocol.ConnectAsync(bldr, ip, port, SocketEncryption.None, eventData);

            var subscriptionItem = new SubscriptionItem
            {
                TopicName = "a/b/+",
                QualityOfService = QualityOfService.AtMostOnce
            };

            var subClient = new SubscriptionClient(protocol, subscriptionItem, connectionKey);

            Assert.IsTrue(subClient.IsClosed);

            subClient.OnMessage(msg =>
            {
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.MessageType == MessageType.Publish);
            });

            Assert.IsFalse(subClient.IsClosed);
            subClient.Close();
            Assert.IsTrue(subClient.IsClosed);
        }
Exemplo n.º 7
0
        private Task<MqttSubscribeAckMessage> SubscribeAsync(SubscriptionItem[] items, int messageId)
        {
            var msgBuilder = new MqttSubscribeMessageBuilder
            {
                MessageId = messageId
            };

            foreach (var item in items)
            {
                msgBuilder.Subscriptions.Add(item);
            }

            var tcs = new TaskCompletionSource<MqttSubscribeAckMessage>();
            _mqtt.SendMessageAsync(msgBuilder, tcs, _clientUid);
            return tcs.Task;
        }
Exemplo n.º 8
0
 /// <summary>
 /// Create a subscriber client on the specified topic.
 /// </summary>
 /// <param name="subscription"></param>
 /// <returns></returns>
 public SubscriptionClient CreateSubscription(SubscriptionItem subscription)
 {
     var subClient = new SubscriptionClient(_mqtt, subscription, _clientUid);
     return subClient;
 }
 public int IndexOf(SubscriptionItem item)
 {
     return _list.IndexOf(item);
 }
Exemplo n.º 10
0
        private void ReadPayload()
        {
            lock (_msg.SyncLock)
            {
                if (!_msg.PayloadRead)
                {
                    int pos = ReadVariableHeader();

                    while (pos < _msg.MsgBuffer.Length)
                    {
                        var subscription = new SubscriptionItem
                        {
                            TopicName = Frame.DecodeString(_msg.MsgBuffer, ref pos),
                            QualityOfService = (QualityOfService)_msg.MsgBuffer[pos++]
                        };
                        _subscriptionItems.Add(subscription);
                    }

                    _msg.PayloadRead = true;
                }
            }
        }
 public void Remove(SubscriptionItem item)
 {
     _list.Remove(item);
 }
 public int IndexOf(SubscriptionItem item)
 {
     return(_list.IndexOf(item));
 }
 public bool Contains(SubscriptionItem item)
 {
     return(_list.Contains(item));
 }
 public void Remove(SubscriptionItem item)
 {
     _list.Remove(item);
 }
 public bool Contains(SubscriptionItem item)
 {
     return _list.Contains(item);
 }
Exemplo n.º 16
0
        public void SubscribeAsync(SubscriptionItem[] items, int messageId)
        {
            var msgBuilder = new MqttSubscribeMessageBuilder
            {
                MessageId = messageId,
                QualityOfService = QualityOfService.AtLeastOnce
            };

            foreach (var item in items)
            {
                msgBuilder.Subscriptions.Add(item);
            }

            SendMessageAsync(msgBuilder);
        }
        //private readonly MqttClient _mqtt;
        //private readonly SubscriptionItem _subscription;
        //private int _subMessageId;
        //private int _unsubMessageId;

        //public event MqttPublishMessageEventHandler OnMessage;
        //public event MqttMessageEventHandler CloseComplete;

        internal SubscriptionClient(MqttClient mqtt, SubscriptionItem subscription)
        {
        }