Esempio n. 1
0
                public LookupInfo(ISubscriptionMessage criteria)
                {
                    if (criteria == null)
                    {
                        throw new ArgumentNullException(nameof(criteria));
                    }

                    Criteria = (ISubscriptionMessage)criteria.Clone();
                }
        private bool ProcessInSubscriptionMessage(ISubscriptionMessage message,
                                                  DataType dataType, SecurityId securityId = default)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (dataType == null)
            {
                throw new ArgumentNullException(nameof(dataType));
            }

            var transId = message.TransactionId;

            var isSubscribe = message.IsSubscribe;

            ISubscriptionMessage sendInMsg = null;

            Message[] sendOutMsgs = null;

            lock (_sync)
            {
                if (isSubscribe)
                {
                    if (message.To == null)
                    {
                        var key = Tuple.Create(dataType, securityId);

                        if (!_subscriptionsByKey.TryGetValue(key, out var info))
                        {
                            sendInMsg = message;

                            info = new SubscriptionInfo((ISubscriptionMessage)message.Clone());

                            _subscriptionsByKey.Add(key, info);
                        }
                        else
                        {
                            var resultMsg = message.CreateResult();

                            if (message.Type == MessageTypes.MarketData)
                            {
                                sendOutMsgs = new[]
                                {
                                    message.CreateResponse(),
                                        resultMsg,
                                };
                            }
                            else
                            {
                                sendOutMsgs = new[] { resultMsg };
                            }
                        }

                        _subscriptionsById.Add(transId, info);
                        info.Subscribers.Add(transId);
                    }
                    else
                    {
                        sendInMsg = message;
                    }
                }
                else
                {
                    ISubscriptionMessage MakeUnsubscribe(ISubscriptionMessage m, long subscriptionId)
                    {
                        m.IsSubscribe           = false;
                        m.TransactionId         = transId;
                        m.OriginalTransactionId = subscriptionId;

                        return(m);
                    }

                    var originId = message.OriginalTransactionId;

                    if (_subscriptionsById.TryGetValue(originId, out var info))
                    {
                        if (!info.Subscribers.Remove(originId))
                        {
                            sendOutMsgs = new[]
                            {
                                (Message)originId.CreateSubscriptionResponse(new InvalidOperationException(LocalizedStrings.SubscriptionNonExist.Put(originId)))
                            };
                        }
                        else
                        {
                            if (info.Subscribers.Count == 0)
                            {
                                _subscriptionsByKey.RemoveByValue(info);
                                _subscriptionsById.Remove(originId);

                                if (info.State.IsActive())
                                {
                                    // copy full subscription's details into unsubscribe request
                                    sendInMsg = MakeUnsubscribe((ISubscriptionMessage)info.Subscription.Clone(), info.Subscription.TransactionId);
                                }
                                else
                                {
                                    this.AddWarningLog(LocalizedStrings.SubscriptionInState, originId, info.State);
                                }
                            }
                            else
                            {
                                sendOutMsgs = new[] { message.CreateResult() };
                            }
                        }
                    }
                    else
                    {
                        sendOutMsgs = new[]
                        {
                            (Message)originId.CreateSubscriptionResponse(new InvalidOperationException(LocalizedStrings.SubscriptionNonExist.Put(originId)))
                        };
                    }
                }
            }

            var retVal = true;

            if (sendInMsg != null)
            {
                this.AddInfoLog("In: {0}", sendInMsg);
                retVal = base.OnSendInMessage((Message)sendInMsg);
            }

            if (sendOutMsgs != null)
            {
                foreach (var sendOutMsg in sendOutMsgs)
                {
                    this.AddInfoLog("Out: {0}", sendOutMsg);
                    RaiseNewOutMessage(sendOutMsg);
                }
            }

            return(retVal);
        }
        private bool ProcessInSubscriptionMessage(ISubscriptionMessage message,
                                                  DataType dataType, SecurityId securityId = default)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (dataType == null)
            {
                throw new ArgumentNullException(nameof(dataType));
            }

            var transId = message.TransactionId;

            var isSubscribe = message.IsSubscribe;

            ISubscriptionMessage sendInMsg = null;

            Message[] sendOutMsgs = null;

            lock (_sync)
            {
                if (isSubscribe)
                {
                    if (_replaceId.ContainsKey(transId))
                    {
                        sendInMsg = message;
                    }
                    else
                    {
                        var clone = (ISubscriptionMessage)message.Clone();

                        if (message.To != null)
                        {
                            _historicalRequests.Add(transId, clone);
                        }
                        else
                        {
                            _subscriptionsById.Add(transId, new SubscriptionInfo(clone));
                        }

                        sendInMsg = message;
                    }
                }
                else
                {
                    ISubscriptionMessage MakeUnsubscribe(ISubscriptionMessage m)
                    {
                        m = (ISubscriptionMessage)m.Clone();

                        m.IsSubscribe           = false;
                        m.OriginalTransactionId = m.TransactionId;
                        m.TransactionId         = transId;

                        return(m);
                    }

                    var originId = message.OriginalTransactionId;

                    if (_historicalRequests.TryGetAndRemove(originId, out var subscription))
                    {
                        sendInMsg = MakeUnsubscribe(subscription);
                    }
                    else if (_subscriptionsById.TryGetValue(originId, out var info))
                    {
                        if (info.State.IsActive())
                        {
                            // copy full subscription's details into unsubscribe request
                            sendInMsg = MakeUnsubscribe(info.Subscription);
                        }
                        else
                        {
                            this.AddWarningLog(LocalizedStrings.SubscriptionInState, originId, info.State);
                        }
                    }
                    else
                    {
                        sendOutMsgs = new[]
                        {
                            (Message)originId.CreateSubscriptionResponse(new InvalidOperationException(LocalizedStrings.SubscriptionNonExist.Put(originId)))
                        };
                    }
                }
            }

            var retVal = true;

            if (sendInMsg != null)
            {
                this.AddInfoLog("In: {0}", sendInMsg);
                retVal = base.OnSendInMessage((Message)sendInMsg);
            }

            if (sendOutMsgs != null)
            {
                foreach (var sendOutMsg in sendOutMsgs)
                {
                    this.AddInfoLog("Out: {0}", sendOutMsg);
                    RaiseNewOutMessage(sendOutMsg);
                }
            }

            return(retVal);
        }