Skip to content

Commit 962e46b

Browse files
committed
Edited Readme files and minor adjustments
1 parent 38f0644 commit 962e46b

File tree

9 files changed

+201
-245
lines changed

9 files changed

+201
-245
lines changed

examples/routing_service/udp_socket_adapter/test/read_shape_from_socket.py

Whitespace-only changes.

examples/routing_service/udp_socket_adapter_dynamic/README.md

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
This example shows how to implement a simple Routing Service Adapter plugin
66
in C++11 to receive data from a UDP socket using RTI Routing Service.
77

8+
This examples uses dynamic data API and there is no need to know the data type
9+
information beforehand.
10+
811
The code in this directory provides the following components:
912

1013
- `src/SocketAdapter` implements the plugin that is loaded by *RTI Routing
@@ -16,8 +19,9 @@ propagate information about the discovered input streams (in this case
1619
sockets) to the Routing Service.
1720
- `src/SocketStreamReader` implements an `StreamReader` that reads sample
1821
information from a UDP socket.
19-
- `test/send_shape_to_socket.py` implements a simple tester to send shape
20-
type data to a UDP socket.
22+
- `src/SocketStreamWriter` implements an `StreamWriter` that sends sample
23+
information to a UDP socket.
24+
2125

2226
For more details, please refer to the *RTI Routing Service SDK* documentation.
2327

@@ -97,19 +101,17 @@ cmake -DCONNEXTDDS_DIR=<connext dir> -DCMAKE_TOOLCHAIN_FILE=<toolchain file crea
97101

98102
To run the example, you just need to run the following commands from the top
99103
level folder. This example has been written to allow easy experimentation with
100-
the Shapes Demo shipped with *RTI Connext DDS* installer bundle. You will find
101-
some hardcoded references to ShapeType and Square. If you wish to create a
102-
real Routing Service adapter, you should modify the code and XML accordingly.
104+
the RTI DDSPing tool shipped with *RTI Connext DDS* installer bundle. If you wish
105+
to create a real Routing Service adapter, you should modify the code and XML accordingly.
103106

104-
There is 1 configuration (`-cfgName`) in the Routing Service XML file:
107+
There are 2 configurations (`-cfgName`) in the Routing Service XML file:
105108

106109
- **SocketAdapterToDDS** - It reads data from a UDP socket using the
107-
SocketAdapter and outputs it to DDS. You can visualize the ouptut by
108-
subscribing to Squares in Shapes Demo or running:
110+
SocketAdapter and outputs it to DDS. You can visualize the ouptut by running:
111+
112+
- **DDSToSocketAdapter** - It sends data from DDS to a UDP socket. You can
113+
publish DDS data by running command:
109114

110-
```bash
111-
$NDDSHOME/bin/rtiddsspy -printSample
112-
```
113115

114116
To run Routing Service, you will need first to set up your environment as
115117
follows.
@@ -139,24 +141,67 @@ $NDDSHOME/bin/rtiroutingservice -cfgFile RsSocketAdapter.xml -cfgName SocketAdap
139141
Here is an output from a sample run:
140142

141143
```bash
142-
$export RTI_LD_LIBRARY_PATH=~/$NDDSHOME/lib/$CONNEXT_ARCH:~/udp_socket_adapter/build/
144+
$export RTI_LD_LIBRARY_PATH=~/$NDDSHOME/lib/$CONNEXT_ARCH:~/udp_socket_adapter_dynamic/build/
143145

144146
$ $NDDSHOME/bin/rtiroutingservice -cfgFile RsSocketAdapter.xml -cfgName SocketAdapterToDDS
145147
RTI Routing Service 7.3.0 executing (with name SocketAdapterToSocketAdapter)
146148
```
147149

148-
Now you'll need to send data to the UDP sockets. By default, Shapes are
149-
expected on `127.0.0.1:10203`. You can change these default values on
150-
`RsSocketAdapter.xml`.
150+
Now you'll need to send data to the UDP sockets. By default, DDS Ping data is
151+
expected on `127.0.0.1:10203`. You can change both the expected type and topic name
152+
and the UDP socket configuration on `RsSocketAdapter.xml`.
153+
154+
To run a simple test, run in different terminals:
155+
156+
```bash
157+
$export RTI_LD_LIBRARY_PATH=~/$NDDSHOME/lib/$CONNEXT_ARCH:~/udp_socket_adapter_dynamic/build/
158+
159+
$ $NDDSHOME/bin/rtiroutingservice -cfgFile RsSocketAdapter.xml -cfgName DDSToSocketAdapter
160+
RTI Routing Service 7.3.0 executing (with configuration=DDSToSocketAdapter)
161+
```
162+
163+
164+
```bash
165+
$NDDSHOME/bin/rtiddsping -publisher -domainId 0
166+
```
167+
168+
## Running a data-diode example
169+
170+
You can configure a data-diode scenario by using two Routing Services instances;
171+
- One using **DDSToSocketAdapter** configuration to publish DDS data over a one direction UDP socket
172+
- The other using **SocketAdapterToDDS** configuration to convert back to DDS samples
173+
174+
┌───────────┐ ┌─────────────┐ ┌─────────────┐ ┌───────────┐
175+
│ Connext │ │ Routing │ ┌────────────────┐ │ Routing │ │ Connext │
176+
│ App ├─►│ Service ├───►│ UDP DATA DIODE ├──►│ Service ├─►│ App │
177+
│ │ │ DDS TO UDP │ └────────────────┘ │ UDP TO DDS │ │ │
178+
└───────────┘ └─────────────┘ └─────────────┘ └───────────┘
179+
180+
To run this example in a local machine:
181+
```bash
182+
$export RTI_LD_LIBRARY_PATH=~/$NDDSHOME/lib/$CONNEXT_ARCH:~/udp_socket_adapter_dynamic/build/
183+
184+
$ $NDDSHOME/bin/rtiroutingservice -cfgFile RsSocketAdapter.xml -cfgName SocketAdapterToDDS
185+
RTI Routing Service 7.3.0 executing (with configuration=SocketAdapterToDDS)
186+
```
187+
And in a different terminal:
188+
```bash
189+
$export RTI_LD_LIBRARY_PATH=~/$NDDSHOME/lib/$CONNEXT_ARCH:~/udp_socket_adapter_dynamic/build/
190+
191+
$ $NDDSHOME/bin/rtiroutingservice -cfgFile RsSocketAdapter.xml -cfgName DDSToSocketAdapter
192+
RTI Routing Service 7.3.0 executing (with configuration=DDSToSocketAdapter)
193+
```
151194

152-
To run the Shape tester that mimics a legacy UDP socket sender, run:
195+
Using the default configuration from RsSocketAdapter.xml, you need to publish DDS Ping data
196+
on domain id 0 and subscribe to DSS Ping data on domain id 1:
153197

154198
```bash
155-
python3 test/send_shape_to_socket.py 127.0.0.1 10203
199+
$NDDSHOME/bin/rtiddsping -publisher -domainId 0
156200
```
157201

158-
You can now open a Shapes Demo instance on domain 0 and subscribe to Squares.
159-
You should start receiving a red Square.
202+
```bash
203+
$NDDSHOME/bin/rtiddsping -subscriber -domainId 1
204+
```
160205

161206
## Requirements
162207

examples/routing_service/udp_socket_adapter_dynamic/RsSocketAdapter.xml

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,25 @@
99
</adapter_plugin>
1010
</plugin_library>
1111

12-
<!-- Demonstrates a scenario where the SocketAdapter reads squares data from a socket
13-
and the DDSAdapter writes it to a DDS domain which can be subscribed to in
14-
Shapes Demo or using 'rtiddsspy -printSample' -->
12+
<!-- Demonstrates a scenario where the SocketAdapter reads ping data from a socket
13+
and the DDSAdapter writes it to a DDS domain -->
1514
<routing_service name="SocketAdapterToDDS">
1615
<domain_route name="SocketBridge">
1716
<connection name="SocketConnection" plugin_name="AdapterLib::SocketAdapter">
18-
<registered_type name="ShapeType" type_name="ShapeType" />
17+
<registered_type name="PingType" type_name="PingType" />
1918
</connection>
2019
<participant name="DDSConnection">
21-
<domain_id>0</domain_id>
20+
<domain_id>1</domain_id>
2221
<domain_participant_qos base_name="BuiltinQosLib::Generic.Common" />
23-
24-
<registered_type name="ShapeType" type_name="ShapeType" />
22+
<registered_type name="PingType" type_name="PingType" />
2523
</participant>
2624
<session name="session">
2725
<route>
2826
<input connection="SocketConnection">
29-
<creation_mode>ON_DOMAIN_MATCH</creation_mode>
30-
<registered_type_name>ShapeType</registered_type_name>
31-
<stream_name>Square</stream_name>
32-
<!-- You must set these 3 properties -->
27+
<creation_mode>IMMEDIATE</creation_mode>
28+
<registered_type_name>PingType</registered_type_name>
29+
<stream_name>PingStream</stream_name>
30+
<!-- You must set these 2 properties -->
3331
<property>
3432
<value>
3533
<!-- Receive address for UDP data -->
@@ -40,27 +38,38 @@
4038
<!-- Receive port for UDP data -->
4139
<element>
4240
<name>receive_port</name>
43-
<value>10204</value>
41+
<value>10203</value>
4442
</element>
4543
</value>
4644
</property>
4745
</input>
4846
<dds_output participant="DDSConnection">
49-
<creation_mode>ON_ROUTE_MATCH</creation_mode>
50-
<registered_type_name>ShapeType</registered_type_name>
51-
<!-- Topic used to send data. We only support 1 topic for simplicity.
52-
You could use Triangle or Circle if you also modify the other
53-
references to Square that are hardcoded in the adapter code -->
54-
<topic_name>Square</topic_name>
47+
<creation_mode>ON_DOMAIN_MATCH</creation_mode>
48+
<registered_type_name>PingType</registered_type_name>
49+
<topic_name>PingTopic</topic_name>
5550
<datawriter_qos base_name="BuiltinQosLib::Generic.Common" />
5651
</dds_output>
5752
</route>
53+
</session>
54+
</domain_route>
55+
</routing_service>
56+
57+
<routing_service name="DDSToSocketAdapter">
58+
<domain_route name="SocketBridge">
59+
<connection name="SocketConnection" plugin_name="AdapterLib::SocketAdapter">
60+
<registered_type name="PingType" type_name="PingType" />
61+
</connection>
62+
<participant name="DDSConnection">
63+
<domain_id>0</domain_id>
64+
<domain_participant_qos base_name="BuiltinQosLib::Generic.Common" />
65+
<registered_type name="PingType" type_name="PingType" />
66+
</participant>
67+
<session>
5868
<route>
5969
<output connection="SocketConnection">
6070
<creation_mode>ON_ROUTE_MATCH</creation_mode>
61-
<!--creation_mode>IMMEDIATE</creation_mode-->
62-
<registered_type_name>ShapeType</registered_type_name>
63-
<stream_name>Square</stream_name>
71+
<registered_type_name>PingType</registered_type_name>
72+
<stream_name>PingStream</stream_name>
6473
<property> <!--Set
6574
the IP addresses and ports for sender and destination-->
6675
<value>
@@ -79,16 +88,15 @@
7988
</element>
8089
<element>
8190
<name>dest_port</name>
82-
<value>10204</value>
91+
<value>10203</value>
8392
</element>
8493
</value>
8594
</property>
8695
</output>
8796
<dds_input participant="DDSConnection">
8897
<creation_mode>ON_DOMAIN_MATCH</creation_mode>
89-
<!--creation_mode>IMMEDIATE</creation_mode-->
90-
<registered_type_name>ShapeType</registered_type_name>
91-
<topic_name>Square</topic_name>
98+
<registered_type_name>PingType</registered_type_name>
99+
<topic_name>PingTopic</topic_name>
92100
<datareader_qos base_name="BuiltinQosLib::Generic.Common" />
93101
</dds_input>
94102
</route>

examples/routing_service/udp_socket_adapter_dynamic/src/SocketConnection.cxx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "SocketConnection.hpp"
1414
#include "SocketStreamReader.hpp"
1515
#include "SocketStreamWriter.hpp"
16+
#include "SocketStreamWriter.hpp"
1617

1718
using namespace rti::routing;
1819
using namespace rti::routing::adapter;
@@ -35,10 +36,10 @@ StreamReader *SocketConnection::create_stream_reader(
3536
}
3637

3738
StreamWriter *SocketConnection::create_stream_writer(
38-
Session *session,
39-
const StreamInfo &info,
40-
const PropertySet &properties)
41-
{
39+
Session *session,
40+
const StreamInfo &info,
41+
const PropertySet &properties)
42+
{
4243
return new SocketStreamWriter(this, info, properties);
4344
}
4445

examples/routing_service/udp_socket_adapter_dynamic/src/SocketStreamReader.cxx

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
2+
* (c) 2019 Copyright, Real-Time Innovations, Inc. All rights reserved.
33
*
44
* RTI grants Licensee a license to use, modify, compile, and create derivative
55
* works of the Software. Licensee has the right to distribute object form
@@ -12,58 +12,55 @@
1212

1313
#include <algorithm>
1414
#include <cctype>
15+
#include <chrono>
1516
#include <sstream>
1617
#include <thread>
17-
#include <chrono>
1818

19-
#include <iostream>
2019
#include <cstring>
20+
#include <iostream>
2121
#ifdef _WIN32
2222
#include <winsock2.h>
2323
#pragma comment(lib, "ws2_32.lib")
2424
#else
25-
#include <sys/types.h>
26-
#include <sys/socket.h>
27-
#include <netinet/in.h>
2825
#include <arpa/inet.h>
26+
#include <netinet/in.h>
27+
#include <sys/socket.h>
28+
#include <sys/types.h>
2929
#include <unistd.h>
3030
#endif
31-
3231
#include "SocketStreamReader.hpp"
3332
#include <rti/core/Exception.hpp>
33+
#include <rti/routing/Logger.hpp>
3434
#include <rti/topic/cdr/Serialization.hpp>
3535

36+
3637
using namespace dds::core::xtypes;
3738
using namespace rti::routing;
3839
using namespace rti::routing::adapter;
3940

4041
void SocketStreamReader::socket_reading_thread()
4142
{
4243
while (!stop_thread_) {
43-
/**
44-
* Essential to protect against concurrent data access to
45-
* buffer_ from the take() methods running on a different
46-
* Routing Service thread.
47-
*/
48-
std::unique_lock<std::mutex> lock(buffer_mutex_);
44+
int received_bytes = 0;
4945
socket->receive_data(
5046
received_buffer_,
51-
&received_bytes_,
47+
&received_bytes,
5248
BUFFER_MAX_SIZE);
53-
lock.unlock();
5449

5550
// Most likely received nothing or there was an error
5651
// Not doing any error handling here
57-
if (received_bytes_ <= 0) {
52+
if (received_bytes <= 0) {
5853
// Sleep for a small period of time to avoid busy waiting
5954
std::this_thread::sleep_for(std::chrono::milliseconds(10));
6055
continue;
6156
}
62-
6357
/**
6458
* Here we notify Routing Service, that there is data available
6559
* on the StreamReader, triggering a call to take().
6660
*/
61+
62+
received_bytes_ = received_bytes;
63+
6764
reader_listener_->on_data_available(this);
6865
}
6966

@@ -80,8 +77,7 @@ SocketStreamReader::SocketStreamReader(
8077
{
8178
socket_connection_ = connection;
8279
reader_listener_ = listener;
83-
adapter_type_ =
84-
static_cast<DynamicType *>(info.type_info().type_representation());
80+
adapter_type_ = static_cast<DynamicType *>(info.type_info().type_representation());
8581

8682
// Parse the properties provided in the xml configuration file
8783
for (const auto &property : properties) {
@@ -92,35 +88,21 @@ SocketStreamReader::SocketStreamReader(
9288
}
9389
}
9490

95-
// If any of the mandatory properties is not specified, throw exception
96-
if (receive_address_.size() == 0 || receive_port_ == 0) {
97-
throw dds::core::IllegalOperationError(
98-
"You must set receive_address and receive_port "
99-
"in the RsSocketAdapter.xml file");
100-
}
101-
102-
// Create the UDP socket to receive data
10391
socket = std::unique_ptr<UdpSocket>(
10492
new UdpSocket(receive_address_.c_str(), receive_port_));
10593

106-
// Start the receive thread for UDP data
10794
socketreader_thread_ =
10895
std::thread(&SocketStreamReader::socket_reading_thread, this);
10996
}
11097

111-
/**
112-
* This is the Routing Service take(). It's called when the
113-
* socket_receive_thread calls on_data_available()
114-
*/
11598
void SocketStreamReader::take(
11699
std::vector<dds::core::xtypes::DynamicData *> &samples,
117100
std::vector<dds::sub::SampleInfo *> &infos)
118101
{
119102
dds::core::xtypes::DynamicData deserialized_sample(*adapter_type_);
120103
std::vector<char> received_buffer = std::vector<char>(received_buffer_, received_buffer_ + received_bytes_);
121104
rti::core::xtypes::from_cdr_buffer(deserialized_sample, received_buffer);
122-
std::cout << deserialized_sample << std::endl;
123-
105+
124106
samples.resize(1);
125107
infos.resize(1);
126108

0 commit comments

Comments
 (0)