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