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
38 void onInitialize() override
39 {
40 if (!initialized_)
41 {
42 if (!serviceName_)
43 {
44 RCLCPP_ERROR(getLogger(), "service client with no service name set. Skipping.");
45 }
46 else
47 {
48 RCLCPP_INFO_STREAM(
49 getLogger(), "[" << this->getName() << "] Client Service: " << *serviceName_);
50 this->initialized_ = true;
51 client_ = getNode()->create_client<ServiceType>(*serviceName_);
52 }
53 }
54 }
55
56 std::shared_ptr<typename ServiceType::Response> call(
57 std::shared_ptr<typename ServiceType::Request> & request)
58 {
59 auto result = client_->async_send_request(request);
60 //rclcpp::spin_until_future_complete(getNode(), result);
61 return result.get();
62 }
63
64protected:
65 //rclcpp::NodeHandle nh_;
66 std::shared_ptr<rclcpp::Client<ServiceType>> client_;
68};
69} // namespace client_bases
70} // 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)