Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

data distribution service - DDS Openslice: How to create a listener in python

I am using ADLINK's OpenSplice and their Python API. I cannot seem to find good documentation or a class reference. I'd like to setup a non-blocking way to receive multiple messages. Their Listener seems to provide this but it is not clear how to set it up in python.

Their DDS tutorial gives a C example:

class TempSensorListener :
   public dds::sub::NoOpDataReaderListener<tutorial::TempSensorType>
   {
      public:
      virtual void on_data_available(dds::sub::DataReader<tutorial::TempSensorType>& dr)
      {
         ...
      });
   }
};

TempSensorListener listener;
dr.listener(&listener, dds::core::status::StatusMask::data_available()); 

This seems to indicate that the datareceiver has a "listener" method used to assign a listener to the datareader.

How is this done using the Python API? I cannot seem to find the listener method from the python datareceiver.

The provided Python examples (example1.py and example2.py) provide

# Data available listener
class DataAvailableListener(Listener):
    def __init__(self):
        Listener.__init__(self)

    def on_data_available(self, entity):
        print('on_data_available called')
        l = entity.read(10)
        for (sd, si) in l:
            sd.print_vars()

But I see no instantiation of the class. The example seems to use Waitset and not use the listener at all

I expected something like:

listener = DataAvailableListener()
reader.listener(listener)

reader does have an attribute called listener. I assigning the object to that attribute but it didnt seem to have any effect.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How I found a solution to the same problem as of 6.10.4:

Referring to the docs from the dds package (included in your $OSPL_HOME/tools/python/docs/html/dds.html) you can setup a listener using the create_datareader method of the Subscriber class:

from dds import *
from foo import foo_type # idlpp generated module/class

# Data available listener
class DataAvailableListener(Listener):
    def __init__(self):
        Listener.__init__(self)

    def on_data_available(self, entity):
        print('on_data_available called')
        l = entity.read(10)
        for (sd, si) in l:
            sd.print_vars()

dp = DomainParticipant()
topic = dp.create_topic('foo_topic',foo_type)
sub = dp.create_subscriber()
sub.create_datareader(topic,listener=DataAvailableListener())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...