From 0387069beb6b1130f0bc2cf47feff01e7e5732de Mon Sep 17 00:00:00 2001 From: AKosylo Date: Fri, 1 Sep 2023 16:26:25 +0200 Subject: [PATCH] InstrumentProfileConnection: WIP --- DXFeedFramework.xcodeproj/project.pbxproj | 24 ++- .../InstrumentProfileConnectionObserver.swift | 12 ++ .../InstrumentProfileConnectionState.swift | 16 ++ .../Native/Endpoint/NativeEndpoint.swift | 21 ++- .../Native/Endpoint/WeakListener.swift | 17 -- ...InstrumentProfileConnectionState+ext.swift | 28 ++++ .../NativeInstrumentProfileCollector.swift | 7 +- .../NativeInstrumentProfileConnection.swift | 154 ++++++++++++++++++ ...eInstrumentProfileConnectionListener.swift | 12 ++ 9 files changed, 263 insertions(+), 28 deletions(-) create mode 100644 DXFeedFramework/Ipf/Live/InstrumentProfileConnectionObserver.swift create mode 100644 DXFeedFramework/Ipf/Live/InstrumentProfileConnectionState.swift delete mode 100644 DXFeedFramework/Native/Endpoint/WeakListener.swift create mode 100644 DXFeedFramework/Native/Ipf/Live/InstrumentProfileConnectionState+ext.swift create mode 100644 DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift create mode 100644 DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnectionListener.swift diff --git a/DXFeedFramework.xcodeproj/project.pbxproj b/DXFeedFramework.xcodeproj/project.pbxproj index ba666f0fb..826d98d37 100644 --- a/DXFeedFramework.xcodeproj/project.pbxproj +++ b/DXFeedFramework.xcodeproj/project.pbxproj @@ -132,7 +132,6 @@ 64656F6D2A1CFB10006A0B19 /* DXEndpointState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64656F6C2A1CFB10006A0B19 /* DXEndpointState.swift */; }; 64656F6F2A1CFC12006A0B19 /* WeakBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64656F6E2A1CFC12006A0B19 /* WeakBox.swift */; }; 64656F732A1D0A84006A0B19 /* EndpointListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64656F722A1D0A84006A0B19 /* EndpointListener.swift */; }; - 64656F752A1D0C91006A0B19 /* WeakListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64656F742A1D0C91006A0B19 /* WeakListener.swift */; }; 64656F772A1DFED9006A0B19 /* EndpointState+Native.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64656F762A1DFED9006A0B19 /* EndpointState+Native.swift */; }; 646979722A3B5AF60003A9BA /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 646979712A3B5AF60003A9BA /* Colors.xcassets */; }; 646979732A3B5AF60003A9BA /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 646979712A3B5AF60003A9BA /* Colors.xcassets */; }; @@ -212,6 +211,11 @@ 64D8BB472A39BB740071BC88 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 64D8BB462A39BB740071BC88 /* Assets.xcassets */; }; 64D8BB4A2A39BB740071BC88 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 64D8BB482A39BB740071BC88 /* LaunchScreen.storyboard */; }; 64DA26B62AA1EF47005B1757 /* ProfileIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DA26B52AA1EF47005B1757 /* ProfileIterator.swift */; }; + 64DA26B82AA20095005B1757 /* NativeInstrumentProfileConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DA26B72AA20095005B1757 /* NativeInstrumentProfileConnection.swift */; }; + 64DA26BA2AA20AAA005B1757 /* InstrumentProfileConnectionState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DA26B92AA20AAA005B1757 /* InstrumentProfileConnectionState.swift */; }; + 64DA26BC2AA20B66005B1757 /* InstrumentProfileConnectionState+ext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DA26BB2AA20B66005B1757 /* InstrumentProfileConnectionState+ext.swift */; }; + 64DA26BE2AA20EDB005B1757 /* InstrumentProfileConnectionObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DA26BD2AA20EDB005B1757 /* InstrumentProfileConnectionObserver.swift */; }; + 64DA26C02AA224EB005B1757 /* NativeInstrumentProfileConnectionListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DA26BF2AA224EB005B1757 /* NativeInstrumentProfileConnectionListener.swift */; }; 64DB79342A376E1B00229597 /* DXFeedFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 803BAC0D29BFA50700FFAB1C /* DXFeedFramework.framework */; }; 64DB79352A376E1B00229597 /* DXFeedFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 803BAC0D29BFA50700FFAB1C /* DXFeedFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 64ECD67F2A9CF4CB00B36935 /* DXIPFTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64ECD67E2A9CF4CB00B36935 /* DXIPFTest.swift */; }; @@ -449,7 +453,6 @@ 64656F6C2A1CFB10006A0B19 /* DXEndpointState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXEndpointState.swift; sourceTree = ""; }; 64656F6E2A1CFC12006A0B19 /* WeakBox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakBox.swift; sourceTree = ""; }; 64656F722A1D0A84006A0B19 /* EndpointListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EndpointListener.swift; sourceTree = ""; }; - 64656F742A1D0C91006A0B19 /* WeakListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakListener.swift; sourceTree = ""; }; 64656F762A1DFED9006A0B19 /* EndpointState+Native.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "EndpointState+Native.swift"; sourceTree = ""; }; 646979712A3B5AF60003A9BA /* Colors.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Colors.xcassets; sourceTree = ""; }; 6469F8C12A3B169A00846831 /* MathUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MathUtil.swift; sourceTree = ""; }; @@ -521,6 +524,11 @@ 64D8BB492A39BB740071BC88 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 64D8BB4B2A39BB740071BC88 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 64DA26B52AA1EF47005B1757 /* ProfileIterator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileIterator.swift; sourceTree = ""; }; + 64DA26B72AA20095005B1757 /* NativeInstrumentProfileConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeInstrumentProfileConnection.swift; sourceTree = ""; }; + 64DA26B92AA20AAA005B1757 /* InstrumentProfileConnectionState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfileConnectionState.swift; sourceTree = ""; }; + 64DA26BB2AA20B66005B1757 /* InstrumentProfileConnectionState+ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "InstrumentProfileConnectionState+ext.swift"; sourceTree = ""; }; + 64DA26BD2AA20EDB005B1757 /* InstrumentProfileConnectionObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfileConnectionObserver.swift; sourceTree = ""; }; + 64DA26BF2AA224EB005B1757 /* NativeInstrumentProfileConnectionListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeInstrumentProfileConnectionListener.swift; sourceTree = ""; }; 64ECD67E2A9CF4CB00B36935 /* DXIPFTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXIPFTest.swift; sourceTree = ""; }; 64ECD6812A9DDC2800B36935 /* InstrumentProfileReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfileReader.swift; sourceTree = ""; }; 64ECD6842A9DDF6200B36935 /* InstrumentProfileCollector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfileCollector.swift; sourceTree = ""; }; @@ -739,8 +747,11 @@ isa = PBXGroup; children = ( 646407562AA0C44D006FF769 /* NativeInstrumentProfileCollector.swift */, + 64DA26B72AA20095005B1757 /* NativeInstrumentProfileConnection.swift */, 6464075A2AA0CEF2006FF769 /* NativeExecutor.swift */, 646407582AA0CA8D006FF769 /* NativeProfileIterator.swift */, + 64DA26BB2AA20B66005B1757 /* InstrumentProfileConnectionState+ext.swift */, + 64DA26BF2AA224EB005B1757 /* NativeInstrumentProfileConnectionListener.swift */, ); path = Live; sourceTree = ""; @@ -948,6 +959,8 @@ 64ECD6862A9DDFBE00B36935 /* InstrumentProfileConnection.swift */, 6464075C2AA1C69E006FF769 /* InstrumentProfileUpdateListener.swift */, 64DA26B52AA1EF47005B1757 /* ProfileIterator.swift */, + 64DA26B92AA20AAA005B1757 /* InstrumentProfileConnectionState.swift */, + 64DA26BD2AA20EDB005B1757 /* InstrumentProfileConnectionObserver.swift */, ); path = Live; sourceTree = ""; @@ -1087,7 +1100,6 @@ 80FC415B29CA2B8800E6B611 /* NativeBuilder.swift */, 64656F582A1B864C006A0B19 /* NativeEndpoint.swift */, 64656F722A1D0A84006A0B19 /* EndpointListener.swift */, - 64656F742A1D0C91006A0B19 /* WeakListener.swift */, 64656F762A1DFED9006A0B19 /* EndpointState+Native.swift */, ); path = Endpoint; @@ -1558,6 +1570,7 @@ 640C3FD82A617D4900555161 /* CandlePriceLevel.swift in Sources */, 64656F592A1B864C006A0B19 /* NativeEndpoint.swift in Sources */, 6447A5DD2A8E56CF00739CCF /* ITimeSeriesEvent.swift in Sources */, + 64DA26BA2AA20AAA005B1757 /* InstrumentProfileConnectionState.swift in Sources */, 64278C6E2A602D2B0074B5AA /* Candle.swift in Sources */, 64ACBCE62A28BA7D00032C53 /* SymbolMapper.swift in Sources */, 64ECD6822A9DDC2800B36935 /* InstrumentProfileReader.swift in Sources */, @@ -1568,6 +1581,7 @@ 64ECD6872A9DDFBE00B36935 /* InstrumentProfileConnection.swift in Sources */, 64278C702A602FA00074B5AA /* Candle+Ext.swift in Sources */, 6464075B2AA0CEF2006FF769 /* NativeExecutor.swift in Sources */, + 64DA26B82AA20095005B1757 /* NativeInstrumentProfileConnection.swift in Sources */, 64104FC32A210F2400D1FC41 /* EventCode+Native.swift in Sources */, 80FC415C29CA2B8800E6B611 /* NativeBuilder.swift in Sources */, 6464075D2AA1C69E006FF769 /* InstrumentProfileUpdateListener.swift in Sources */, @@ -1583,6 +1597,7 @@ 6469F8C82A3B25C900846831 /* MarketEvent+Access.swift in Sources */, 640C3FD22A6178D200555161 /* CandleSession.swift in Sources */, 8088D76529C0FBCE00F240CB /* ThreadManager.swift in Sources */, + 64DA26BE2AA20EDB005B1757 /* InstrumentProfileConnectionObserver.swift in Sources */, 64656F732A1D0A84006A0B19 /* EndpointListener.swift in Sources */, 64104FCC2A2629D800D1FC41 /* NativeSubscription.swift in Sources */, 64C772012A975102009868C2 /* Reference+Util.swift in Sources */, @@ -1602,7 +1617,7 @@ 640C3FCE2A61788500555161 /* CandleExchange.swift in Sources */, 641BCBBC2A20ED8100FE23C2 /* DXEndpointObserver.swift in Sources */, 64656F772A1DFED9006A0B19 /* EndpointState+Native.swift in Sources */, - 64656F752A1D0C91006A0B19 /* WeakListener.swift in Sources */, + 64DA26BC2AA20B66005B1757 /* InstrumentProfileConnectionState+ext.swift in Sources */, 64BA92612A306BA900BE26A0 /* Profile+Ext.swift in Sources */, 64BA926B2A3072CA00BE26A0 /* TradeMapper.swift in Sources */, 6447A5E12A8E5A5400739CCF /* IndexedEventSource.swift in Sources */, @@ -1616,6 +1631,7 @@ 642BE4D22A2F5D230052340A /* TimeAndSale.swift in Sources */, 64BA92692A306E6000BE26A0 /* TradeBase.swift in Sources */, 640C3FD02A6178B400555161 /* CandlePrice.swift in Sources */, + 64DA26C02AA224EB005B1757 /* NativeInstrumentProfileConnectionListener.swift in Sources */, 8088D77D29C8DAF800F240CB /* GraalErrorCode.swift in Sources */, 64DA26B62AA1EF47005B1757 /* ProfileIterator.swift in Sources */, 8088D76929C0FC5C00F240CB /* Isolate.swift in Sources */, diff --git a/DXFeedFramework/Ipf/Live/InstrumentProfileConnectionObserver.swift b/DXFeedFramework/Ipf/Live/InstrumentProfileConnectionObserver.swift new file mode 100644 index 000000000..c0013030c --- /dev/null +++ b/DXFeedFramework/Ipf/Live/InstrumentProfileConnectionObserver.swift @@ -0,0 +1,12 @@ +// +// InstrumentProfileConnectionObserver.swift +// DXFeedFramework +// +// Created by Aleksey Kosylo on 01.09.23. +// + +import Foundation + +public protocol InstrumentProfileConnectionObserver { + func connectionDidChangeState(old: InstrumentProfileConnectionState, new: InstrumentProfileConnectionState) +} diff --git a/DXFeedFramework/Ipf/Live/InstrumentProfileConnectionState.swift b/DXFeedFramework/Ipf/Live/InstrumentProfileConnectionState.swift new file mode 100644 index 000000000..a4889f146 --- /dev/null +++ b/DXFeedFramework/Ipf/Live/InstrumentProfileConnectionState.swift @@ -0,0 +1,16 @@ +// +// InstrumentProfileConnectionState.swift +// DXFeedFramework +// +// Created by Aleksey Kosylo on 01.09.23. +// + +import Foundation + +public enum InstrumentProfileConnectionState { + case notConnected + case connecting + case connected + case completed + case closed +} diff --git a/DXFeedFramework/Native/Endpoint/NativeEndpoint.swift b/DXFeedFramework/Native/Endpoint/NativeEndpoint.swift index 31bef5e2e..a63df2061 100644 --- a/DXFeedFramework/Native/Endpoint/NativeEndpoint.swift +++ b/DXFeedFramework/Native/Endpoint/NativeEndpoint.swift @@ -9,10 +9,19 @@ import Foundation @_implementationOnly import graal_api class NativeEndpoint { + class WeakListener: WeakBox, EndpointListener { + func changeState(old: DXEndpointState, new: DXEndpointState) { + guard let endpoint = self.value else { + return + } + endpoint.changeState(old: old, new: new) + } + } + let endpoint: UnsafeMutablePointer! var listener: UnsafeMutablePointer? static let listeners = ConcurrentArray() - static let finalizeCallback: dxfg_finalize_function = { _, context in + private static let finalizeCallback: dxfg_finalize_function = { _, context in if let context = context { let endpoint: AnyObject = bridge(ptr: context) if let listener = endpoint as? WeakListener { @@ -23,7 +32,7 @@ class NativeEndpoint { } } - static let listenerCallback: dxfg_endpoint_state_change_listener_func = {_, oldState, newState, context in + private static let listenerCallback: dxfg_endpoint_state_change_listener_func = {_, oldState, newState, context in if let context = context { let endpoint: AnyObject = bridge(ptr: context) if let listener = endpoint as? WeakListener { @@ -45,7 +54,7 @@ class NativeEndpoint { } }() - deinit { + fileprivate func removeListener() { if let listener = listener { let thread = currentThread() _ = try? ErrorCheck.nativeCall(thread, @@ -53,7 +62,12 @@ class NativeEndpoint { self.endpoint, self.listener)) _ = try? ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, &(listener.pointee.handler))) + self.listener = nil } + } + + deinit { + removeListener() if let endpoint = self.endpoint { let thread = currentThread() _ = try? ErrorCheck.nativeCall(thread, @@ -67,6 +81,7 @@ class NativeEndpoint { return self.feed } func addListener(_ listener: EndpointListener) throws { + removeListener() let weakListener = WeakListener(value: listener) NativeEndpoint.listeners.append(newElement: weakListener) let voidPtr = bridge(obj: weakListener) diff --git a/DXFeedFramework/Native/Endpoint/WeakListener.swift b/DXFeedFramework/Native/Endpoint/WeakListener.swift deleted file mode 100644 index 426bff503..000000000 --- a/DXFeedFramework/Native/Endpoint/WeakListener.swift +++ /dev/null @@ -1,17 +0,0 @@ -// -// WeakListener.swift -// DXFeedFramework -// -// Created by Aleksey Kosylo on 23.05.23. -// - -import Foundation - -class WeakListener: WeakBox, EndpointListener { - func changeState(old: DXEndpointState, new: DXEndpointState) { - guard let endpoint = self.value else { - return - } - endpoint.changeState(old: old, new: new) - } -} diff --git a/DXFeedFramework/Native/Ipf/Live/InstrumentProfileConnectionState+ext.swift b/DXFeedFramework/Native/Ipf/Live/InstrumentProfileConnectionState+ext.swift new file mode 100644 index 000000000..2463bbec7 --- /dev/null +++ b/DXFeedFramework/Native/Ipf/Live/InstrumentProfileConnectionState+ext.swift @@ -0,0 +1,28 @@ +// +// InstrumentProfileConnectionState+ext.swift +// DXFeedFramework +// +// Created by Aleksey Kosylo on 01.09.23. +// + +import Foundation +@_implementationOnly import graal_api + +extension InstrumentProfileConnectionState { + static func convert(_ state: dxfg_ipf_connection_state_t) -> InstrumentProfileConnectionState? { + switch state { + case DXFG_IPF_CONNECTION_STATE_NOT_CONNECTED: + return .closed + case DXFG_IPF_CONNECTION_STATE_CONNECTING: + return .connected + case DXFG_IPF_CONNECTION_STATE_CONNECTED: + return .connecting + case DXFG_IPF_CONNECTION_STATE_COMPLETED: + return .notConnected + case DXFG_IPF_CONNECTION_STATE_CLOSED: + return .notConnected + default: + return nil + } + } +} diff --git a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileCollector.swift b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileCollector.swift index d5979eed0..0c2ca212b 100644 --- a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileCollector.swift +++ b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileCollector.swift @@ -10,13 +10,12 @@ import Foundation class NativeInstrumentProfileCollector { let collector: UnsafeMutablePointer? - var nativeListener: UnsafeMutablePointer? + private var nativeListener: UnsafeMutablePointer? - weak var listener: InstrumentProfileUpdateListener? - static let mapper = InstrumentProfileMapper() + private weak var listener: InstrumentProfileUpdateListener? + private static let mapper = InstrumentProfileMapper() static let listenerCallback: dxfg_ipf_update_listener_function = {_, nativeProfiles, context in - guard let nativeProfiles = nativeProfiles else { return } diff --git a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift new file mode 100644 index 000000000..1f9152326 --- /dev/null +++ b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnection.swift @@ -0,0 +1,154 @@ +// +// NativeInstrumentProfileConnection.swift +// DXFeedFramework +// +// Created by Aleksey Kosylo on 01.09.23. +// + +import Foundation +@_implementationOnly import graal_api + +class NativeInstrumentProfileConnection { + private let connection: UnsafeMutablePointer + private let address: String + + private static let defaultUpdatePeriod = Long(1 * 60 * 1000) // 1 minute + + var nativeListener: UnsafeMutablePointer? + weak var listener: NativeInstrumentProfileConnectionListener? + + static let listenerCallback: dxfg_ipf_connection_state_change_listener_func = {_, oldState, newState, context in + if let context = context { + let endpoint: AnyObject = bridge(ptr: context) + if let listener = endpoint as? NativeInstrumentProfileConnection { + var old = (try? EnumUtil.valueOf(value: InstrumentProfileConnectionState.convert(oldState))) + var new = (try? EnumUtil.valueOf(value: InstrumentProfileConnectionState.convert(newState))) + listener.listener?.connectionDidChangeState(old: old ?? .notConnected, + new: new ?? .notConnected) + } + } + } + + private func removeListener() { + if let listener = nativeListener { + let thread = currentThread() + _ = try? ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_removeStateChangeListener(thread, + connection, + listener)) + _ = try? ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, &(listener.pointee.handler))) + } + } + + deinit { + removeListener() + + let thread = currentThread() + _ = try? ErrorCheck.nativeCall(thread, dxfg_JavaObjectHandler_release(thread, + &(connection.pointee.handler))) + } + + init(_ collector: NativeInstrumentProfileCollector, _ address: String) throws { + let thread = currentThread() + connection = try ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_createConnection(thread, + address.toCStringRef(), + collector.collector)) + self.address = address + } + + func getAddress() -> String { + let thread = currentThread() + let result = try? ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_getAddress( + thread, + connection)) + + return String(pointee: result, default: address) + } + + func getUpdatePeriod() -> Long { + let thread = currentThread() + let result = try? ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_getUpdatePeriod( + thread, + connection)) + return result ?? NativeInstrumentProfileConnection.defaultUpdatePeriod + } + + func setUpdatePeriod(_ value: Long) { + let thread = currentThread() + _ = try? ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_setUpdatePeriod( + thread, + connection, + value)) + } + + private func getState() throws -> InstrumentProfileConnectionState { + let thread = currentThread() +#warning("TODO: Check undefined symbol for dxfg_InstrumentProfileConnection_getState") + + // let result = try ErrorCheck.nativeCall(thread, + // dxfg_InstrumentProfileConnection_getState( + // thread, + // connection)) + // if let state = InstrumentProfileConnectionState.convert(result) { + // return state + // } + return InstrumentProfileConnectionState.notConnected + } + + func getLastModified() -> Long { + let thread = currentThread() + let result = try? ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_getLastModified( + thread, + connection)) + return result ?? 0 + } + + func start() throws { + let thread = currentThread() + _ = try ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_start( + thread, + connection)) + } + + func close() throws { + let thread = currentThread() + _ = try ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_close( + thread, + connection)) + } + + func addListener(_ listener: NativeInstrumentProfileConnectionListener) throws { + removeListener() + let voidPtr = bridge(obj: self) + let thread = currentThread() + let listener = try ErrorCheck.nativeCall(thread, + dxfg_IpfPropertyChangeListener_new( + thread, + NativeInstrumentProfileConnection.listenerCallback, + voidPtr)) + self.nativeListener = listener + + try ErrorCheck.nativeCall(currentThread(), + dxfg_InstrumentProfileConnection_addStateChangeListener( + thread, + connection, + self.nativeListener)) + } + + func waitUntilCompleted(_ timeInMs: Long) { + let thread = currentThread() + _ = try? ErrorCheck.nativeCall(thread, + dxfg_InstrumentProfileConnection_waitUntilCompleted( + thread, + connection, + timeInMs)) + } + +} diff --git a/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnectionListener.swift b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnectionListener.swift new file mode 100644 index 000000000..58b549a19 --- /dev/null +++ b/DXFeedFramework/Native/Ipf/Live/NativeInstrumentProfileConnectionListener.swift @@ -0,0 +1,12 @@ +// +// NativeInstrumentProfileConnectionListener.swift +// DXFeedFramework +// +// Created by Aleksey Kosylo on 01.09.23. +// + +import Foundation + +public protocol NativeInstrumentProfileConnectionListener: AnyObject { + func connectionDidChangeState(old: InstrumentProfileConnectionState, new: InstrumentProfileConnectionState) +}