Runtime configuration

eCAL provides an interface to access and modify its options before initialization. The corresponding structure reflects the configuration file (ecal.yaml).

Custom types

In order to rule out configuration errors, custom datatypes for IP addresses (IpAddressV4) and sizes (ConstrainedInteger) are introduced.

IpAddressV4: For assigning an IP address simply assign a string with the desired address. Decimal and hexadecimal format is supported. In case the IP address is not valid, the type will throw a std::invalid_argument exception.

The IP address can be used like a normal string object. For example:

eCAL::Types::IpAddressV4 ip_address = "192.168.7.1"; // in hex: "C0.A8.7.1"
std::cout << ip_address << "\n";

ConstrainedInteger: ConstrainedInteger are specified with a minimum (default: 0), step (default: 1) and maximum (default: maximum of int) value. In case the assigned value does not fit into the specified limitation, the type will throw a std::invalid_argument exception.

The size object can be used like a normal integer.

eCAL::Types::ConstrainedInteger<1024, 512, 8192> size_4mb = 1024 + 6 * 512;
std::cout << size_4mb << "\n";

For specifying sizes in the ecal configuration object, refer to the .yaml file or “ecal/config/configuration.h” for the limitations.

Global configuration initialization

The configuration will be first initialized with the default values specified by eCAL. If you want to use the systems eCAL .yaml file, call the InitFromConfig() function of the config object.

In case the .yaml to use is specified via command line parameter, this one is chosen instead. The object will throw an error, in case the specified .yaml file cannot be found.

It is also possible to specify the .yaml by calling the function InitFromFile(const std::string yaml_path_) of the config object.

  • |fa-file-alt| hello_config/main.cpp:

     1#include <ecal/ecal.h>
     2#include <ecal/ecal_config.h>
     3#include <string>
     4#include <stdexcept>
     5
     6int main(int argc, char** argv)
     7{
     8  // Create a configuration object with the command line arguments
     9  eCAL::Configuration custom_config(argc, argv);
    10
    11  // Use the .yaml file of the system or current folder if available
    12  custom_config.InitFromConfig();
    13
    14  // Set the values in a try/catch block, as wrong configuration leads to exceptions
    15  try
    16  {
    17      // In case you decided to specify an own .yaml file to use
    18      // Configuration based on previous ini file will be overwritten
    19    custom_config.InitFromFile("C:\\eCAL_local.yaml");
    20
    21    // Set the communication layer to network
    22    custom_config.registration.network_enabled = true;
    23
    24    // Set a custom udp multicast group, correct IP address necessary
    25    custom_config.transport_layer.udp.network.group = std::string("239.0.1.1");
    26
    27    // Increase the send buffer, size increase in 1024 bytes steps
    28    custom_config.transport_layer.udp.send_buffer = (5242880 + 10 * 1024);
    29  }
    30  catch (std::invalid_argument& e)
    31  {
    32    throw std::runtime_error("Error while configuring eCALConfig: " + std::string(e.what()));
    33  }
    34
    35  // Initialize eCAL with the prepared configuration object
    36  eCAL::Initialize(custom_config, "UserConfigExample", eCAL::Init::Default);
    37
    38  // ...
    39  // Use eCAL for your needs
    40  // ...
    41
    42  // Finalize eCAL API
    43  eCAL::Finalize();
    44
    45  return 0;
    46}
    

Individual publisher/subscriber configuration

Like a global configuration to pass at initialization, it is also possible to create indiviual configurations for publisher and subscriber. That means it is possible to, e.g., create two publisher which send on different transport layers:

  • |fa-file-alt| publisher/main.cpp:

     1#include <ecal/ecal.h>
     2#include <ecal/msg/string/publisher.h>
     3#include <string>
     4#include <stdexcept>
     5#include <thread>
     6#include <chrono>
     7
     8int main(int argc, char** argv)
     9{
    10  // initialize eCAL API
    11  eCAL::Initialize(0, nullptr, "PublisherConfig", eCAL::Init::All);
    12
    13  // create publisher config
    14  eCAL::Publisher::Configuration pub_config;
    15
    16  // disable all layers except for SHM
    17  pub_config.layer.shm.enable = true;
    18  pub_config.layer.udp.enable = false;
    19  pub_config.layer.tcp.enable = false;
    20
    21  // create publisher 1
    22  eCAL::string::CPublisher<std::string> pub_1("topic_1", pub_config);
    23
    24  // enable for the second publisher also tcp
    25  pub_config.layer.tcp.enable = true;
    26
    27  // create publisher 2
    28  eCAL::string::CPublisher<std::string> pub_2("topic_2", pub_config);
    29
    30  int counter {0};
    31  while (eCAL::Ok())
    32  {
    33    std::string msg = "Send message number: " + std::to_string(counter++);
    34    
    35    // send message
    36    pub_1.Send(msg);
    37    pub_2.Send(msg);
    38
    39    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    40  }
    41
    42  // finalize eCAL API
    43  eCAL::Finalize();
    44}