From c80f232d32012e542e3ea558834b94a1c2cc5fca Mon Sep 17 00:00:00 2001 From: AKosylo Date: Mon, 4 Dec 2023 11:54:53 +0100 Subject: [PATCH] Connect Tool: Listener is not created if not required. --- DXFeedFramework.xcodeproj/project.pbxproj | 4 ++ Samples/Tools/ConnectTool.swift | 43 +++++++++------------- Samples/Tools/DumpTool.swift | 45 ++++++++++------------- Samples/Tools/EventsListener.swift | 30 +++++++++++++++ Samples/Tools/LatencyTestTool.swift | 2 +- Samples/Tools/PerfTestTool.swift | 2 +- Samples/Tools/SubscriptionUtils.swift | 7 +++- 7 files changed, 77 insertions(+), 56 deletions(-) create mode 100644 Samples/Tools/EventsListener.swift diff --git a/DXFeedFramework.xcodeproj/project.pbxproj b/DXFeedFramework.xcodeproj/project.pbxproj index cb71107d5..b152da2b9 100644 --- a/DXFeedFramework.xcodeproj/project.pbxproj +++ b/DXFeedFramework.xcodeproj/project.pbxproj @@ -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 */; }; @@ -577,6 +578,7 @@ 641BDD5A2AC72BD400236B78 /* ConcurrentWeakHashTable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConcurrentWeakHashTable.swift; sourceTree = ""; }; 641BDD5C2ACD67A000236B78 /* LatencyListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LatencyListener.swift; sourceTree = ""; }; 641BDD602ACD697B00236B78 /* AbstractEventListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbstractEventListener.swift; sourceTree = ""; }; + 641E45F82B1DE51700649363 /* EventsListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventsListener.swift; sourceTree = ""; }; 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 = ""; }; 64262CCF2A4DA64700BA6BA3 /* VisionQuoteTableAppApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VisionQuoteTableAppApp.swift; sourceTree = ""; }; @@ -966,6 +968,7 @@ 645CD0032AE145E600F99FCF /* DumpTool.swift */, 648BD56A2AC4576F004A3A95 /* HelpTool.swift */, 648BD56C2AC56A04004A3A95 /* SubscriptionUtils.swift */, + 641E45F82B1DE51700649363 /* EventsListener.swift */, ); path = Tools; sourceTree = ""; @@ -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 */, diff --git a/Samples/Tools/ConnectTool.swift b/Samples/Tools/ConnectTool.swift index 62607d2a8..446803e5b 100644 --- a/Samples/Tools/ConnectTool.swift +++ b/Samples/Tools/ConnectTool.swift @@ -55,6 +55,7 @@ Where: exit(0) } }() + private var listeners = [EventListener]() func execute() { isQuite = arguments.isQuite @@ -62,6 +63,13 @@ Where: 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 @@ -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) @@ -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)") - } - } -} diff --git a/Samples/Tools/DumpTool.swift b/Samples/Tools/DumpTool.swift index fba37fc4c..d236f18a9 100644 --- a/Samples/Tools/DumpTool.swift +++ b/Samples/Tools/DumpTool.swift @@ -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) @@ -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) @@ -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)") - } - } -} diff --git a/Samples/Tools/EventsListener.swift b/Samples/Tools/EventsListener.swift new file mode 100644 index 000000000..ee50d90cb --- /dev/null +++ b/Samples/Tools/EventsListener.swift @@ -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)) + } +} diff --git a/Samples/Tools/LatencyTestTool.swift b/Samples/Tools/LatencyTestTool.swift index 498da11fa..5e18f6353 100644 --- a/Samples/Tools/LatencyTestTool.swift +++ b/Samples/Tools/LatencyTestTool.swift @@ -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) diff --git a/Samples/Tools/PerfTestTool.swift b/Samples/Tools/PerfTestTool.swift index 3be10988e..0880ab1e1 100644 --- a/Samples/Tools/PerfTestTool.swift +++ b/Samples/Tools/PerfTestTool.swift @@ -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) diff --git a/Samples/Tools/SubscriptionUtils.swift b/Samples/Tools/SubscriptionUtils.swift index bb1ff2003..3fd7d174e 100644 --- a/Samples/Tools/SubscriptionUtils.swift +++ b/Samples/Tools/SubscriptionUtils.swift @@ -16,7 +16,7 @@ class Subscription { func createSubscription(address: String, symbols: [Symbol], types: [EventCode], - listener: O, + listeners: [O], properties: [String: String], time: String?, source: String? = nil) @@ -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")