TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
vision_receiver_example.cpp
Go to the documentation of this file.
1 /**
2  * @file vision_receiver_example.cpp
3  *
4  * @author Lucas Haug <lucas.haug@thuneratz.org>
5  * @author Lucas Schneider <lucas.schneider@thuneratz.org>
6  * @author Felipe Gomes de Melo <felipe.gomes@thuneratz.org>
7  *
8  * @brief Example to read data published from vision adapter
9  *
10  * @date 04/2021
11  *
12  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
13  */
14 
15 #include <iostream>
16 #include <string>
17 #include <google/protobuf/util/json_util.h>
18 #include <google/protobuf/text_format.h>
19 
20 #include "packet.pb.h"
22 
23 /*****************************************
24  * Private Constants
25  *****************************************/
26 
27 #define BUFFER_SIZE 1024U
28 
29 /*****************************************
30  * Main Function
31  *****************************************/
32 
33 int main(int argc, char* argv[]) {
34  const std::string listen_address_str = "0.0.0.0";
35  const std::string multicast_address_str = "224.0.0.1";
36  const short multicast_port = 10002;
37 
38  char data_buff[BUFFER_SIZE];
39 
40  try {
41  travesim::udp::MulticastReceiver my_receiver(multicast_address_str, multicast_port);
42 
43  size_t data_size = 0;
44 
45  for (long int i = 0;; i++) {
46  data_size = my_receiver.receive(data_buff, BUFFER_SIZE);
47 
48  if (data_size > 0) {
49  fira_message::sim_to_ref::Environment env_data;
50  env_data.ParseFromString(data_buff);
51 
52  std::string parsed_msg;
53 
54  google::protobuf::util::JsonPrintOptions options;
55  options.add_whitespace = true;
56  options.always_print_primitive_fields = true;
57 
58  google::protobuf::util::MessageToJsonString(env_data, &parsed_msg, options);
59 
60  std::cout << parsed_msg << std::endl;
61  }
62 
63  if (i % 100000 == 0) {
64  std::cout << "Loop count: " << i << std::endl;
65  }
66  }
67  } catch (std::exception& e) {
68  std::cerr << "Exception: " << e.what() << "\n";
69  }
70 
71  return 0;
72 }
#define BUFFER_SIZE
Receiver class using UDP in multicast mode.
size_t receive(char *buffer, const size_t buffer_size)
Receive data using UDP.
Definition: receiver.cpp:44
Receiver data using UDP in multicast mode.
int main(int argc, char *argv[])