0
|
1 using System;
|
|
2 using System.Collections.Concurrent;
|
|
3 using System.Collections.Generic;
|
|
4 using System.Diagnostics;
|
|
5 using System.Linq;
|
|
6 using System.Net;
|
|
7 using System.Threading.Tasks;
|
|
8 using NLog;
|
|
9 using Stocks.Common;
|
|
10 using Stocks.Common.Models;
|
|
11 using Stocks.Common.Events;
|
|
12
|
|
13 namespace Stocks.Service
|
|
14 {
|
|
15 public class StocksService : IStocksService
|
|
16 {
|
|
17 private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
|
18
|
|
19 private IList<Company> _companies;
|
|
20 private IConfigurationService _configurationService;
|
|
21 private List<Price> _currentPrices;
|
|
22 private string _serviceUrl;
|
|
23 private WebClient _webClient;
|
|
24
|
|
25 public bool IsActive { get; private set; }
|
|
26 public SummaryStats Stats { get; private set; }
|
|
27
|
|
28 public StocksService(IConfigurationService configurationService)
|
|
29 {
|
|
30 new AssemblyInit();
|
|
31
|
|
32 _webClient = new WebClient();
|
|
33 _configurationService = configurationService;
|
|
34 _companies = configurationService.GetCompanies();
|
|
35
|
|
36 string symbolsCsv = _companies.Select(
|
|
37 c => c.Symbol).Aggregate((c, d) => c + "," + d);
|
|
38 _serviceUrl = _configurationService.GetServiceUrl(symbolsCsv);
|
|
39
|
|
40 Stats = new SummaryStats();
|
|
41 }
|
|
42
|
|
43 public delegate void PriceChangedEventHandler(object sender, PriceChangedEventArgs e);
|
|
44 public event PriceChangedEventHandler PriceChanged;
|
|
45 protected virtual void RaisePriceChanged(Price price)
|
|
46 {
|
|
47 Stats.PriceChangeEvents++;
|
|
48
|
|
49 if (PriceChanged != null)
|
|
50 PriceChanged(this, new PriceChangedEventArgs() { Price = price });
|
|
51 }
|
|
52
|
|
53 public void Start()
|
|
54 {
|
|
55 PrepareForServiceStarting();
|
|
56 Task.Factory.StartNew(() => DownloadPrices());
|
|
57 }
|
|
58
|
|
59 public void Stop()
|
|
60 {
|
|
61 IsActive = false;
|
|
62 Log.Debug("StockService stopped");
|
|
63 }
|
|
64
|
|
65 private void PrepareForServiceStarting()
|
|
66 {
|
|
67 Log.Debug("StockService starting");
|
|
68 IsActive = true;
|
|
69 Stats.Reset();
|
|
70 _currentPrices = new List<Price>(_companies.Count);
|
|
71 }
|
|
72
|
|
73 private void DownloadPrices()
|
|
74 {
|
|
75 try
|
|
76 {
|
|
77 Stopwatch timeToDownload = new Stopwatch();
|
|
78
|
|
79 while (IsActive)
|
|
80 {
|
|
81 timeToDownload.Restart();
|
|
82 string webResponse = _webClient.DownloadString(_serviceUrl);
|
|
83 timeToDownload.Stop();
|
|
84
|
|
85 PopulatePricesFromWebResponse(webResponse);
|
|
86 UpdateStats(timeToDownload, webResponse);
|
|
87 }
|
|
88 }
|
|
89 catch (Exception e)
|
|
90 {
|
|
91 Log.Error("Exception during DownloadPrices()");
|
|
92 Log.Error("Stack Trace {0}: /r/nException Message: {1}", e.StackTrace, e.Message);
|
|
93 this.Stop();
|
|
94 }
|
|
95 }
|
|
96
|
|
97 private void PopulatePricesFromWebResponse(string webResponse)
|
|
98 {
|
|
99 string[] webPrices = webResponse.Split(
|
|
100 new string[] { "\n", "\r\n" },
|
|
101 StringSplitOptions.RemoveEmptyEntries);
|
|
102
|
|
103 foreach (string webPriceData in webPrices)
|
|
104 {
|
|
105 var webPrice = Factory.CreatePrice(webPriceData);
|
|
106 var localPrice = _currentPrices.Find(x => x.Symbol == webPrice.Symbol);
|
|
107
|
|
108 if (localPrice == null) {
|
|
109 _currentPrices.Add(new Price(webPrice.Symbol, webPrice.CurrentPrice, webPrice.PreviousPrice));
|
|
110 continue;
|
|
111 }
|
|
112
|
|
113 if (localPrice.Equals(webPrice)) {
|
|
114 continue;
|
|
115 }
|
|
116 else {
|
|
117 localPrice.PreviousPrice = localPrice.CurrentPrice;
|
|
118 localPrice.CurrentPrice = webPrice.CurrentPrice;
|
|
119 RaisePriceChanged(localPrice);
|
|
120 }
|
|
121 }
|
|
122 }
|
|
123
|
|
124 private void UpdateStats(Stopwatch timeToDownload, string webResponse)
|
|
125 {
|
|
126 Stats.LastWebRequest.Duration = (int)timeToDownload.ElapsedMilliseconds;
|
|
127 Stats.LastWebRequest.PricesDownloaded = _currentPrices.Count;
|
|
128 Stats.LastWebRequest.Response = webResponse;
|
|
129 Stats.LastWebRequest.Request = _serviceUrl;
|
|
130 Stats.LastWebRequest.SymbolCount = _companies.Count;
|
|
131 Stats.NumberOfRequests++;
|
|
132 Log.Trace(Stats);
|
|
133 }
|
|
134 }
|
|
135 } |