Skip to content

Commit

Permalink
add documentations for API
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Sep 8, 2023
1 parent 1ef31c7 commit 7e20dd7
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 9 deletions.
6 changes: 6 additions & 0 deletions DXFeedFramework/Api/DXEndpointObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

import Foundation

/// Notifies a change in the state of this endpoint.
public protocol DXEndpointObserver {
/// Fired when state changed
///
/// - Parameters:
/// - old: The old state of endpoint
/// - new: The new state of endpoint
func endpointDidChangeState(old: DXEndpointState, new: DXEndpointState)
}
6 changes: 6 additions & 0 deletions DXFeedFramework/Api/DXEndpointState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@

import Foundation

/// A list of endpoint states.
public enum DXEndpointState {
/// Endpoint was created by is not connected to remote endpoints.
case notConnected
/// The ``DXEndpoint/connect(_:)`` method was called to establish connection to remove endpoint,
/// but connection is not actually established yet or was lost.
case connecting
/// The connection to remote endpoint is established.
case connected
/// Endpoint was ``DXEndpoint/close()``
case closed
}
7 changes: 6 additions & 1 deletion DXFeedFramework/Api/DXEventListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
//

import Foundation

/// The listener delegate for receiving events.
///
public protocol DXEventListener: AnyObject {
/// Invoked when events of type are received.
///
/// - Parameters:
/// - events: The collection of received events.
func receiveEvents(_ events: [MarketEvent])
}
49 changes: 47 additions & 2 deletions DXFeedFramework/Api/DXFeedSubcription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

import Foundation

/// Subscription for a set of symbols and event types.
///
/// [Read it first Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/DXFeedSubscription.html)
public class DXFeedSubcription {
/// Subscription native wrapper.
private let native: NativeSubscription
/// List of event types associated with this ``DXFeedSubscription``
fileprivate let events: Set<EventCode>
/// A set listeners of events
/// observers - typed list wrapper.
private let listeners = ConcurrentSet<AnyHashable>()

internal init(native: NativeSubscription?, events: [EventCode]) throws {
Expand All @@ -20,7 +27,14 @@ public class DXFeedSubcription {
}
self.events = Set(events)
}

/// Adds listener for events.
/// Event lister can be added only when subscription is not producing any events.
/// The subscription must be either empty (no symbols have been added)
/// or not attached to any feed.
/// This method does nothing if this subscription is closed.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/DXFeedSubscription.html#addEventListener-com.dxfeed.api.DXFeedEventListener)
/// - Throws: GraalException. Rethrows exception from Java.
public func add<O>(observer: O) throws
where O: DXEventListener,
O: Hashable {
Expand All @@ -31,20 +45,51 @@ public class DXFeedSubcription {
}
listeners.insert(observer)
}

/// Removes listener for events.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/DXFeedSubscription.html#addEventListener-com.dxfeed.api.DXFeedEventListener-)
public func remove<O>(observer: O)
where O: DXEventListener,
O: Hashable {
listeners.remove(observer)
}

/// Adds the specified symbol to the set of subscribed symbols.
///
/// All registered event listeners will receive update on the last events for
/// newly added symbol.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/DXFeedSubscription.html#addSymbols-java.util.Collection-)
/// - Parameters:
/// - symbol: One symbol
/// - Throws: GraalException. Rethrows exception from Java.
public func addSymbols(_ symbol: Symbol) throws {
try native.addSymbols(symbol)
}

/// Adds the specified collection of symbols to the set of subscribed symbols.
///
/// All registered event listeners will receive update on the last events for all
/// newly added symbols.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/DXFeedSubscription.html#addSymbols-java.util.Collection-)
/// - Parameters:
/// - symbol: The collection of symbols.
/// - Throws: GraalException. Rethrows exception from Java.
public func addSymbols(_ symbols: [Symbol]) throws {
try native.addSymbols(symbols)
}

/// Removes the specified collection of symbols from the set of subscribed symbols.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/DXFeedSubscription.html#removeSymbols-java.util.Collection-)
/// - Parameters:
/// - symbol: The collection of symbols.
/// - Throws: GraalException. Rethrows exception from Java.
public func removeSymbols(_ symbols: [Symbol]) throws {
try native.removeSymbols(symbols)
}

}

extension DXFeedSubcription: DXEventListener {
Expand Down
3 changes: 3 additions & 0 deletions DXFeedFramework/Api/DXPublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import Foundation

/// Provides API for publishing of events to local or remote ``DXFeed``
///
/// [Read it first Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/DXPublisher.html)
public class DXPublisher {
#warning("TODO: implement it")

Expand Down
16 changes: 16 additions & 0 deletions DXFeedFramework/Api/Osub/IObservableSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@

import Foundation

/// Observable set of subscription symbols for the specific event type.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/osub/ObservableSubscription.html)
public protocol IObservableSubscription {
/// Gets a value indicating whether if this subscription is closed.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/osub/ObservableSubscription.html#isClosed--)
func isClosed() -> Bool
/// Gets a set of subscribed event types. The resulting set cannot be modified.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/osub/ObservableSubscription.html#getEventTypes--)
/// - Returns: a set of subscribed event types.
var eventTypes: Set<EventCode> { get }
/// Gets a value indicating whether if this subscription contains the corresponding event type.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/osub/ObservableSubscription.html#containsEventType-java.lang.Class-)
/// - Parameters:
/// - eventType: The event type.
/// - Returns: **true** if this subscription contains the corresponding event type
func isContains(_ eventType: EventCode) -> Bool
}
23 changes: 21 additions & 2 deletions DXFeedFramework/Api/Osub/IndexedEventSubscriptionSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,34 @@

import Foundation

/// Represents subscription to a specific source of indexed events.
///
/// Instances of this class can be used with ``DXFeedSubcription``
/// to specify subscription to a particular source of indexed events.
/// By default, when subscribing to indexed events by their event symbol object,
/// the subscription is performed to all supported sources.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/osub/IndexedEventSubscriptionSymbol.html)
///
/// `T`: The type of event symbol
public class IndexedEventSubscriptionSymbol<T: Equatable>: Symbol {
/// Gets event symbol.
let symbol: T
/// Gets indexed event source. ``IndexedEventSource``
let source: IndexedEventSource
init(symbol: T, source: IndexedEventSource) {
/// Initializes a new instance of the ``IndexedEventSubscriptionSymbol`` class
/// with a specified event symbol and source.
///
/// - Parameters:
/// - symbol: The event symbol.
/// - source: The event source.
public init(symbol: T, source: IndexedEventSource) {
self.symbol = symbol
self.source = source
}

func toString() -> String {
/// Custom symbol has to return string representation.
public var stringValue: String {
return "\(symbol)source=\(source.toString())"
}
}
Expand Down
1 change: 1 addition & 0 deletions DXFeedFramework/Api/Osub/String+Symbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation

/// String extends Symbol protocol for using inside ``DXFeedSubcription``
extension String: Symbol {
public var stringValue: String {
return description
Expand Down
5 changes: 5 additions & 0 deletions DXFeedFramework/Api/Osub/Symbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

import Foundation

/// Protocol for implement your custom symbol
///
/// Dervied from this procol can be used with ``DXFeedSubcription`` to specify subscription

public protocol Symbol {
/// Custom symbol has to return string representation.
var stringValue: String { get }
}

Expand Down
26 changes: 23 additions & 3 deletions DXFeedFramework/Api/Osub/TimeSeriesSubscriptionSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@
//

import Foundation

/// Represents subscription to time-series of events.
///
/// Instances of this class can be used with ``DXFeedSubcription`` to specify subscription
/// for time series events from a specific time. By default, subscribing to time-series events by
/// their event symbol object, the subscription is performed to a stream of new events as they happen only.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/osub/TimeSeriesSubscriptionSymbol.html)
///
/// `T`: The type of event symbol.
public class TimeSeriesSubscriptionSymbol: IndexedEventSubscriptionSymbol<AnyHashable> {
let fromTime: Long
/// Initializes a new instance of the ``TimeSeriesSubscriptionSymbol`` class
/// with a specified event symbol and from time in milliseconds since Unix epoch.
///
/// - Parameters:
/// - symbol: The event symbol.
/// - fromTime: The from time is measured in milliseconds between the current time and midnight, January 1, 1970 UTC.
public init(symbol: AnyHashable, fromTime: Long) {
self.fromTime = fromTime
super.init(symbol: symbol, source: IndexedEventSource.defaultSource)
}

/// Initializes a new instance of the ``TimeSeriesSubscriptionSymbol`` class
/// with a specified event symbol and from date
///
/// - Parameters:
/// - symbol: The event symbol.
/// - date: Date. Just for easing initialization with date object
convenience public init(symbol: AnyHashable, date: Date) {
self.init(symbol: symbol, fromTime: Long(date.timeIntervalSince1970) * 1000)
}
Expand All @@ -22,7 +41,8 @@ public class TimeSeriesSubscriptionSymbol: IndexedEventSubscriptionSymbol<AnyHas
return lhs === rhs || lhs.symbol == rhs.symbol
}

public var stringValue: String {
/// Custom symbol has to return string representation.
override public var stringValue: String {
return "\(symbol){fromTime=\(TimeUtil.toLocalDateString(millis: fromTime))}"
}
}
Expand Down
27 changes: 26 additions & 1 deletion DXFeedFramework/Api/Osub/WildcardSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,42 @@
//

import Foundation

/// Represents [wildcard] subscription to all events of the specific event type.
///
/// The ``all`` constant can be added to any ``DXFeedSubcription`` instance with ``DXFeedSubcription/addSymbol`` method
/// to the effect of subscribing to all possible event symbols. The corresponding subscription will start
/// receiving all published events of the corresponding types.
///
/// [Javadoc](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/api/osub/WildcardSymbol.html)
public class WildcardSymbol: Symbol {
/// Symbol prefix that is reserved for wildcard subscriptions.
/// Any subscription starting with "*" is ignored with the exception of ``WildcardSymbol`` subscription.
static let reservedPrefix = "*"
private let symbol: String

/// Represents [wildcard] subscription to all events of the specific event type.
///
///
/// **Note**
///
/// Wildcard subscription can create extremely high network and CPU load for certain kinds of
/// high-frequency events like quotes. It requires a special arrangement on the side of upstream data provider and
/// is disabled by default in upstream feed configuration.
/// Make that sure you have adequate resources and understand the impact before using it.
/// It can be used for low-frequency events only (like Forex quotes), because each instance
/// of ``DXFeedSubcription`` processes events in a single thread
/// and there is no provision to load-balance wildcard
/// subscription amongst multiple threads.
public static let all = WildcardSymbol(symbol: reservedPrefix)

/// Initializes a new instance of the ``WildcardSymbol`` class.
/// - Parameters:
/// - symbol: The wildcard symbol
private init(symbol: String) {
self.symbol = symbol
}

/// Custom symbol has to return string representation.
public var stringValue: String {
return symbol
}
Expand Down
18 changes: 18 additions & 0 deletions DXFeedFramework/Native/Subscription/NativeSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ class NativeSubscription {
self.subscription,
listPointer))
}

func removeSymbols(_ symbols: [Symbol]) throws {
let nativeSymbols = symbols.compactMap { SymbolMapper.newNative($0) }
let elements = ListNative(pointers: nativeSymbols)
let listPointer = elements.newList()
defer {
listPointer.deinitialize(count: 1)
listPointer.deallocate()
nativeSymbols.forEach { SymbolMapper.clearNative(symbol: $0) }
}
let thread = currentThread()
_ = try ErrorCheck.nativeCall(thread,
dxfg_DXFeedSubscription_removeSymbols(thread,
self.subscription,
listPointer))

}

func isClosed() -> Bool {
let thread = currentThread()
let success = try? ErrorCheck.nativeCall(thread, dxfg_DXFeedSubscription_isClosed(thread, self.subscription))
Expand Down

0 comments on commit 7e20dd7

Please sign in to comment.