bt_recorder.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <iostream>
3 #include <fstream>
4 #include <signal.h>
5 #include <zmq.hpp>
6 #include <fstream>
8 
9 // http://zguide.zeromq.org/cpp:interrupt
10 static bool s_interrupted = false;
11 
12 static void s_signal_handler(int)
13 {
14  s_interrupted = true;
15 }
16 
17 static void CatchSignals(void)
18 {
19  struct sigaction action;
20  action.sa_handler = s_signal_handler;
21  action.sa_flags = 0;
22  sigemptyset(&action.sa_mask);
23  sigaction(SIGINT, &action, NULL);
24  sigaction(SIGTERM, &action, NULL);
25 }
26 
27 int main(int argc, char* argv[])
28 {
29  if (argc != 2)
30  {
31  printf("Wrong number of arguments\nUsage: %s [filename]\n", argv[0]);
32  return 1;
33  }
34 
35  // register CTRL+C signal handler
36  CatchSignals();
37 
38  zmq::context_t context(1);
39 
40  // Socket to talk to server
41  std::cout << "Trying to connect to [tcp://localhost:1666]\n" << std::endl;
42 
43  zmq::socket_t subscriber(context, ZMQ_SUB);
44  subscriber.connect("tcp://localhost:1666");
45 
46  // Subscribe to everything
47  subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
48 
49  printf("----------- Started -----------------\n");
50 
51  bool first_message = true;
52  std::ofstream file_os;
53 
54  while (!s_interrupted)
55  {
56  zmq::message_t update;
57  zmq::message_t msg;
58  try
59  {
60  subscriber.recv(&update);
61  }
62  catch (zmq::error_t& e)
63  {
64  if (!s_interrupted)
65  {
66  std::cout << "subscriber.recv() failed with exception: " << e.what() << std::endl;
67  return -1;
68  }
69  }
70 
71  if (!s_interrupted)
72  {
73  char* data_ptr = static_cast<char*>(update.data());
74  const uint32_t header_size = flatbuffers::ReadScalar<uint32_t>(data_ptr);
75 
76  if (first_message)
77  {
78  printf("First message received\n");
79  first_message = false;
80 
81  file_os.open(argv[1], std::ofstream::binary | std::ofstream::out);
82  file_os.write(data_ptr, 4 + header_size);
83  }
84  data_ptr += 4 + header_size;
85 
86  const uint32_t transition_count = flatbuffers::ReadScalar<uint32_t>(data_ptr);
87  data_ptr += sizeof(uint32_t);
88 
89  file_os.write(data_ptr, 12 * transition_count);
90  }
91  }
92 
93  subscriber.close();
94 
95  printf("Results saved to file\n");
96  file_os.close();
97 
98  return 0;
99 }
static void s_signal_handler(int)
Definition: bt_recorder.cpp:12
static void CatchSignals(void)
Definition: bt_recorder.cpp:17
static bool s_interrupted
Definition: bt_recorder.cpp:10
int main(int argc, char *argv[])
Definition: bt_recorder.cpp:27


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32