Search code examples
pythondata-distribution-serviceopensplice

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.


Solution

  • 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())