SMACC2
Loading...
Searching...
No Matches
smacc_service_server_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>
26
27namespace smacc2
28{
29namespace client_bases
30{
31template <typename TService>
33{
34 using TServiceRequest = typename TService::Request;
35 using TServiceResponse = typename TService::Response;
36
37public:
38 std::optional<std::string> serviceName_;
40 SmaccServiceServerClient(std::string service_name) : serviceName_(std::move(service_name))
41 {
42 initialized_ = false;
43 }
44
46
48 const std::shared_ptr<typename TService::Request>,
49 std::shared_ptr<typename TService::Response>)>
51
52 template <typename T>
54 void (T::*callback)(
55 const std::shared_ptr<typename TService::Request>,
56 std::shared_ptr<typename TService::Response>),
57 T * object)
58 {
60 onServiceRequestReceived_, callback, object);
61 }
62
63 void onInitialize() override
64 {
65 if (!initialized_)
66 {
67 if (!serviceName_)
68 {
69 RCLCPP_ERROR_STREAM(
70 getLogger(),
71 "[" << this->getName() << "] service server with no service name set. Skipping.");
72 }
73 else
74 {
75 RCLCPP_INFO_STREAM(
76 getLogger(), "[" << this->getName() << "] Client Service: " << *serviceName_);
77
78 server_ = getNode()->template create_service<TService>(
79 *serviceName_, std::bind(
81 std::placeholders::_1, std::placeholders::_2));
82
83 this->initialized_ = true;
84 }
85 }
86 }
87
88private:
90 const std::shared_ptr<typename TService::Request> req,
91 std::shared_ptr<typename TService::Response> res)
92 {
94 }
95 typename rclcpp::Service<TService>::SharedPtr server_;
97};
98} // namespace client_bases
99} // namespace smacc2
rclcpp::Node::SharedPtr getNode()
Definition client.cpp:60
ISmaccStateMachine * getStateMachine()
virtual std::string getName() const
Definition client.cpp:42
rclcpp::Logger getLogger()
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
void serviceCallback(const std::shared_ptr< typename TService::Request > req, std::shared_ptr< typename TService::Response > res)
smacc2::SmaccSignal< void(const std::shared_ptr< typename TService::Request >, std::shared_ptr< typename TService::Response >)> onServiceRequestReceived_
smacc2::SmaccSignalConnection onServiceRequestReceived(void(T::*callback)(const std::shared_ptr< typename TService::Request >, std::shared_ptr< typename TService::Response >), T *object)
boost::signals2::connection SmaccSignalConnection