Skip to content

Commit

Permalink
add CLI DXFeedLiveIpfSample
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Oct 9, 2023
1 parent 89516a5 commit 8c32edc
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
4 changes: 4 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
647426AD2ABC85F20012F793 /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647426AC2ABC85F20012F793 /* ArgumentParser.swift */; };
647426AF2ABC93900012F793 /* EventCode+String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647426AE2ABC93900012F793 /* EventCode+String.swift */; };
6479BC2B2A4F35A300A3D404 /* QuoteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6479BC2A2A4F35A300A3D404 /* QuoteView.swift */; };
6486B97F2AD4167800D8D5FA /* LiveIpfCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6486B97E2AD4167800D8D5FA /* LiveIpfCommand.swift */; };
648BD5692AC450D6004A3A95 /* ConnectCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648BD5682AC450D6004A3A95 /* ConnectCommand.swift */; };
648BD56B2AC4576F004A3A95 /* HelpCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648BD56A2AC4576F004A3A95 /* HelpCommand.swift */; };
648BD56D2AC56A04004A3A95 /* SubscriptionUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648BD56C2AC56A04004A3A95 /* SubscriptionUtils.swift */; };
Expand Down Expand Up @@ -608,6 +609,7 @@
647426AC2ABC85F20012F793 /* ArgumentParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentParser.swift; sourceTree = "<group>"; };
647426AE2ABC93900012F793 /* EventCode+String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "EventCode+String.swift"; sourceTree = "<group>"; };
6479BC2A2A4F35A300A3D404 /* QuoteView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuoteView.swift; sourceTree = "<group>"; };
6486B97E2AD4167800D8D5FA /* LiveIpfCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveIpfCommand.swift; sourceTree = "<group>"; };
648BD5682AC450D6004A3A95 /* ConnectCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectCommand.swift; sourceTree = "<group>"; };
648BD56A2AC4576F004A3A95 /* HelpCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelpCommand.swift; sourceTree = "<group>"; };
648BD56C2AC56A04004A3A95 /* SubscriptionUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionUtils.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -851,6 +853,7 @@
641BDD572AC71CCE00236B78 /* LatencyTestCommand.swift */,
645BE8512AC31E7C0028243D /* PerfTestCommand.swift */,
648BD5682AC450D6004A3A95 /* ConnectCommand.swift */,
6486B97E2AD4167800D8D5FA /* LiveIpfCommand.swift */,
648BD56A2AC4576F004A3A95 /* HelpCommand.swift */,
648BD56C2AC56A04004A3A95 /* SubscriptionUtils.swift */,
);
Expand Down Expand Up @@ -1847,6 +1850,7 @@
647426AF2ABC93900012F793 /* EventCode+String.swift in Sources */,
645BE8542AC3229D0028243D /* ToolsCommand.swift in Sources */,
644FE5D12AC1F34000580E3A /* LatencyMetricsPrinter.swift in Sources */,
6486B97F2AD4167800D8D5FA /* LiveIpfCommand.swift in Sources */,
645BE8522AC31E7C0028243D /* PerfTestCommand.swift in Sources */,
641BDD5D2ACD67A000236B78 /* LatencyListener.swift in Sources */,
641BDD622ACD697B00236B78 /* AbstractEventListener.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<CommandLineArguments>
<CommandLineArgument
argument = "PerfTest localhost:6666 TimeAndSale YQKNT"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "DXFeedLiveIpfSample https://demo:[email protected]/ipf"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
Expand Down
1 change: 1 addition & 0 deletions Samples/IpfTableApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ViewController: UIViewController {
try connection?.start()
// We can wait until we get first full snapshot of instrument profiles
connection?.waitUntilCompleted(5000)
// It is possible to add listener after connection is started - updates will not be missed in this case
try collector?.add(observer: self)
} catch {
print("Error during creation IPF data source: \(error)")
Expand Down
106 changes: 106 additions & 0 deletions Samples/PerfTestCL/LiveIpfCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// LiveIpfCommand.swift
// Tools
//
// Created by Aleksey Kosylo on 09.10.23.
//

import Foundation
import DXFeedFramework

class LiveIpfCommand: ToolsCommand {
lazy var name = {
stringReference(self)
}()

private var ipfList = [InstrumentProfile]()
private var buffer = [String: InstrumentProfile]()

static let defaultIpfUrl = "https://demo:[email protected]/ipf"

var cmd = "DXFeedLiveIpfSample"

var shortDescription = "An sample that demonstrates a subscription using InstrumentProfile."

var fullDescription: String =
"""
Connects to the specified address(es) and calculates latency.
Usage:
usage: DXFeedLiveIpfSample [<ipf-url>]
Where:
where: <ipf-url> is URL for the instruments profiles.
Example of url: " + \(LiveIpfCommand.defaultIpfUrl)
"""
var collector: DXInstrumentProfileCollector?
var connection: DXInstrumentProfileConnection?

func execute() {
var arguments: [String]!
do {
arguments = try ArgumentParser().parse(ProcessInfo.processInfo.arguments, requiredNumberOfArguments: 1)
} catch {
print(fullDescription)
}
do {
collector = try DXInstrumentProfileCollector()
connection = try DXInstrumentProfileConnection(arguments[1], collector!)
// Update period can be used to re-read IPF files, not needed for services supporting IPF "live-update"
try connection?.setUpdatePeriod(60000)
connection?.add(observer: self)
try connection?.start()
// We can wait until we get first full snapshot of instrument profiles
connection?.waitUntilCompleted(10000)
// It is possible to add listener after connection is started - updates will not be missed in this case
try collector?.add(observer: self)
} catch {
print("Error: \(error)")
}

_ = readLine()
}
}

extension LiveIpfCommand: DXInstrumentProfileConnectionObserver {
func connectionDidChangeState(old: DXInstrumentProfileConnectionState, new: DXInstrumentProfileConnectionState) {
print("Connection state: \(new)")
}
}

extension LiveIpfCommand: DXInstrumentProfileUpdateListener {
func instrumentProfilesUpdated(_ instruments: [DXFeedFramework.InstrumentProfile]) {
instruments.forEach { ipf in
if ipf.getIpfType() == .removed {
self.buffer.removeValue(forKey: ipf.symbol)
} else {
self.buffer[ipf.symbol] = ipf
}
}
self.ipfList = self.buffer.map { _, value in
value
}.sorted(by: { ipf1, ipf2 in
ipf1.symbol < ipf2.symbol
})
print(
"""
Instrument Profiles:
Total number of profiles (1): \(self.ipfList.count)
Last modified: \(TimeUtil.toLocalDateString(millis: collector?.getLastUpdateTime() ?? 0))
"""
)
}
}

extension LiveIpfCommand: Hashable {
static func == (lhs: LiveIpfCommand, rhs: LiveIpfCommand) -> Bool {
return lhs === rhs || lhs.name == rhs.name
}

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

}
6 changes: 5 additions & 1 deletion Samples/PerfTestCL/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import Foundation

let commands: [ToolsCommand] = [PerfTestCommand(), ConnectCommand(), LatencyTestCommand(), HelpCommand()]
let commands: [ToolsCommand] = [PerfTestCommand(),
ConnectCommand(),
LatencyTestCommand(),
LiveIpfCommand(),
HelpCommand()]

func getCommand(_ cmd: String) -> ToolsCommand? {
return commands.first { command in
Expand Down

0 comments on commit 8c32edc

Please sign in to comment.