Skip to content

Commit

Permalink
Connect Tool:
Browse files Browse the repository at this point in the history
Listener is not created if not required.
  • Loading branch information
kosyloa committed Dec 4, 2023
1 parent c9e8d12 commit c80f232
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 56 deletions.
4 changes: 4 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
641BDD5E2ACD67A400236B78 /* LatencyListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 641BDD5C2ACD67A000236B78 /* LatencyListener.swift */; };
641BDD612ACD697B00236B78 /* AbstractEventListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 641BDD602ACD697B00236B78 /* AbstractEventListener.swift */; };
641BDD622ACD697B00236B78 /* AbstractEventListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 641BDD602ACD697B00236B78 /* AbstractEventListener.swift */; };
641E45F92B1DE51700649363 /* EventsListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 641E45F82B1DE51700649363 /* EventsListener.swift */; };
642528D02A3C534D00A04E41 /* TimeInterval+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8CD2A3B2F9900846831 /* TimeInterval+Ext.swift */; };
642528D12A3C534D00A04E41 /* TimeInterval+Ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8CD2A3B2F9900846831 /* TimeInterval+Ext.swift */; };
64262CCE2A4DA64700BA6BA3 /* RealityKitContent in Frameworks */ = {isa = PBXBuildFile; platformFilters = (xros, ); productRef = 64262CCD2A4DA64700BA6BA3 /* RealityKitContent */; };
Expand Down Expand Up @@ -577,6 +578,7 @@
641BDD5A2AC72BD400236B78 /* ConcurrentWeakHashTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConcurrentWeakHashTable.swift; sourceTree = "<group>"; };
641BDD5C2ACD67A000236B78 /* LatencyListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LatencyListener.swift; sourceTree = "<group>"; };
641BDD602ACD697B00236B78 /* AbstractEventListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbstractEventListener.swift; sourceTree = "<group>"; };
641E45F82B1DE51700649363 /* EventsListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventsListener.swift; sourceTree = "<group>"; };
64262CC92A4DA64600BA6BA3 /* DXVisionQuoteTableApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DXVisionQuoteTableApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
64262CCC2A4DA64700BA6BA3 /* RealityKitContent */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = RealityKitContent; sourceTree = "<group>"; };
64262CCF2A4DA64700BA6BA3 /* VisionQuoteTableAppApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VisionQuoteTableAppApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -966,6 +968,7 @@
645CD0032AE145E600F99FCF /* DumpTool.swift */,
648BD56A2AC4576F004A3A95 /* HelpTool.swift */,
648BD56C2AC56A04004A3A95 /* SubscriptionUtils.swift */,
641E45F82B1DE51700649363 /* EventsListener.swift */,
);
path = Tools;
sourceTree = "<group>";
Expand Down Expand Up @@ -2047,6 +2050,7 @@
6486B97F2AD4167800D8D5FA /* LiveIpfSample.swift in Sources */,
645BE8522AC31E7C0028243D /* PerfTestTool.swift in Sources */,
645CD0042AE145E600F99FCF /* DumpTool.swift in Sources */,
641E45F92B1DE51700649363 /* EventsListener.swift in Sources */,
641BDD5D2ACD67A000236B78 /* LatencyListener.swift in Sources */,
641BDD622ACD697B00236B78 /* AbstractEventListener.swift in Sources */,
641BDD582AC71CCE00236B78 /* LatencyTestTool.swift in Sources */,
Expand Down
43 changes: 17 additions & 26 deletions Samples/Tools/ConnectTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,21 @@ Where:
exit(0)
}
}()
private var listeners = [EventListener]()

func execute() {
isQuite = arguments.isQuite

arguments.properties.forEach { key, value in
try? SystemProperty.setProperty(key, value)
}
if !isQuite {
listeners.append(EventListener(callback: { events in
events.forEach { event in
print(event.toString())
}
}))
}

if let tapeFile = arguments.tape {
outputEndpoint = try? DXEndpoint
Expand All @@ -73,11 +81,19 @@ Where:
.build()
_ = try? outputEndpoint?.connect("tape:\(tapeFile)")
publisher = outputEndpoint?.getPublisher()
listeners.append(EventListener(callback: { [weak self] events in
do {
_ = try self?.publisher?.publish(events: events)
} catch {
print("Connect tool publish error: \(error)")
}
}))
}

subscription.createSubscription(address: arguments[1],
symbols: arguments.parseSymbols(at: 3),
types: arguments.parseTypes(at: 2),
listener: self,
listeners: listeners,
properties: arguments.properties,
time: arguments.time,
source: arguments.source)
Expand All @@ -86,28 +102,3 @@ Where:
_ = readLine()
}
}

extension ConnectTool: Hashable {
static func == (lhs: ConnectTool, rhs: ConnectTool) -> Bool {
return lhs === rhs
}

func hash(into hasher: inout Hasher) {
hasher.combine(stringReference(self))
}
}

extension ConnectTool: DXEventListener {
func receiveEvents(_ events: [DXFeedFramework.MarketEvent]) {
do {
if !isQuite {
events.forEach { event in
print(event.toString())
}
}
_ = try publisher?.publish(events: events)
} catch {
print("Connect tool publish error: \(error)")
}
}
}
45 changes: 19 additions & 26 deletions Samples/Tools/DumpTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,21 @@ class DumpTool: ToolsCommand {
}
}()

private var listeners = [EventListener]()

func execute() {
let address = arguments[1]
let symbols = arguments.parseSymbols(at: 3)

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)
Expand All @@ -82,9 +91,18 @@ class DumpTool: ToolsCommand {
.build()
try outputEndpoint?.connect("tape:\(tapeFile)")
publisher = outputEndpoint?.getPublisher()
listeners.append(EventListener(callback: { [weak self] events in
do {
_ = try self?.publisher?.publish(events: events)
} catch {
print("Connect tool publish error: \(error)")
}
}))
}
try listeners.forEach { listener in
try subscription?.add(listener: listener)

try subscription?.add(listener: self)
}
try subscription?.addSymbols(symbols)

try inputEndpoint.connect(address)
Expand All @@ -99,28 +117,3 @@ class DumpTool: ToolsCommand {
}
}
}

extension DumpTool: Hashable {
static func == (lhs: DumpTool, rhs: DumpTool) -> Bool {
return lhs === rhs
}

func hash(into hasher: inout Hasher) {
hasher.combine(stringReference(self))
}
}

extension DumpTool: DXEventListener {
func receiveEvents(_ events: [DXFeedFramework.MarketEvent]) {
do {
if !isQuite {
events.forEach { event in
print(event.toString())
}
}
try publisher?.publish(events: events)
} catch {
print("Dump tool publish error: \(error)")
}
}
}
30 changes: 30 additions & 0 deletions Samples/Tools/EventsListener.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// EventsListener.swift
// Tools
//
// Created by Aleksey Kosylo on 04.12.23.
//

import Foundation
import DXFeedFramework

class EventListener: DXEventListener, Hashable {

let callback: ([MarketEvent]) -> Void

init(callback: @escaping ([MarketEvent]) -> Void) {
self.callback = callback
}

func receiveEvents(_ events: [DXFeedFramework.MarketEvent]) {
callback(events)
}

static func == (lhs: EventListener, rhs: EventListener) -> Bool {
return lhs === rhs
}

func hash(into hasher: inout Hasher) {
hasher.combine(stringReference(self))
}
}
2 changes: 1 addition & 1 deletion Samples/Tools/LatencyTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class LatencyTestTool: ToolsCommand {
subscription.createSubscription(address: address,
symbols: arguments.parseSymbols(at: 3),
types: types,
listener: listener,
listeners: [listener],
properties: arguments.properties,
time: nil)

Expand Down
2 changes: 1 addition & 1 deletion Samples/Tools/PerfTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PerfTestTool: ToolsCommand {
subscription.createSubscription(address: address,
symbols: arguments.parseSymbols(at: 3),
types: arguments.parseTypes(at: 2),
listener: listener,
listeners: [listener],
properties: arguments.properties,
time: nil)

Expand Down
7 changes: 5 additions & 2 deletions Samples/Tools/SubscriptionUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Subscription {
func createSubscription<O>(address: String,
symbols: [Symbol],
types: [EventCode],
listener: O,
listeners: [O],
properties: [String: String],
time: String?,
source: String? = nil)
Expand All @@ -34,7 +34,10 @@ Create subscription to \(address) for \(types): \
_ = try? endpoint?.connect(address)
types.forEach { str in
let subscription = try? endpoint?.getFeed()?.createSubscription(str)
try? subscription?.add(listener: listener)

listeners.forEach { listener in
try? subscription?.add(listener: listener)
}
if time != nil {
guard let date: Date = try? DXTimeFormat.defaultTimeFormat?.parse(time!) else {
fatalError("Couldn't parse string \(time ?? "") to Date object")
Expand Down

0 comments on commit c80f232

Please sign in to comment.