7.2.2. String: Hello World#

In the good habit of every tutorial, we will write a Hello World application, that sends the string “Hello World” to an eCAL topic. It will utilize the eCAL string publisher to send the message, which is a really thin layer on top of the binary. The second part will be all about the subscriber, which will receive the message and print it to the console.

Basically, by using eCAL’s message API, we no longer send raw binary data, but utf-8 encoded strings. Hence, depending on the native string handling in the programming language, the serialization is “just” an utf-8 encoding / decoding of a string.

All other available methods for binary publishers and subscribers are also available for message publishers and subscribers.

7.2.2.1. Hello World Publisher#

 1// Include the basic eCAL header
 2#include <ecal/ecal.h>
 3/* 
 4  We want to publish raw strings, so wie include the string publisher header.
 5  eCAL supports multiple message formats.
 6*/
 7#include <ecal/msg/string/publisher.h>
 8
 9#include <iostream>
10#include <sstream>
11
12int main()
13{
14  std::cout << "--------------------------" << std::endl;
15  std::cout << " C++: HELLO WORLD SENDER"        << std::endl;
16  std::cout << "--------------------------" << std::endl;
17
18  /* 
19    Initialize eCAL. You always have to initialize eCAL before using its API.
20    The name of our eCAL Process will be "hello send". 
21    This name will be visible in the eCAL Monitor, once the process is running.
22  */
23  eCAL::Initialize("hello send");
24
25  /*
26    Print some eCAL version information.
27  */
28  std::cout << "eCAL " << eCAL::GetVersionString() << " (" << eCAL::GetVersionDateString() << ")" << "\n";
29
30  /*
31    Set the state for the program.
32    You can vary between different states like healthy, warning, critical ...
33    This can be used to communicate the application state to applications like eCAL Monitor/Sys.
34  */
35  eCAL::Process::SetState(eCAL::Process::eSeverity::healthy, eCAL::Process::eSeverityLevel::level1, "I feel good!");
36
37  /*
38    Creating the eCAL Publisher. An eCAL Process can create multiple publishers (and subscribers).
39    The topic we are going to publish is called "hello".
40  */
41  eCAL::string::CPublisher publisher("hello");
42
43  /*
44    Creating an inifite publish-loop.
45    eCAL Supports a stop signal; when an eCAL Process is stopped, eCAL::Ok() will return false.
46  */
47  int loop_count = 0;
48  while (eCAL::Ok())
49  {
50    /*
51      Build the string you want to send, using a stringstream in this example.
52    */
53    std::stringstream message;
54    message << "HELLO WORLD FROM C++" << " (" << ++loop_count << ")";
55
56    /*
57      Send the content to other eCAL Processes that have subscribed to the topic "hello".
58    */
59    if(publisher.Send(message.str()))
60      std::cout << "Sent string message in C++ \"" << message.str() << "\"" << "\n";
61    else
62      std::cout << "Sending string message in C++ failed!" << "\n";
63
64    /*
65      Sleep for 500 ms so we send with a frequency of 2 Hz.
66    */
67    eCAL::Process::SleepMS(500);
68  }
69
70  /*
71    Deinitialize eCAL.
72    You should always do that before your application exits.
73  */
74  eCAL::Finalize();
75
76  return(0);
77}

7.2.2.2. Hello World Publisher Files#

 Publisher samples
├─  C++
│  └─  hello_send.cpp
│
├─  C
│  └─  hello_send.c
│
├─  C#
│  └─  hello_send.cs
│
├─  Python
│  └─  hello_send.py
│
└─  Python (legacy)
   └─  hello_send.py

7.2.2.3. Hello World Subscriber#

 1// Include the basic eCAL header
 2#include <ecal/ecal.h>
 3/* 
 4  We want to receive raw strings, so wie include the string subscriber header.
 5  eCAL supports multiple message formats.
 6*/
 7#include <ecal/msg/string/subscriber.h>
 8
 9#include <iostream>
10#include <sstream>
11#include <chrono>
12#include <thread>
13
14int main()
15{
16  std::cout << "----------------------------" << "\n";
17  std::cout << " C++: HELLO WORLD RECEIVER"   << "\n";
18  std::cout << "----------------------------" << "\n";
19
20  /* 
21    Initialize eCAL. You always have to initialize eCAL before using its API.
22    The name of our eCAL Process will be "hello receive". 
23    This name will be visible in the eCAL Monitor, once the process is running.
24  */
25  eCAL::Initialize("hello receive");
26
27  /*
28    Print some eCAL version information.
29  */
30  std::cout << "eCAL " << eCAL::GetVersionString() << " (" << eCAL::GetVersionDateString() << ")" << "\n";
31
32  /*
33    Set the state for the program.
34    You can vary between different states like healthy, warning, critical ...
35    This can be used to communicate the application state to applications like eCAL Monitor/Sys.
36  */
37  eCAL::Process::SetState(eCAL::Process::eSeverity::healthy, eCAL::Process::eSeverityLevel::level1, "I feel good!");
38
39  /*
40    Creating the eCAL Subscriber. An eCAL Process can create multiple subscribers (and publishers).
41    The topic we are going to receive is called "hello".
42  */
43  eCAL::string::CSubscriber subscriber("hello");
44
45  /*
46    Creating a receive callback. The callback will be called whenever a new message is received.
47  */
48  auto message_callback = [](const eCAL::STopicId& publisher_id_, const std::string& message_, long long time_, long long clock_) { 
49    std::cout << "---------------------------------------------------"                                << "\n";
50    std::cout << " Received string message from topic \"" << publisher_id_.topic_name << "\" in C++ " << "\n";
51    std::cout << "---------------------------------------------------"                                << "\n";
52    std::cout << " Size    : " << message_.size()                                                     << "\n";
53    std::cout << " Time    : " << time_                                                               << "\n";
54    std::cout << " Clock   : " << clock_                                                              << "\n";
55    std::cout << " Message : " << message_                                                            << "\n";
56    std::cout << "\n";
57  };
58  
59  /*
60    Register the callback with the subscriber so it can be called.
61  */
62  subscriber.SetReceiveCallback(message_callback);
63
64  /*
65    Creating an infinite loop.
66    eCAL Supports a stop signal; when an eCAL Process is stopped, eCAL::Ok() will return false.
67  */
68  while (eCAL::Ok())
69  {
70    /*
71      Sleep for 500ms to avoid busy waiting.
72      You can use eCAL::Process::SleepMS() to sleep in milliseconds.
73    */
74    eCAL::Process::SleepMS(500);
75  }
76
77  /*
78    Deinitialize eCAL.
79    You should always do that before your application exits.
80  */
81  eCAL::Finalize();
82
83  return(0);
84}

7.2.2.4. Hello World Subscriber Files#

 Subscriber samples
├─  C++
│  └─  hello_receive.cpp
│
├─  C
│  └─  hello_receive.c
│
├─  C#
│  └─  hello_receive.cs
│
├─  Python
│  └─  hello_receive.py
│
└─  Python (legacy)
   └─  hello_receive.py