Don't forget to add these items under /protobuf to the project:
hello_world.pb.cc
hello_world.pb.h
Test Code:Snd
#include <ecal/ecal.h>
#include <ecal/msg/protobuf/publisher.h>
#include <iostream>
#include <thread>
#include "protobuf/hello_world.pb.h"
int main(int argc, char** argv)
{
// Initialize eCAL and create a protobuf publisher
eCAL::Initialize(argc, argv, "Hello World Protobuf Publisher");
eCAL::protobuf::CPublisher<proto_messages::HelloWorld> publisher("hello_world_protobuf");
// Ask the user to input his name
std::cout << "Please enter your name: ";
std::string name;
std::getline(std::cin, name);
unsigned int id = 0;
// Infinite loop (using eCAL::Ok() will enable us to gracefully shutdown the
// Process from another application)
while (eCAL::Ok())
{
// Let the user input a message
std::cout << "Type the message you want to send: ";
std::string message;
std::getline(std::cin, message);
// Create a protobuf message object
proto_messages::HelloWorld hello_world_message;
hello_world_message.set_name(name);
hello_world_message.set_msg(message);
hello_world_message.set_id(id++);
// Send the message
publisher.Send(hello_world_message);
std::cout << "Sent message!" << std::endl << std::endl;
}
// finalize eCAL API
eCAL::Finalize();
}
Test Code:Rec
#include <ecal/ecal.h>
#include <ecal/msg/protobuf/subscriber.h>
#include <iostream>
#include <thread>
#include "hello_world.pb.h"
void HelloWorldCallback(const proto_messages::HelloWorld& hello_world_msg)
{
std::cout << hello_world_msg.name() << " sent a message with ID "
<< hello_world_msg.id() << ":" << std::endl
<< hello_world_msg.msg() << std::endl << std::endl;
}
int main(int argc, char** argv)
{
// Initialize eCAL and create a protobuf subscriber
eCAL::Initialize(argc, argv, "Hello World Protobuf Subscriber");
eCAL::protobuf::CSubscriber<proto_messages::HelloWorld> subscriber("hello_world_protobuf");
// Set the Callback
subscriber.AddReceiveCallback(std::bind(&HelloWorldCallback, std::placeholders::_2));
// Just don't exit
while (eCAL::Ok())
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
// finalize eCAL API
eCAL::Finalize();
}
#include <ecal/ecal.h>
#include <ecal/msg/protobuf/publisher.h>
#include <iostream>
#include <thread>
#include "hello_world.pb.h"
int main(int argc, char** argv)
{
// Initialize eCAL and create a protobuf publisher
eCAL::Initialize(argc, argv, "Hello World Protobuf Publisher");
eCAL::protobuf::CPublisher<proto_messages::HelloWorld> publisher("hello_world_protobuf");
// Ask the user to input his name
std::cout << "Please enter your name: ";
std::string name;
std::getline(std::cin, name);
unsigned int id = 0;
// Infinite loop (using eCAL::Ok() will enable us to gracefully shutdown the
// Process from another application)
while (eCAL::Ok())
{
// Let the user input a message
std::cout << "Type the message you want to send: ";
std::string message;
std::getline(std::cin, message);
// Create a protobuf message object
proto_messages::HelloWorld hello_world_message;
hello_world_message.set_name(name);
hello_world_message.set_msg (message);
hello_world_message.set_id (id++);
// Send the message
publisher.Send(hello_world_message);
std::cout << "Sent message!" << std::endl << std::endl;
}
// finalize eCAL API
eCAL::Finalize();
}
#include <ecal/ecal.h>
#include <ecal/msg/protobuf/subscriber.h>
#include <iostream>
#include <thread>
#include "hello_world.pb.h"
void HelloWorldCallback(const proto_messages::HelloWorld& hello_world_msg)
{
std::cout << hello_world_msg.name() << " sent a message with ID "
<< hello_world_msg.id() << ":" << std::endl
<< hello_world_msg.msg() << std::endl << std::endl;
}
int main(int argc, char** argv)
{
// Initialize eCAL and create a protobuf subscriber
eCAL::Initialize(argc, argv, "Hello World Protobuf Subscriber");
eCAL::protobuf::CSubscriber<proto_messages::HelloWorld> subscriber("hello_world_protobuf");
// Set the Callback
subscriber.AddReceiveCallback(std::bind(&HelloWorldCallback, std::placeholders::_2));
// Just don't exit
while (eCAL::Ok())
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
// finalize eCAL API
eCAL::Finalize();
}
Generate pb.cc and pb.h
Because we ues eCAL ,so the protoc.exe ,we just use what eCAL provides.
In Edit the system environment varialbes ,open the Environment Variables, choose the Path under the User variables for {user} ,click Edit ,and add a new one:
C:\eCAL\bin
Once this is set up, we can use the following command to generate pb files at any time.
#include <ecal/ecal.h>
#include <ecal/msg/protobuf/publisher.h>
#include <iostream>
#include "person.pb.h"
int main(int argc, char **argv)
{
eCAL::Initialize(argc, argv, "person publisher");// initialize eCAL API
eCAL::Process::SetState(proc_sev_healthy, proc_sev_level1, "I feel good !");// set process state
}
single or dynamic publisher
// create a publisher (topic name "person")
eCAL::protobuf::CPublisher<pb::People::Person> pub("person");
// generate a class instance of Person
pb::People::Person person;
or
// create a dynamic publisher (topic name "person")
eCAL::protobuf::CDynamicPublisher pub1("person", std::make_shared<pb::People::Person>());
std::shared_ptr<pb::People::Person> person1 = pub1.GetAs<pb::People::Person>();
// create a dynamic publisher (topic name "person")
eCAL::protobuf::CDynamicPublisher pub2("person", "pb.People.Person");
std::shared_ptr<pb::People::Person> person2 = pub2.GetAs<pb::People::Person>();
// set person1 object content
person1->set_name("Max");
person1->set_stype(pb::People::Person_SType_MALE);
person1->set_email("max@mail.net");
person1->mutable_dog()->set_name("Brandy");
person1->mutable_house()->set_rooms(4);
// set person2 object content
person2->set_name("Romy");
person2->set_stype(pb::People::Person_SType_FEMALE);
person2->set_email("romy@mail.net");
person2->mutable_dog()->set_name("Gorky");
person2->mutable_house()->set_rooms(4);