Skip to content

Commit

Permalink
#2193 Add try/catch block around client creation
Browse files Browse the repository at this point in the history
The try...catch block should catch any RCL-related issues when the
CapabilitiesInterface instance tries to create a ROS 2 service client
but fails.
  • Loading branch information
adamlm committed Nov 16, 2023
1 parent f5c71b9 commit c0fe457
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions arbitrator/include/capabilities_interface.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,34 @@
#include <string>
#include <functional>
#include <carma_planning_msgs/srv/plan_maneuvers.hpp>
#include <rclcpp/exceptions/exceptions.hpp>

namespace arbitrator
namespace arbitrator
{
template<typename MSrvReq, typename MSrvRes>
std::map<std::string, std::shared_ptr<MSrvRes>>
std::map<std::string, std::shared_ptr<MSrvRes>>
CapabilitiesInterface::multiplex_service_call_for_capability(const std::string& query_string, std::shared_ptr<MSrvReq> msg)
{
std::vector<std::string> topics = get_topics_for_capability(query_string);

std::map<std::string, std::shared_ptr<MSrvRes>> responses;

for (auto i = topics.begin(); i != topics.end(); i++)
for (auto i = topics.begin(); i != topics.end(); i++)
{
auto topic = *i;
auto sc = nh_->create_client<carma_planning_msgs::srv::PlanManeuvers>(topic);
RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"), "found client: " << topic);

std::shared_future<std::shared_ptr<MSrvRes>> resp = sc->async_send_request(msg);
try {
auto sc = nh_->create_client<carma_planning_msgs::srv::PlanManeuvers>(topic);
RCLCPP_DEBUG_STREAM(rclcpp::get_logger("arbitrator"), "found client: " << topic);

std::shared_future<std::shared_ptr<MSrvRes>> resp = sc->async_send_request(msg);
} catch(const rclcpp::exceptions::RCLErrorBase& exception) {
RCLCPP_ERROR_STREAM(rclcpp::get_logger("arbitrator"),
"Cannot make service request for service '" << topic << "': " << exception.what());
continue;
}

auto future_status = resp.wait_for(std::chrono::milliseconds(500));

if (future_status == std::future_status::ready) {
responses.emplace(topic, resp.get());
}
Expand Down

0 comments on commit c0fe457

Please sign in to comment.