-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDumpTool.swift
119 lines (101 loc) · 4.34 KB
/
DumpTool.swift
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
110
111
112
113
114
115
116
117
118
119
//
// Copyright (C) 2023 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/.
//
import Foundation
import DXFeedFramework
class DumpTool: ToolsCommand {
var isTools: Bool = true
var cmd: String = "Dump"
var shortDescription = "Dumps all events received from address."
var fullDescription =
"""
Dumps all events received from address.
Enforces a streaming contract for subscription. A wildcard enabled by default.
This was designed to receive data from a file.
Usage: Dump <address> <types> <symbols> [<options>]
Where:
<address> is a URL to Schedule API defaults file
<types> is comma-separated list of dxfeed event types ({eventTypeNames}).
If <types> is not specified, creates a subscription for all available event types.
<symbol> is comma-separated list of symbol names to get events for (e.g. ""IBM,AAPL,MSFT"").
for Candle event specify symbol with aggregation like in ""AAPL{{=d}}""
If <symbol> is not specified, the wildcard symbol is used.
Usage:
Dump <address> [<options>]
Dump <address> <types> [<options>]
Dump <address> <types> <symbols> [<options>]
Sample: Dump demo.dxfeed.com:7300 quote AAPL,IBM,ETH/USD:GDAX -t "tape_test.txt[format=text]"
Sample: Dump tapeK2.tape[speed=max] all all -q -t ios_tapeK2.tape
Sample: Dump tapeK2.tape[speed=max] -q -t ios_tapeK2.tape
"""
var publisher: DXPublisher?
var isQuite = false
private lazy var arguments: Arguments = {
do {
let arguments = try Arguments(ProcessInfo.processInfo.arguments, requiredNumberOfArguments: 4)
return arguments
} catch {
print(fullDescription)
exit(0)
}
}()
private var listeners = [EventListener]()
fileprivate func close(_ inputEndpoint: DXEndpoint, _ outputEndpoint: DXEndpoint?) throws {
try inputEndpoint.awaitNotConnected()
try inputEndpoint.closeAndAwaitTermination()
try outputEndpoint?.awaitProcessed()
try outputEndpoint?.closeAndAwaitTermination()
}
func execute() {
let address = arguments[1]
let symbols = arguments.parseSymbols()
isQuite = arguments.isQuite
if !isQuite {
listeners.append(EventListener(callback: { events in
events.forEach { event in
print(event.toString())
}
}))
}
do {
try arguments.properties.forEach { key, value in
try SystemProperty.setProperty(key, value)
}
let inputEndpoint = try DXEndpoint
.builder()
.withRole(.streamFeed)
.withProperty(DXEndpoint.Property.wildcardEnable.rawValue, "true")
.withProperties(arguments.properties)
.withName("DumpTool")
.build()
let eventTypes = arguments.parseTypes(at: 2)
let subscription = try inputEndpoint.getFeed()?.createSubscription(eventTypes)
var outputEndpoint: DXEndpoint?
if let tapeFile = arguments.tape {
outputEndpoint = try DXEndpoint
.builder()
.withRole(.streamPublisher)
.withProperty(DXEndpoint.Property.wildcardEnable.rawValue, "true")
.withName("DumpTool")
.build()
try outputEndpoint?.connect("tape:\(tapeFile)")
publisher = outputEndpoint?.getPublisher()
listeners.append(EventListener(callback: { [weak self] events in
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.3) {
try? self?.publisher?.publish(events: events)
}
}))
}
try listeners.forEach { listener in
try subscription?.add(listener: listener)
}
try subscription?.addSymbols(symbols)
try inputEndpoint.connect(address)
try close(inputEndpoint, outputEndpoint)
} catch {
print("Dump tool error: \(error)")
}
}
}