Exemple #1
0
def subscriber_main(domain_id, sample_count):
    participant = dds.DomainParticipant(domain_id)

    wsqc_type = dds.QosProvider("waitset_cond.xml").type("wssc_lib", "Foo")
    topic = dds.DynamicData.Topic(participant, "Example Foo", wsqc_type)
    reader_qos = dds.QosProvider.default.datareader_qos
    reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic, reader_qos)

    # Get the StatusCondition associated with the reader and set the mask to get liveliness updates
    status_condition = dds.StatusCondition(reader)
    status_condition.enabled_statuses = dds.StatusMask.LIVELINESS_CHANGED

    # Create a ReadCondition to get any data
    read_condition = dds.ReadCondition(reader, dds.DataState.any_data)

    # Create WaitSet and attach conditions
    waitset = dds.WaitSet()
    waitset += status_condition
    waitset += read_condition

    count = Counter()
    while (sample_count == 0) or (count.value < sample_count):
        active = waitset.wait(4.0)
        # Check conditions after wait to see if anything triggered
        if status_condition in active:
            status_handler(reader)
        if read_condition in active:
            rc_handler(reader, count)
Exemple #2
0
def run_example(domain_id, sample_count):

    # A DomainParticipant allows an application to begin communicating in
    # a DDS domain. Typically there is one DomainParticipant per application.
    # Create a DomainParticipant with default Qos
    with dds.DomainParticipant(domain_id) as participant:
        # A QosProvider is used here to get the type from the file
        # HelloWorld.xml
        provider = dds.QosProvider(FILE)

        provider_type = provider.type("HelloWorld")

        # A Topic has a name and a datatype. Create a Topic named
        # "HelloWorld Topic" with type HelloWorld
        topic = dds.DynamicData.Topic(
            participant, "Example HelloWorld", provider_type
        )

        # A Subscriber allows an application to create one or more DataReaders
        # Subscriber QoS is configured in USER_QOS_PROFILES.xml
        subscriber = dds.Subscriber(participant)

        # This DataReader will read data of type HelloWorld on Topic
        # "HelloWorld Topic". DataReader QoS is configured in
        # USER_QOS_PROFILES.xml
        reader = dds.DynamicData.DataReader(subscriber, topic)

        # Obtain the DataReader's Status Condition
        status_condition = dds.StatusCondition(reader)

        # Enable the 'data available' status.
        status_condition.enabled_statuses = dds.StatusMask.data_available()

        # Initialize samples_read to zero
        samples_read = 0

        # Associate a handler with the status condition. This will run when the
        # condition is triggered, in the context of the dispatch call (see below)
        def handler(_):  # condition argument is not used
            nonlocal samples_read
            nonlocal reader
            samples_read += process_data(reader)

        status_condition.set_handler(handler)

        # Create a WaitSet and attach the StatusCondition
        waitset = dds.WaitSet()
        waitset += status_condition

        # Catch control c interrupt
        try:
            while samples_read < sample_count:
                # Dispatch will call the handlers associated to the WaitSet conditions
                # when they activate
                print(f"Hello World subscriber sleeping for 4 seconds...")

                waitset.dispatch(dds.Duration(4))  # Wait up to 4s each time
        except KeyboardInterrupt:
            pass
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
Exemple #4
0
def run_example(domain_id, sample_count, sensor_id):

    # A DomainParticipant allows an application to begin communicating in
    # a DDS domain. Typically there is one DomainParticipant per application.
    # Create a DomainParticipant with default Qos
    participant = dds.DomainParticipant(domain_id)

    # A Topic has a name and a datatype. Create a Topic named
    # "ChocolateTemperature" with type Temperature
    temperature_type = dds.QosProvider(FILE).type("Temperature")
    topic = dds.DynamicData.Topic(participant, "ChocolateTemperature",
                                  temperature_type)

    # A Subscriber allows an application to create one or more DataReaders
    # Subscriber QoS is configured in USER_QOS_PROFILES.xml
    subscriber = dds.Subscriber(participant)

    # This DataReader reads data of type Temperature on Topic
    # "ChocolateTemperature". DataReader QoS is configured in
    # USER_QOS_PROFILES.xml
    reader = dds.DynamicData.DataReader(subscriber, topic)

    # Obtain the DataReader's Status Condition
    status_condition = dds.StatusCondition(reader)

    # Enable the 'data available' status.
    status_condition.enabled_statuses = dds.StatusMask.data_available()

    # Associate a handler with the status condition. This will run when the
    # condition is triggered, in the context of the dispatch call (see below)
    samples_read = 0

    def handler(_):
        nonlocal samples_read
        nonlocal reader
        samples_read += process_data(reader)

    status_condition.set_handler(handler)

    # Create a WaitSet and attach the StatusCondition
    waitset = dds.WaitSet()
    waitset += status_condition

    try:
        # Dispatch will call the handlers associated to the WaitSet conditions
        # when they activate
        while sample_count is None or samples_read < sample_count:
            print("ChocolateTemperature subcriber sleeping for 4 sec...")
            waitset.dispatch(dds.Duration(4))  # Wait up to 4s each time
    except KeyboardInterrupt:
        pass
def subscriber_main(domain_id, sample_count):
    participant = dds.DomainParticipant(domain_id)

    subscriber_qos = dds.QosProvider.default.subscriber_qos
    subscriber = dds.Subscriber(participant, subscriber_qos)

    coherent_type = dds.QosProvider("coherent.xml").type("coherent_lib", "coherent")
    topic = dds.DynamicData.Topic(participant, "Example coherent", coherent_type)
    datareader_qos = dds.QosProvider.default.datareader_qos
    reader = dds.DynamicData.DataReader(subscriber, topic, datareader_qos)
    reader.bind_listener(CoherentListener(), dds.StatusMask.data_available())

    count = 0
    while (sample_count == 0) or (count < sample_count):
        time.sleep(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
Exemple #8
0
    def __init__(self, domain_id, sample_type):
        self.participant = create_participant(domain_id)

        reader_qos = self.participant.implicit_subscriber.default_datareader_qos
        reader_qos << dds.Durability.transient_local
        reader_qos << dds.Reliability.reliable()
        reader_qos << dds.History.keep_all

        writer_qos = self.participant.implicit_publisher.default_datawriter_qos
        writer_qos << dds.Durability.transient_local
        writer_qos << dds.Reliability.reliable()
        writer_qos << dds.History.keep_all
        if sample_type == "StringTopicType":
            self.topic = dds.StringTopicType.Topic(self.participant,
                                                   "StringTopicType")
            self.reader = dds.StringTopicType.DataReader(
                self.participant, self.topic, reader_qos)
            self.writer = dds.StringTopicType.DataWriter(
                self.participant, self.topic, writer_qos)
        elif sample_type == "KeyedStringTopicType":
            self.topic = dds.KeyedStringTopicType.Topic(
                self.participant, "KeyedStringTopicType")
            self.reader = dds.KeyedStringTopicType.DataReader(
                self.participant, self.topic, reader_qos)
            self.writer = dds.KeyedStringTopicType.DataWriter(
                self.participant, self.topic, writer_qos)
        elif sample_type == "PerformanceTest":
            provider = dds.QosProvider(
                str(pathlib.Path(__file__).parent.absolute()) +
                "/../xml/PerfTest.xml")
            provider_type = provider.type("PerformanceTest")
            self.topic = dds.DynamicData.Topic(self.participant,
                                               "PerformanceTest",
                                               provider_type)
            self.reader = dds.DynamicData.DataReader(
                dds.Subscriber(self.participant), self.topic, reader_qos)
            self.writer = dds.DynamicData.DataWriter(
                dds.Publisher(self.participant), self.topic, writer_qos)
        else:
            raise Exception(sample_type + " not supported in test system")
def subscriber_main(domain_id, sample_count, participant_auth):
    participant_qos = dds.QosProvider.default.participant_qos
    resource_limits_qos = participant_qos.resource_limits
    max_participant_user_data = resource_limits_qos.participant_user_data_max_length

    # Set the participant auth string as user data bytes
    if len(participant_auth) > max_participant_user_data:
        raise ValueError("participant user data exceeds resource limits")
    else:
        participant_qos.user_data.value = bytearray(participant_auth.encode())

    participant = dds.DomainParticipant(domain_id, participant_qos)
    participant.enable()

    msg_type = dds.QosProvider("msg.xml").type("builtin_topics_lib", "msg")
    topic = dds.DynamicData.Topic(participant, "Example msg", msg_type)
    reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic)
    reader.bind_listener(MsgListener(), dds.StatusMask.data_available())

    count = 0
    while (sample_count == 0) or (count < sample_count):
        time.sleep(1)
        count += 1
    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)
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
def run_example(domain_id, sensor_id):
    # A DomainParticipant allows an application to begin communicating in
    # a DDS domain. Typically there is one DomainParticipant per application.
    # DomainParticipant QoS is configured in USER_QOS_PROFILES.xml
    participant = dds.DomainParticipant(domain_id)

    # A Topic has a name and a datatype. Create Topics.
    # Topic names are constants defined in the XML file.
    temperature_topic = dds.DynamicData.Topic(participant,
                                              CHOCOLATE_TEMPERATURE_TOPIC,
                                              TEMPERATURE_TYPE)
    lot_state_topic = dds.DynamicData.Topic(participant,
                                            CHOCOLATE_LOT_STATE_TOPIC,
                                            CHOCOLATE_LOT_TYPE)

    # A Publisher allows an application to create one or more DataWriters
    # Publisher QoS is configured in USER_QOS_PROFILES.xml
    publisher = dds.Publisher(participant)

    # Create DataWriters of Topics "ChocolateTemperature" & "ChocolateLotState"
    # DataWriter QoS is configured in USER_QOS_PROFILES.xml
    temperature_writer = dds.DynamicData.DataWriter(publisher,
                                                    temperature_topic)
    lot_state_writer = dds.DynamicData.DataWriter(publisher, lot_state_topic)

    # A Subscriber allows an application to create one or more DataReaders
    # Subscriber QoS is configured in USER_QOS_PROFILES.xml
    subscriber = dds.Subscriber(participant)

    # Create DataReader of Topic "ChocolateLotState".
    # DataReader QoS is configured in USER_QOS_PROFILES.xml
    lot_state_reader = dds.DynamicData.DataReader(subscriber, lot_state_topic)

    # Obtain the DataReader's Status Condition
    status_condition = dds.StatusCondition(lot_state_reader)
    # Enable the 'data available' status.
    status_condition.enabled_statuses = dds.StatusMask.data_available()

    # Associate a handler with the status condition. This will run when the
    # condition is triggered, in the context of the dispatch call (see below)
    def handler(_):
        nonlocal lot_state_reader
        nonlocal lot_state_writer
        process_lot(lot_state_reader, lot_state_writer)

    # Create a WaitSet and attach the StatusCondition
    status_condition.set_handler(handler)

    waitset = dds.WaitSet()
    waitset += status_condition

    # Create a thread to periodically publish the temperature
    print(f"ChocolateTemperature Sensor with ID: {sensor_id} starting")
    temperature_thread = threading.Thread(target=publish_temperature,
                                          args=(temperature_writer, sensor_id))
    temperature_thread.start()
    try:
        while True:
            # Wait for ChocolateLotState
            print("Waiting for lot")
            waitset.dispatch(dds.Duration(10))  # Wait up to 10s for update
    except KeyboardInterrupt:
        pass

    temperature_thread.join()
Exemple #13
0
def run_example(domain_id, lots_to_process, sensor_id):

    # A DomainParticipant allows an application to begin communicating in
    # a DDS domain. Typically there is one DomainParticipant per application.
    # DomainParticipant QoS is configured in USER_QOS_PROFILES.xml
    participant = dds.DomainParticipant(domain_id)

    # A Topic has a name and a datatype. Create a Topic with type
    # ChocolateLotState.  Topic name is a constant defined in the XML file.
    topic = dds.DynamicData.Topic(participant, CHOCOLATE_LOT_STATE_TOPIC,
                                  CHOCOLATE_LOT_TYPE)

    # Exercise #4.1: Add a Topic for Temperature to this application
    temperature_topic = dds.DynamicData.Topic(participant,
                                              CHOCOLATE_TEMPERATURE_TOPIC,
                                              provider.type("Temperature"))

    # A Publisher allows an application to create one or more DataWriters
    # Publisher QoS is configured in USER_QOS_PROFILES.xml
    publisher = dds.Publisher(participant)

    # This DataWriter writes data on Topic "ChocolateLotState"
    # DataWriter QoS is configured in USER_QOS_PROFILES.xml
    writer = dds.DynamicData.DataWriter(publisher, topic)

    # A Subscriber allows an application to create one or more DataReaders
    # Subscriber QoS is configured in USER_QOS_PROFILES.xml
    subscriber = dds.Subscriber(participant)

    # Create DataReader of Topic "ChocolateLotState".
    # DataReader QoS is configured in USER_QOS_PROFILES.xml
    reader = dds.DynamicData.DataReader(subscriber, topic)

    # Exercise #4.2: Add a DataReader for Temperature to this application
    temperature_reader = dds.DynamicData.DataReader(subscriber,
                                                    temperature_topic)

    # Obtain the DataReader's Status Condition
    status_condition = dds.StatusCondition(reader)
    # Enable the 'data available' status.
    status_condition.enabled_statuses = dds.StatusMask.data_available()

    # Associate a handler with the status condition. This will run when the
    # condition is triggered, in the context of the dispatch call (see below)
    lots_processed = 0

    def handler(_):
        nonlocal lots_processed
        nonlocal reader
        lots_processed += monitor_lot_state(reader)

    status_condition.set_handler(handler)

    temperature_status_condition = dds.StatusCondition(reader)

    temperature_status_condition.enabled_statuses = (
        dds.StatusMask.data_available())

    def temperature_handler(_):
        nonlocal temperature_reader
        monitor_lot_state(temperature_reader)

    temperature_status_condition.set_handler(temperature_handler)

    # Create a WaitSet and attach the StatusCondition
    waitset = dds.WaitSet()
    waitset += status_condition

    # Exercise #4.3: Add the new DataReader's StatusCondition to the Waitset
    waitset += temperature_status_condition

    # Create a thread to periodically publish the temperature
    start_lot_thread = threading.Thread(target=publish_start_lot,
                                        args=(writer, lots_to_process))
    start_lot_thread.start()
    try:
        while lots_to_process is None or lots_processed < lots_to_process:
            # Dispatch will call the handlers associated to the WaitSet conditions
            # when they activate
            waitset.dispatch(dds.Duration(4))  # Wait for up to 4s each time
    except KeyboardInterrupt:
        pass

    start_lot_thread.join()
Exemple #14
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
Exemple #15
0
def subscriber_main(domain_id, sample_count):
    participant = dds.DomainParticipant(domain_id)

    wsqc_type = dds.QosProvider("waitset_query_cond.xml").type(
        "wsqc_lib", "waitset_query_cond"
    )
    topic = dds.DynamicData.Topic(participant, "Example waitset_query_cond", wsqc_type)
    reader_qos = dds.QosProvider.default.datareader_qos
    reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic, reader_qos)

    # Query against even samples at the start
    query_parameters = ["'EVEN'"]
    query = dds.Query(reader, "name MATCH %0", query_parameters)
    query_condition = dds.QueryCondition(query, dds.DataState.any_data)

    # Create a WaitSet and attach the QueryCondition
    waitset = dds.WaitSet()
    waitset += query_condition

    print(
        textwrap.dedent(
            """\
        >>> Timeout: {} sec
        >>> Query conditions: {}
        \t %0 = {}
        ----------------------------------
        
        """.format(
                1, query_condition.expression, query_parameters[0]
            )
        )
    )

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

        if count == 7:
            # Update the query after 7 seconds
            query_parameters[0] = "'ODD'"
            query_condition.parameters = query_parameters
            print(
                textwrap.dedent(
                    """
                CHANGING THE QUERY CONDITION
                >>> Query conditions:
                {}
                \t %%0 = {}
                >>> We keep one sample in the history
                -------------------------------------
                """.format(
                        query_condition.expression, query_parameters[0]
                    )
                )
            )

        active = waitset.wait((1, 0))

        # Check to see if the query was triggered
        if query_condition in active:
            query_handler(reader, query_condition)

        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