Exemplo n.º 1
0
def subscriber_main(domain_id, sample_count):
    participant = dds.DomainParticipant(domain_id)

    ccf_type = dds.QosProvider("ccf.xml").type("ccf_lib", "Foo")
    topic = dds.DynamicData.Topic(participant, "Example ccf", ccf_type)

    # Register the custom filter with the Participant
    participant.register_contentfilter(ccf.CustomFilterType(), "CustomFilter")

    # Set a filter expression and the filter name, then create the CFT
    custom_filter = dds.Filter("%0 %1 x", ["2", "divides"])
    custom_filter.name = "CustomFilter"
    topic = dds.DynamicData.ContentFilteredTopic(
        topic, "ContentFilteredTopic", custom_filter
    )

    print("Filter: 2 divides x")

    reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic)
    reader.bind_listener(CcfListener(), dds.StatusMask.DATA_AVAILABLE)

    count = 0
    while (sample_count == 0) or (count < sample_count):
        time.sleep(1)

        if count == 10:
            # Update the filter parameters after 10 seconds
            print(
                textwrap.dedent(
                    """
                ==========================
                Changing Filter Parameters
                Filter: 15 greater-than x
                =========================="""
                )
            )
            topic.filter_parameters = ["15", "greater-than"]
        elif count == 20:
            # Update the filter again after 20 seconds
            print(
                textwrap.dedent(
                    """
                ==========================
                Changing Filter Parameters
                Filter: 3 divides x
                =========================="""
                )
            )
            topic.filter_parameters = ["3", "divides"]

        count += 1
def test_close_datareader_contained_entity(retain):
    p = utils.create_participant()
    topic = dds.StringTopicType.Topic(p, "test_topic")
    sub = dds.Subscriber(p)
    reader = dds.StringTopicType.DataReader(sub, topic)
    topic_query = dds.TopicQuery(
        dds.AnyDataReader(reader),
        dds.TopicQuerySelection(dds.Filter("foo = 1 or foo = 3")),
    )

    if retain:
        topic_query.retain()
    reader.close()
    assert topic_query.closed
    assert reader.closed
def test_close_subscriber_contained_entities(implicit_sub):
    p = utils.create_participant()
    topic1 = dds.StringTopicType.Topic(p, "test topic1")
    topic2 = dds.StringTopicType.Topic(p, "test topic2")
    topic3 = dds.StringTopicType.Topic(p, "test topic3")

    sub1 = None
    sub2 = dds.Subscriber(p)
    if not implicit_sub:
        sub1 = dds.Subscriber(p)
    else:
        sub1 = p.implicit_subscriber

    reader10 = dds.StringTopicType.DataReader(sub1, topic1)
    reader11 = dds.StringTopicType.DataReader(sub1, topic1)
    reader20 = dds.StringTopicType.DataReader(sub1, topic2)
    reader30 = dds.StringTopicType.DataReader(sub1, topic3)
    reader31 = dds.StringTopicType.DataReader(sub2, topic3)

    topic_query1 = dds.TopicQuery(
        dds.AnyDataReader(reader10),
        dds.TopicQuerySelection(dds.Filter("foo = 1 or foo = 3")),
    )
    topic_query2 = dds.TopicQuery(
        dds.AnyDataReader(reader31),
        dds.TopicQuerySelection(dds.Filter("foo = 4 or foo = 6")),
    )
    sub1.close()

    assert reader10.closed
    assert reader11.closed
    assert reader20.closed
    assert reader30.closed
    assert topic_query1.closed
    assert not reader31.closed
    assert not topic_query2.closed
    def __init__(self, domain_id, use_dynamic_data, use_qos_object,
                 use_custom_topic, use_replier_cft, use_pub_sub_args,
                 use_callback_funcs, use_simple_replier):
        qos = dds.DomainParticipantQos()
        qos.database.shutdown_cleanup_period = dds.Duration.from_milliseconds(
            100)
        self._participant = dds.DomainParticipant(domain_id, qos)
        if use_dynamic_data:
            self._type = get_keyed_string_dynamic_type()
            self.create_data_fnc = create_dynamic_rr_data
            self.parse_data_fnc = parse_dynamic_rr_data
            self.copy_sample_fnc = copy_dynamic_sample
            create_topic_func = create_dynamic_topic
        else:
            self._type = type_cls = dds.KeyedStringTopicType
            self.create_data_fnc = create_rr_data
            self.parse_data_fnc = parse_rr_data
            self.copy_sample_fnc = copy_sample
            create_topic_func = create_topic

        service_name = 'Test'

        if use_qos_object:
            request_writer_qos = request.Requester.default_writer_qos
            request_reader_qos = request.Replier.default_reader_qos
            reply_writer_qos = request.Replier.default_writer_qos
            reply_reader_qos = request.Requester.default_reader_qos
        else:
            request_writer_qos = request_reader_qos = reply_writer_qos = reply_reader_qos = None

        if use_custom_topic:
            # Test with custom Topic object
            request_topic = create_topic_func(self._participant, 'TestRequest')
            # Test with custom string Topic name
            reply_topic = 'TestReply'
        else:
            request_topic = reply_topic = None

        if use_replier_cft:
            replier_filter = dds.Filter("NOT (key MATCH 'filtered')")
            request_reader_topic = create_topic_func(
                self._participant,
                'TestRequest') if request_topic is None else request_topic
            if isinstance(self._type, dds.DynamicType):
                request_reader_topic = dds.DynamicData.ContentFilteredTopic(
                    request_reader_topic, 'request_cft', replier_filter)
            else:
                request_reader_topic = dds.KeyedStringTopicType.ContentFilteredTopic(
                    request_reader_topic, 'request_cft', replier_filter)
        else:
            request_reader_topic = request_topic

        if use_pub_sub_args:
            publisher = dds.Publisher(self._participant)
            subscriber = dds.Subscriber(self._participant)
        else:
            publisher = subscriber = None

        self._cb = use_callback_funcs
        self._request_queue = queue.Queue()
        self._reply_queue = queue.Queue()
        if use_callback_funcs:

            def replier_callback(replier):
                for sample in (s for s in replier.take_requests()
                               if s.info.valid):
                    self._request_queue.put(self.copy_sample_fnc(sample))

            def requester_callback(requester):
                for sample in (s for s in requester.take_replies()
                               if s.info.valid):
                    self._reply_queue.put(self.copy_sample_fnc(sample))
        else:
            replier_callback = requester_callback = None

        self._requester = request.Requester(self._type, self._type,
                                            self._participant, service_name,
                                            request_topic, reply_topic,
                                            request_writer_qos,
                                            reply_reader_qos, publisher,
                                            subscriber, requester_callback)

        self._simple = use_simple_replier
        if use_simple_replier:

            def simple_replier_callback(request):
                key, value = self.parse_data_fnc(request)
                reply = self.create_data_fnc(key, value + ' reply')
                return reply

            self._replier = request.SimpleReplier(
                self._type, self._type, self._participant,
                simple_replier_callback, service_name, request_reader_topic,
                reply_topic, reply_writer_qos, request_reader_qos, publisher,
                subscriber)
        else:
            self._replier = request.Replier(self._type, self._type,
                                            self._participant, service_name,
                                            request_reader_topic, reply_topic,
                                            reply_writer_qos,
                                            request_reader_qos, publisher,
                                            subscriber, replier_callback)
Exemplo n.º 5
0
def subscriber_main(domain_id, sample_count, is_cft):
    participant = dds.DomainParticipant(domain_id)

    cft_type = dds.QosProvider("cft.xml").type("cft_lib", "cft")
    topic = dds.DynamicData.Topic(participant, "Example cft", cft_type)

    if is_cft:
        # Create a CFT that filters for incoming data within a range
        topic = dds.DynamicData.ContentFilteredTopic(
            topic, "ContentFilteredTopic", dds.Filter("x >= %0 AND x <= %1", ["1", "4"])
        )
        print(
            textwrap.dedent(
                """
            ==========================
            Using CFT
            Filter: 1 <= x <= 4
            =========================="""
            )
        )
    else:
        # Filtering disabled by default
        print(
            textwrap.dedent(
                """
            ==========================
            Using Normal Topic
            =========================="""
            )
        )

    reader_qos = dds.QosProvider.default.datareader_qos
    reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic, reader_qos)
    reader.bind_listener(CftListener(), dds.StatusMask.DATA_AVAILABLE)

    count = 0
    while (sample_count == 0) or (count < sample_count):
        time.sleep(1)

        if is_cft:
            if count == 10:
                # After 10 seconds, udpdate the filter range
                print(
                    textwrap.dedent(
                        """
                    ==========================
                    Changing Filter Parameters
                    Filter: 5 <= x <= 9
                    =========================="""
                    )
                )
                topic.filter_parameters = ["5", "9"]
            if count == 20:
                # After 20 seconds, update the filter again
                print(
                    textwrap.dedent(
                        """
                    ==========================
                    Changing Filter Parameters
                    Filter: 3 <= x <= 9
                    =========================="""
                    )
                )
                topic.filter_parameters = ["3", "9"]
        count += 1
Exemplo n.º 6
0
def subscriber_main(domain_id, sample_count, is_cft):
    participant = dds.DomainParticipant(domain_id)

    cft_type = dds.QosProvider("cft.xml").type("cft_lib", "cft")
    topic = dds.DynamicData.Topic(participant, "Example cft", cft_type)

    if is_cft:
        # Use a stringmatch CFT
        str_filter = dds.Filter("name MATCH %0", ["SOME_STRING"])
        str_filter.name = dds.Filter.stringmatch_filter_name
        topic = dds.DynamicData.ContentFilteredTopic(topic,
                                                     "ContentFilteredTopic",
                                                     str_filter)
        # Initial filter is a simple name match
        print(
            textwrap.dedent("""
            ==========================
            Using ContentFilteredTopic
            name MATCH %0
            =========================="""))
    else:
        # Default topic does not use a CFT
        print(
            textwrap.dedent("""
            ==========================
            Using Normal Topic
            =========================="""))

    reader_qos = dds.QosProvider.default.datareader_qos
    reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic,
                                        reader_qos)
    reader.bind_listener(CftListener(), dds.StatusMask.DATA_AVAILABLE)

    # Change the filter
    if is_cft:
        print('Now setting a new filter: name MATCH "EVEN"')
        topic.append_to_expression_parameter(0, "EVEN")

    count = 0
    while (sample_count == 0) or (count < sample_count):
        time.sleep(1)

        if is_cft:
            if count == 10:
                # Change the filter again after 10 seconds
                print(
                    textwrap.dedent("""
                    ==========================
                    Changing Filter Parameters
                    Append 'ODD' filter
                    =========================="""))
                topic.append_to_expression_parameter(0, "ODD")
            if count == 20:
                # Change the filter one more time after 20 seconds
                print(
                    textwrap.dedent("""
                    ==========================
                    Changing Filter Parameters
                    Remove 'EVEN' filter
                    =========================="""))
                topic.remove_from_expression_parameter(0, "EVEN")
        count += 1
def test_close_participant_contained_entities(implicit_pub, test_retain):
    p1 = utils.create_participant()
    p2 = utils.create_participant()
    topic1 = dds.StringTopicType.Topic(p1, "test_topic1")
    topic2 = dds.StringTopicType.Topic(p1, "test topic2")
    topic30 = dds.StringTopicType.Topic(p1, "test topic3")
    topic31 = dds.StringTopicType.Topic(p2, "test topic3")
    cft10 = dds.StringTopicType.ContentFilteredTopic(topic1, "cft10",
                                                     dds.Filter("value = '1'"))
    cft11 = dds.StringTopicType.ContentFilteredTopic(topic1, "cft11",
                                                     dds.Filter("value = '1'"))
    cft20 = dds.StringTopicType.ContentFilteredTopic(topic31, "cft20",
                                                     dds.Filter("value = '1'"))

    pub1 = None
    pub2 = None
    sub1 = None
    sub2 = None

    if not implicit_pub:
        pub1 = dds.Publisher(p1)
        pub2 = dds.Publisher(p2)
        sub1 = dds.Subscriber(p1)
        sub2 = dds.Subscriber(p2)
    else:
        pub1 = p1.implicit_publisher
        pub2 = p2.implicit_publisher
        sub1 = p1.implicit_subscriber
        sub2 = p2.implicit_subscriber

    writer10 = dds.StringTopicType.DataWriter(pub1, topic1)
    writer11 = dds.StringTopicType.DataWriter(pub1, topic1)
    writer20 = dds.StringTopicType.DataWriter(pub1, topic2)
    writer30 = dds.StringTopicType.DataWriter(pub1, topic30)
    writer31 = dds.StringTopicType.DataWriter(pub2, topic31)

    reader10 = dds.StringTopicType.DataReader(sub1, topic1)
    reader11 = dds.StringTopicType.DataReader(sub1, topic2)
    reader20 = dds.StringTopicType.DataReader(sub1, cft10)
    reader30 = dds.StringTopicType.DataReader(sub1, topic30)
    reader31 = dds.StringTopicType.DataReader(sub2, topic31)
    reader32 = dds.StringTopicType.DataReader(sub2, cft20)

    flow_controller10 = dds.FlowController(p1, "fc10")
    flow_controller11 = dds.FlowController(p1, "fc11")
    flow_controller12 = dds.FlowController(p1, "fc12")
    flow_controller20 = dds.FlowController(p2, "fc20")

    tq1 = dds.TopicQuery(
        reader10,
        dds.TopicQuerySelection(dds.Filter("value = '1' or value = '3'")))
    tq2 = dds.TopicQuery(
        reader31,
        dds.TopicQuerySelection(dds.Filter("value = '4' or value = '6'")))

    if test_retain:
        pub1.retain()
        reader11.retain()
        writer11.retain()
        topic2.retain()
        cft11.retain()
        flow_controller12.retain()
        tq1.retain()

    flow_controller12.close()
    p1.close()

    assert p1.closed

    assert pub1.closed
    assert sub1.closed
    assert not pub2.closed
    assert not sub2.closed

    assert topic1.closed
    assert topic2.closed
    assert topic30.closed
    assert not topic31.closed

    assert cft10.closed
    assert cft11.closed
    assert not cft20.closed

    assert writer10.closed
    assert writer11.closed
    assert writer20.closed
    assert writer30.closed
    assert not writer31.closed

    assert reader10.closed
    assert reader11.closed
    assert reader20.closed
    assert reader30.closed
    assert not reader31.closed

    assert flow_controller10.closed
    assert flow_controller11.closed
    assert flow_controller12.closed
    assert not flow_controller20.closed

    assert tq1.closed
    assert not tq2.closed