public SubscriptionInfo Unsubscribe(Topic topic, Subscriber subscriber)
        {
            topic.Requires("topic").IsNotNull();
            subscriber.Requires("subscriber").IsNotNull();

            if (isDisposed)
            {
                throw new ObjectDisposedException(MethodBase.GetCurrentMethod()?.DeclaringType?.Name,
                    "instance has been disposed");
            }

            var key = GetKey(topic, subscriber);
            SubscriptionInfo subscriptionInfo;
            if (subscriptions.TryRemove(key, out subscriptionInfo))
            {
                subscriptionInfo.CancelToken.Cancel();
                if (!subscriber.LongLived || resourceCounter.Decrement(subscriptionInfo.Subscriber.Name) == 0)
                {
                    amazonSnsFacade.UnsubscribeQueueFromTopic(subscriptionInfo.SubscriptionArn);
                }
                subscriptionInfo.CancelToken = null;
                subscriptionInfo.SubscriptionArn = null;
            }
            return subscriptionInfo;
        }
        public void Publish(Topic topic, string subject, string message)
        {
            topic.Requires().IsNotNull();
            subject.Requires().IsNotNullOrWhiteSpace();
            message.Requires().IsNotNullOrWhiteSpace();

            amazonSnsFacade.PublishMessageToTopic(topic.TopicArn, subject, message);
        }
        public bool IsSubscribed(Topic topic, Subscriber subscriber)
        {
            topic.Requires("topic").IsNotNull();
            subscriber.Requires("subscriber").IsNotNull();

            if (isDisposed)
            {
                throw new ObjectDisposedException(MethodBase.GetCurrentMethod()?.DeclaringType?.Name,
                    "instance has been disposed");
            }

            var key = GetKey(topic, subscriber);
            return subscriptions.ContainsKey(key);
        }
        public Topic GetTopic(string channelName)
        {
            channelName.Requires("channelName").IsNotNullOrWhiteSpace();

            var topicName = GetTopicName(channelName);
            var topicArn = amazonSnsFacade.CreateOrRetrieveTopic(topicName);
            if (topics.ContainsKey(channelName))
            {
                return topics[channelName];
            }
            var topic = new Topic(topicName, topicArn);
            topics[channelName] = topic;
            return topic;
        }
        public SubscriptionInfo Subscribe(Topic topic, Subscriber subscriber, Action<Message> messageHandler)
        {
            topic.Requires("topic").IsNotNull();
            subscriber.Requires("subscriber").IsNotNull();
            messageHandler.Requires("messageHandler").IsNotNull();

            if (isDisposed)
            {
                throw new ObjectDisposedException(MethodBase.GetCurrentMethod()?.DeclaringType?.Name,
                    "instance has been disposed");
            }

            var key = GetKey(topic, subscriber);
            return subscriptions.AddOrUpdate(key, k => AddSubscription(topic, subscriber, messageHandler),
                (x, s) => UpdateSubscription(s, messageHandler));
        }
        private SubscriptionInfo AddSubscription(Topic topic, Subscriber subscriber, Action<Message> messageHandler)
        {
            var subscriptionInfo = new SubscriptionInfo
            {
                Topic = topic,
                Subscriber = subscriber
            };

            amazonSqsFacade.SetSqsPolicyForSnsPublish(subscriber.QueueUrl, subscriber.QueueArn, topic.TopicArn);
            subscriptionInfo.SubscriptionArn =
                amazonSnsFacade.SubscribeQueueToTopic(subscriptionInfo.Subscriber.QueueArn,
                    subscriptionInfo.Topic.TopicArn);
            if (subscriptionInfo.Subscriber.LongLived)
            {
                resourceCounter.Increment(subscriptionInfo.Subscriber.Name);
            }
            subscriptionInfo.CancelToken = queuePoller.Start(subscriptionInfo, messageHandler);

            return subscriptionInfo;
        }
        public void Publish(Topic topic, string subject, string message)
        {
            topic.Requires("topic").IsNotNull();
            subject.Requires("subject").IsNotNullOrWhiteSpace();
            message.Requires("message").IsNotNullOrWhiteSpace();

            if (isDisposed)
            {
                throw new ObjectDisposedException(MethodBase.GetCurrentMethod()?.DeclaringType?.Name,
                    "instance has been disposed");
            }

            amazonSnsFacade.PublishMessageToTopic(topic.TopicArn, subject, message);
        }
 private static string GetKey(Topic topic, Subscriber subscriber)
 {
     return string.Concat(topic.Name, "-", subscriber.Name);
 }