bt_recorder.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <iostream>
00003 #include <fstream>
00004 #include <signal.h>
00005 #include <zmq.hpp>
00006 #include <fstream>
00007 #include "behaviortree_cpp/loggers/BT_logger_generated.h"
00008 
00009 // http://zguide.zeromq.org/cpp:interrupt
00010 static bool s_interrupted = false;
00011 
00012 static void s_signal_handler(int)
00013 {
00014     s_interrupted = true;
00015 }
00016 
00017 static void CatchSignals(void)
00018 {
00019     struct sigaction action;
00020     action.sa_handler = s_signal_handler;
00021     action.sa_flags = 0;
00022     sigemptyset(&action.sa_mask);
00023     sigaction(SIGINT, &action, NULL);
00024     sigaction(SIGTERM, &action, NULL);
00025 }
00026 
00027 int main(int argc, char* argv[])
00028 {
00029     if (argc != 2)
00030     {
00031         printf("Wrong number of arguments\nUsage: %s [filename]\n", argv[0]);
00032         return 1;
00033     }
00034 
00035     // register CTRL+C signal handler
00036     CatchSignals();
00037 
00038     zmq::context_t context(1);
00039 
00040     //  Socket to talk to server
00041     std::cout << "Trying to connect to [tcp://localhost:1666]\n" << std::endl;
00042 
00043     zmq::socket_t subscriber(context, ZMQ_SUB);
00044     subscriber.connect("tcp://localhost:1666");
00045 
00046     //  Subscribe to everything
00047     subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
00048 
00049     printf("----------- Started -----------------\n");
00050 
00051     bool first_message = true;
00052     std::ofstream file_os;
00053 
00054     while (!s_interrupted)
00055     {
00056         zmq::message_t update;
00057         zmq::message_t msg;
00058         try
00059         {
00060             subscriber.recv(&update);
00061         }
00062         catch (zmq::error_t& e)
00063         {
00064             if (!s_interrupted)
00065             {
00066                 std::cout << "subscriber.recv() failed with exception: " << e.what() << std::endl;
00067                 return -1;
00068             }
00069         }
00070 
00071         if (!s_interrupted)
00072         {
00073             char* data_ptr = static_cast<char*>(update.data());
00074             const uint32_t header_size = flatbuffers::ReadScalar<uint32_t>(data_ptr);
00075 
00076             if (first_message)
00077             {
00078                 printf("First message received\n");
00079                 first_message = false;
00080 
00081                 file_os.open(argv[1], std::ofstream::binary | std::ofstream::out);
00082                 file_os.write(data_ptr, 4 + header_size);
00083             }
00084             data_ptr += 4 + header_size;
00085 
00086             const uint32_t transition_count = flatbuffers::ReadScalar<uint32_t>(data_ptr);
00087             data_ptr += sizeof(uint32_t);
00088 
00089             file_os.write(data_ptr, 12 * transition_count);
00090         }
00091     }
00092 
00093     subscriber.close();
00094 
00095     printf("Results saved to file\n");
00096     file_os.close();
00097 
00098     return 0;
00099 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10