Publisher.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 
19 #include <ros/console.h>
20 
25 
26 namespace variant_topic_tools {
27 
28 /*****************************************************************************/
29 /* Constructors and Destructor */
30 /*****************************************************************************/
31 
33 }
34 
36  impl(src.impl) {
37 }
38 
40 }
41 
43  type, const std::string& topic, size_t queueSize, bool latch, const
45  type(type),
46  sequenceNumber(0) {
47  DataTypeRegistry registry;
48  DataType dataType = registry.getDataType(type.getDataType());
49 
50  if (!dataType) {
52  dataType = definition.getMessageDataType();
53  }
54 
55  serializer = dataType.createSerializer();
56 
57  ros::AdvertiseOptions options(topic, queueSize, type.getMD5Sum(),
58  type.getDataType(), type.getDefinition(), connectCallback);
59  options.latch = latch;
60 
61  publisher = nodeHandle.advertise(options);
62 }
63 
65  shutdown();
66 }
67 
68 /*****************************************************************************/
69 /* Operators */
70 /*****************************************************************************/
71 
72 Publisher::operator ros::Publisher() const {
73  if (impl)
74  return impl->publisher;
75  else
76  return ros::Publisher();
77 }
78 
79 /*****************************************************************************/
80 /* Accessors */
81 /*****************************************************************************/
82 
83 std::string Publisher::getTopic() const {
84  if (impl)
85  return impl->publisher.getTopic();
86  else
87  return std::string();
88 }
89 
91  if (impl)
92  return impl->publisher.getNumSubscribers();
93  else
94  return 0;
95 }
96 
97 bool Publisher::isLatched() const {
98  if (impl)
99  return impl->publisher.isLatched();
100  else
101  return false;
102 }
103 
105  return type.isValid() && serializer && publisher;
106 }
107 
108 /*****************************************************************************/
109 /* Methods */
110 /*****************************************************************************/
111 
113  if (impl)
114  impl->shutdown();
115 }
116 
117 void Publisher::publish(const MessageVariant& variant) {
118  if (impl && impl->isValid())
119  impl->publish(variant);
120  else
121  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher");
122 }
123 
125  type = MessageType();
127  sequenceNumber = 0;
129 }
130 
132  if (variant.getType().getIdentifier() == type.getDataType()) {
133  Message message;
134  MessageDataType dataType = variant.getType();
135 
136  if (dataType.hasHeader())
137  variant["header/seq"] = sequenceNumber+1;
138 
139  message.setType(type);
140  message.setSize(serializer.getSerializedLength(variant));
141  ros::serialization::OStream stream(const_cast<uint8_t*>(
142  message.getData().data()), message.getSize());
143 
144  serializer.serialize(stream, variant);
145  publisher.publish(message);
146 
147  ++sequenceNumber;
148  }
149  else
151  variant.getType().getIdentifier());
152 }
153 
154 }
void shutdown()
Perform shutdown of the publisher.
Definition: Publisher.cpp:124
const DataType & getType() const
Retrieve the data type of this variant.
Definition: Variant.cpp:52
Generic message type.
Definition: Message.h:43
void publish(const MessageVariant &variant)
Publish a message variant on the topic associated with this publisher.
Definition: Publisher.cpp:117
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
Variant message publisher.
Definition: Publisher.h:36
Exception thrown in case of a message type mismatch.
Definition: Exceptions.h:158
Serializer createSerializer() const
Create a serializer for this data type.
Definition: DataType.cpp:161
void publish(const boost::shared_ptr< M > &message) const
Header file providing the Message class interface.
uint32_t sequenceNumber
The sequence number associated with this publisher.
Definition: Publisher.h:122
Variant message type information.
Definition: MessageType.h:33
MessageType type
The message type associated with this publisher.
Definition: Publisher.h:114
size_t getSize() const
Retrieve the message size.
Definition: Message.cpp:87
Header file providing the Publisher class interface.
const std::string & getDefinition() const
Retrieve the message definition.
Definition: MessageType.cpp:90
bool isLatched() const
True, if the topic this publisher publishes on is latched.
Definition: Publisher.cpp:97
void serialize(ros::serialization::OStream &stream, const Variant &value)
Serialize a variant value to an output stream.
Definition: Serializer.cpp:73
void connectCallback(const ros::SingleSubscriberPublisher &)
Definition: relay.cpp:65
const std::string & getIdentifier() const
Retrieve the identifier representing this data type.
Definition: DataType.cpp:75
void setSize(size_t size)
Set the message size.
Definition: Message.cpp:83
size_t getNumSubscribers() const
Retrieve this publisher&#39;s number of subscribers.
Definition: Publisher.cpp:90
Header file defining exceptions for the variant topic tools.
size_t getSerializedLength(const Variant &value) const
Retrieve the serialized length of a variant value.
Definition: Serializer.cpp:54
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.
Publisher()
Default constructor.
Definition: Publisher.cpp:32
ros::NodeHandlePtr nodeHandle
Definition: echo.cpp:23
Header file providing the DataTypeRegistry class interface.
#define ROS_ASSERT_MSG(cond,...)
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)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void setType(const MessageType &type)
Set the message type.
Definition: Message.cpp:59
bool isValid() const
True, if this publisher implementation is valid.
Definition: Publisher.cpp:104
void publish(const MessageVariant &variant)
Publish a message variant on the topic associated with this publisher.
Definition: Publisher.cpp:131
bool isValid() const
True, if this message type is valid.
Definition: MessageType.cpp:94
ros::Publisher publisher
The ROS publisher.
Definition: Publisher.h:126
ImplPtr impl
The publisher&#39;s implementation.
Definition: Publisher.h:140
std::string getTopic() const
Retrieve the topic this publisher publishes on.
Definition: Publisher.cpp:83
void shutdown()
Perform shutdown of the publisher.
Definition: Publisher.cpp:112
MessageSerializer serializer
The message serializer associated with this publisher.
Definition: Publisher.h:118
bool hasHeader() const
True, if this message data type has a header.
const char * definition()
std::vector< uint8_t > & getData()
Retrieve the message data (non-const version)
Definition: Message.cpp:75
Impl(ros::NodeHandle &nodeHandle, const MessageType &type, const std::string &topic, size_t queueSize, bool latch=false, const ros::SubscriberStatusCallback &connectCallback=ros::SubscriberStatusCallback())
Constructor.
Definition: Publisher.cpp:42


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