Skip to content

Commit

Permalink
Enchance listener information, change listener interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaRU committed Feb 3, 2024
1 parent c6a2470 commit 170185e
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/////
//// FastRTPSNotifications.swift
//// FastRTPSEnum_CustomStringConvertible.swift
/// Copyright © 2019 Dmitriy Borovikov. All rights reserved.
//


import Foundation

extension RTPSNotification: CustomStringConvertible {
extension RTPSStatus: CustomStringConvertible {
public var description: String {
switch self {
case .readerMatchedMatching : return "readerMatchedMatching"
Expand All @@ -19,22 +19,29 @@ extension RTPSNotification: CustomStringConvertible {
}
}

extension RTPSReaderWriterNotification: CustomStringConvertible {
extension RTPSReaderStatus: CustomStringConvertible {
public var description: String {
switch self {
case .discoveredReader : return "discoveredReader"
case .changedQosReader : return "changedQosReader"
case .removedReader : return "removedReader"
case .ignoredReader : return "ignoredReader"
}
}
}

extension RTPSWriterStatus: CustomStringConvertible {
public var description: String {
switch self {
case .discoveredWriter : return "discoveredWriter"
case .changedQosWriter : return "changedQosWriter"
case .removedWriter : return "removedWriter"
case .ignoredReader : return "ignoredReader"
case .ignoredWriter : return "ignoredWriter"
}
}
}

extension RTPSParticipantNotification: CustomStringConvertible {
extension RTPSParticipantStatus: CustomStringConvertible {
public var description: String {
switch self {
case .discoveredParticipant : return "discoveredParticipant"
Expand All @@ -45,3 +52,23 @@ extension RTPSParticipantNotification: CustomStringConvertible {
}
}
}

extension Durability: CustomStringConvertible {
public var description: String {
switch self {
case .volatile : return "volatile"
case .transientLocal : return "transientLocal"
case .transient : return "transient"
case .persistent : return "persistent"
}
}
}

extension Reliability: CustomStringConvertible {
public var description: String {
switch self {
case .bestEffort : return "bestEffort"
case .reliable : return "reliable"
}
}
}
52 changes: 40 additions & 12 deletions Sources/FastRTPSSwift/FastRTPSSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,34 @@ public protocol RTPSListenerDelegate {
/// - Parameters:
/// - reason: event reason
/// - topic: topic name
func RTPSNotification(reason: RTPSNotification, topic: String)
func RTPSNotification(reason: RTPSStatus, topic: String)
}

/// RTPS Participant listener delegate requrements
public protocol RTPSParticipantListenerDelegate {
/// Intercepts paricipant discovery events
/// - Parameters:
/// - reason: event reaason, see RTPSParticipantNotification
/// - reason: event reaason, see RTPSParticipantStatus
/// - participant: participant name
/// - unicastLocators: participant unicast locators list
/// - properties: participant properties strings
func participantNotification(reason: RTPSParticipantNotification, participant: String, unicastLocators: String, properties: [String:String])
/// Intercepts readers and writers discovery events
func participantNotification(reason: RTPSParticipantStatus, participant: String, unicastLocators: String, properties: [String:String])
/// Intercepts reader discovery events
/// - Parameters:
/// - reason: event reason, see RTPSReaderWriterNotification enum
/// - reason: event reason, see RTPSReaderStatus enum
/// - topic: topic name
/// - type: topic data type name
/// - remoteLocators: remote locators list
func readerWriterNotificaton(reason: RTPSReaderWriterNotification, topic: String, type: String, remoteLocators: String)
/// - readerProfile: reader qos data
func readerNotificaton(reason: RTPSReaderStatus, topic: String, type: String, remoteLocators: String, readerProfile: RTPSReaderProfile)
/// Intercepts writer discovery events
/// - Parameters:
/// - reason: event reason, see RTPSWriterStatus enum
/// - topic: topic name
/// - type: topic data type name
/// - remoteLocators: remote locators list
/// - writerProfile: writer qos data
func writerNotificaton(reason: RTPSWriterStatus, topic: String, type: String, remoteLocators: String, writerProfile: RTPSWriterProfile)
}

/// FastRTPSSwift errors enum
Expand Down Expand Up @@ -93,18 +102,37 @@ open class FastRTPSSwift {
participant: String(cString: participantName),
unicastLocators: locators,
properties: propertiesDict)
}, discoveryReaderWriterCallback: {
(listenerObject, reason, topicName, typeName, remoteLocators) in
}, discoveryReaderCallback: {
(listenerObject, reason, readerInfo) in
let mySelf = Unmanaged<FastRTPSSwift>.fromOpaque(listenerObject).takeUnretainedValue()
guard let delegate = mySelf.participantListenerDelegate else { return }

let topic = String(cString: topicName)
let type = String(cString: typeName)
let topic = String(cString: readerInfo.pointee.topic)
let type = String(cString: readerInfo.pointee.ddstype)
var locators = ""
if let remoteLocators = readerInfo.pointee.locators {
locators = String(cString: remoteLocators)
}
let readerProfile = RTPSReaderProfile(keyed: readerInfo.pointee.readerProfile.keyed,
reliability: readerInfo.pointee.readerProfile.reliability,
durability: readerInfo.pointee.readerProfile.durability)
delegate.readerNotificaton(reason: reason, topic: topic, type: type, remoteLocators: locators, readerProfile: readerProfile)
}, discoveryWriterCallback: {
(listenerObject, reason, writerInfo) in
let mySelf = Unmanaged<FastRTPSSwift>.fromOpaque(listenerObject).takeUnretainedValue()
guard let delegate = mySelf.participantListenerDelegate else { return }

let topic = String(cString: writerInfo.pointee.topic)
let type = String(cString: writerInfo.pointee.ddstype)
var locators = ""
if let remoteLocators = remoteLocators {
if let remoteLocators = writerInfo.pointee.locators {
locators = String(cString: remoteLocators)
}
delegate.readerWriterNotificaton(reason: reason, topic: topic, type: type, remoteLocators: locators)
let writerProfile = RTPSWriterProfile(keyed: writerInfo.pointee.writerProfile.keyed,
reliability: writerInfo.pointee.writerProfile.reliability,
durability: writerInfo.pointee.writerProfile.durability,
disablePositiveACKs: writerInfo.pointee.writerProfile.disablePositiveACKs)
delegate.writerNotificaton(reason: reason, topic: topic, type: type, remoteLocators: locators, writerProfile: writerProfile)
}, listnerObject: Unmanaged.passUnretained(self).toOpaque())

wrapper.setupBridgeContainer(container: container)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FastRTPSWrapper/BridgedParticipant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bool BridgedParticipant::createParticipant(const char* name,
participantAttributes.builtin.discovery_config.discoveryProtocol = eprosima::fastrtps::rtps::DiscoveryProtocol::SIMPLE;
participantAttributes.setName(name);
if (participantProfile != nullptr) {
participantAttributes.builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(participantProfile->leaseDuration_announcementperiod);
participantAttributes.builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(participantProfile->leaseDurationAnnouncementperiod);
participantAttributes.builtin.discovery_config.leaseDuration = Duration_t(participantProfile->leaseDuration);
switch (participantProfile->participantFilter) {
case Disabled:
Expand Down
55 changes: 37 additions & 18 deletions Sources/FastRTPSWrapper/BridgedParticipantListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,41 @@
#include <fastrtps/rtps/common/Locator.h>
#include <fastrtps/utils/IPLocator.h>
#include <memory.h>
#include "FastRTPSDefs.h"
#include "FastDDSVersion.h"

using namespace eprosima::fastrtps;
using namespace eprosima::fastrtps::rtps;

void BridgedParticipantListener::onReaderDiscovery(RTPSParticipant *participant, ReaderDiscoveryInfo &&info)
{
(void)participant;
auto topicName = info.info.topicName();
auto typeName = info.info.typeName();
std::ostringstream stream;
struct ReaderInfo readerInfo;
std::string str;

readerInfo.locators = nullptr;
readerInfo.topic = info.info.topicName().c_str();
readerInfo.ddstype = info.info.typeName().c_str();
readerInfo.readerProfile.reliability = static_cast<Reliability>(info.info.m_qos.m_reliability.kind);
readerInfo.readerProfile.durability = static_cast<Durability>(info.info.m_qos.m_durability.kind);
readerInfo.readerProfile.keyed = info.info.topicKind() == eprosima::fastrtps::rtps::WITH_KEY;

switch(info.status) {
case ReaderDiscoveryInfo::DISCOVERED_READER:
dumpLocators(info.info.remote_locators().unicast, stream);
container.discoveryReaderWriterCallback(container.listnerObject, RTPSReaderWriterNotificationDiscoveredReader, topicName, typeName, stream.str().c_str());
str = stream.str();
readerInfo.locators = str.c_str();
container.discoveryReaderCallback(container.listnerObject, RTPSReaderStatusDiscoveredReader, &readerInfo);
break;
case ReaderDiscoveryInfo::CHANGED_QOS_READER:
container.discoveryReaderWriterCallback(container.listnerObject, RTPSReaderWriterNotificationChangedQosReader, topicName, typeName, nullptr);
container.discoveryReaderCallback(container.listnerObject, RTPSReaderStatusChangedQosReader, &readerInfo);
break;
case ReaderDiscoveryInfo::REMOVED_READER:
container.discoveryReaderWriterCallback(container.listnerObject, RTPSReaderWriterNotificationRemovedReader, topicName, typeName, nullptr);
container.discoveryReaderCallback(container.listnerObject, RTPSReaderStatusRemovedReader, &readerInfo);
break;
#if FASTDDS_VERSION >= 21000
case ReaderDiscoveryInfo::IGNORED_READER:
container.discoveryReaderWriterCallback(container.listnerObject, RTPSReaderWriterNotificationIgnoredReader, topicName, typeName, nullptr);
container.discoveryReaderCallback(container.listnerObject, RTPSReaderStatusIgnoredReader, &readerInfo);
break;
#endif
}
Expand All @@ -43,24 +52,34 @@ void BridgedParticipantListener::onReaderDiscovery(RTPSParticipant *participant,
void BridgedParticipantListener::onWriterDiscovery(RTPSParticipant *participant, WriterDiscoveryInfo &&info)
{
(void)participant;
auto topicName = info.info.topicName();
auto typeName = info.info.typeName();
std::ostringstream stream;
struct WriterInfo writerInfo;
std::string str;

writerInfo.locators = nullptr;
writerInfo.topic = info.info.topicName().c_str();
writerInfo.ddstype = info.info.typeName().c_str();
writerInfo.writerProfile.reliability = static_cast<Reliability>(info.info.m_qos.m_reliability.kind);
writerInfo.writerProfile.durability = static_cast<Durability>(info.info.m_qos.m_durability.kind);
writerInfo.writerProfile.keyed = info.info.topicKind() == eprosima::fastrtps::rtps::WITH_KEY;
writerInfo.writerProfile.disablePositiveACKs = info.info.m_qos.m_disablePositiveACKs.enabled;

switch(info.status) {
case WriterDiscoveryInfo::DISCOVERED_WRITER:
dumpLocators(info.info.remote_locators().unicast, stream);
container.discoveryReaderWriterCallback(container.listnerObject, RTPSReaderWriterNotificationDiscoveredWriter, topicName, typeName, stream.str().c_str());
str = stream.str();
writerInfo.locators = str.c_str();
container.discoveryWriterCallback(container.listnerObject, RTPSWriterStatusDiscoveredWriter, &writerInfo);
break;
case WriterDiscoveryInfo::CHANGED_QOS_WRITER:
container.discoveryReaderWriterCallback(container.listnerObject, RTPSReaderWriterNotificationChangedQosWriter, topicName, typeName, nullptr);
container.discoveryWriterCallback(container.listnerObject, RTPSWriterStatusChangedQosWriter, &writerInfo);
break;
case WriterDiscoveryInfo::REMOVED_WRITER:
container.discoveryReaderWriterCallback(container.listnerObject, RTPSReaderWriterNotificationRemovedWriter, topicName, typeName, nullptr);
container.discoveryWriterCallback(container.listnerObject, RTPSWriterStatusRemovedWriter, &writerInfo);
break;
#if FASTDDS_VERSION >= 21000
case WriterDiscoveryInfo::IGNORED_WRITER:
container.discoveryReaderWriterCallback(container.listnerObject, RTPSReaderWriterNotificationIgnoredWriter, topicName, typeName, nullptr);
container.discoveryWriterCallback(container.listnerObject, RTPSWriterStatusIgnoredWriter, &writerInfo);
break;
#endif
}
Expand All @@ -87,24 +106,24 @@ void BridgedParticipantListener::onParticipantDiscovery(RTPSParticipant *partici
}
propDict[i] = nullptr;
dumpLocators(info.info.default_locators.unicast, stream);
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantNotificationDiscoveredParticipant, info.info.m_participantName, stream.str().c_str(), propDict);
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantStatusDiscoveredParticipant, info.info.m_participantName, stream.str().c_str(), propDict);
do {
free((void *)propDict[i--]);
} while (i != 0);
delete [] propDict;
break;
case ParticipantDiscoveryInfo::DROPPED_PARTICIPANT:
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantNotificationDroppedParticipant, info.info.m_participantName, nullptr, nullptr);
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantStatusDroppedParticipant, info.info.m_participantName, nullptr, nullptr);
break;
case ParticipantDiscoveryInfo::REMOVED_PARTICIPANT:
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantNotificationRemovedParticipant, info.info.m_participantName, nullptr, nullptr);
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantStatusRemovedParticipant, info.info.m_participantName, nullptr, nullptr);
break;
case ParticipantDiscoveryInfo::CHANGED_QOS_PARTICIPANT:
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantNotificationChangedQosParticipant, info.info.m_participantName, nullptr, nullptr);
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantStatusChangedQosParticipant, info.info.m_participantName, nullptr, nullptr);
break;
#if FASTDDS_VERSION >= 21000
case ParticipantDiscoveryInfo::IGNORED_PARTICIPANT:
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantNotificationIgnoredParticipant, info.info.m_participantName, nullptr, nullptr);
container.discoveryParticipantCallback(container.listnerObject, RTPSParticipantStatusIgnoredParticipant, info.info.m_participantName, nullptr, nullptr);
break;
#endif
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/FastRTPSWrapper/BridgedReaderListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void BridgedReaderListener::onNewCacheChangeAdded(RTPSReader* reader, const Cach

void BridgedReaderListener::on_liveliness_changed(RTPSReader *reader, const LivelinessChangedStatus &status)
{
container.readerWriterListenerCallback(container.listnerObject, RTPSNotificationReaderLivelinessLost, topicName.c_str());
container.readerWriterListenerCallback(container.listnerObject, RTPSStatusReaderLivelinessLost, topicName.c_str());
}

void BridgedReaderListener::onReaderMatched(RTPSReader* reader, MatchingInfo& info)
Expand All @@ -48,11 +48,11 @@ void BridgedReaderListener::onReaderMatched(RTPSReader* reader, MatchingInfo& in
{
case MATCHED_MATCHING:
n_matched++;
container.readerWriterListenerCallback(container.listnerObject, RTPSNotificationReaderMatchedMatching, topicName.c_str());
container.readerWriterListenerCallback(container.listnerObject, RTPSStatusReaderMatchedMatching, topicName.c_str());
break;
case REMOVED_MATCHING:
n_matched--;
container.readerWriterListenerCallback(container.listnerObject, RTPSNotificationReaderRemovedMatching, topicName.c_str());
container.readerWriterListenerCallback(container.listnerObject, RTPSStatusReaderRemovedMatching, topicName.c_str());
break;
}
}
6 changes: 3 additions & 3 deletions Sources/FastRTPSWrapper/BridgedWriterListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BridgedWriterListener::~BridgedWriterListener()

void BridgedWriterListener::on_liveliness_lost(RTPSWriter* writer, const LivelinessLostStatus& status)
{
container.readerWriterListenerCallback(container.listnerObject, RTPSNotificationWriterLivelinessLost, topicName.c_str());
container.readerWriterListenerCallback(container.listnerObject, RTPSStatusWriterLivelinessLost, topicName.c_str());
}

void BridgedWriterListener::onWriterMatched(RTPSWriter* writer, MatchingInfo& info)
Expand All @@ -31,11 +31,11 @@ void BridgedWriterListener::onWriterMatched(RTPSWriter* writer, MatchingInfo& in
{
case MATCHED_MATCHING:
n_matched++;
container.readerWriterListenerCallback(container.listnerObject, RTPSNotificationWriterMatchedMatching, topicName.c_str());
container.readerWriterListenerCallback(container.listnerObject, RTPSStatusWriterMatchedMatching, topicName.c_str());
break;
case REMOVED_MATCHING:
n_matched--;
container.readerWriterListenerCallback(container.listnerObject, RTPSNotificationWriterRemovedMatching, topicName.c_str());
container.readerWriterListenerCallback(container.listnerObject, RTPSStatusWriterRemovedMatching, topicName.c_str());
break;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// FastRTPSDefs.h
// FastDDSVersion.h
//
//
// Created by Dmitriy Borovikov on 22.01.2024.
Expand Down
Loading

0 comments on commit 170185e

Please sign in to comment.