Repository navigation
refactor(gateway): SSE fault stream reads a neutral event source #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/faults/fault_event_source.hpp
This file contains hidden or 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,44 @@ | ||
| // Copyright 2026 bburda | ||
| // | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <functional> | ||
|
|
||
| #include "ros2_medkit_fault_detection/fault_types.hpp" | ||
|
|
||
| namespace ros2_medkit_gateway { | ||
|
|
||
| /// Push side of the fault stream: delivers each fault event the fault manager | ||
| /// publishes. Implementations must be thread-safe. | ||
| class FaultEventSource { | ||
| public: | ||
| using Callback = std::function<void(const ros2_medkit::fault_detection::FaultEventRecord &)>; | ||
|
|
||
| FaultEventSource() = default; | ||
| virtual ~FaultEventSource() = default; | ||
| FaultEventSource(const FaultEventSource &) = delete; | ||
| FaultEventSource & operator=(const FaultEventSource &) = delete; | ||
| FaultEventSource(FaultEventSource &&) = delete; | ||
| FaultEventSource & operator=(FaultEventSource &&) = delete; | ||
|
|
||
| /// Start delivering events to `callback`; replaces any earlier callback. | ||
| /// The callback may run on any thread. | ||
| virtual void subscribe(Callback callback) = 0; | ||
|
|
||
| /// Stop delivery. Returns only when no callback is running any more. | ||
| virtual void unsubscribe() = 0; | ||
| }; | ||
|
|
||
| } // namespace ros2_medkit_gateway | ||
27 changes: 27 additions & 0 deletions
27
src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/faults/fault_json.hpp
This file contains hidden or 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,27 @@ | ||
| // Copyright 2026 bburda | ||
| // | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "ros2_medkit_fault_detection/fault_types.hpp" | ||
|
|
||
| namespace ros2_medkit_gateway { | ||
|
|
||
| /// Fault JSON object served by /faults and the fault stream. Times are seconds | ||
| /// as doubles; ``last_passed`` is present only when set. | ||
| nlohmann::json fault_record_to_json(const ros2_medkit::fault_detection::FaultRecord & fault); | ||
|
|
||
| } // namespace ros2_medkit_gateway |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
53 changes: 53 additions & 0 deletions
53
src/ros2_medkit_gateway/include/ros2_medkit_gateway/ros2/ros2_fault_event_source.hpp
This file contains hidden or 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,53 @@ | ||
| // Copyright 2026 bburda | ||
| // | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <mutex> | ||
| #include <string> | ||
|
|
||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "ros2_medkit_gateway/core/faults/fault_event_source.hpp" | ||
| #include "ros2_medkit_msgs/msg/fault_event.hpp" | ||
|
|
||
| namespace ros2_medkit_gateway { | ||
|
|
||
| /// Fault events from the fault manager's events topic. | ||
| class Ros2FaultEventSource final : public FaultEventSource { | ||
| public: | ||
| /// @param node Node the subscription lives on; must outlive this object. | ||
| /// @param topic Fully qualified events topic. | ||
| Ros2FaultEventSource(rclcpp::Node & node, std::string topic); | ||
| ~Ros2FaultEventSource() override; | ||
| Ros2FaultEventSource(const Ros2FaultEventSource &) = delete; | ||
| Ros2FaultEventSource & operator=(const Ros2FaultEventSource &) = delete; | ||
| Ros2FaultEventSource(Ros2FaultEventSource &&) = delete; | ||
| Ros2FaultEventSource & operator=(Ros2FaultEventSource &&) = delete; | ||
|
|
||
| void subscribe(Callback callback) override; | ||
| void unsubscribe() override; | ||
|
|
||
| private: | ||
| void on_message(const ros2_medkit_msgs::msg::FaultEvent & msg); | ||
|
|
||
| rclcpp::Node & node_; | ||
| std::string topic_; | ||
| /// Held while a callback runs, so unsubscribe() waits for it. | ||
| std::mutex callback_mutex_; | ||
| Callback callback_; | ||
| rclcpp::Subscription<ros2_medkit_msgs::msg::FaultEvent>::SharedPtr subscription_; | ||
| }; | ||
|
|
||
| } // namespace ros2_medkit_gateway |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subscribe()replaces the single callback andunsubscribe()clears it for everyone. A secondSSEFaultHandleron the same services takes the stream from the first one, and when either is destroyed the other stops getting events. Before this PR each handler had its own subscription. The test fixture already hits this:GatewayNode's own handler, thenhandler_, thenlimited_handler/fast_handler. Havesubscribe()return an owned handle that removes only its own callback, and add a test where two handlers both receive an event and one survives the other's destruction.