SMACC2
Loading...
Searching...
No Matches
smacc_service_client.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
23#include <optional>
25
26namespace smacc2
27{
28namespace client_bases
29{
30template <typename ServiceType>
32{
33public:
34 std::optional<std::string> serviceName_;
35
36 SmaccServiceClient(std::string serviceName) : serviceName_(std::move(serviceName))
37 {
38 initialized_ = false;
39 }
40
42
43 void onInitialize() override
44 {
45 if (!initialized_)
46 {
47 if (!serviceName_)
48 {
49 RCLCPP_ERROR(getLogger(), "service client with no service name set. Skipping.");
50 }
51 else
52 {
53 RCLCPP_INFO_STREAM(
54 getLogger(), "[" << this->getName() << "] Client Service: " << *serviceName_);
55 this->initialized_ = true;
56 client_ = getNode()->template create_client<ServiceType>(*serviceName_);
57 }
58 }
59 }
60
61 std::shared_ptr<typename ServiceType::Response> call(
62 std::shared_ptr<typename ServiceType::Request> & request)
63 {
64 auto result = client_->async_send_request(request);
65 //rclcpp::spin_until_future_complete(getNode(), result);
66 return result.get();
67 }
68
69protected:
70 //rclcpp::NodeHandle nh_;
71 std::shared_ptr<rclcpp::Client<ServiceType>> client_;
73};
74} // namespace client_bases
75} // namespace smacc2
rclcpp::Node::SharedPtr getNode()
Definition client.cpp:60
virtual std::string getName() const
Definition client.cpp:42
rclcpp::Logger getLogger()
std::shared_ptr< rclcpp::Client< ServiceType > > client_
std::shared_ptr< typename ServiceType::Response > call(std::shared_ptr< typename ServiceType::Request > &request)