forked from StockSharp/StockSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InMemoryMessageAdapterProvider.cs
109 lines (95 loc) · 2.91 KB
/
InMemoryMessageAdapterProvider.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
namespace StockSharp.Configuration
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using Ecng.Common;
using Ecng.Reflection;
using StockSharp.Logging;
using StockSharp.Messages;
/// <summary>
/// In memory configuration message adapter's provider.
/// </summary>
public class InMemoryMessageAdapterProvider : IMessageAdapterProvider
{
/// <summary>
/// Initialize <see cref="InMemoryMessageAdapterProvider"/>.
/// </summary>
/// <param name="currentAdapters">All currently available adapters.</param>
public InMemoryMessageAdapterProvider(IEnumerable<IMessageAdapter> currentAdapters)
{
CurrentAdapters = currentAdapters ?? throw new ArgumentNullException(nameof(currentAdapters));
var idGenerator = new IncrementalIdGenerator();
PossibleAdapters = GetAdapters().Select(t => t.CreateAdapter(idGenerator)).ToArray();
}
/// <inheritdoc />
public virtual IEnumerable<IMessageAdapter> CurrentAdapters { get; }
/// <inheritdoc />
public virtual IEnumerable<IMessageAdapter> PossibleAdapters { get; }
private static readonly HashSet<string> _exceptions = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
{
"StockSharp.Alerts",
"StockSharp.Algo",
"StockSharp.Algo.History",
"StockSharp.Algo.Strategies",
"StockSharp.BusinessEntities",
"StockSharp.Community",
"StockSharp.Configuration",
"StockSharp.Licensing",
"StockSharp.Localization",
"StockSharp.Logging",
"StockSharp.Messages",
"StockSharp.Xaml",
"StockSharp.Xaml.Actipro",
"StockSharp.Xaml.Charting",
"StockSharp.Xaml.Diagram",
"StockSharp.Studio.Core",
"StockSharp.Studio.Controls",
"StockSharp.QuikLua",
"StockSharp.QuikLua32",
};
/// <summary>
/// Get all available adapters.
/// </summary>
/// <returns>All available adapters.</returns>
protected virtual IEnumerable<Type> GetAdapters()
{
var adapters = new List<Type>();
try
{
var assemblies = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll").Where(p =>
{
var name = Path.GetFileNameWithoutExtension(p);
return !_exceptions.Contains(name) && name.StartsWithIgnoreCase("StockSharp.");
});
foreach (var assembly in assemblies)
{
if (!assembly.IsAssembly())
continue;
try
{
var asm = Assembly.Load(AssemblyName.GetAssemblyName(assembly));
adapters.AddRange(asm
.GetTypes()
.Where(t => typeof(IMessageAdapter).IsAssignableFrom(t) && !t.IsAbstract && !t.IsObsolete())
.ToArray());
}
catch (Exception e)
{
e.LogError();
}
}
}
catch (Exception e)
{
e.LogError();
}
return adapters;
}
/// <inheritdoc />
public virtual IEnumerable<IMessageAdapter> CreateStockSharpAdapters(IdGenerator transactionIdGenerator, string login, SecureString password) => Enumerable.Empty<IMessageAdapter>();
}
}