-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathSubscriptionsCacheManager.cs
76 lines (63 loc) · 2.75 KB
/
SubscriptionsCacheManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using DevelopmentInProgress.TradeView.Core.TradeStrategy;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace DevelopmentInProgress.TradeServer.StrategyExecution.WebHost.Cache.Subscriptions
{
public class SubscriptionsCacheManager : ISubscriptionsCacheManager
{
private bool disposed;
public SubscriptionsCacheManager(IExchangeSubscriptionsCacheFactory exchangeSubscriptionsCacheFactory)
{
ExchangeSubscriptionsCacheFactory = exchangeSubscriptionsCacheFactory;
}
public IExchangeSubscriptionsCacheFactory ExchangeSubscriptionsCacheFactory { get; private set; }
public async Task Subscribe(Strategy strategy, ITradeStrategy tradeStrategy)
{
if (strategy == null)
{
throw new ArgumentNullException(nameof(strategy));
}
var exchangeSymbolsList = (from s in strategy.StrategySubscriptions
group s by s.Exchange into es
select new { Exchange = es.Key, StrategySubscriptions = es.ToList() }).ToList();
foreach(var exchangeSymbols in exchangeSymbolsList)
{
var exchangeSubscriptionsCache = ExchangeSubscriptionsCacheFactory.GetExchangeSubscriptionsCache(exchangeSymbols.Exchange);
await exchangeSubscriptionsCache.Subscribe(strategy.Name, exchangeSymbols.StrategySubscriptions, tradeStrategy).ConfigureAwait(false);
}
}
public void Unsubscribe(Strategy strategy, ITradeStrategy tradeStrategy)
{
if (strategy == null)
{
throw new ArgumentNullException(nameof(strategy));
}
var exchangeSymbolsList = (from s in strategy.StrategySubscriptions
group s by s.Exchange into es
select new { Exchange = es.Key, StrategySubscriptions = es.ToList() }).ToList();
foreach (var exchangeSymbols in exchangeSymbolsList)
{
var exchangeSubscriptionsCache = ExchangeSubscriptionsCacheFactory.GetExchangeSubscriptionsCache(exchangeSymbols.Exchange);
exchangeSubscriptionsCache.Unsubscribe(strategy.Name, exchangeSymbols.StrategySubscriptions, tradeStrategy);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
if (disposing)
{
ExchangeSubscriptionsCacheFactory.Dispose();
}
disposed = true;
}
}
}