|
| 1 | +# Example Code: Routing Service C++11 Socket Adapter using Dynamic Data |
| 2 | + |
| 3 | +## Example Description |
| 4 | + |
| 5 | +This example shows how to implement a simple Routing Service Adapter plugin |
| 6 | +in C++11 to receive data from a UDP socket using RTI Routing Service. |
| 7 | + |
| 8 | +This examples uses dynamic data API and there is no need to know the data type |
| 9 | +information beforehand. |
| 10 | + |
| 11 | +The code in this directory provides the following components: |
| 12 | + |
| 13 | +- `src/SocketAdapter` implements the plugin that is loaded by *RTI Routing |
| 14 | +Service*. It responsible for creating and deleting connections. |
| 15 | +- `src/SocketConnection` implements a connection. This component is |
| 16 | +responsible for the creation and deletion of `StreamReaders`. |
| 17 | +- `src/SocketInputDiscoveryStreamReader` implements the logic necessary to |
| 18 | +propagate information about the discovered input streams (in this case |
| 19 | +sockets) to the Routing Service. |
| 20 | +- `src/SocketStreamReader` implements a `StreamReader` that reads sample |
| 21 | +information from a UDP socket. |
| 22 | +- `src/SocketStreamWriter` implements a `StreamWriter` that sends sample |
| 23 | +information to a UDP socket. |
| 24 | + |
| 25 | + |
| 26 | +For more details, please refer to the *RTI Routing Service SDK* documentation. |
| 27 | + |
| 28 | +## Building the C++ example |
| 29 | + |
| 30 | +In order to build this example, you need to define the variables |
| 31 | +`CONNEXTDDS_DIR` and `CONNEXTDDS_ARCH`. You can do so by exporting them |
| 32 | +manually, by sourcing the `rtisetenv` script for your architecture, or by |
| 33 | +passing them to the `cmake` command as arguments: |
| 34 | + |
| 35 | +```bash |
| 36 | +mkdir build |
| 37 | +cd build |
| 38 | +cmake -DCONNEXTDDS_DIR=<Connext DDS Directory> \ # If not exported |
| 39 | + -DCONNEXTDDS_ARCH=<Connext DDS Architecture> \ # If not exported |
| 40 | + -DBUILD_SHARED_LIBS=ON|OFF \ # ON is preferred |
| 41 | + -DCMAKE_BUILD_TYPE=Debug|Release .. |
| 42 | +cmake --build . |
| 43 | +cd .. |
| 44 | +``` |
| 45 | + |
| 46 | +Example command for Windows: |
| 47 | + |
| 48 | +```bash |
| 49 | +cmake .. -DCONNEXTDDS_DIR="%NDDSHOME%" -DCONNEXTDDS_ARCH=x64Win64VS2015 -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -A x64 -G "Visual Studio 17 2022" |
| 50 | +cd .. |
| 51 | +``` |
| 52 | + |
| 53 | +**Note**: You do not need to define `CONNEXTDDS_ARCH` if you only have one |
| 54 | +architecture target installed in your system. |
| 55 | + |
| 56 | +**Note**: When compiling on a Windows 64-bit machine you will need to add the |
| 57 | +`-A x64` parameter to the call to CMake. |
| 58 | + |
| 59 | +**Note:** If you are using a multi-configuration generator, such as Visual |
| 60 | +Studio Solutions, you can specify the configuration mode to build as follows: |
| 61 | + |
| 62 | +```bash |
| 63 | +cmake --build . --config Release|Debug |
| 64 | +``` |
| 65 | + |
| 66 | +Here is more information about generating |
| 67 | +[Visual Studio Solutions for Windows using CMake](https://cmake.org/cmake/help/v3.16/generator/Visual%20Studio%2016%202019.html#platform-selection). |
| 68 | + |
| 69 | +**Note:** `BUILD_SHARED_LIBS` allows you to control if the generated library |
| 70 | +for this example is a static or a dynamic shared library. The following |
| 71 | +sections assume you are building a dynamic shared library. However, Routing |
| 72 | +Service also supports static linking of adapters. To use this functionality |
| 73 | +you would need to create an application that uses Routing Service as a library |
| 74 | +component and statically links to this `SocketAdapter` library. |
| 75 | + |
| 76 | +### Cross-compilation |
| 77 | + |
| 78 | +When you need to cross-compile the example, the above |
| 79 | +command will not work, the assigned compiler won't be the cross-compiler and |
| 80 | +errors may happen when linking against the cross-compiled Connext binaries. |
| 81 | +To fix this, you have to create a file with the architecture name and call |
| 82 | +CMake with a specific flag called ``-DCMAKE_TOOLCHAIN_FILE``. |
| 83 | +An example of the file to create with the toolchain settings (e.g. for an |
| 84 | +ARM architectures): |
| 85 | + |
| 86 | +```cmake |
| 87 | +set(CMAKE_SYSTEM_NAME Linux) |
| 88 | +set(toolchain_path "<path to>/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian") |
| 89 | +set(CMAKE_C_COMPILER "${toolchain_path}/bin/arm-linux-gnueabihf-gcc") |
| 90 | +set(CMAKE_CXX_COMPILER "${toolchain_path}/bin/arm-linux-gnueabihf-g++") |
| 91 | +``` |
| 92 | + |
| 93 | +Then you can call CMake like this: |
| 94 | + |
| 95 | +```bash |
| 96 | +cmake -DCONNEXTDDS_DIR=<connext dir> -DCMAKE_TOOLCHAIN_FILE=<toolchain file created above> |
| 97 | + -DCONNEXTDDS_ARCH=<connext architecture> .. |
| 98 | +``` |
| 99 | + |
| 100 | +## Running the C++ example |
| 101 | + |
| 102 | +To run the example, you just need to run the following commands from the top |
| 103 | +level folder. This example has been written to allow easy experimentation with |
| 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. |
| 106 | + |
| 107 | +There are 2 configurations (`-cfgName`) in the Routing Service XML file: |
| 108 | + |
| 109 | +- **SocketAdapterToDDS** - It reads data from a UDP socket using the |
| 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: |
| 114 | + |
| 115 | + |
| 116 | +To run Routing Service, you will need first to set up your environment as |
| 117 | +follows. |
| 118 | + |
| 119 | +Before running the RTI Routing Service, you need to specify where the |
| 120 | +`SocketAdapterCpp` library is located as shown below: |
| 121 | + |
| 122 | +Linux: |
| 123 | + |
| 124 | +```bash |
| 125 | +$export RTI_LD_LIBRARY_PATH=$NDDSHOME/lib/<Connext DDS Architecture>:<Path to SocketAdapterCpp library, the build/ folder> |
| 126 | +``` |
| 127 | + |
| 128 | +Windows: |
| 129 | + |
| 130 | +```bash |
| 131 | +set PATH=%NDDSHOME%/lib/<Connext DDS Architecture>;<Path to SocketAdapterCpp library, the build/Release folder> |
| 132 | +``` |
| 133 | + |
| 134 | +The SocketAdapterCpp library will be in the `./build` folder. |
| 135 | + |
| 136 | +```bash |
| 137 | +# From the build/ directory |
| 138 | +$NDDSHOME/bin/rtiroutingservice -cfgFile RsSocketAdapter.xml -cfgName SocketAdapterToDDS |
| 139 | +``` |
| 140 | + |
| 141 | +Here is an output from a sample run: |
| 142 | + |
| 143 | +```bash |
| 144 | +$export RTI_LD_LIBRARY_PATH=~/$NDDSHOME/lib/$CONNEXT_ARCH:~/udp_socket_adapter_dynamic/build/ |
| 145 | + |
| 146 | +$ $NDDSHOME/bin/rtiroutingservice -cfgFile RsSocketAdapter.xml -cfgName SocketAdapterToDDS |
| 147 | +RTI Routing Service 7.3.0 executing (with name SocketAdapterToSocketAdapter) |
| 148 | +``` |
| 149 | + |
| 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 | +``` |
| 194 | + |
| 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: |
| 197 | + |
| 198 | +```bash |
| 199 | + $NDDSHOME/bin/rtiddsping -publisher -domainId 0 |
| 200 | +``` |
| 201 | + |
| 202 | +```bash |
| 203 | + $NDDSHOME/bin/rtiddsping -subscriber -domainId 1 |
| 204 | +``` |
| 205 | + |
| 206 | +## Requirements |
| 207 | + |
| 208 | +To run this example you will need: |
| 209 | + |
| 210 | +- RTI Connext Professional version 6.0.0 or higher. |
| 211 | +- CMake version 3.10 or higher. |
| 212 | +- A target platform with support for RTI Routing Service and C++11. |
| 213 | +- Python3. |
0 commit comments