public void Serialization()
        {
            var dictionary = new SynchronizedDictionary <string, string>();

            var collection = new[] {
                new KeyValuePair <string, string>("22", "33"),
                new KeyValuePair <string, string>("44", "55"),
                new KeyValuePair <string, string>("66", "77"),
            };

            dictionary.AddRange(collection);

            var serialized = dictionary.Serialization().Binary();

            Assert.True(dictionary.ToList().Equality().Equal(serialized));
        }
        public void RemoveKeys()
        {
            var dictionary = new SynchronizedDictionary <string, string>();

            var collection = new[] {
                new KeyValuePair <string, string>("22", "33"),
                new KeyValuePair <string, string>("44", "55"),
                new KeyValuePair <string, string>("66", "77"),
            };

            dictionary.AddRange(collection);

            Assert.Equal(dictionary.Count, collection.Count());

            dictionary.RemoveKeys(collection.Select(t => t.Key));

            Assert.Equal(0, dictionary.Count());
        }
        public void RemoveKeys()
        {
            var dictionary = new SynchronizedDictionary<string, string>();

            var collection = new[] {
                new KeyValuePair<string, string>("22", "33"),
                new KeyValuePair<string, string>("44", "55"),
                new KeyValuePair<string, string>("66", "77"),
            };

            dictionary.AddRange(collection);

            Assert.Equal(dictionary.Count, collection.Count());

            dictionary.RemoveKeys(collection.Select(t => t.Key));

            Assert.Equal(0, dictionary.Count());
        }
        /// <summary>
        /// Запустить загрузку данных.
        /// </summary>
        protected override void OnStarting()
        {
            var connectorSettings = (ConnectorHydraTaskSettings)Settings;

            var settings = Connector.ReConnectionSettings;

            if (connectorSettings == null)
            {
                settings.AttemptCount   = -1;
                settings.ReAttemptCount = -1;
            }
            else
            {
                settings.Load(connectorSettings.ReConnectionSettings.Save());

                Connector.LogLevel = LogLevel;
            }

            Adapter  = GetAdapter(Connector.TransactionIdGenerator);
            _adapter = new BufferMessageAdapter(Adapter);

            Connector.Adapter.InnerAdapters.Clear();
            Connector.Adapter.InnerAdapters.Add(_adapter);

            // если фильтр по инструментам выключен (выбран инструмент все инструменты)
            _allSecurity = this.GetAllSecurity();

            _securityMap.Clear();
            _associatedSecurityCodes.Clear();

            if (_allSecurity == null)
            {
                _securityMap.AddRange(Settings.Securities.ToDictionary(s => s.Security, s => s));

                var associatedSecurities = Settings
                                           .Securities
                                           .Where(p => p.Security.Board == ExchangeBoard.Associated)
                                           .DistinctBy(sec => sec.Security.Code);

                _associatedSecurityCodes.AddRange(associatedSecurities.ToDictionary(s => s.Security.Code, s => s));
            }

            _adapter.SendInMessage(new ConnectMessage());
        }
Example #5
0
        /// <summary>
        /// Запустить загрузку данных.
        /// </summary>
        protected override void OnStarting()
        {
            // если фильтр по инструментам выключен (выбран инструмент все инструменты)
            _allSecurity = this.GetAllSecurity();

            _securityMap.Clear();
            _associatedSecurityCodes.Clear();

            if (_allSecurity == null)
            {
                _securityMap.AddRange(Settings.Securities.ToDictionary(s => s.Security, s => s));

                var associatedSecurities = Settings
                                           .Securities
                                           .Where(p => p.Security.Board == ExchangeBoard.Associated)
                                           .DistinctBy(sec => sec.Security.Code);

                _associatedSecurityCodes.AddRange(associatedSecurities.ToDictionary(s => s.Security.Code, s => s));
            }

            Connector.Start();
        }
Example #6
0
        /// <summary>
        /// Send message.
        /// </summary>
        /// <param name="message">Message.</param>
        protected override void OnSendInMessage(Message message)
        {
            if (message.IsBack)
            {
                var adapter = message.Adapter;

                if (adapter == null)
                {
                    throw new InvalidOperationException();
                }

                adapter.SendInMessage(message);
                return;
            }

            switch (message.Type)
            {
            case MessageTypes.Reset:
                ProcessReset(message);
                break;

            case MessageTypes.Connect:
            {
                if (_isFirstConnect)
                {
                    _isFirstConnect = false;
                }
                else
                {
                    ProcessReset(new ResetMessage());
                }

                _hearbeatAdapters.AddRange(GetSortedAdapters().Select(CreateWrappers).ToDictionary(a => a, a =>
                    {
                        var hearbeatAdapter = new HeartbeatMessageAdapter(a);
                        ((IMessageAdapter)hearbeatAdapter).Parent = this;
                        hearbeatAdapter.NewOutMessage            += m => OnInnerAdapterNewOutMessage(a, m);
                        return(hearbeatAdapter);
                    }));

                if (_hearbeatAdapters.Count == 0)
                {
                    throw new InvalidOperationException(LocalizedStrings.Str3650);
                }

                _hearbeatAdapters.Values.ForEach(a => a.SendInMessage(message));
                break;
            }

            case MessageTypes.Disconnect:
            {
                _connectedAdapters.Cache.ForEach(a => a.SendInMessage(message));
                break;
            }

            case MessageTypes.Portfolio:
            {
                var pfMsg = (PortfolioMessage)message;
                ProcessPortfolioMessage(pfMsg.PortfolioName, pfMsg);
                break;
            }

            case MessageTypes.OrderRegister:
            case MessageTypes.OrderReplace:
            case MessageTypes.OrderCancel:
            case MessageTypes.OrderGroupCancel:
            {
                var ordMsg = (OrderMessage)message;
                ProcessAdapterMessage(ordMsg.PortfolioName, ordMsg);
                break;
            }

            case MessageTypes.OrderPairReplace:
            {
                var ordMsg = (OrderPairReplaceMessage)message;
                ProcessAdapterMessage(ordMsg.Message1.PortfolioName, ordMsg);
                break;
            }

            case MessageTypes.MarketData:
            {
                IMessageAdapter[] adapters = null;

                if (message.Adapter != null)
                {
                    var wrapper = _hearbeatAdapters.Values.FirstOrDefault(w => GetUnderlyingAdapter(w) == message.Adapter);

                    if (wrapper != null)
                    {
                        adapters = new IMessageAdapter[] { wrapper }
                    }
                    ;
                }

                if (adapters == null)
                {
                    adapters = _messageTypeAdapters.TryGetValue(message.Type)?.Cache;
                }

                if (adapters == null)
                {
                    throw new InvalidOperationException(LocalizedStrings.Str629Params.Put(message.Type));
                }

                var mdMsg = (MarketDataMessage)message;

                switch (mdMsg.DataType)
                {
                case MarketDataTypes.News:
                    adapters.ForEach(a => a.SendInMessage(mdMsg));
                    break;

                default:
                {
                    var key = mdMsg.CreateKey();

                    var state = _subscriptionStates.TryGetValue2(key);

                    if (mdMsg.IsSubscribe)
                    {
                        if (state != null)
                        {
                            switch (state)
                            {
                            case SubscriptionStates.Subscribed:
                            {
                                var adapter = _subscriptions.TryGetValue(key);

                                if (adapter != null)
                                {
                                    adapter.SendInMessage(mdMsg);
                                }
                                else
                                {
                                    RaiseMarketDataMessage(null, mdMsg.OriginalTransactionId, new InvalidOperationException(state.Value.ToString()), true);
                                }

                                break;
                            }

                            case SubscriptionStates.Subscribing:
                                //case SubscriptionStates.Unsubscribing:
                                _suspendedSubscriptions.SafeAdd(key).Add(mdMsg);
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }

                            break;
                        }
                        else
                        {
                            _subscriptionStates.Add(key, SubscriptionStates.Subscribing);
                        }
                    }
                    else
                    {
                        var canProcess = false;

                        switch (state)
                        {
                        case SubscriptionStates.Subscribed:
                            canProcess = true;
                            //_subscriptionStates[key] = SubscriptionStates.Unsubscribing;
                            break;

                        case SubscriptionStates.Subscribing:
                            //case SubscriptionStates.Unsubscribing:
                            RaiseMarketDataMessage(null, mdMsg.OriginalTransactionId, new InvalidOperationException(state.Value.ToString()), false);
                            break;

                        case null:
                            RaiseMarketDataMessage(null, mdMsg.OriginalTransactionId, null, false);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        if (!canProcess)
                        {
                            break;
                        }
                    }

                    if (mdMsg.TransactionId != 0)
                    {
                        _subscriptionKeys.Add(mdMsg.TransactionId, key);
                    }

                    if (mdMsg.IsSubscribe)
                    {
                        //if (_subscriptionQueue.ContainsKey(key))
                        //	return;

                        var enumerator = adapters.Cast <IMessageAdapter>().GetEnumerator();

                        _subscriptionQueue.Add(key, enumerator);
                        ProcessSubscriptionAction(enumerator, mdMsg, mdMsg.TransactionId);
                    }
                    else
                    {
                        var adapter = _subscriptions.TryGetValue(key);

                        if (adapter != null)
                        {
                            //_subscriptions.Remove(key);
                            adapter.SendInMessage(message);
                        }
                    }

                    break;
                }
                }

                break;
            }

            case MessageTypes.ChangePassword:
            {
                var adapter = GetSortedAdapters().FirstOrDefault(a => a.SupportedMessages.Contains(MessageTypes.ChangePassword));

                if (adapter == null)
                {
                    throw new InvalidOperationException(LocalizedStrings.Str629Params.Put(message.Type));
                }

                adapter.SendInMessage(message);
                break;
            }

            default:
            {
                if (message.Adapter != null)
                {
                    message.Adapter.SendInMessage(message);
                    break;
                }

                var adapters = _messageTypeAdapters.TryGetValue(message.Type)?.Cache;

                if (adapters == null)
                {
                    throw new InvalidOperationException(LocalizedStrings.Str629Params.Put(message.Type));
                }

                adapters.ForEach(a => a.SendInMessage(message));
                break;
            }
            }
        }
        public void Serialization()
        {
            var dictionary = new SynchronizedDictionary<string, string>();

            var collection = new[] {
                new KeyValuePair<string, string>("22", "33"),
                new KeyValuePair<string, string>("44", "55"),
                new KeyValuePair<string, string>("66", "77"),
            };

            dictionary.AddRange(collection);

            var serialized = dictionary.Serialization().Binary();

            Assert.True(dictionary.ToList().Equality().Equal(serialized));
        }
        /// <summary>
        /// Отправить сообщение.
        /// </summary>
        /// <param name="message">Сообщение.</param>
        protected override void OnSendInMessage(Message message)
        {
            switch (message.Type)
            {
            case MessageTypes.Reset:
                ProcessReset(message);
                break;

            case MessageTypes.Connect:
                if (_isFirstConnect)
                {
                    _isFirstConnect = false;
                }
                else
                {
                    ProcessReset(new ResetMessage());
                }

                _hearbeatAdapters.AddRange(GetSortedAdapters().ToDictionary(a => a, a =>
                {
                    var hearbeatAdapter            = (IMessageAdapter) new HeartbeatAdapter(a);
                    hearbeatAdapter.Parent         = this;
                    hearbeatAdapter.NewOutMessage += m => OnInnerAdapterNewMessage(a, m);
                    return(hearbeatAdapter);
                }));

                if (_hearbeatAdapters.Count == 0)
                {
                    throw new InvalidOperationException(LocalizedStrings.Str3650);
                }

                _hearbeatAdapters.Values.ForEach(a => a.SendInMessage(message));
                break;

            case MessageTypes.Disconnect:
                _connectedAdapters
                .CachedValues
                .SelectMany(c => c.Cache)
                .Distinct()
                .ForEach(a => a.SendInMessage(message));
                break;

            case MessageTypes.Portfolio:
            {
                var pfMsg = (PortfolioMessage)message;
                ProcessPortfolioMessage(pfMsg.PortfolioName, pfMsg);
                break;
            }

            case MessageTypes.OrderRegister:
            case MessageTypes.OrderReplace:
            case MessageTypes.OrderCancel:
            case MessageTypes.OrderGroupCancel:
            {
                var ordMsg = (OrderMessage)message;
                ProcessPortfolioMessage(ordMsg.PortfolioName, ordMsg);
                break;
            }

            case MessageTypes.OrderPairReplace:
            {
                var ordMsg = (OrderPairReplaceMessage)message;
                ProcessPortfolioMessage(ordMsg.Message1.PortfolioName, ordMsg);
                break;
            }

            case MessageTypes.MarketData:
            {
                var adapters = _connectedAdapters.TryGetValue(message.Type);

                if (adapters == null)
                {
                    throw new InvalidOperationException(LocalizedStrings.Str629Params.Put(message.Type));
                }

                var mdMsg = (MarketDataMessage)message;

                switch (mdMsg.DataType)
                {
                case MarketDataTypes.News:
                    adapters.Cache.ForEach(a => a.SendInMessage(mdMsg));
                    break;

                default:
                {
                    var key = Tuple.Create(mdMsg.SecurityId, mdMsg.DataType);

                    if (mdMsg.IsSubscribe)
                    {
                        if (_subscriptionQueue.ContainsKey(key))
                        {
                            return;
                        }

                        var enumerator = adapters.Cache.Cast <IMessageAdapter>().GetEnumerator();

                        _subscriptionQueue.Add(key, enumerator);

                        ProcessSubscriptionAction(enumerator, mdMsg);
                    }
                    else
                    {
                        var adapter = _subscriptions.TryGetValue(key);

                        if (adapter != null)
                        {
                            _subscriptions.Remove(key);
                            adapter.SendInMessage(message);
                        }
                    }

                    break;
                }
                }

                break;
            }

            default:
            {
                var adapters = _connectedAdapters.TryGetValue(message.Type);

                if (adapters == null)
                {
                    throw new InvalidOperationException(LocalizedStrings.Str629Params.Put(message.Type));
                }

                adapters.Cache.ForEach(a => a.SendInMessage(message));
                break;
            }
            }
        }