2. Building a publish/subscribe application

First of all you need to follow the steps outlined in Requirements for the installation of required packages, and in Installation from Binaries or Installation from Sources for the installation of eprosima Fast RTPS and all its dependencies. If you have followed the steps, you should have Fast DDS, Fast CDR, and Fast-RTPS-Gen installed.

To build a minimal application, you must first define the Topic. To do this, the data type of the Topic is defined by means of an IDL file. An example of this is shown below. In this case, a single string is sufficient. Topics are explained in more detail in Topic, while the topic data types to be defined using IDL are presented in Introduction.

mkdir HelloWorldExample && cd HelloWorldExample
// HelloWorld.idl
struct HelloWorld
{
    string message;
};

Now we need to translate this file to something Fast RTPS understands. For this we have a code generation tool called Fast DDS-Gen (see Execution and IDL Definition), which can do two different things:

  1. Generate C++ definitions for your custom topic.

  2. Generate a functional example that uses your topic data.

It is the second option which is used to create this publish/subscribe application, while the first option is applied in this other tutorial: Writing a simple publisher and subscriber application.

2.1. Generating a minimal functional example

The Fast DDS-Gen tool is used to build the publication/subscription application. This is a Java application for building a fully functional publication/subscription application from an Interface Definition Language (IDL) file that defines the Topic under which messages are published and received. The application generated allows the creation of as many publishers and subscribers as desired, all belonging to the same Domain and communicating using the same Topic.

First, locate the installation directories of Fast DDS, Fast CDR and add these directories to your PATH.

  • If you have followed the colcon installation, run the following command:

source <path-to-Fast-DDS-workspace>/install/setup.bash
  • However, if you have followed the manual installation the libraries are already accessible.

The application files are generated using the following command. The -example option creates an example application, and the CMake files needed to build it.

  • On Linux:

    • If you have followed the colcon installation:

    <path-to-Fast-DDS-workspace>/src/fastrtpsgen/scripts/fastrtpsgen -example CMake HelloWorld.idl
    
    • However, if you have followed the manual installation, run:

    <path-to-Fast-RTPS-Gen>/scripts/fastrtpsgen -example CMake HelloWorld.idl
    
  • On Windows:

    • If you have followed the colcon installation:

    <path-to-Fast-DDS-workspace>/src/fastrtpsgen/scripts/fastrtpsgen.bat -example CMake HelloWorld.idl
    
    • However, if you have followed the manual installation, run:

    <path-to-Fast-RTPS-Gen>/scripts/fastrtpsgen.bat -example CMake HelloWorld.idl
    

Finally, we compile the generated code.

  • On Linux:

mkdir build && cd build
cmake ..
make
  • On Windows:

mkdir build && cd build
cmake -G "Visual Studio 15 2017 Win64" ..
cmake --build .

The application build can be used to spawn any number of publishers and subscribers associated with your topic.

  • On Linux:

./HelloWorld publisher
./HelloWorld subscriber
  • On Windows:

HelloWorld.exe publisher
HelloWorld.exe subscriber

Each time you press <Enter> on the Publisher, a new datagram is generated, sent over the network and receiver by Subscribers currently online. If more than one subscriber is available, it can be seen that the message is equally received on all listening nodes.

You can also modify any values on your custom, IDL-generated data type.

    HelloWorld sample; //Auto-generated container class for topic data from FastRTPSGen
    sample.msg("Hello there!"); // Add contents to the message
    publisher->write(&sample); //Publish

Take a look at the eProsima Fast RTPS examples on github for ideas on how to improve this basic application through different configuration options, and for examples of advanced Fast DDS features.

You may need to set up a special rule in your Firewall for eprosima Fast DDS to work correctly on Windows.