Beispiel #1
0
        public void ProcessUnsuback(Int32 packetID)
        {
            SNMessage message = _timers.Remove(packetID);

            if (message != null)
            {
                SNUnsubscribe unsubscribe = (SNUnsubscribe)message;
                String        topicName   = String.Empty;
                if (unsubscribe.SnTopic is IdentifierTopic)
                {
                    topicName = mappedTopics[((IdentifierTopic)unsubscribe.SnTopic).Value];
                }
                else
                {
                    topicName = ((FullTopic)unsubscribe.SnTopic).Value;
                }

                _dbInterface.DeleteTopic(topicName);
            }

            if (_listener != null)
            {
                _listener.MessageReceived(MessageType.UNSUBACK);
            }
        }
 public void Unsubscribe(String[] topics)
 {
     foreach (String topic in topics)
     {
         if (_usedIncomingMappings.ContainsKey(topic))
         {
             AMQPDetach detach = new AMQPDetach();
             detach.Channel = _channel;
             detach.Closed  = true;
             detach.Handle  = _usedIncomingMappings[topic];
             _client.Send(detach);
         }
         else
         {
             _dbInterface.DeleteTopic(topic);
             if (_listener != null)
             {
                 _listener.MessageReceived(MessageType.UNSUBACK);
             }
         }
     }
 }
        public void ProcessUnsuback(Int32 packetID)
        {
            MQMessage message = _timers.Remove(packetID);

            if (message != null)
            {
                Unsubscribe unsubscribe = (Unsubscribe)message;
                String[]    topics      = unsubscribe.Topics;
                foreach (String topic in topics)
                {
                    _dbInterface.DeleteTopic(topic);
                }
            }

            if (_listener != null)
            {
                _listener.MessageReceived(MessageType.UNSUBACK);
            }
        }
        public void PacketReceived(CoapMessage message)
        {
            CoapType type = message.CoapType;

            if ((message.CoapCode == CoapCode.POST || message.CoapCode == CoapCode.PUT) && type != CoapType.ACKNOWLEDGEMENT)
            {
                String topic = null;
                QOS    qos   = QOS.AT_MOST_ONCE;
                foreach (CoapOption option in message.Options)
                {
                    if (option.Number == (int)CoapOptionType.URI_PATH)
                    {
                        topic = Encoding.UTF8.GetString(option.Value);
                    }
                    else if (option.Number == (int)CoapOptionType.ACCEPT)
                    {
                        qos = (QOS)option.Value[option.Value.Length - 1];
                    }
                }

                if (topic == null)
                {
                    List <CoapOption> options   = new List <CoapOption>();
                    byte[]            textBytes = Encoding.UTF8.GetBytes("text/plain");
                    options.Add(new CoapOption((int)CoapOptionType.CONTENT_FORMAT, textBytes.Length, textBytes));
                    byte[] nodeIdBytes = Encoding.UTF8.GetBytes(_clientID);
                    options.Add(new CoapOption((int)CoapOptionType.NODE_ID, nodeIdBytes.Length, nodeIdBytes));
                    CoapMessage ack = new CoapMessage(VERSION, CoapType.ACKNOWLEDGEMENT, CoapCode.BAD_OPTION, message.MessageID, message.Token, options, new byte[0]);
                    _client.Send(ack);
                    return;
                }

                byte[] content = message.Payload;
                _dbInterface.StoreMessage(topic, content, 0);

                if (_listener != null)
                {
                    _listener.MessageReceived(MessageType.PUBLISH);
                }
            }

            switch (type)
            {
            case CoapType.CONFIRMABLE:
            {
                List <CoapOption> options     = new List <CoapOption>();
                byte[]            nodeIdBytes = Encoding.UTF8.GetBytes(_clientID);
                options.Add(new CoapOption((int)CoapOptionType.NODE_ID, nodeIdBytes.Length, nodeIdBytes));
                CoapMessage ack = new CoapMessage(message.Version, CoapType.ACKNOWLEDGEMENT, message.CoapCode, message.MessageID, message.Token, options, new byte[0]);
                _client.Send(ack);
            }
            break;

            case CoapType.NON_CONFIRMABLE:
            {
                _timers.Remove(message.Token);
            }
            break;

            case CoapType.ACKNOWLEDGEMENT:
            {
                if (message.CoapCode == CoapCode.GET)
                {
                    Boolean?observe = null;
                    QOS     qos     = QOS.AT_MOST_ONCE;
                    foreach (CoapOption option in message.Options)
                    {
                        if (option.Number == (int)CoapOptionType.OBSERVE && option.Value.Length > 0)
                        {
                            if (option.Value[option.Value.Length - 1] == 0x00)
                            {
                                observe = false;
                            }
                            else
                            {
                                observe = true;
                            }
                        }
                        else if (option.Number == (int)CoapOptionType.ACCEPT)
                        {
                            if (option.Value[option.Value.Length - 1] == 0)
                            {
                                qos = QOS.AT_LEAST_ONCE;
                            }
                        }
                    }

                    if (observe.HasValue)
                    {
                        if (!observe.Value)
                        {
                            CoapMessage originalMessage = _timers.Remove(message.Token);
                            if (originalMessage != null)
                            {
                                List <String> topics = new List <String>();
                                foreach (CoapOption option in originalMessage.Options)
                                {
                                    if (option.Number == (int)CoapOptionType.URI_PATH)
                                    {
                                        topics.Add(Encoding.UTF8.GetString(option.Value));
                                    }
                                }

                                for (int i = 0; i < topics.Count; i++)
                                {
                                    _dbInterface.StoreTopic(topics[i], qos);
                                }

                                if (_listener != null)
                                {
                                    _listener.MessageReceived(MessageType.SUBACK);
                                }
                            }
                        }
                        else
                        {
                            CoapMessage originalMessage = _timers.Remove(message.Token);
                            if (originalMessage != null)
                            {
                                List <String> topics = new List <String>();
                                foreach (CoapOption option in originalMessage.Options)
                                {
                                    if (option.Number == (int)CoapOptionType.URI_PATH)
                                    {
                                        topics.Add(Encoding.UTF8.GetString(option.Value));
                                    }
                                }

                                for (int i = 0; i < topics.Count; i++)
                                {
                                    _dbInterface.DeleteTopic(topics[i]);
                                }
                            }

                            if (_listener != null)
                            {
                                _listener.MessageReceived(MessageType.UNSUBACK);
                            }
                        }
                    }
                }
                else
                {
                    _timers.Remove(message.Token);
                    if (_listener != null)
                    {
                        _listener.MessageReceived(MessageType.PUBACK);
                    }
                }
            }
            break;

            case CoapType.RESET:
            {
                _timers.Remove(message.Token);
            }
            break;
            }
        }