Ejemplo n.º 1
0
        public bool RemoveSubscriber(SubscriptionInfo subscriptionInfo, bool clientDisconnected = false)
        {
            bool result = false;

            lock (_mutex)
            {
                ClientSubscriptionManager clientSubscriptionManger = null;
                if (_subscribers.TryGetValue(subscriptionInfo.ClientId, out clientSubscriptionManger))
                {
                    if (clientDisconnected || subscriptionInfo.SubscriptionId == null)
                    {
                        clientSubscriptionManger.ClientDisconnected(subscriptionInfo);
                        result = true;
                    }
                    else
                    {
                        result = clientSubscriptionManger.RemoveSubscription(subscriptionInfo);
                    }

                    if (!clientSubscriptionManger.HasActiveSubscriptions)
                    {
                        result |= _subscribers.Remove(subscriptionInfo.ClientId);
                        clientSubscriptionManger.Dispose();
                    }
                }
            }

            if (_listener != null && result)
            {
                _listener.OnSubscriptionRemoved(this);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public void CreateSubscription(SubscriptionInfo subscriptionInfo, bool isUserOperation = true)
        {
            SubscriptionIdentifier    subscriptionIdentifier   = new SubscriptionIdentifier(subscriptionInfo.SubscriptionId, subscriptionInfo.SubPolicyType);
            ClientSubscriptionManager clientSubscriptionManger = null;


            if (AddSubscription(subscriptionInfo, subscriptionIdentifier, isUserOperation))
            {
                lock (_mutex)
                {
                    if (subscriptionInfo.SubPolicyType != SubscriptionPolicyType.EventSubscription)
                    {
                        _subscriptionLoadBalancer.Add(subscriptionIdentifier);
                    }

                    //while state transfer if any durable subscription has no active subscriber
                    if (subscriptionInfo.ClientId != null)
                    {
                        if (!_subscribers.TryGetValue(subscriptionInfo.ClientId, out clientSubscriptionManger))
                        {
                            clientSubscriptionManger = new ClientSubscriptionManager(this, subscriptionInfo.ClientId);
                            _subscribers.Add(subscriptionInfo.ClientId, clientSubscriptionManger);
                        }

                        clientSubscriptionManger.AddSubscription(subscriptionInfo);
                    }
                }

                if (_listener != null)
                {
                    _listener.OnSubscriptionCreated(this);
                }
            }
        }
Ejemplo n.º 3
0
        public void RevokeAssignment(MessageInfo messageInfo, SubscriptionInfo subscriptionInfo)
        {
            bool messageRequed = false;

            ClientSubscriptionManager client = null;

            lock (_mutex)
            {
                Message message = null;
                if (!_messages.TryGetValue(messageInfo.MessageId, out message))
                {
                    throw new Exception("Message with given id does not exist");
                }

                if (subscriptionInfo == null) //revoke all subscriptions
                {
                    message.MessageMetaData.RevokeSubscriptions();
                }
                else
                {
                    if (_subscriptions.TryGetValue(subscriptionInfo.ClientId, out client))
                    {
                        message.MessageMetaData.RevokeSubscriptions(client);
                    }
                }

                messageRequed = RequeMessage(message);
            }

            if (messageRequed && _listener != null)
            {
                _listener.OnMessageArrived(this);
            }
        }
Ejemplo n.º 4
0
        public SubscriptionInfo GetSubscriberForAssignment(SubscriptionType type, OperationContext context)
        {
            lock (_mutex)
            {
                if (_subscriptionLoadBalancer.IsValid)
                {
                    string firstselectedClientId = null;
                    string selectedClient        = null;
                    while (true)
                    {
                        selectedClient = _subscriptionLoadBalancer.GetNext();

                        //breaks the loop
                        if (firstselectedClientId != null && string.Compare(selectedClient, firstselectedClientId, true) == 0)
                        {
                            return(null);
                        }

                        if (firstselectedClientId == null)
                        {
                            firstselectedClientId = selectedClient;
                        }
                        ClientSubscriptionManager clientManager = _subscriptions[selectedClient];

                        if (clientManager.IsActive)
                        {
                            return(clientManager.GetNextAvailableSubscription(type));
                        }
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 5
0
 public void RefreshSubscription(SubscriptionInfo subscriptionInfo)
 {
     lock (_mutex)
     {
         ClientSubscriptionManager clientSubscriptionManger = null;
         if (_subscribers.TryGetValue(subscriptionInfo.ClientId, out clientSubscriptionManger))
         {
             clientSubscriptionManger.Refesh();
         }
     }
 }
Ejemplo n.º 6
0
        public IList <object> GetAssignedMessages(SubscriptionInfo subscriptionInfo)
        {
            IList <object>            messages = null;
            ClientSubscriptionManager client   = null;

            lock (_mutex)
            {
                if (_subscribers.TryGetValue(subscriptionInfo.ClientId, out client))
                {
                    messages = client.GetAssignedMessages();
                }
            }

            return(messages);
        }
Ejemplo n.º 7
0
        private string GetActiveSubscriber(Subscriptions subscription)
        {
            string activeSbuscriber = null;

            string subscriber      = null;
            string firstSubscriber = null;

            ClientSubscriptionManager client = null;

            while (activeSbuscriber == null)
            {
                subscriber = subscription.GetSubscriber();

                if (firstSubscriber == null)
                {
                    if (subscriber == null)
                    {
                        break;
                    }
                    firstSubscriber = subscriber;
                }

                if (subscriber != null && _subscribers.TryGetValue(subscriber, out client))
                {
                    if (client.IsActive)
                    {
                        activeSbuscriber = subscriber;
                        break;
                    }
                }

                if (subscriber == firstSubscriber)
                {
                    break;
                }
            }

            if (activeSbuscriber == null && firstSubscriber != null)
            {
                activeSbuscriber = firstSubscriber;
            }

            return(activeSbuscriber);
        }
Ejemplo n.º 8
0
        public void RevokeAssignment(MessageInfo messageInfo, SubscriptionInfo subscriptionInfo)
        {
            bool messageRequed = false;

            ClientSubscriptionManager client = null;

            lock (_mutex)
            {
                Message message = null;

                if (!_messages.TryGetValue(messageInfo.MessageId, out message))
                {
                    return;
                }

                if (subscriptionInfo == null)     //revoke all subscriptions
                {
                    message.MessageMetaData.RevokeSubscriptions();
                }
                else
                {
                    if (_subscribers.TryGetValue(subscriptionInfo.ClientId, out client))
                    {
                        message.MessageMetaData.RevokeSubscriptions(client);
                    }
                }

                SubscriptionIdentifier subscriptonId = null;
                if (subscriptionInfo != null)
                {
                    subscriptonId = new SubscriptionIdentifier();
                    subscriptonId.SubscriptionName   = subscriptionInfo.SubscriptionId;
                    subscriptonId.SubscriptionPolicy = subscriptionInfo.SubPolicyType;
                }

                messageRequed = RequeMessage(message, subscriptonId);
            }

            if (messageRequed && _listener != null)
            {
                _listener.OnMessageArrived(this);
            }
        }
Ejemplo n.º 9
0
        public void CreateSubscription(SubscriptionInfo subscriptionInfo)
        {
            lock (_mutex)
            {
                ClientSubscriptionManager clientSubscriptionManger = null;
                if (!_subscriptions.TryGetValue(subscriptionInfo.ClientId, out clientSubscriptionManger))
                {
                    clientSubscriptionManger = new ClientSubscriptionManager(this, subscriptionInfo.ClientId);
                    _subscriptions.Add(subscriptionInfo.ClientId, clientSubscriptionManger);
                    _subscriptionLoadBalancer.Add(subscriptionInfo.ClientId);
                }

                clientSubscriptionManger.AddSubscription(subscriptionInfo);
            }

            if (_listener != null)
            {
                _listener.OnSubscriptionCreated(this);
            }
        }