publisher.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009, Willow Garage, Inc.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *   * Redistributions of source code must retain the above copyright notice,
00007  *     this list of conditions and the following disclaimer.
00008  *   * Redistributions in binary form must reproduce the above copyright
00009  *     notice, this list of conditions and the following disclaimer in the
00010  *     documentation and/or other materials provided with the distribution.
00011  *   * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
00012  *     contributors may be used to endorse or promote products derived from
00013  *     this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #include "ros/publisher.h"
00029 #include "ros/node_handle.h"
00030 #include "ros/topic_manager.h"
00031 
00032 namespace ros
00033 {
00034 
00035 Publisher::Impl::Impl() : unadvertised_(false) { }
00036 
00037 Publisher::Impl::~Impl()
00038 {
00039   ROS_DEBUG("Publisher on '%s' deregistering callbacks.", topic_.c_str());
00040   unadvertise();
00041 }
00042 
00043 bool Publisher::Impl::isValid() const
00044 {
00045   return !unadvertised_;
00046 }
00047 
00048 void Publisher::Impl::unadvertise()
00049 {
00050   if (!unadvertised_)
00051   {
00052     unadvertised_ = true;
00053     TopicManager::instance()->unadvertise(topic_, callbacks_);
00054     node_handle_.reset();
00055   }
00056 }
00057 
00058 Publisher::Publisher(const std::string& topic, const std::string& md5sum, const std::string& datatype, const NodeHandle& node_handle, const SubscriberCallbacksPtr& callbacks)
00059 : impl_(new Impl)
00060 {
00061   impl_->topic_ = topic;
00062   impl_->md5sum_ = md5sum;
00063   impl_->datatype_ = datatype;
00064   impl_->node_handle_ = NodeHandlePtr(new NodeHandle(node_handle));
00065   impl_->callbacks_ = callbacks;
00066 }
00067 
00068 Publisher::Publisher(const Publisher& rhs)
00069 {
00070   impl_ = rhs.impl_;
00071 }
00072 
00073 Publisher::~Publisher()
00074 {
00075 }
00076 
00077 void Publisher::publish(const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m) const
00078 {
00079   if (!impl_)
00080   {
00081     ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
00082     return;
00083   }
00084 
00085   if (!impl_->isValid())
00086   {
00087     ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
00088     return;
00089   }
00090 
00091   TopicManager::instance()->publish(impl_->topic_, serfunc, m);
00092 }
00093 
00094 void Publisher::incrementSequence() const
00095 {
00096   if (impl_ && impl_->isValid())
00097   {
00098     TopicManager::instance()->incrementSequence(impl_->topic_);
00099   }
00100 }
00101 
00102 void Publisher::shutdown()
00103 {
00104   if (impl_)
00105   {
00106     impl_->unadvertise();
00107     impl_.reset();
00108   }
00109 }
00110 
00111 std::string Publisher::getTopic() const
00112 {
00113   if (impl_)
00114   {
00115     return impl_->topic_;
00116   }
00117 
00118   return std::string();
00119 }
00120 
00121 uint32_t Publisher::getNumSubscribers() const
00122 {
00123   if (impl_ && impl_->isValid())
00124   {
00125     return TopicManager::instance()->getNumSubscribers(impl_->topic_);
00126   }
00127 
00128   return 0;
00129 }
00130 
00131 } // namespace ros


roscpp
Author(s): Morgan Quigley mquigley@cs.stanford.edu, Josh Faust jfaust@willowgarage.com, Brian Gerkey gerkey@willowgarage.com, Troy Straszheim straszheim@willowgarage.com
autogenerated on Sat Dec 28 2013 17:35:52