00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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 }