-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notification of significant events during bag recording and playback (…
…#908) (#1037) * Simple hacky prototype Signed-off-by: Geoffrey Biggs <[email protected]> * Improved prototype with more flexibility and event information Signed-off-by: Geoffrey Biggs <[email protected]> * Use the full relative file path for event info Signed-off-by: Geoffrey Biggs <[email protected]> * Remove no-longer required dependency Signed-off-by: Geoffrey Biggs <[email protected]> * Remove left-over merge Signed-off-by: Geoffrey Biggs <[email protected]> * Change event names Signed-off-by: Geoffrey Biggs <[email protected]> * Add overrides to mocks Signed-off-by: Geoffrey Biggs <[email protected]> * Add missing include Signed-off-by: Geoffrey Biggs <[email protected]> * Remove empty file Signed-off-by: Geoffrey Biggs <[email protected]> * Remove unnecessary virtual Signed-off-by: Geoffrey Biggs <[email protected]> * Correct formatting Signed-off-by: Geoffrey Biggs <[email protected]> * Add missing doc Signed-off-by: Geoffrey Biggs <[email protected]> * Reformat documentation Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Change read event topic name Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Change read event topic name Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Simplify for loop Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Remove unnecessary blank line Signed-off-by: Geoffrey Biggs <[email protected]> * Change argument type to const Signed-off-by: Geoffrey Biggs <[email protected]> * Add events handling to mocks Signed-off-by: Geoffrey Biggs <[email protected]> * Add comment explaining 5-message split Signed-off-by: Geoffrey Biggs <[email protected]> * Add integration tests for player and recorder Signed-off-by: Geoffrey Biggs <[email protected]> * Add ability to check if an event has callbacks Signed-off-by: Geoffrey Biggs <[email protected]> * Shift event topic publishing to a separate thread Signed-off-by: Geoffrey Biggs <[email protected]> * Fix formatting Signed-off-by: Geoffrey Biggs <[email protected]> * Test SequentialReader event callbacks Signed-off-by: Geoffrey Biggs <[email protected]> * Test SequentialWriter event callbacks Signed-off-by: Geoffrey Biggs <[email protected]> * Document bag events Signed-off-by: Geoffrey Biggs <[email protected]> * Check if joinable before joining Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Correct spelling mistake Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Correct spelling mistake Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Reduce time wasted waiting for test to reach intended behaviour Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Address test flakiness Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Improve size check Co-authored-by: Michael Orlov <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Use constant for split size in tests Signed-off-by: Geoffrey Biggs <[email protected]> * Add missing header Signed-off-by: Geoffrey Biggs <[email protected]> * Allocate thread on the stack Signed-off-by: Geoffrey Biggs <[email protected]> * Add getters for number of messages per 'file' Signed-off-by: Geoffrey Biggs <[email protected]> * Replace magic number Signed-off-by: Geoffrey Biggs <[email protected]> * Address unstable OSX build Signed-off-by: Geoffrey Biggs <[email protected]> * Make path in test cross-platform Co-authored-by: Emerson Knapp <[email protected]> Signed-off-by: Geoffrey Biggs <[email protected]> * Add missing virtual destructor Signed-off-by: Geoffrey Biggs <[email protected]> * Follow uncrustify suggestion Signed-off-by: Geoffrey Biggs <[email protected]> * Fix rebase errors Signed-off-by: Geoffrey Biggs <[email protected]> * Remove double include Signed-off-by: Geoffrey Biggs <[email protected]> Co-authored-by: Michael Orlov <[email protected]> Co-authored-by: Emerson Knapp <[email protected]> Co-authored-by: Michael Orlov <[email protected]> Co-authored-by: Emerson Knapp <[email protected]>
- Loading branch information
1 parent
193caee
commit f3a3454
Showing
25 changed files
with
668 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// Copyright 2021 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef ROSBAG2_CPP__BAG_EVENTS_HPP_ | ||
#define ROSBAG2_CPP__BAG_EVENTS_HPP_ | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "rclcpp/function_traits.hpp" | ||
#include "rosbag2_cpp/visibility_control.hpp" | ||
|
||
namespace rosbag2_cpp | ||
{ | ||
|
||
namespace bag_events | ||
{ | ||
|
||
/** | ||
* \brief The types of bag events available for registering callbacks. | ||
*/ | ||
enum class BagEvent | ||
{ | ||
/// The output bag file has been split, starting a new file. | ||
WRITE_SPLIT, | ||
/// Reading of the input bag file has gone over a split, opening the next file. | ||
READ_SPLIT, | ||
}; | ||
|
||
/** | ||
* \brief The information structure passed to callbacks for the WRITE_SPLIT and READ_SPLIT events. | ||
*/ | ||
struct BagSplitInfo | ||
{ | ||
/// The URI of the file that was closed. | ||
std::string closed_file; | ||
/// The URI of the file that was opened. | ||
std::string opened_file; | ||
}; | ||
|
||
using BagSplitCallbackType = std::function<void (BagSplitInfo &)>; | ||
|
||
/** | ||
* \brief Use this structure to register callbacks with Writers. | ||
*/ | ||
struct WriterEventCallbacks | ||
{ | ||
/// The callback to call for the WRITE_SPLIT event. | ||
BagSplitCallbackType write_split_callback; | ||
}; | ||
|
||
/** | ||
* \brief Use this structure to register callbacks with Readers. | ||
*/ | ||
struct ReaderEventCallbacks | ||
{ | ||
/// The callback to call for the READ_SPLIT event. | ||
BagSplitCallbackType read_split_callback; | ||
}; | ||
|
||
/** | ||
* \brief Base class for event callbacks. | ||
* | ||
* This class should not be used directly. | ||
*/ | ||
class BagEventCallbackBase | ||
{ | ||
public: | ||
using SharedPtr = std::shared_ptr<BagEventCallbackBase>; | ||
using InfoPtr = std::shared_ptr<void>; | ||
|
||
virtual ~BagEventCallbackBase() | ||
{} | ||
|
||
virtual void execute(InfoPtr & info) = 0; | ||
|
||
virtual bool is_type(BagEvent event) const = 0; | ||
}; | ||
|
||
/** | ||
* \brief Templated class for storing an event callback. | ||
* | ||
* This class should not be used directly by users. | ||
*/ | ||
template<typename EventCallbackT> | ||
class BagEventCallback : public BagEventCallbackBase | ||
{ | ||
public: | ||
BagEventCallback(const EventCallbackT & callback, BagEvent event) | ||
: callback_(callback), | ||
event_(event) | ||
{} | ||
|
||
virtual ~BagEventCallback() | ||
{} | ||
|
||
void execute(InfoPtr & info) override | ||
{ | ||
callback_(*std::static_pointer_cast<EventCallbackInfoT>(info)); | ||
} | ||
|
||
bool is_type(BagEvent event) const override | ||
{ | ||
return event == event_; | ||
} | ||
|
||
private: | ||
using EventCallbackInfoT = typename std::remove_reference<typename | ||
rclcpp::function_traits::function_traits<EventCallbackT>::template argument_type<0>>::type; | ||
|
||
EventCallbackT callback_; | ||
BagEvent event_; | ||
}; | ||
|
||
/** | ||
* \brief The class used to manage event callbacks registered with a Writer or Reader. | ||
* | ||
* Each implementation of the Writer and Reader interfaces should store one instance of this type. | ||
* When new callbacks are registered, they should be passed to that instance using \ref | ||
* add_event_callback. When an event occurs, the callbacks registered for it can be called by | ||
* calling the \ref execute_callbacks method, passing in the event information. | ||
*/ | ||
class EventCallbackManager | ||
{ | ||
public: | ||
/** | ||
* \brief Add an event callback. | ||
* | ||
* \param callback The callback that should be called for the event. | ||
* \param event The event, one of the values of the \ref BagEvent enumeration. | ||
*/ | ||
template<typename EventCallbackT> | ||
void add_event_callback(const EventCallbackT & callback, const BagEvent event) | ||
{ | ||
auto cb = std::make_shared<BagEventCallback<EventCallbackT>>(callback, event); | ||
callbacks_.push_back(cb); | ||
} | ||
|
||
/** | ||
* \brief Check if a callback is registered for the given event. | ||
* | ||
* \param event The event, one of the values of the \ref BagEvent enumeration. | ||
* \return True if a callback is registered for the event, false otherwise. | ||
*/ | ||
bool has_callback_for_event(const BagEvent event) const | ||
{ | ||
for (auto & cb : callbacks_) { | ||
if (cb->is_type(event)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* \brief Execute all callbacks registered for the given event. | ||
* | ||
* The provided information value is passed to each callback by copy. | ||
* | ||
* \param event The event, one of the values of the \ref BagEvent enumeration. | ||
* \param info The information relevant to the event that has occurred. The type varies by event. | ||
*/ | ||
void execute_callbacks(const BagEvent event, BagEventCallbackBase::InfoPtr info) | ||
{ | ||
for (auto & cb : callbacks_) { | ||
if (cb->is_type(event)) { | ||
cb->execute(info); | ||
} | ||
} | ||
} | ||
|
||
private: | ||
std::vector<BagEventCallbackBase::SharedPtr> callbacks_; | ||
}; | ||
|
||
} // namespace bag_events | ||
|
||
} // namespace rosbag2_cpp | ||
|
||
#endif // ROSBAG2_CPP__BAG_EVENTS_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.