Subscriber.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
23 
24 namespace variant_topic_tools {
25 
26 /*****************************************************************************/
27 /* Constructors and Destructor */
28 /*****************************************************************************/
29 
31 }
32 
34  impl(src.impl) {
35 }
36 
38 }
39 
41  const std::string& topic, size_t queueSize, const SubscriberCallback&
42  callback) :
43  type(type),
44  callback(callback) {
45  subscriber = nodeHandle.subscribe(topic, queueSize,
47 }
48 
50  shutdown();
51 }
52 
53 /*****************************************************************************/
54 /* Operators */
55 /*****************************************************************************/
56 
57 Subscriber::operator ros::Subscriber() const {
58  if (impl)
59  return impl->subscriber;
60  else
61  return ros::Subscriber();
62 }
63 
64 /*****************************************************************************/
65 /* Accessors */
66 /*****************************************************************************/
67 
68 std::string Subscriber::getTopic() const {
69  if (impl)
70  return impl->subscriber.getTopic();
71  else
72  return std::string();
73 }
74 
76  if (impl)
77  return impl->subscriber.getNumPublishers();
78  else
79  return 0;
80 }
81 
83  return subscriber;
84 }
85 
86 /*****************************************************************************/
87 /* Methods */
88 /*****************************************************************************/
89 
91  if (impl)
92  impl->shutdown();
93 }
94 
97 
98  type = MessageType();
99  dataType = DataType();
101 }
102 
104  event) {
105  boost::shared_ptr<const Message> message = event.getConstMessage();
106 
107  if (!type.isValid())
108  type = message->getType();
109 
110  if (message->getType().getDataType() == type.getDataType()) {
111  if ((type.getMD5Sum() == "*") || (message->getType().getMD5Sum() == "*") ||
112  (message->getType().getMD5Sum() == type.getMD5Sum())) {
113  if (!dataType) {
114  DataTypeRegistry registry;
115  dataType = registry.getDataType(type.getDataType());
116 
117  if (!dataType) {
118  type = message->getType();
119 
121  dataType = definition.getMessageDataType();
122  }
123  }
124 
125  if (dataType && !serializer)
127 
128  if (serializer) {
130  ros::serialization::IStream stream(const_cast<uint8_t*>(
131  message->getData().data()), message->getSize());
132 
133  serializer.deserialize(stream, variant);
134 
135  callback(variant, event.getReceiptTime());
136  }
137  }
138  else
140  message->getType().getMD5Sum());
141  }
142  else
144  message->getType().getDataType());
145 }
146 
147 }
ros::Time getReceiptTime() const
Exception thrown in case of a message type mismatch.
Definition: Exceptions.h:158
Exception thrown in case of a message MD5 sum mismatch.
Definition: Exceptions.h:137
Serializer createSerializer() const
Create a serializer for this data type.
Definition: DataType.cpp:161
Variant message subscriber.
Definition: Subscriber.h:36
Header file providing the Message class interface.
Variant message type information.
Definition: MessageType.h:33
void eventCallback(const ros::MessageEvent< Message const > &event)
This subscriber&#39;s event callback.
Definition: Subscriber.cpp:103
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
Impl(ros::NodeHandle &nodeHandle, const MessageType &type, const std::string &topic, size_t queueSize, const SubscriberCallback &callback)
Constructor.
Definition: Subscriber.cpp:40
Header file providing the Subscriber class interface.
void callback(const variant_topic_tools::MessageVariant &variant, const ros::Time &receiptTime)
Definition: echo.cpp:58
ros::Subscriber subscriber
The ROS subscriber.
Definition: Subscriber.h:119
boost::function< void(const MessageVariant &, const ros::Time &)> SubscriberCallback
Definition of the subscriber callback type.
Definition: Forwards.h:120
Header file defining exceptions for the variant topic tools.
Subscriber()
Default constructor.
Definition: Subscriber.cpp:30
const std::string & getDataType() const
Retrieve the data type of the message.
Definition: MessageType.cpp:74
MessageDataType getMessageDataType() const
Retrieve the message data type represented by this message definition.
ros::NodeHandlePtr nodeHandle
Definition: echo.cpp:23
size_t getNumPublishers() const
Retrieve this subscriber&#39;s number of publishers.
Definition: Subscriber.cpp:75
MessageType type
The message type associated with this subscriber.
Definition: Subscriber.h:103
Header file providing the DataTypeRegistry class interface.
MessageDataType dataType
The message data type associated with this subscriber.
Definition: Subscriber.h:107
const std::string & getMD5Sum() const
Retrieve the MD5 sum of the message.
Definition: MessageType.cpp:82
DataType getDataType(const std::string &identifier)
Retrieve a data type from the registry by identifier (non-const version)
void deserialize(ros::serialization::IStream &stream, Variant &value)
Deserialize a variant value from an input stream.
Definition: Serializer.cpp:81
bool isValid() const
True, if this subscriber implementation is valid.
Definition: Subscriber.cpp:82
void shutdown()
Perform shutdown of the subscriber.
Definition: Subscriber.cpp:90
bool isValid() const
True, if this message type is valid.
Definition: MessageType.cpp:94
ImplPtr impl
The subscriber&#39;s implementation.
Definition: Subscriber.h:133
Variant createVariant() const
Create a variant from this data type.
Definition: DataType.cpp:168
void shutdown()
Perform shutdown of the subscriber.
Definition: Subscriber.cpp:95
std::string getTopic() const
Retrieve the topic this subscriber is subscribed to.
Definition: Subscriber.cpp:68
SubscriberCallback callback
The subscriber&#39;s callback.
Definition: Subscriber.h:115
const char * definition()
MessageSerializer serializer
The message serializer associated with this subscriber.
Definition: Subscriber.h:111


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Sat Jan 9 2021 03:56:50