rosbag_recorder.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #pragma once
16 
17 #include <chrono>
18 #include <functional>
19 #include <future>
20 #include <mutex>
21 #include <thread>
22 
23 #include <aws/core/utils/logging/LogMacros.h>
24 
26 
27 namespace Aws
28 {
29 namespace Rosbag
30 {
31 namespace Utils
32 {
33 
35 {
36  // Started the rosbag recorder successfully
37  STARTED = 0,
38  // Rosbag recorder not run as it may be running already
40 };
41 
42 /*
43  * Wrapper class around robag::Recorder that allows for executing callback
44  * functions before and after collecting rosbag files for the specified
45  * duration.
46  *
47  * Utils::Recorder has non-virtual members that it impractical to extend and
48  * therefore hard to mock. RosbagRecorder is therefore wrapping Utils::recorder
49  * and is also templatized to allow for injection based mocking.
50  */
51 template<typename T>
53 {
54 public:
55  explicit RosbagRecorder()
56  {
57  std::promise<void> p;
58  barrier_ = p.get_future();
59  };
60 
61  virtual ~RosbagRecorder() = default;
62 
64  const RecorderOptions & recorder_options,
65  const std::function<void()> & pre_record = nullptr,
66  const std::function<void(int)> & post_record = nullptr
67  )
68  {
69  {
70  std::lock_guard<std::mutex> lock(mutex_);
71  if (IsActive()) {
72  AWS_LOG_INFO(__func__, "Failed to run RosbagRecorder, recorder already active");
74  }
75  AWS_LOG_INFO(__func__, "Starting a new RosbagRecorder session");
76  static auto function_name = __func__;
77  barrier_ = std::async(std::launch::async, [recorder_options, pre_record, post_record]
78  {
79  if (pre_record) {
80  pre_record();
81  }
82  int exit_code;
83  {
84  T rosbag_recorder(recorder_options);
85  exit_code = rosbag_recorder.Run();
86  }
87  if (exit_code != 0) {
88  AWS_LOGSTREAM_ERROR(function_name, "RosbagRecorder encountered an error (code: " << exit_code << ')');
89  }
90  if (post_record) {
91  post_record(exit_code);
92  }
93  }
94  );
96  }
97  }
98 
99  virtual bool IsActive() const
100  {
101  using namespace std::chrono_literals;
102  auto status = barrier_.wait_for(0ms);
103  return std::future_status::ready != status;
104  }
105 
106 private:
107  mutable std::mutex mutex_;
108  std::future<void> barrier_;
109 };
110 
111 } // namespace Utils
112 } // namespace Rosbag
113 } // namespace Aws
virtual RosbagRecorderRunResult Run(const RecorderOptions &recorder_options, const std::function< void()> &pre_record=nullptr, const std::function< void(int)> &post_record=nullptr)


rosbag_cloud_recorders
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:27