Skip to content

Commit

Permalink
Refactoring: Weakening type requirements to give more freedom to the …
Browse files Browse the repository at this point in the history
…package consumer.
  • Loading branch information
DimaRU committed Feb 7, 2024
1 parent bda39fd commit 9407fc1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 78 deletions.
26 changes: 4 additions & 22 deletions Sources/FastRTPSSwift/DDSTopic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import Foundation

/// Common DDS reader/writer topic requirements. Must define topic name Raw value as string
public protocol DDSTopic: RawRepresentable where RawValue == String {}
public protocol DDSTopic {
var name: String { get }
var typeName: String { get }
}

/// Reader topic requirements
public protocol DDSReaderTopic: DDSTopic {
Expand All @@ -20,24 +23,3 @@ public protocol DDSWriterTopic: DDSTopic {
/// Return profile which defines Writer parameters: durability, reliability and disablePositiveACKs QoS
var writerProfile: RTPSWriterProfile { get }
}

public extension RTPSReaderProfile {
/// Initializer for RTPSReaderProfile
/// - Parameters:
/// - reliability: Reliability QoS
/// - durability: Durability QoS
init(reliability: Reliability, durability: Durability) {
self.init(keyed: false, reliability: reliability, durability: durability)
}
}

public extension RTPSWriterProfile {
/// Initializer for RTPRTPSWriterProfileSReaderProfile
/// - Parameters:
/// - reliability: Reliability QoS
/// - durability: Durability QoS
/// - disablePositiveACKs: disablePositiveACKs QoS
init(reliability: Reliability, durability: Durability, disablePositiveACKs: Bool) {
self.init(keyed: false, reliability: reliability, durability: durability, disablePositiveACKs: disablePositiveACKs)
}
}
11 changes: 1 addition & 10 deletions Sources/FastRTPSSwift/DDSType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,8 @@

import Foundation

/// DDS data types requirements
public protocol DDSType: Codable {
/// Defines DDS type name, 'DDS::String' for example
static var ddsTypeName: String { get }
}

/// Keyed DDS data type requirements
public protocol DDSKeyed: DDSType {
public protocol DDSKeyed {
/// Data for topic key generation
var key: Data { get }
}

/// Unkeyed DDS data type requirements
public protocol DDSUnkeyed: DDSType {}
71 changes: 35 additions & 36 deletions Sources/FastRTPSSwift/FastRTPSSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,11 @@ open class FastRTPSSwift {
/// - block: The block to execute when topic data arrives. This block has no return value and sequence and data parameters:
/// - sequence: topic sequence number
/// - data: topic raw binary data
public func registerReaderRaw<D: DDSType, T: DDSReaderTopic>(topic: T, ddsType: D.Type, partition: String? = nil, block: @escaping (UInt64, Data)->Void) throws {
public func registerReaderRaw<T: DDSReaderTopic>(topic: T, partition: String? = nil, block: @escaping (UInt64, Data)->Void) throws {
let payloadDecoderProxy = Unmanaged.passRetained(PayloadDecoderProxy(block: block)).toOpaque()
var profile = topic.readerProfile
profile.keyed = ddsType is DDSKeyed.Type
if !wrapper.registerReader(topicName: topic.rawValue,
typeName: D.ddsTypeName,
readerProfile: profile,
if !wrapper.registerReader(topicName: topic.name,
typeName: topic.typeName,
readerProfile: topic.readerProfile,
payloadDecoder: payloadDecoderProxy,
partition: partition) {
throw FastRTPSSwiftError.fastRTPSError
Expand All @@ -251,8 +249,8 @@ open class FastRTPSSwift {
/// - topic: DDSReader topic description
/// - block: The block to execute when topic data arrives. This block has no return value and Result<D, Error> parameter
/// with deserialized data when success deserialization or error otherwize
public func registerReader<D: DDSType, T: DDSReaderTopic>(topic: T, partition: String? = nil, block: @escaping (Result<D, Error>)->Void) throws {
try registerReaderRaw(topic: topic, ddsType: D.self, partition: partition) { (_, data) in
public func registerReader<D: Decodable, T: DDSReaderTopic>(topic: T, partition: String? = nil, block: @escaping (Result<D, Error>)->Void) throws {
try registerReaderRaw(topic: topic, partition: partition) { (_, data) in
let decoder = CDRDecoder()
let result = Result.init { try decoder.decode(D.self, from: data) }
block(result)
Expand All @@ -262,7 +260,7 @@ open class FastRTPSSwift {
/// Remove a RTPS reader for topic
/// - Parameter topic: DDSReader topic descriptor
public func removeReader<T: DDSReaderTopic>(topic: T) throws {
if !wrapper.removeReader(topicName: topic.rawValue) {
if !wrapper.removeReader(topicName: topic.name) {
throw FastRTPSSwiftError.fastRTPSError
}
}
Expand All @@ -272,12 +270,10 @@ open class FastRTPSSwift {
/// - Parameters:
/// - Parameter topic: DDSWriterTopic topic descriptor
/// - ddsType: data type descriptor
public func registerWriter<D: DDSType, T: DDSWriterTopic>(topic: T, ddsType: D.Type, partition: String? = nil) throws {
var profile = topic.writerProfile
profile.keyed = ddsType is DDSKeyed.Type
if !wrapper.registerWriter(topicName: topic.rawValue,
typeName: D.ddsTypeName,
writerProfile: profile,
public func registerWriter<T: DDSWriterTopic>(topic: T, partition: String? = nil) throws {
if !wrapper.registerWriter(topicName: topic.name,
typeName: topic.typeName,
writerProfile: topic.writerProfile,
partition: partition) {
throw FastRTPSSwiftError.fastRTPSError
}
Expand All @@ -286,40 +282,43 @@ open class FastRTPSSwift {
/// Remove RTPS writer for topic
/// - Parameter topic: DDSWriterTopic topic descriptor
public func removeWriter<T: DDSWriterTopic>(topic: T) throws {
if !wrapper.removeWriter(topicName: topic.rawValue) {
if !wrapper.removeWriter(topicName: topic.name) {
throw FastRTPSSwiftError.fastRTPSError
}
}

/// Send data change for topic
/// Send unkeyed data change for topic
/// - Parameters:
/// - Parameter topic: DDSWriter topic descriptor
/// - ddsData: data to be send
public func send<D: DDSType, T: DDSWriterTopic>(topic: T, ddsData: D) throws {
public func send<D: Encodable, T: DDSWriterTopic>(topic: T, ddsData: D) throws {
let encoder = CDREncoder()
let data = try encoder.encode(ddsData)
try data.withUnsafeBytes { dataPtr in
if ddsData is DDSKeyed {
let key = (ddsData as! DDSKeyed).key
try key.withUnsafeBytes { keyPtr in
if !wrapper.sendDataWithKey(topicName: topic.rawValue,
data: dataPtr.baseAddress!,
length: UInt32(data.count),
key: keyPtr.baseAddress!,
keyLength: UInt32(key.count)) {
throw FastRTPSSwiftError.fastRTPSError
}
}
} else {
if !wrapper.sendData(topicName: topic.rawValue,
data: dataPtr.baseAddress!,
length: UInt32(data.count)) {
throw FastRTPSSwiftError.fastRTPSError
}
if !wrapper.sendData(topicName: topic.name,
data: dataPtr.baseAddress!,
length: UInt32(data.count)) {
throw FastRTPSSwiftError.fastRTPSError
}
}
}


/// Send keyed data change for topic
/// - Parameters:
/// - Parameter topic: DDSWriter topic descriptor
/// - ddsData: data to be send
public func send<D: DDSKeyed & Encodable, T: DDSWriterTopic>(topic: T, ddsData: D) throws {
let encoder = CDREncoder()
let data = try encoder.encode(ddsData)
if !wrapper.sendDataWithKey(topicName: topic.name,
data: data.withUnsafeBytes { $0.baseAddress! },
length: UInt32(data.count),
key: ddsData.key.withUnsafeBytes { $0.baseAddress! },
keyLength: UInt32(ddsData.key.count)) {
throw FastRTPSSwiftError.fastRTPSError
}
}

/// Remove all readers and writers from participant
public func resignAll() {
wrapper.resignAll()
Expand Down
2 changes: 1 addition & 1 deletion Tests/FastRTPSSwiftTests/FastRTPSSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FastRTPSSwiftTests: XCTestCase {
func testCreateReader() throws {
try fastRTPSSwift?.createParticipant(name: "TestParticipant")
fastRTPSSwift?.setRTPSParticipantListener(delegate: self)
try fastRTPSSwift?.registerReaderRaw(topic: ReaderTopic.rovDepth, ddsType: RovDepth.self, partition: "*") { (sequence, data) in
try fastRTPSSwift?.registerReaderRaw(topic: ReaderTopic.rovDepth, partition: "*") { (sequence, data) in
print("Depth sequence, data count:", sequence, data.count)
}
print("Reader created")
Expand Down
4 changes: 1 addition & 3 deletions Tests/FastRTPSSwiftTests/RovBarometer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ struct FluidPressure: Codable {
let variance: Double
}

struct RovBarometer: DDSKeyed {
struct RovBarometer: DDSKeyed, Codable {
let pressure: FluidPressure
let id: String

var key: Data { id.data(using: .utf8)! }
static var ddsTypeName: String { "orov::msg::sensor::Barometer" }
}

3 changes: 1 addition & 2 deletions Tests/FastRTPSSwiftTests/RovDepth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ struct RovHeader: Codable {
let frameId: String
}

struct RovDepth: DDSKeyed {
struct RovDepth: DDSKeyed, Codable {
let pressure: FluidPressure
let id: String // @key
let depth: Float // Unit: meters

var key: Data { id.data(using: .utf8)! }
static var ddsTypeName: String { "orov::msg::sensor::Depth" }
}
19 changes: 15 additions & 4 deletions Tests/FastRTPSSwiftTests/RovTopic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@
import Foundation
import FastRTPSSwift

enum ReaderTopic: String, DDSReaderTopic {
enum ReaderTopic: DDSReaderTopic {

case rovDepth = "rov_depth" // orov::msg::sensor::Depth
case rovPressureInternal = "rov_pressure_internal" // orov::msg::sensor::Barometer
case rovDepth
case rovPressureInternal

var topicConfig: (name: String, typeName: String, durability: Durability, reliability: Reliability) {
switch self {
case .rovDepth: return ("rov_depth", "orov::msg::sensor::Depth", .volatile, .bestEffort)
case .rovPressureInternal: return ("rov_pressure_internal", "orov::msg::sensor::Barometer", .volatile, .bestEffort)
}
}
var name: String { topicConfig.name }
var typeName: String { topicConfig.typeName }
var readerProfile: RTPSReaderProfile {
RTPSReaderProfile(keyed: true, reliability: .bestEffort, durability: .volatile)
let config = topicConfig
return .init(keyed: false,
reliability: config.reliability,
durability: config.durability)
}
}

0 comments on commit 9407fc1

Please sign in to comment.