SMACC
Loading...
Searching...
No Matches
smacc_service_server_client.h
Go to the documentation of this file.
1#pragma once
2
5#include <std_srvs/Empty.h>
6
7#include <boost/optional/optional_io.hpp>
8
9namespace smacc {
10namespace client_bases {
11template <typename TService>
13 using TServiceRequest = typename TService::Request;
14 using TServiceResponse = typename TService::Response;
15
16 public:
17 boost::optional<std::string> serviceName_;
19 SmaccServiceServerClient(std::string service_name) {
20 serviceName_ = service_name;
21 initialized_ = false;
22 }
23
24 virtual ~SmaccServiceServerClient() { server_.shutdown(); }
25
26 smacc::SmaccSignal<void(TServiceRequest&, std::shared_ptr<TServiceResponse>)>
28
29 template <typename T>
30 boost::signals2::connection onServiceRequestReceived(
31 void (T::*callback)(TServiceRequest&, std::shared_ptr<TServiceResponse>),
32 T* object) {
34 onServiceRequestReceived_, callback, object);
35 }
36
37 virtual void initialize() override {
38 if (!initialized_) {
39 if (!serviceName_) {
40 ROS_ERROR("service server with no service name set. Skipping.");
41 } else {
42 ROS_INFO_STREAM("[" << this->getName()
43 << "] Client Service: " << serviceName_);
44
45 server_ = nh_.advertiseService(
47 this);
48 this->initialized_ = true;
49 }
50 }
51 }
52
53 protected:
54 ros::NodeHandle nh_;
55
56 private:
58 {
59 std::shared_ptr<TServiceResponse> response{new TServiceResponse};
60 onServiceRequestReceived_(req, response);
61 res = *response;
62 return true;
63 }
64 ros::ServiceServer server_;
66};
67} // namespace client_bases
68} // namespace smacc
virtual std::string getName() const
Definition: client.cpp:34
ISmaccStateMachine * getStateMachine()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
smacc::SmaccSignal< void(TServiceRequest &, std::shared_ptr< TServiceResponse >)> onServiceRequestReceived_
boost::signals2::connection onServiceRequestReceived(void(T::*callback)(TServiceRequest &, std::shared_ptr< TServiceResponse >), T *object)
bool serviceCallback(TServiceRequest &req, TServiceResponse &res)