Пример #1
0
        public void Validate()
        {
            try
            {
                InitDictionaries();

                SignalService.Logger.Debug("Consistency validate started...");

                var serversResult = accountService.GetAccountTypesWithServers();
                if (!serversResult.IsSuccess)
                {
                    SignalService.Logger.Error(serversResult.Error);
                    return;
                }
                serversDictionary = serversResult.Result.ToDictionary(x => (int)x.AccountType, x => x.ServerName);

                var serverNames = serversDictionary.Values.Distinct().ToArray();

                handlersDictionary = serverNames.ToDictionary(x => x,
                                                              x => new EventWaitHandle(false, EventResetMode.ManualReset));

                var mt4Locations = repository.GetAccountsMt4Location();

                accountsDictionary = mt4Locations.ToDictionary(x => x.AccountId,
                                                               x => new Tuple <string, int>(serversDictionary[x.AccountType], x.Login));

                var waitHandles = new WaitHandle[serverNames.Count()];

                for (var i = 0; i < serverNames.Count(); i++)
                {
                    var j      = i;
                    var server = serverNames[j];
                    var thread = new Thread(() =>
                    {
                        var typeIds = serversDictionary.Where(x => x.Value == server).Select(x => x.Key).ToList();
                        var request = new OrdersStatusRequest();
                        request.logins.AddRange(mt4Locations.Where(x => typeIds.Contains(x.AccountType)).Select(x => x.Login));
                        serverController.OrdersStatusRequestsOnNext(new Tuple <string, OrdersStatusRequest>(server, request));
                        SignalService.Logger.Debug("Orders status request sended to server - {0}", server);
                    });
                    waitHandles[j] = handlersDictionary[server];
                    thread.Start();
                }

                var result = WaitHandle.WaitAll(waitHandles, waitHandleTimeout);
                if (!result)
                {
                    SignalService.Logger.Error("Some responses from mt4 were not handled");
                    return;
                }

                CheckConsistency();

                TradeSignalProcessor.OpenedOrdersDictionary = openedOrderDictionary;
            }
            catch (Exception e)
            {
                SignalService.Logger.Error("Consistency validation exception: {0}", e.ToString());
            }
        }