Ejemplo n.º 1
0
    def __init__(self, domain, idl_path, index=0, partitions=("ddsexample", )):
        self.isopen = True
        self.domain = domain
        self.index = index
        self.idl_path = pathlib.Path(idl_path).resolve()
        if not self.idl_path.is_file():
            raise ValueError(f"idl_path={idl_path} is not a file")
        self.name = self.idl_path.stem

        self.log = logging.getLogger(self.name)

        self.partitions = partitions

        # Create the publisher and subscriber. Both depend on the DDS
        # partitions, and so are created here instead of in Domain,
        # where most similar objects are created.
        partition_qos_policy = dds.PartitionQosPolicy(self.partitions)

        publisher_qos = domain.qos_provider.get_publisher_qos()
        publisher_qos.set_policies([partition_qos_policy])
        self.publisher = domain.participant.create_publisher(publisher_qos)

        subscriber_qos = domain.qos_provider.get_subscriber_qos()
        subscriber_qos.set_policies([partition_qos_policy])
        self.subscriber = domain.participant.create_subscriber(subscriber_qos)

        # A task that is set done when Component.start is done
        # (or to the exception if that fails).
        self.start_task = asyncio.Future()

        # dict of dds.ReadCondition: Topic
        self.readers = dict()
        # wait_timeout is a failsafe for shutdown; normally all you have to do
        # is call `close` to trigger the guard condition and stop the wait
        self._wait_timeout = dds.DDSDuration(sec=10)
        self._guardcond = dds.GuardCondition()
        self._waitset = dds.WaitSet()
        self._waitset.attach(self._guardcond)
        self._read_loop_task = make_done_future()
        self._start_begtime = None
        self._wait_history_begtime = None
        self._read_history_begtime = None
        self._start_endtime = None
        self._wait_history_duration = None

        domain.add_component(self)
Ejemplo n.º 2
0
    def make_subscriber(
            self, partition_names: typing.Sequence[str]) -> dds.Subscriber:
        """Make a dds subscriber.

        Parameters
        ----------
        partition_names : `list` [`str`]
            List of DDS partition names.
        """
        partition_qos_policy = dds.PartitionQosPolicy(partition_names)

        # Any qos set will do, because the publisher and subscriber QoS
        # is the same for all of them.
        subscriber_qos = self.event_qos_set.qos_provider.get_subscriber_qos()
        subscriber_qos.set_policies([partition_qos_policy])

        return self.participant.create_subscriber(subscriber_qos)
Ejemplo n.º 3
0
    def testGroup_textData(self):
        myData = 'Pub Text Group Data'
        builtinTopicName = 'DCPSPublication'
        sawMyData = Event()

        class L(dds.Listener):
            def on_data_available(self, reader):
                nonlocal sawMyData
                while True:
                    r = reader.take(n=1)
                    if len(r) == 0:
                        break
                    elif r[0].status.valid_data:
                        try:
                            v = bytes(r[0].data.group_data.value).decode(
                                'ISO-8859-1')
                            if v == myData:
                                sawMyData.set()
                                break
                        except UnicodeError:
                            pass

        builtin_sub = self.dp.create_subscriber(
            qos=dds.Qos([dds.PartitionQosPolicy(['__BUILT-IN PARTITION__'])]))
        builtin_topic_info = ddsutil.find_and_register_topic(
            self.dp, builtinTopicName)
        builtin_reader = builtin_sub.create_datareader(
            builtin_topic_info.topic,
            qos=builtin_topic_info.topic.qos,
            listener=L())

        # Register a topic with a TopicdataQosPolicy
        p_qos = dds.Qos(policies=[dds.GroupdataQosPolicy(myData)])
        pub = self.dp.create_publisher(p_qos)

        info = ddsutil.get_dds_classes_from_idl('idl/Shapes.idl', "ShapeType")
        t = info.register_topic(self.dp, "T_testGroup_textData")
        self.assertIsNotNone(t, "Topic registration failed")

        dw = pub.create_datawriter(t)

        self.assertTrue(sawMyData.wait(10), 'Did not see expected data')
        builtin_reader.close()
        dw.close()
        pub.close()
Ejemplo n.º 4
0
    def testParticipant_textData(self):
        myData = 'DP Text User Data'
        builtinTopicName = 'DCPSParticipant'
        sawMyData = Event()
        dp_qos_txt = dds.Qos(policies=[dds.UserdataQosPolicy(myData)])
        dp2 = dds.DomainParticipant(qos=dp_qos_txt)

        class L(dds.Listener):
            def on_data_available(self, reader):
                nonlocal sawMyData
                while True:
                    r = reader.take(n=1)
                    if len(r) == 0:
                        break
                    elif r[0].status.valid_data:
                        try:
                            v = bytes(
                                r[0].data.user_data.value).decode('ISO-8859-1')
                            if v == myData:
                                sawMyData.set()
                                break
                        except UnicodeError:
                            pass

        builtin_sub = self.dp.create_subscriber(
            qos=dds.Qos([dds.PartitionQosPolicy(['__BUILT-IN PARTITION__'])]))
        builtin_topic_info = ddsutil.find_and_register_topic(
            self.dp, builtinTopicName)

        builtin_reader = builtin_sub.create_datareader(
            builtin_topic_info.topic,
            qos=builtin_topic_info.topic.qos,
            listener=L())

        self.assertTrue(sawMyData.wait(10), 'Did not see expected data')
        builtin_reader.close()
        dp2.close()