-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathConnectTool.cs
92 lines (78 loc) · 3.08 KB
/
ConnectTool.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
// <copyright file="ConnectTool.cs" company="Devexperts LLC">
// Copyright © 2022 Devexperts LLC. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// </copyright>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DxFeed.Graal.Net.Api;
using DxFeed.Graal.Net.Api.Osub;
using DxFeed.Graal.Net.Events.Market;
using DxFeed.Graal.Net.Tools.Attributes;
using DxFeed.Graal.Net.Utils;
namespace DxFeed.Graal.Net.Tools.Connect;
[ToolInfo(
"Connect",
ShortDescription = "Connects to specified address(es).",
Description = "Connects to the specified address(es) and subscribes to the specified symbols.",
Usage = new[] { "Connect <address> <types> <symbols> [<options>]" })]
public sealed class ConnectTool : AbstractTool<ConnectArgs>, IDisposable
{
private readonly StreamWriter _output = new(Console.OpenStandardOutput());
public override void Run(ConnectArgs args)
{
SystemProperty.SetProperties(ParseProperties(args.Properties));
using var endpoint = DXEndpoint
.NewBuilder()
.WithRole(args.ForceStream ? DXEndpoint.Role.StreamFeed : DXEndpoint.Role.Feed)
.WithProperties(ParseProperties(args.Properties))
.WithName(nameof(ConnectTool))
.Build();
using var sub = endpoint
.GetFeed()
.CreateSubscription(ParseEventTypes(args.Types!));
if (!args.IsQuite)
{
sub.AddEventListener(events =>
{
foreach (var e in events)
{
_output.WriteLine(e);
}
_output.Flush();
});
}
IEnumerable<object> symbols = ParseSymbols(args.Symbols!).ToList();
if (args.FromTime != null)
{
var fromTime = CmdArgsUtil.ParseFromTime(args.FromTime);
symbols = symbols.Select(s => new TimeSeriesSubscriptionSymbol(s, fromTime));
}
else if (args.Source != null)
{
symbols = symbols.Select(s =>
new IndexedEventSubscriptionSymbol(s, OrderSource.ValueOf(args.Source)));
}
if (args.Tape != null)
{
var pub = DXEndpoint
.NewBuilder()
.WithRole(DXEndpoint.Role.StreamPublisher)
.WithProperty(DXEndpoint.DXFeedWildcardEnableProperty, "true") // Enabled by default.
.WithProperties(ParseProperties(args.Properties))
.WithName(nameof(ConnectTool))
.Build()
.Connect(args.Tape.StartsWith("tape:") ? args.Tape : $"tape:{args.Tape}").GetPublisher();
sub.AddEventListener(pub.PublishEvents);
}
sub.AddSymbols(symbols);
endpoint.Connect(args.Address);
Task.Delay(Timeout.Infinite).Wait();
}
public void Dispose() =>
_output.Dispose();
}