diff --git a/Sources/FastRTPSSwift/DDSTopic.swift b/Sources/FastRTPSSwift/DDSTopic.swift index 0ed2b85..4f74eef 100644 --- a/Sources/FastRTPSSwift/DDSTopic.swift +++ b/Sources/FastRTPSSwift/DDSTopic.swift @@ -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 { @@ -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) - } -} diff --git a/Sources/FastRTPSSwift/DDSType.swift b/Sources/FastRTPSSwift/DDSType.swift index 32fb604..f0d80d6 100644 --- a/Sources/FastRTPSSwift/DDSType.swift +++ b/Sources/FastRTPSSwift/DDSType.swift @@ -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 {} diff --git a/Sources/FastRTPSSwift/FastRTPSSwift.swift b/Sources/FastRTPSSwift/FastRTPSSwift.swift index eb6bba3..de24107 100644 --- a/Sources/FastRTPSSwift/FastRTPSSwift.swift +++ b/Sources/FastRTPSSwift/FastRTPSSwift.swift @@ -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(topic: T, ddsType: D.Type, partition: String? = nil, block: @escaping (UInt64, Data)->Void) throws { + public func registerReaderRaw(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 @@ -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 parameter /// with deserialized data when success deserialization or error otherwize - public func registerReader(topic: T, partition: String? = nil, block: @escaping (Result)->Void) throws { - try registerReaderRaw(topic: topic, ddsType: D.self, partition: partition) { (_, data) in + public func registerReader(topic: T, partition: String? = nil, block: @escaping (Result)->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) @@ -262,7 +260,7 @@ open class FastRTPSSwift { /// Remove a RTPS reader for topic /// - Parameter topic: DDSReader topic descriptor public func removeReader(topic: T) throws { - if !wrapper.removeReader(topicName: topic.rawValue) { + if !wrapper.removeReader(topicName: topic.name) { throw FastRTPSSwiftError.fastRTPSError } } @@ -272,12 +270,10 @@ open class FastRTPSSwift { /// - Parameters: /// - Parameter topic: DDSWriterTopic topic descriptor /// - ddsType: data type descriptor - public func registerWriter(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(topic: T, partition: String? = nil) throws { + if !wrapper.registerWriter(topicName: topic.name, + typeName: topic.typeName, + writerProfile: topic.writerProfile, partition: partition) { throw FastRTPSSwiftError.fastRTPSError } @@ -286,40 +282,43 @@ open class FastRTPSSwift { /// Remove RTPS writer for topic /// - Parameter topic: DDSWriterTopic topic descriptor public func removeWriter(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(topic: T, ddsData: D) throws { + public func send(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(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() diff --git a/Tests/FastRTPSSwiftTests/FastRTPSSwiftTests.swift b/Tests/FastRTPSSwiftTests/FastRTPSSwiftTests.swift index 6a97df4..fc43b13 100644 --- a/Tests/FastRTPSSwiftTests/FastRTPSSwiftTests.swift +++ b/Tests/FastRTPSSwiftTests/FastRTPSSwiftTests.swift @@ -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") diff --git a/Tests/FastRTPSSwiftTests/RovBarometer.swift b/Tests/FastRTPSSwiftTests/RovBarometer.swift index 7156e8c..03e5cda 100644 --- a/Tests/FastRTPSSwiftTests/RovBarometer.swift +++ b/Tests/FastRTPSSwiftTests/RovBarometer.swift @@ -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" } } - diff --git a/Tests/FastRTPSSwiftTests/RovDepth.swift b/Tests/FastRTPSSwiftTests/RovDepth.swift index 4909eb7..a17ac8f 100644 --- a/Tests/FastRTPSSwiftTests/RovDepth.swift +++ b/Tests/FastRTPSSwiftTests/RovDepth.swift @@ -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" } } diff --git a/Tests/FastRTPSSwiftTests/RovTopic.swift b/Tests/FastRTPSSwiftTests/RovTopic.swift index d9ff7d6..d06995a 100644 --- a/Tests/FastRTPSSwiftTests/RovTopic.swift +++ b/Tests/FastRTPSSwiftTests/RovTopic.swift @@ -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) } }