| 32 | } |
| 33 | |
| 34 | int main(int argc, char* argv[]) |
| 35 | { |
| 36 | if(argc != 2) |
| 37 | { |
| 38 | printf("Wrong number of arguments\nUsage: %s [filename]\n", argv[0]); |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | // register CTRL+C signal handler |
| 43 | CatchSignals(); |
| 44 | |
| 45 | zmq::context_t context(1); |
| 46 | |
| 47 | // Socket to talk to server |
| 48 | std::cout << "Trying to connect to [tcp://localhost:1666]\n" << std::endl; |
| 49 | |
| 50 | zmq::socket_t subscriber(context, ZMQ_SUB); |
| 51 | subscriber.connect("tcp://localhost:1666"); |
| 52 | |
| 53 | // Subscribe to everything |
| 54 | subscriber.set(zmq::sockopt::subscribe, ""); |
| 55 | |
| 56 | printf("----------- Started -----------------\n"); |
| 57 | |
| 58 | bool first_message = true; |
| 59 | std::ofstream file_os; |
| 60 | |
| 61 | while(!s_interrupted) |
| 62 | { |
| 63 | zmq::message_t update; |
| 64 | zmq::message_t msg; |
| 65 | try |
| 66 | { |
| 67 | auto ret = subscriber.recv(update, zmq::recv_flags::none); |
| 68 | (void)ret; |
| 69 | } |
| 70 | catch(zmq::error_t& e) |
| 71 | { |
| 72 | if(!s_interrupted) |
| 73 | { |
| 74 | std::cout << "subscriber.recv() failed with exception: " << e.what() << std::endl; |
| 75 | return -1; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if(!s_interrupted) |
| 80 | { |
| 81 | char* data_ptr = static_cast<char*>(update.data()); |
| 82 | const uint32_t header_size = flatbuffers::ReadScalar<uint32_t>(data_ptr); |
| 83 | |
| 84 | if(first_message) |
| 85 | { |
| 86 | printf("First message received\n"); |
| 87 | first_message = false; |
| 88 | |
| 89 | file_os.open(argv[1], std::ofstream::binary | std::ofstream::out); |
| 90 | file_os.write(data_ptr, 4 + header_size); |
| 91 | } |