18 #ifndef IGN_TRANSPORT_SUBSCRIPTIONHANDLER_HH_
19 #define IGN_TRANSPORT_SUBSCRIPTIONHANDLER_HH_
22 #pragma warning(push, 0)
24 #include <google/protobuf/message.h>
34 #include <ignition/msgs/Factory.hh>
56 : hUuid(
Uuid().ToString()),
58 lastCbTimestamp(std::chrono::seconds{0}),
62 if (this->opts.Throttled())
63 this->periodNs = 1e9 / this->opts.MsgsPerSec();
75 public:
virtual bool RunLocalCallback(
const ProtoMsg &_msg,
82 public:
virtual const std::shared_ptr<ProtoMsg> CreateMsg(
83 const std::string &_data,
84 const std::string &_type)
const = 0;
89 public:
virtual std::string TypeName() = 0;
110 if (!this->opts.Throttled())
113 Timestamp now = std::chrono::steady_clock::now();
116 auto elapsed = now - this->lastCbTimestamp;
118 if (std::chrono::duration_cast<std::chrono::nanoseconds>(
119 elapsed).count() < this->periodNs)
125 this->lastCbTimestamp = now;
143 private: std::string nUuid;
162 const std::string &_data,
163 const std::string &)
const
166 auto msgPtr = std::make_shared<T>();
169 if (!msgPtr->ParseFromString(_data))
171 std::cerr <<
"SubscriptionHandler::CreateMsg() error: ParseFromString"
172 <<
" failed" << std::endl;
181 return T().GetTypeName();
198 std::cerr <<
"SubscriptionHandler::RunLocalCallback() error: "
199 <<
"Callback is NULL" << std::endl;
207 #if GOOGLE_PROTOBUF_VERSION > 2999999
208 auto msgPtr = google::protobuf::down_cast<
const T*>(&_msg);
210 auto msgPtr = google::protobuf::internal::down_cast<
const T*>(&_msg);
213 this->cb(*msgPtr, _info);
235 const std::string &_data,
236 const std::string &_type)
const
238 std::shared_ptr<google::protobuf::Message> msgPtr;
240 const google::protobuf::Descriptor *desc =
241 google::protobuf::DescriptorPool::generated_pool()
242 ->FindMessageTypeByName(_type);
248 msgPtr.reset(google::protobuf::MessageFactory::generated_factory()
249 ->GetPrototype(desc)->New());
254 msgPtr = ignition::msgs::Factory::New(_type);
261 if (!msgPtr->ParseFromString(_data))
263 std::cerr <<
"CreateMsg() error: ParseFromString failed" << std::endl;
267 return std::move(msgPtr);
290 std::cerr <<
"SubscriptionHandler::RunLocalCallback() "
291 <<
"error: Callback is NULL" << std::endl;
299 this->cb(_msg, _info);
std::string NodeUuid() const
Get the node UUID.
Definition: SubscriptionHandler.hh:93
bool UpdateThrottling()
Check if message subscription is throttled.
Definition: SubscriptionHandler.hh:108
std::string HandlerUuid() const
Get the unique UUID of this handler.
Definition: SubscriptionHandler.hh:100
bool RunLocalCallback(const ProtoMsg &_msg, const MessageInfo &_info)
Executes the local callback registered for this handler.
Definition: SubscriptionHandler.hh:284
Timestamp lastCbTimestamp
Timestamp of the last callback executed.
Definition: SubscriptionHandler.hh:136
SubscriptionHandler(const std::string &_nUuid, const SubscribeOptions &_opts=SubscribeOptions())
Definition: SubscriptionHandler.hh:227
ISubscriptionHandler(const std::string &_nUuid, const SubscribeOptions &_opts=SubscribeOptions())
Constructor.
Definition: SubscriptionHandler.hh:54
void SetCallback(const MsgCallback< T > &_cb)
Set the callback for this handler.
Definition: SubscriptionHandler.hh:186
ignition/transport/SubscribeOptions.hh
Definition: SubscribeOptions.hh:35
double periodNs
If throttling is enabled, the minimum period for receiving a message in nanoseconds.
Definition: SubscriptionHandler.hh:140
std::function< void(const T &_msg, const MessageInfo &_info)> MsgCallback
Definition: TransportTypes.hh:125
SubscriptionHandler(const std::string &_nUuid, const SubscribeOptions &_opts=SubscribeOptions())
Definition: SubscriptionHandler.hh:154
SubscribeOptions opts
Subscribe options.
Definition: SubscriptionHandler.hh:133
std::string hUuid
Unique handler's UUID.
Definition: SubscriptionHandler.hh:130
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:41
A class that provides information about the message received.
Definition: MessageInfo.hh:33
google::protobuf::Message ProtoMsg
Definition: TransportTypes.hh:65
std::string TypeName()
Get the type of the messages from which this subscriber handler is subscribed.
Definition: SubscriptionHandler.hh:179
const std::shared_ptr< ProtoMsg > CreateMsg(const std::string &_data, const std::string &) const
Create a specific protobuf message given its serialized data.
Definition: SubscriptionHandler.hh:161
#define IGNITION_TRANSPORT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:57
const std::shared_ptr< ProtoMsg > CreateMsg(const std::string &_data, const std::string &_type) const
Create a specific protobuf message given its serialized data.
Definition: SubscriptionHandler.hh:234
It creates a subscription handler for a specific protobuf message.
Definition: SubscriptionHandler.hh:150
std::string TypeName()
Get the type of the messages from which this subscriber handler is subscribed.
Definition: SubscriptionHandler.hh:271
std::chrono::steady_clock::time_point Timestamp
Definition: TransportTypes.hh:129
const std::string kGenericMessageType
The string type used for generic messages.
Definition: TransportTypes.hh:132
bool RunLocalCallback(const ProtoMsg &_msg, const MessageInfo &_info)
Executes the local callback registered for this handler.
Definition: SubscriptionHandler.hh:192
ignition/transport/SubscriptionHandler.hh
Definition: SubscriptionHandler.hh:49
virtual ~ISubscriptionHandler()
Destructor.
Definition: SubscriptionHandler.hh:67
void SetCallback(const MsgCallback< ProtoMsg > &_cb)
Set the callback for this handler.
Definition: SubscriptionHandler.hh:278