publisher.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "ros/publisher.h"
29 #include "ros/publication.h"
30 #include "ros/node_handle.h"
31 #include "ros/topic_manager.h"
32 
33 namespace ros
34 {
35 
36 Publisher::Impl::Impl() : unadvertised_(false) { }
37 
39 {
40  ROS_DEBUG("Publisher on '%s' deregistering callbacks.", topic_.c_str());
41  unadvertise();
42 }
43 
45 {
46  return !unadvertised_;
47 }
48 
50 {
51  if (!unadvertised_)
52  {
53  unadvertised_ = true;
54  TopicManager::instance()->unadvertise(topic_, callbacks_);
55  node_handle_.reset();
56  }
57 }
58 
59 Publisher::Publisher(const std::string& topic, const std::string& md5sum, const std::string& datatype, const NodeHandle& node_handle, const SubscriberCallbacksPtr& callbacks)
60 : impl_(boost::make_shared<Impl>())
61 {
62  impl_->topic_ = topic;
63  impl_->md5sum_ = md5sum;
64  impl_->datatype_ = datatype;
65  impl_->node_handle_ = boost::make_shared<NodeHandle>(node_handle);
66  impl_->callbacks_ = callbacks;
67 }
68 
70 {
71  impl_ = rhs.impl_;
72 }
73 
75 {
76 }
77 
78 void Publisher::publish(const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m) const
79 {
80  if (!impl_)
81  {
82  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
83  return;
84  }
85 
86  if (!impl_->isValid())
87  {
88  ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
89  return;
90  }
91 
92  TopicManager::instance()->publish(impl_->topic_, serfunc, m);
93 }
94 
96 {
97  if (impl_ && impl_->isValid())
98  {
99  TopicManager::instance()->incrementSequence(impl_->topic_);
100  }
101 }
102 
104 {
105  if (impl_)
106  {
107  impl_->unadvertise();
108  impl_.reset();
109  }
110 }
111 
112 std::string Publisher::getTopic() const
113 {
114  if (impl_)
115  {
116  return impl_->topic_;
117  }
118 
119  return std::string();
120 }
121 
123 {
124  if (impl_ && impl_->isValid())
125  {
126  return TopicManager::instance()->getNumSubscribers(impl_->topic_);
127  }
128 
129  return 0;
130 }
131 
132 bool Publisher::isLatched() const {
133  PublicationPtr publication_ptr;
134  if (impl_ && impl_->isValid()) {
135  publication_ptr =
136  TopicManager::instance()->lookupPublication(impl_->topic_);
137  } else {
138  ROS_ASSERT_MSG(false, "Call to isLatched() on an invalid Publisher");
139  throw ros::Exception("Call to isLatched() on an invalid Publisher");
140  }
141  if (publication_ptr) {
142  return publication_ptr->isLatched();
143  } else {
144  ROS_ASSERT_MSG(false, "Call to isLatched() on an invalid Publisher");
145  throw ros::Exception("Call to isLatched() on an invalid Publisher");
146  }
147 }
148 
149 } // namespace ros
void publish(const boost::shared_ptr< M > &message) const
Publish a message on the topic associated with this Publisher.
Definition: publisher.h:63
NodeHandlePtr node_handle_
Definition: publisher.h:186
std::string topic_
Definition: publisher.h:183
ImplPtr impl_
Definition: publisher.h:193
#define ROS_ASSERT_MSG(cond,...)
roscpp&#39;s interface for creating subscribers, publishers, etc.
Definition: node_handle.h:87
Manages an advertisement on a specific topic.
Definition: publisher.h:47
void incrementSequence() const
Definition: publisher.cpp:95
uint32_t getNumSubscribers() const
Returns the number of subscribers that are currently connected to this Publisher. ...
Definition: publisher.cpp:122
SubscriberCallbacksPtr callbacks_
Definition: publisher.h:187
bool isValid() const
Definition: publisher.cpp:44
void shutdown()
Shutdown the advertisement associated with this Publisher.
Definition: publisher.cpp:103
std::string getTopic() const
Returns the topic that this Publisher will publish on.
Definition: publisher.cpp:112
bool isLatched() const
Returns whether or not this topic is latched.
Definition: publisher.cpp:132
static const TopicManagerPtr & instance()
#define ROS_DEBUG(...)


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Feb 3 2019 03:29:54