Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property qos Python example #726

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions examples/connext_dds/property_qos/py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Example code: Using Property QoS

If you haven't used the RTI Connext Python API before, first check the
[Getting Started Guide](https://community.rti.com/static/documentation/connext-dds/7.0.0/doc/manuals/connext_dds_professional/getting_started_guide/index.html).

## Building the Example :wrench:

First use **rtiddsgen** to generate the python type file from
`numbers.idl`:

```sh
<install dir>/bin/rtiddsgen -language python -platform universal numbers.idl
```

Where `<install dir>` refers to your RTI Connext installation.

You will see messages that look like:

```plaintext
INFO com.rti.ndds.nddsgen.Main Running rtiddsgen version 4.3.0, please wait ...
WARN com.rti.ndds.nddsgen.emitters.FileEmitter File exists and will not be overwritten : some/path/numbers.py
INFO com.rti.ndds.nddsgen.Main Done
```

This is normal and is only informing you that some of the files that **rtiddsgen**
can generate were already available in the repository.

## Running the Example

In two separate command prompt windows for the publisher and subscriber. Run the
following commands from the example directory (this is necessary to ensure the
application loads the QoS defined in USER_QOS_PROFILES.xml):

```sh
python numbers_publisher.py
python numbers_subscriber.py
```

The applications accept up to two arguments:

1. The `<domain_id>`. Both applications must use the same domain id in order to
communicate. The default is 0.

2. How long the examples should run, measured in samples. The default is
infinite.

While generating the output below, we used values that would capture the most
interesting behavior.

```plaintext
Publisher Output
================
Ok, send_socket_buffer_size....modified
Ok, recv_socket_buffer_size....modified
New UDPv4 send socket buffer size is: 65507
New UDPv4 receive socket buffer size is: 65507
Writing numbers, count 0
Writing numbers, count 1
Writing numbers, count 2
Writing numbers, count 3

Subscriber Output
=================
Ok, send_socket_buffer_size....modified
Ok, recv_socket_buffer_size....modified
New UDPv4 send socket buffer size is: 65507
New UDPv4 receive socket buffer size is: 65507
numbers subscriber sleeping for 4 sec...
number: 500
halfNumber: 250.000000
numbers subscriber sleeping for 4 sec...
number: 250
halfNumber: 125.000000
numbers subscriber sleeping for 4 sec...
number: 125
halfNumber: 62.500000
numbers subscriber sleeping for 4 sec...
```
102 changes: 102 additions & 0 deletions examples/connext_dds/property_qos/py/USER_QOS_PROFILES.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0"?>
<!--
(c) 2005-2014 Copyright, Real-Time Innovations, Inc. All rights reserved.
RTI grants Licensee a license to use, modify, compile, and create derivative
works of the Software. Licensee has the right to distribute object form only
for use with RTI products. The Software is provided "as is", with no warranty
of any type, including any warranty for fitness for any purpose. RTI is under
no obligation to maintain or support the Software. RTI shall not be liable for
any incidental or consequential damages arising out of the use or inability to
use the software.
-->
<!--
Description
XML QoS Profile for numbers

The QoS configuration of the DDS entities in the generated example is
loaded from this file.

This file is used only when it is in the current working directory
or when the enviroment variable
NDDS_QOS_PROFILES is defined and points to this file.

For more information about XML QoS Profiles see Chapter 15 in the
RTI Connext user manual.
-->
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://community.rti.com/schema/7.0.0/rti_dds_qos_profiles.xsd">
<!-- QoS Library containing the QoS profile used in the generated example.

A QoS library is a named set of QoS profiles.
-->
<qos_library name="numbers_Library">

<!-- QoS profile used to configure reliable communication between the DataWriter
and DataReader created in the example code.

A QoS profile groups a set of related QoS.
-->
<qos_profile name="numbers_Profile" is_default_qos="true">
<!-- QoS used to configure the data writer created in the example code -->
<datawriter_qos>
<reliability>
<kind>RELIABLE_RELIABILITY_QOS</kind>
<max_blocking_time>
<sec>60</sec>
</max_blocking_time>
</reliability>

<history>
<kind>KEEP_ALL_HISTORY_QOS</kind>
</history>

<protocol>
<rtps_reliable_writer>
<min_send_window_size>50</min_send_window_size>
<max_send_window_size>50</max_send_window_size>
</rtps_reliable_writer>
</protocol>

</datawriter_qos>

<!-- QoS used to configure the data reader created in the example code -->
<datareader_qos>

<reliability>
<kind>RELIABLE_RELIABILITY_QOS</kind>
</reliability>

<history>
<kind>KEEP_ALL_HISTORY_QOS</kind>
</history>

</datareader_qos>

<domain_participant_qos>
<!--
The participant name, if it is set, will be displayed in the
RTI Analyzer tool, making it easier for you to tell one
application from another when you're debugging.
-->
<participant_name>
<name>RTI Property QoS Example</name>
</participant_name>
<property>
<value>
<!-- transport properties for example -->
<element>
<name>dds.transport.UDPv4.builtin.recv_socket_buffer_size</name>
<value>65507</value>
</element>
<element>
<name>dds.transport.UDPv4.builtin.send_socket_buffer_size</name>
<value>65507</value>
</element>
</value>
</property>

</domain_participant_qos>
</qos_profile>

</qos_library>
</dds>
16 changes: 16 additions & 0 deletions examples/connext_dds/property_qos/py/numbers.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* (c) 2013-2019 Copyright, Real-Time Innovations, Inc. All rights reserved.
*
* RTI grants Licensee a license to use, modify, compile, and create derivative
* works of the Software. Licensee has the right to distribute object form
* only for use with RTI products. The Software is provided "as is", with no
* warranty of any type, including any warranty for fitness for any purpose.
* RTI is under no obligation to maintain or support the Software. RTI shall
* not be liable for any incidental or consequential damages arising out of the
* use or inability to use the software.
*/

struct numbers {
int32 number;
float halfNumber;
};
119 changes: 119 additions & 0 deletions examples/connext_dds/property_qos/py/numbers_publisher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

# (c) Copyright, Real-Time Innovations, 2022. All rights reserved.
# RTI grants Licensee a license to use, modify, compile, and create derivative
# works of the software solely for use with RTI Connext DDS. Licensee may
# redistribute copies of the software provided that all such copies are subject
# to this license. The software is provided "as is", with no warranty of any
# type, including any warranty for fitness for any purpose. RTI is under no
# obligation to maintain or support the software. RTI shall not be liable for
# any incidental or consequential damages arising out of the use or inability
# to use the software.

import time
import sys
import rti.connextdds as dds

from numbers import numbers

class numbersPublisher:

NEW_SOCKET_BUFFER_SIZE_STRING = "65507"

@staticmethod
def create_participant(domain_id: int):

participant_qos = dds.DomainParticipantQos()

# Set the send socket buffer size
participant_qos.property["dds.transport.UDPv4.builtin.send_socket_buffer_size"] = numbersPublisher.NEW_SOCKET_BUFFER_SIZE_STRING

# Set the recv socket buffer size
participant_qos.property["dds.transport.UDPv4.builtin.recv_socket_buffer_size"] = numbersPublisher.NEW_SOCKET_BUFFER_SIZE_STRING

# Create the participant
participant = dds.DomainParticipant(domain_id, participant_qos)

return participant


@staticmethod
def check_participant(participant: dds.DomainParticipant):
participant_qos = participant.qos
send_socket = participant_qos.property.get("dds.transport.UDPv4.builtin.send_socket_buffer_size")
recv_socket = participant_qos.property.try_get("dds.transport.UDPv4.builtin.recv_socket_buffer_size")

if send_socket is None:
print("Error impossible to find send_socket_buffer_size")
return False
elif send_socket != numbersPublisher.NEW_SOCKET_BUFFER_SIZE_STRING:
print("Error, send_socket_buffer_size...not modified")
return False
else:
print("Ok, send_socket_buffer_size....modified")

if recv_socket is None:
print("Error impossible to find recv_socket_buffer_size")
return False
elif recv_socket != numbersPublisher.NEW_SOCKET_BUFFER_SIZE_STRING:
print("Error, recv_socket_buffer_size...not modified")
return False
else:
print("Ok, recv_socket_buffer_size....modified")

print(f"New UDPv4 send socket buffer size is: {send_socket}")
print(f"New UDPv4 receive socket buffer size is: {recv_socket}")

return True

@staticmethod
def run_publisher(domain_id: int, sample_count: int):

# 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)

# If you want to change the DomainParticipant's QoS programmatically
# rather than using the XML file, you will need to add the following lines
# to your code and comment out the create_participant call above.

# In this case, we set the transport settings in the XML by default, but
# in the create_participant call, we set up the transport
# properties using the Properties QoS in code.

# participant = numbersPublisher.create_participant(domain_id)

if(numbersPublisher.check_participant(participant) != True):
print("check_participant error")

# A Topic has a name and a datatype.
topic = dds.Topic(participant, "Example numbers", numbers)

# This DataWriter will write data on Topic "Example numbers"
# DataWriter QoS is configured in USER_QOS_PROFILES.xml
writer = dds.DataWriter(participant.implicit_publisher, topic)
sample = numbers()

sample.number = 1000
sample.halfNumber = sample.number / 2

for count in range(sample_count):
# Catch control-C interrupt
try:
print(f"Writing numbers, count {count}")
writer.write(sample)

sample.number = int(sample.halfNumber)
sample.halfNumber = sample.number / 2

time.sleep(1)
except KeyboardInterrupt:
break

print("preparing to shut down...")


if __name__ == "__main__":
numbersPublisher.run_publisher(
domain_id=0,
sample_count=sys.maxsize)
Loading