SMACC2
Loading...
Searching...
No Matches
smacc_service_client.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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
37
39
40 void onInitialize() override
41 {
42 if (!initialized_)
43 {
44 if (!serviceName_)
45 {
46 RCLCPP_ERROR(getLogger(), "service client with no service name set. Skipping.");
47 }
48 else
49 {
51 getLogger(), "[" << this->getName() << "] Client Service: " << *serviceName_);
52 this->initialized_ = true;
53 client_ = getNode()->create_client<ServiceType>(*serviceName_);
54 }
55 }
56 }
57
58 std::shared_ptr<typename ServiceType::Response> call(
59 std::shared_ptr<typename ServiceType::Request> & request)
60 {
61 auto result = client_->async_send_request(request);
62 //rclcpp::spin_until_future_complete(getNode(), result);
63 return result.get();
64 }
65
66protected:
67 //rclcpp::NodeHandle nh_;
68 std::shared_ptr<rclcpp::Client<ServiceType>> client_;
70};
71} // namespace client_bases
72} // 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)