Replies: 5 comments 8 replies
-
Hi @ploe24, thanks for using Fast DDS. private:
std::function<void(DataReader*)> data_received;
public:
YourListener::YourListener(std::function<bool(DataReader*)> data_received_callback)
: data_received(data_received_callback) {}
// or
YourListener::YourListener(){}
void YourListener::set_custom_callback(std::function<bool(DataReader*)> data_received_callback) {
data_received = data_received_callback;
}
// Make the on_data_available callback run the given std::function
void YourListener::on_data_available(DataReader* reader) override {
data_received(reader);
} In that way, assuming you propagate the std::function to the listener creation method, you can define the callback behavior in your library API or one step above: IDL_DATA data; // IDL data type
// define the callback
std::function<void(DataReader*)> data_received_callback = [&](DataReader* reader)
{
IDL_DATA data_;
SampleInfo info_;
// Add custom condition in the loop for listening to data, i.e. while true
while (true && (RETCODE_OK == reader->take_next_sample(&data_, &info_))) {
if ((info_.instance_state == ALIVE_INSTANCE_STATE) && info_.valid_data) {
// Use safely your IDL_DATA data_ here
// ... data_.method()
}
}
};
// create the library interface with that callback, so it is (internally) propagated to the listener constructor
MyDDS myDDS(&data_received_callback); // Library interface propagates the callback to the listener constructor
myDDS.readData(&data); // starts internal subscriber in library and runs your custom callback |
Beta Was this translation helpful? Give feedback.
-
Hi @ploe24, other options are accessing data with a waiting thread using waitsets (as it is done in the hello world example), or using non-blocking calls with the DataReade method |
Beta Was this translation helpful? Give feedback.
-
Had to re-open this again 😞 The
and instead of
we use Is there an easy way to use the custom callback approach in a SWIG Python client? Do we have to re-configure the SWIG interface in order to be able to deal with |
Beta Was this translation helpful? Give feedback.
-
Hi @ploe24 I think that the changes would be similar to: class ReaderListener(fastdds.DataReaderListener):
def __init__(self, custom_callback):
super().__init__()
self.custom_callback = custom_callback
def on_data_available(self, reader):
info = fastdds.SampleInfo()
data = HelloWorld.HelloWorld()
reader.take_next_sample(data, info)
self.custom_callback(data)
# ...
class Reader():
def __init__(self, domain, custom_callback):
# ...
self.listener = ReaderListener(custom_callback)
# ...
def my_custom_callback(data):
# Use your IDL_DATA data here
# ... data.method()
# __main__
# ...
reader = Reader(args.domain, my_custom_callback)
reader.run() Hope it helps! |
Beta Was this translation helpful? Give feedback.
-
Hi @ploe24, sorry for the inconvenience. |
Beta Was this translation helpful? Give feedback.
-
Hi there,
a more "DDS-app-design"-related question:
We have included a Discovery Server, a Publisher and a Subscriber in our library.
Each class of them is built similar to the classes used in the DynamicHelloWorld and DiscoveryServer example(s).
The
Subscriber
, for instance, as separate class, with its ownDataReader
/DataReader
-array and a privateSubListener
.So far so good.
However, since we want to provide the received IDL type data (let's call it “IDL_DATA”) via the public API of our library, we wonder what the recommended way is to do so?!
Something alone these lines...
(but open for other suggestions on how to deal with this use case)
In contrast to the idea above, the example utilize a
Subscriber::run()
function to run a subscriber in a thread. There is no "callback" to get the data to the caller... it's only printed inside the privateListener
callback implementation.How to get the data from the
Listener::on_data_available
callback and how to "sync" the callback with our library API? Are there any recommendation or a "standard" way which works well?(As the examples only show how to receive the data, which is completely fine and working in our case, but pretty "basic" and unusable in production code, since only printing the data inside the listener callback is obviously not what you want to do with the data) 😄
And so far I've not found any public examples (open source code) of "real" applications where FastDDS is in use... so one can get an idea how to actually continue with the data you have received in your
Listener::on_data_available
async callback.If you know any real world example which is worth to have a look at, please let me know :)
Any help, suggestions and hints appreaciated :)
Thanks in advance,
Beta Was this translation helpful? Give feedback.
All reactions