SMACC
Loading...
Searching...
No Matches
smacc_subscriber_client.h
Go to the documentation of this file.
1/*****************************************************************************************************************
2 * ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
3 * Authors: Pablo Inigo Blasco, Brett Aldrich
4 *
5 ******************************************************************************************************************/
6
7#pragma once
8
10#include <boost/optional/optional_io.hpp>
12
13namespace smacc
14{
15
16namespace client_bases
17{
18
19using namespace smacc::default_events;
20
21template <typename MessageType>
23{
24public:
25 boost::optional<std::string> topicName;
26 boost::optional<int> queueSize;
27
28 typedef MessageType TMessageType;
29
31 {
32 initialized_ = false;
33 }
34
35 SmaccSubscriberClient(std::string topicname)
36 {
37 topicName = topicname;
38 }
39
41 {
42 sub_.shutdown();
43 }
44
45 smacc::SmaccSignal<void(const MessageType &)> onFirstMessageReceived_;
46 smacc::SmaccSignal<void(const MessageType &)> onMessageReceived_;
47
48 std::function<void(const MessageType &)> postMessageEvent;
49 std::function<void(const MessageType &)> postInitialMessageEvent;
50
51 template <typename T>
52 boost::signals2::connection onMessageReceived(void (T::*callback)(const MessageType &), T *object)
53 {
54 return this->getStateMachine()->createSignalConnection(onMessageReceived_, callback, object);
55 }
56
57 template <typename T>
58 boost::signals2::connection onFirstMessageReceived(void (T::*callback)(const MessageType &), T *object)
59 {
60 return this->getStateMachine()->createSignalConnection(onFirstMessageReceived_, callback, object);
61 }
62
63 template <typename TOrthogonal, typename TSourceObject>
65 {
66 this->postMessageEvent = [=](auto msg) {
68 event->msgData = msg;
69 this->postEvent(event);
70 };
71
72 this->postInitialMessageEvent = [=](auto msg) {
74 event->msgData = msg;
75 this->postEvent(event);
76 };
77 }
78
79 virtual void initialize()
80 {
81 if (!initialized_)
82 {
83 firstMessage_ = true;
84
85 if (!queueSize)
86 queueSize = 1;
87
88 if (!topicName)
89 {
90 ROS_ERROR("topic client with no topic name set. Skipping subscribing");
91 }
92 else
93 {
94 ROS_INFO_STREAM("[" << this->getName() << "] Subscribing to topic: " << topicName);
95
97 this->initialized_ = true;
98 }
99 }
100 }
101
102protected:
103 ros::NodeHandle nh_;
104
105private:
106 ros::Subscriber sub_;
109
110 void messageCallback(const MessageType &msg)
111 {
112 if (firstMessage_)
113 {
116 firstMessage_ = false;
117 }
118
120 postMessageEvent(msg);
121 }
122};
123} // namespace client_bases
124} // 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(const MessageType &)> onMessageReceived_
boost::signals2::connection onFirstMessageReceived(void(T::*callback)(const MessageType &), T *object)
boost::signals2::connection onMessageReceived(void(T::*callback)(const MessageType &), T *object)
std::function< void(const MessageType &)> postInitialMessageEvent
std::function< void(const MessageType &)> postMessageEvent
smacc::SmaccSignal< void(const MessageType &)> onFirstMessageReceived_