Skip to content

Commit e86421d

Browse files
committed
Add default implementation of events, snapshots, plb printers
Add dummy printers Add heartbeat printing to the samples Add quiet mode to the samples
1 parent a7efd06 commit e86421d

File tree

9 files changed

+656
-460
lines changed

9 files changed

+656
-460
lines changed

dxf_api/src/extras/EventPrinter.cs

Lines changed: 507 additions & 6 deletions
Large diffs are not rendered by default.

dxf_client/Program.cs

Lines changed: 30 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -21,131 +21,6 @@
2121

2222
namespace dxf_client
2323
{
24-
public class SnapshotPrinter :
25-
IDxOrderSnapshotListener,
26-
IDxCandleSnapshotListener,
27-
IDxTimeAndSaleSnapshotListener,
28-
IDxSpreadOrderSnapshotListener,
29-
IDxGreeksSnapshotListener,
30-
IDxSeriesSnapshotListener
31-
{
32-
private readonly int recordsPrintLimit;
33-
34-
public SnapshotPrinter(int recordsPrintLimit)
35-
{
36-
this.recordsPrintLimit = recordsPrintLimit;
37-
}
38-
39-
#region Implementation of IDxCandleSnapshotListener
40-
41-
/// <summary>
42-
/// On Candle snapshot event received.
43-
/// </summary>
44-
/// <typeparam name="TB">Event buffer type.</typeparam>
45-
/// <typeparam name="TE">Event type.</typeparam>
46-
/// <param name="buf">Event buffer object.</param>
47-
public void OnCandleSnapshot<TB, TE>(TB buf)
48-
where TB : IDxEventBuf<TE>
49-
where TE : IDxCandle
50-
{
51-
PrintSnapshot(buf);
52-
}
53-
54-
#endregion //IDxCandleSnapshotListener
55-
56-
#region Implementation of IDxGreeksSnapshotListener
57-
58-
public void OnGreeksSnapshot<TB, TE>(TB buf)
59-
where TB : IDxEventBuf<TE>
60-
where TE : IDxGreeks
61-
{
62-
PrintSnapshot(buf);
63-
}
64-
65-
#endregion
66-
67-
#region Implementation of IDxOrderSnapshotListener
68-
69-
/// <summary>
70-
/// On Order snapshot event received.
71-
/// </summary>
72-
/// <typeparam name="TB">Event buffer type.</typeparam>
73-
/// <typeparam name="TE">Event type.</typeparam>
74-
/// <param name="buf">Event buffer object.</param>
75-
public void OnOrderSnapshot<TB, TE>(TB buf)
76-
where TB : IDxEventBuf<TE>
77-
where TE : IDxOrder
78-
{
79-
PrintSnapshot(buf);
80-
}
81-
82-
#endregion //IDxOrderSnapshotListener
83-
84-
#region Implementation of IDxSeriesSnapshotListener
85-
86-
public void OnSeriesSnapshot<TB, TE>(TB buf)
87-
where TB : IDxEventBuf<TE>
88-
where TE : IDxSeries
89-
{
90-
PrintSnapshot(buf);
91-
}
92-
93-
#endregion
94-
95-
#region Implementation of IDxSpreadOrderSnapshotListener
96-
97-
/// <summary>
98-
/// On SpreadOrder snapshot event received.
99-
/// </summary>
100-
/// <typeparam name="TB">Event buffer type.</typeparam>
101-
/// <typeparam name="TE">Event type.</typeparam>
102-
/// <param name="buf">Event buffer object.</param>
103-
public void OnSpreadOrderSnapshot<TB, TE>(TB buf)
104-
where TB : IDxEventBuf<TE>
105-
where TE : IDxSpreadOrder
106-
{
107-
PrintSnapshot(buf);
108-
}
109-
110-
#endregion
111-
112-
#region Implementation of IDxTimeAndSaleSnapshotListener
113-
114-
/// <summary>
115-
/// On TimeAndSale snapshot event received.
116-
/// </summary>
117-
/// <typeparam name="TB">Event buffer type.</typeparam>
118-
/// <typeparam name="TE">Event type.</typeparam>
119-
/// <param name="buf">Event buffer object.</param>
120-
public void OnTimeAndSaleSnapshot<TB, TE>(TB buf)
121-
where TB : IDxEventBuf<TE>
122-
where TE : IDxTimeAndSale
123-
{
124-
PrintSnapshot(buf);
125-
}
126-
127-
#endregion //IDxTimeAndSaleSnapshotListener
128-
129-
private void PrintSnapshot<TE>(IDxEventBuf<TE> buf)
130-
{
131-
Console.WriteLine(string.Format(CultureInfo.InvariantCulture,
132-
"Snapshot {0} {{Symbol: '{1}', RecordsCount: {2}}}",
133-
buf.EventType, buf.Symbol, buf.Size));
134-
135-
var count = 0;
136-
foreach (var o in buf)
137-
{
138-
Console.WriteLine($" {{ {o} }}");
139-
140-
if (++count < recordsPrintLimit || recordsPrintLimit == 0) continue;
141-
142-
Console.WriteLine($" {{ ... {buf.Size - count} records left ...}}");
143-
144-
break;
145-
}
146-
}
147-
}
148-
14924
internal class InputParam<T>
15025
{
15126
private T value;
@@ -248,10 +123,10 @@ private static bool TryParseTaggedStringParam(string tag, string paramTagString,
248123

249124
private static void Main(string[] args)
250125
{
251-
if (args.Length < 3 || args.Length > 13)
126+
if (args.Length < 3 || (args.Length > 0 && args[0].Equals("-h")))
252127
{
253128
Console.WriteLine(
254-
"Usage: dxf_client <host:port>|<path> <event> <symbol> [<date>] [<source>] [snapshot] [-l <records_print_limit>] [-T <token>] [-s <subscr_data>] [-p] [-b]\n" +
129+
"Usage: dxf_client <host:port>|<path> <event> <symbol> [<date>] [<source>] [snapshot] [-l <records_print_limit>] [-T <token>] [-s <subscr_data>] [-p] [-b] [-q]\n" +
255130
"where\n" +
256131
" host:port - The address of dxfeed server (demo.dxfeed.com:7300)\n" +
257132
" path - The path to file with candle data (non zipped Candle Web Service output) or `tape` file\n" +
@@ -279,7 +154,8 @@ private static void Main(string[] args)
279154
" -T <token> - The authorization token\n\n" +
280155
" -s <subscr_data> - The subscription data: ticker|TICKER, stream|STREAM, history|HISTORY\n" +
281156
" -p - Enables the data transfer logging\n" +
282-
" -b - Enables the server's heartbeat logging to console\n\n" +
157+
" -b - Enables the server's heartbeat logging to console\n" +
158+
" -q - Quiet mode (do not print events and snapshots)\n\n" +
283159
"examples:\n" +
284160
" events: dxf_client demo.dxfeed.com:7300 Quote,Trade MSFT.TEST,IBM.TEST\n" +
285161
" events: dxf_client demo.dxfeed.com:7300 Quote,Trade MSFT.TEST,IBM.TEST -s stream\n" +
@@ -318,6 +194,7 @@ private static void Main(string[] args)
318194
var subscriptionData = new InputParam<EventSubscriptionFlag>(EventSubscriptionFlag.Default);
319195
var logDataTransferFlag = false;
320196
var logServerHeartbeatsFlag = false;
197+
var quiteMode = false;
321198

322199
for (var i = SymbolIndex + 1; i < args.Length; i++)
323200
{
@@ -363,6 +240,13 @@ private static void Main(string[] args)
363240
continue;
364241
}
365242

243+
if (quiteMode == false && args[i].Equals("-q"))
244+
{
245+
quiteMode = true;
246+
247+
continue;
248+
}
249+
366250
if (!sources.IsSet)
367251
TryParseSourcesParam(args[i], sources);
368252
}
@@ -377,37 +261,45 @@ private static void Main(string[] args)
377261
NativeTools.LoadConfigFromString("logger.level = \"debug\"\n");
378262
NativeTools.InitializeLogging("dxf_client.log", true, true, logDataTransferFlag);
379263

380-
var listener = new EventPrinter();
264+
var eventPrinter = quiteMode ? (IEventPrinter) new DummyEventPrinter() : new EventPrinter();
265+
381266
using (var con = token.IsSet
382-
? new NativeConnection(address, token.Value, DisconnectHandler, ConnectionStatusChangeHandler)
383-
: new NativeConnection(address, DisconnectHandler, ConnectionStatusChangeHandler))
267+
? new NativeConnection(address, token.Value, DisconnectHandler, ConnectionStatusChangeHandler)
268+
: new NativeConnection(address, DisconnectHandler, ConnectionStatusChangeHandler))
384269
{
385270
if (logServerHeartbeatsFlag)
271+
{
386272
con.SetOnServerHeartbeatHandler((connection, time, lagMark, rtt) =>
387273
{
388274
Console.Error.WriteLine(
389275
$"##### Server time (UTC) = {time}, Server lag = {lagMark} us, RTT = {rtt} us #####");
390276
});
277+
}
391278

392279
IDxSubscription s = null;
393280
try
394281
{
395282
if (dateTime.IsSet && (events & (EventType.Order | EventType.SpreadOrder)) != 0)
396283
{
397-
Console.Error.WriteLine("Date and event type Order or SpreadOrder cannot be used for subscription");
284+
Console.Error.WriteLine(
285+
"Date and event type Order or SpreadOrder cannot be used for subscription");
398286
}
399-
287+
400288
if (isSnapshot.Value)
289+
{
401290
s = con.CreateSnapshotSubscription(events, dateTime.Value,
402-
new SnapshotPrinter(recordsPrintLimit.Value));
291+
quiteMode
292+
? (ISnapshotPrinter) new DummySnapshotPrinter()
293+
: new SnapshotPrinter(recordsPrintLimit.Value));
294+
}
403295
else if (dateTime.IsSet)
404296
s = subscriptionData.IsSet
405-
? con.CreateSubscription(events, dateTime.Value, subscriptionData.Value, listener)
406-
: con.CreateSubscription(events, dateTime.Value, listener);
297+
? con.CreateSubscription(events, dateTime.Value, subscriptionData.Value, eventPrinter)
298+
: con.CreateSubscription(events, dateTime.Value, eventPrinter);
407299
else
408300
s = subscriptionData.IsSet
409-
? con.CreateSubscription(events, subscriptionData.Value, listener)
410-
: con.CreateSubscription(events, listener);
301+
? con.CreateSubscription(events, subscriptionData.Value, eventPrinter)
302+
: con.CreateSubscription(events, eventPrinter);
411303

412304
if (events.HasFlag(EventType.Order) && sources.Value.Length > 0)
413305
s.SetSource(sources.Value);

samples/dxf_inc_order_snapshot_sample/Program.cs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System;
1313
using com.dxfeed.api;
1414
using com.dxfeed.api.events;
15+
using com.dxfeed.api.extras;
1516
using com.dxfeed.native;
1617

1718
namespace dxf_inc_order_snapshot_sample
@@ -66,10 +67,10 @@ private static bool TryParseTaggedStringParam(string tag, string paramTagString,
6667

6768
private static void Main(string[] args)
6869
{
69-
if (args.Length < 2 || args.Length > 8)
70+
if (args.Length < 2 || (args.Length > 0 && args[0].Equals("-h")))
7071
{
7172
Console.WriteLine(
72-
"Usage: dxf_inc_order_snapshot_sample <host:port> <symbol> [<source>] [-l <records_print_limit>] [-T <token>] [-p]\n" +
73+
"Usage: dxf_inc_order_snapshot_sample <host:port> <symbol> [<source>] [-l <records_print_limit>] [-T <token>] [-p] [-b] [-q]\n" +
7374
"where\n" +
7475
" host:port - The address of dxfeed server (demo.dxfeed.com:7300)\n" +
7576
" symbol - The symbol string, it is allowed to use only one symbol (IBM, MSFT, etc)\n" +
@@ -82,8 +83,10 @@ private static void Main(string[] args)
8283
" If source is not specified MarketMaker snapshot will be\n" +
8384
" subscribed by default.\n\n" +
8485
$" -l <records_print_limit> - The number of displayed records (0 - unlimited, default: {DefaultRecordsPrintLimit})\n" +
85-
" -T <token> - The authorization token\n" +
86-
" -p - Enables the data transfer logging\n\n" +
86+
" -T <token> - The authorization token\n" +
87+
" -p - Enables the data transfer logging\n" +
88+
" -b - Enables the server's heartbeat logging to console\n" +
89+
" -q - Quiet mode (do not print snapshots)\n\n" +
8790
"order example: dxf_inc_order_snapshot_sample demo.dxfeed.com:7300 AAPL NTV\n" +
8891
"market maker example:\n" +
8992
" dxf_inc_order_snapshot_sample demo.dxfeed.com:7300 AAPL AGGREGATE_BID\n" +
@@ -100,6 +103,8 @@ private static void Main(string[] args)
100103
var recordsPrintLimit = new InputParam<int>(DefaultRecordsPrintLimit);
101104
var token = new InputParam<string>(null);
102105
var logDataTransferFlag = false;
106+
var logServerHeartbeatsFlag = false;
107+
var quiteMode = false;
103108

104109
for (var i = SymbolIndex + 1; i < args.Length; i++)
105110
{
@@ -122,7 +127,20 @@ private static void Main(string[] args)
122127
if (logDataTransferFlag == false && args[i].Equals("-p"))
123128
{
124129
logDataTransferFlag = true;
125-
i++;
130+
131+
continue;
132+
}
133+
134+
if (logServerHeartbeatsFlag == false && args[i].Equals("-b"))
135+
{
136+
logServerHeartbeatsFlag = true;
137+
138+
continue;
139+
}
140+
141+
if (quiteMode == false && args[i].Equals("-q"))
142+
{
143+
quiteMode = true;
126144

127145
continue;
128146
}
@@ -140,11 +158,22 @@ private static void Main(string[] args)
140158
{
141159
NativeTools.InitializeLogging("dxf_inc_order_snapshot_sample.log", true, true, logDataTransferFlag);
142160
using (var con = token.IsSet
143-
? new NativeConnection(address, token.Value, DisconnectHandler)
144-
: new NativeConnection(address, DisconnectHandler))
161+
? new NativeConnection(address, token.Value, DisconnectHandler)
162+
: new NativeConnection(address, DisconnectHandler))
145163
{
164+
if (logServerHeartbeatsFlag)
165+
{
166+
con.SetOnServerHeartbeatHandler((connection, time, lagMark, rtt) =>
167+
{
168+
Console.Error.WriteLine(
169+
$"##### Server time (UTC) = {time}, Server lag = {lagMark} us, RTT = {rtt} us #####");
170+
});
171+
}
172+
146173
using (var s =
147-
con.CreateIncOrderSnapshotSubscription(new SnapshotListener(recordsPrintLimit.Value)))
174+
con.CreateIncOrderSnapshotSubscription(quiteMode
175+
? (IDxIncOrderSnapshotListener) new DummyIncOrderSnapshotPrinter()
176+
: new IncOrderSnapshotPrinter(recordsPrintLimit.Value)))
148177
{
149178
s.AddSource(source.Value);
150179
s.AddSymbol(symbol);

samples/dxf_inc_order_snapshot_sample/SnapshotListener.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.

samples/dxf_inc_order_snapshot_sample/dxf_inc_order_snapshot_sample.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
<ItemGroup>
4646
<Compile Include="Program.cs" />
4747
<Compile Include="Properties\AssemblyInfo.cs" />
48-
<Compile Include="SnapshotListener.cs" />
4948
</ItemGroup>
5049
<ItemGroup>
5150
<ProjectReference Include="..\..\dxf_api\dxf_api.csproj">

0 commit comments

Comments
 (0)