예제 #1
0
        public IMyNoSqlServerDataWriter <CandleBidAskNoSql> GetWriter(string brokerId, CandleType type)
        {
            if (type == CandleType.Minute)
            {
                if (_minuteWriterByBroker.TryGetValue(brokerId, out var writer))
                {
                    return(writer);
                }

                writer = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleBidAskNoSql>(
                    Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                    CandleBidAskNoSql.TableNameMinute(brokerId), true);

                _minuteWriterByBroker[brokerId] = writer;

                return(writer);
            }

            if (type == CandleType.Hour)
            {
                if (_hourWriterByBroker.TryGetValue(brokerId, out var writer))
                {
                    return(writer);
                }

                writer = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleBidAskNoSql>(
                    Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                    CandleBidAskNoSql.TableNameHour(brokerId), true);

                _hourWriterByBroker[brokerId] = writer;

                return(writer);
            }

            if (type == CandleType.Day)
            {
                if (_dayWriterByBroker.TryGetValue(brokerId, out var writer))
                {
                    return(writer);
                }

                writer = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleBidAskNoSql>(
                    Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                    CandleBidAskNoSql.TableNameDay(brokerId), true);

                _dayWriterByBroker[brokerId] = writer;

                return(writer);
            }

            if (type == CandleType.Month)
            {
                if (_monthWriterByBroker.TryGetValue(brokerId, out var writer))
                {
                    return(writer);
                }

                writer = new MyNoSqlServer.DataWriter.MyNoSqlServerDataWriter <CandleBidAskNoSql>(
                    Program.ReloadedSettings(model => model.MyNoSqlWriterUrl),
                    CandleBidAskNoSql.TableNameMonth(brokerId), true);

                _monthWriterByBroker[brokerId] = writer;

                return(writer);
            }

            throw new Exception($"Unknown candle type {type}");
        }
        private void RegisterNoSqlClient(string brokerId)
        {
            MyNoSqlTcpClient client;

            lock (_gate)
            {
                if (_myNoSqlSubscriberByBroker.TryGetValue(brokerId, out client))
                {
                    return;
                }

                client = new MyNoSqlTcpClient(_getMyNoSqlHostPort, $"{_appName}-{brokerId}");


                _minuteBidAskReaderByBroker[brokerId] = new MyNoSqlReadRepository <CandleBidAskNoSql>(client, CandleBidAskNoSql.TableNameMinute(brokerId));
                _hourBidAskReaderByBroker[brokerId]   = new MyNoSqlReadRepository <CandleBidAskNoSql>(client, CandleBidAskNoSql.TableNameHour(brokerId));
                _dayBidAskReaderByBroker[brokerId]    = new MyNoSqlReadRepository <CandleBidAskNoSql>(client, CandleBidAskNoSql.TableNameDay(brokerId));
                _monthBidAskReaderByBroker[brokerId]  = new MyNoSqlReadRepository <CandleBidAskNoSql>(client, CandleBidAskNoSql.TableNameMonth(brokerId));

                _minuteTradeReaderByBroker[brokerId] = new MyNoSqlReadRepository <CandleTradeNoSql>(client, CandleTradeNoSql.TableNameMinute(brokerId));
                _hourTradeReaderByBroker[brokerId]   = new MyNoSqlReadRepository <CandleTradeNoSql>(client, CandleTradeNoSql.TableNameHour(brokerId));
                _dayTradeReaderByBroker[brokerId]    = new MyNoSqlReadRepository <CandleTradeNoSql>(client, CandleTradeNoSql.TableNameDay(brokerId));
                _monthTradeReaderByBroker[brokerId]  = new MyNoSqlReadRepository <CandleTradeNoSql>(client, CandleTradeNoSql.TableNameMonth(brokerId));

                _myNoSqlSubscriberByBroker[brokerId] = client;
            }

            client.Start();

            //todo: use wait initialization

            var index = 0;

            while (index < 50)
            {
                if (_minuteBidAskReaderByBroker[brokerId].Count() > 0 &&
                    _hourBidAskReaderByBroker[brokerId].Count() > 0 &&
                    _dayBidAskReaderByBroker[brokerId].Count() > 0 &&
                    _monthBidAskReaderByBroker[brokerId].Count() > 0 &&
                    _minuteTradeReaderByBroker[brokerId].Count() > 0 &&
                    _hourTradeReaderByBroker[brokerId].Count() > 0 &&
                    _dayTradeReaderByBroker[brokerId].Count() > 0 &&
                    _monthTradeReaderByBroker[brokerId].Count() > 0)
                {
                    break;
                }

                Thread.Sleep(100);
                index++;
            }
        }