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 <fstream>
6 #include "cppzmq/zmq.hpp"
7 #include "behaviortree_cpp/flatbuffers/BT_logger_generated.h"
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 #ifdef _WIN32
20  signal(SIGINT, s_signal_handler);
21  signal(SIGTERM, s_signal_handler);
22 #else
23  struct sigaction action;
24  action.sa_handler = s_signal_handler;
25  action.sa_flags = 0;
26  sigemptyset(&action.sa_mask);
27  sigaction(SIGINT, &action, nullptr);
28  sigaction(SIGTERM, &action, nullptr);
29 #endif
30 }
31 
32 int main(int argc, char* argv[])
33 {
34  if(argc != 2)
35  {
36  printf("Wrong number of arguments\nUsage: %s [filename]\n", argv[0]);
37  return 1;
38  }
39 
40  // register CTRL+C signal handler
41  CatchSignals();
42 
43  zmq::context_t context(1);
44 
45  // Socket to talk to server
46  std::cout << "Trying to connect to [tcp://localhost:1666]\n" << std::endl;
47 
48  zmq::socket_t subscriber(context, ZMQ_SUB);
49  subscriber.connect("tcp://localhost:1666");
50 
51  // Subscribe to everything
52  subscriber.set(zmq::sockopt::subscribe, "");
53 
54  printf("----------- Started -----------------\n");
55 
56  bool first_message = true;
57  std::ofstream file_os;
58 
59  while(!s_interrupted)
60  {
61  zmq::message_t update;
62  zmq::message_t msg;
63  try
64  {
65  auto ret = subscriber.recv(update, zmq::recv_flags::none);
66  (void)ret;
67  }
68  catch(zmq::error_t& e)
69  {
70  if(!s_interrupted)
71  {
72  std::cout << "subscriber.recv() failed with exception: " << e.what() << std::endl;
73  return -1;
74  }
75  }
76 
77  if(!s_interrupted)
78  {
79  char* data_ptr = static_cast<char*>(update.data());
80  const uint32_t header_size = flatbuffers::ReadScalar<uint32_t>(data_ptr);
81 
82  if(first_message)
83  {
84  printf("First message received\n");
85  first_message = false;
86 
87  file_os.open(argv[1], std::ofstream::binary | std::ofstream::out);
88  file_os.write(data_ptr, 4 + header_size);
89  }
90  data_ptr += 4 + header_size;
91 
92  const uint32_t transition_count = flatbuffers::ReadScalar<uint32_t>(data_ptr);
93  data_ptr += sizeof(uint32_t);
94 
95  file_os.write(data_ptr, 12 * transition_count);
96  }
97  }
98 
99  subscriber.close();
100 
101  printf("Results saved to file\n");
102  file_os.close();
103 
104  return 0;
105 }
zmq::message_t
Definition: 3rdparty/cppzmq/zmq.hpp:408
zmq::socket_t
Definition: 3rdparty/cppzmq/zmq.hpp:2181
zmq::message_t::data
void * data() ZMQ_NOTHROW
Definition: 3rdparty/cppzmq/zmq.hpp:593
s_signal_handler
static void s_signal_handler(int)
Definition: bt_recorder.cpp:12
detail::void
j template void())
Definition: json.hpp:4893
zmq::context_t
Definition: 3rdparty/cppzmq/zmq.hpp:798
zmq::socket_t::close
void close() ZMQ_NOTHROW
Definition: 3rdparty/cppzmq/zmq.hpp:2225
signal.h
zmq.hpp
CatchSignals
static void CatchSignals(void)
Definition: bt_recorder.cpp:17
zmq::detail::socket_base::connect
void connect(std::string const &addr)
Definition: 3rdparty/cppzmq/zmq.hpp:1894
lexyd::binary
_d< 2 > binary
Definition: digit.hpp:54
s_interrupted
static bool s_interrupted
Definition: bt_recorder.cpp:10
zmq::error_t::what
virtual const char * what() const ZMQ_NOTHROW ZMQ_OVERRIDE
Definition: 3rdparty/cppzmq/zmq.hpp:294
main
int main(int argc, char *argv[])
Definition: bt_recorder.cpp:32
zmq::error_t
Definition: 3rdparty/cppzmq/zmq.hpp:289


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16