Пример #1
0
        async Task<List<UikJson>> GetChildren(Node node, int count = 1)
        {
            IRestResponse response = null;
            String json = null;
            RestClient client = null;
            var url = String.Format("http://cikrf.ru/services/lk_tree/?id={0}", node.id);

            try
            {    
                client = new RestClient(url);
                var request = new RestRequest(Method.GET);

                // execute the request
                response = client.Execute(request);
                if (response.RawBytes == null)
                    throw new ApplicationException("Пустой ответ от сервера!");

                json = System.Text.Encoding.Default.GetString(response.RawBytes);
                var uiks = JsonConvert.DeserializeObject<List<UikJson>>(json);
                return uiks;
            }
            catch (Exception ex)
            {
                if (count < 10)
                {
                    await Task.Delay(1000 * count);
                    Logger.Instance.Error(String.Format("Не удалось получить данные из {0} ({1}) попытка: {2}", url, node.GetFullText(), count), ex);
                    return await GetChildren(node, ++count);
                } else
                {
                    throw new ApplicationException(String.Format("Запрос по адресу {0} не удалось выполнить в течение 10 попыток!", url));
                }
            }
        }
Пример #2
0
        public void Mine(String id, String text)
        {
            try
            {
                var bypassNodeStack = new ConcurrentStack<Node>();
                ConcurrentQueue<Node> addressNodeQueue = new ConcurrentQueue<Node>();
                ConcurrentQueue<Uik> uiksQueue = new ConcurrentQueue<Uik>();

                CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
                
                var rootNode = new Node()
                {
                    id = id,
                    text = text
                };

                bypassNodeStack.Push(rootNode);

                var tasks = new List<Task>();

                for (var i = 0; i < Properties.Settings.Default.MineWorkersCount; i++)
                {
                    var bypassMiner = new BypassTreeWorker();
                    var addressMiner = new AddressMineWorker();

                    workers.Add(bypassMiner);
                    workers.Add(addressMiner);

                    tasks.Add(Task.Run(async () => await bypassMiner.BypassTree(
                        bypassNodeStack, 
                        addressNodeQueue, 
                        cancelTokenSource)));

                    tasks.Add(Task.Run(async () => await addressMiner.Mine(
                        uiksQueue, 
                        addressNodeQueue,
                        cancelTokenSource)));
                }

                WriteToFileWorker writer = new WriteToFileWorker(uiksQueue, Properties.Settings.Default.FileName);
                workers.Add(writer);

                tasks.Add(Task.Run(async() =>
                {
                    await writer.Write(cancelTokenSource.Token);
                    Logger.Instance.Info(String.Format("Записано {0} записей ", writer.Count));
                }));

                Task.Delay(10000).Wait();

                while (!AllFinished())
                {
                    Task.Delay(1000).Wait();

                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo key = Console.ReadKey(true);
                        if (key.Key == ConsoleKey.Q)
                        {
                            Logger.Instance.Info("Добыча прервана по команде пользователя!");
                            break;
                        }
                    }
                }

                cancelTokenSource.Cancel();
                Task.WaitAll(tasks.ToArray());
                Logger.Instance.Info("Добыча завершена!");
            }
            catch (Exception ex)
            {
                Logger.Instance.Fatal("Сбой получения данных!", ex);
                throw ex;
            }
        }
Пример #3
0
 public Node(Node parent = null)
 {
     Parent = parent;
 }