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, a custom datatype for IP addresses (IpAddressV4) is 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";
Global configuration initialization#
To get the configuration, create the object by using the function in the init namespace:
auto custom_config = eCAL::Init::Configuration();
The configuration will be first initialized with the values specified by your ecal.yaml.
It is also possible to specify the .yaml by calling the function
custom_config.InitFromFile("path/to/your/ecal.yaml");
main.cpp
:1#include <ecal/ecal.h> 2#include <ecal/config.h> 3#include <string> 4#include <stdexcept> 5 6int main(int argc, char** argv) 7{ 8 // Create a initial configuration object. 9 // By default, this object is initialized with a configuration based on an ecal.yaml file. 10 auto custom_config = eCAL::Init::Configuration(); 11 12 // Furthermore you can adapt the configuration by overwriting values from the file, e.g. 13 // set the communication mode to network 14 custom_config.communication_mode = eCAL::eCommunicationMode::network; 15 16 // Increase the send buffer, size increase in 1024 bytes steps 17 custom_config.transport_layer.udp.send_buffer = (5242880 + 10 * 1024); 18 19 // Set the network addresses in a try/catch block, as wrong configuration 20 // leads to exceptions 21 try 22 { 23 // Set a custom udp multicast group, correct IP address necessary 24 custom_config.transport_layer.udp.network.group = std::string("239.0.1.1"); 25 } 26 catch (std::invalid_argument& e) 27 { 28 // Continue with the program, but let the user of the exception and the used network group 29 std::cout << "Error while configuring network group: " + std::string(e.what()) << "\n"; 30 std::cout << "Using default network group: " << custom_config.transport_layer.udp.network.group.Get() << "\n"; 31 } 32 33 // Initialize eCAL with the prepared configuration object 34 eCAL::Initialize(custom_config, "UserConfigExample", eCAL::Init::Default); 35 36 // ... 37 // Use eCAL for your needs 38 // ... 39 40 while (eCAL::Ok()) 41 { 42 // ... 43 } 44 45 // Finalize eCAL API 46 eCAL::Finalize(); 47 48 return 0; 49}
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:
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("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 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 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}