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()
00036 : unadvertised_(false), constructed_(WallTime::now().toSec())
00037 {
00038 }
00039
00040 Publisher::Impl::~Impl()
00041 {
00042 if (WallTime::now().toSec() - constructed_ < 0.001)
00043 ROS_WARN("Publisher on '%s' destroyed immediately after creation. Did you forget to store the handle?", topic_.c_str());
00044 unadvertise();
00045 }
00046
00047 bool Publisher::Impl::isValid() const
00048 {
00049 return !unadvertised_;
00050 }
00051
00052 void Publisher::Impl::unadvertise()
00053 {
00054 if (!unadvertised_)
00055 {
00056 unadvertised_ = true;
00057 TopicManager::instance()->unadvertise(topic_, callbacks_);
00058 node_handle_.reset();
00059 }
00060 }
00061
00062 Publisher::Publisher(const std::string& topic, const std::string& md5sum, const std::string& datatype, const NodeHandle& node_handle, const SubscriberCallbacksPtr& callbacks)
00063 : impl_(new Impl)
00064 {
00065 impl_->topic_ = topic;
00066 impl_->md5sum_ = md5sum;
00067 impl_->datatype_ = datatype;
00068 impl_->node_handle_ = NodeHandlePtr(new NodeHandle(node_handle));
00069 impl_->callbacks_ = callbacks;
00070 }
00071
00072 Publisher::Publisher(const Publisher& rhs)
00073 {
00074 impl_ = rhs.impl_;
00075 }
00076
00077 Publisher::~Publisher()
00078 {
00079 }
00080
00081 void Publisher::publish(const boost::function<SerializedMessage(void)>& serfunc, SerializedMessage& m) const
00082 {
00083 if (!impl_)
00084 {
00085 ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
00086 return;
00087 }
00088
00089 if (!impl_->isValid())
00090 {
00091 ROS_ASSERT_MSG(false, "Call to publish() on an invalid Publisher (topic [%s])", impl_->topic_.c_str());
00092 return;
00093 }
00094
00095 TopicManager::instance()->publish(impl_->topic_, serfunc, m);
00096 }
00097
00098 void Publisher::incrementSequence() const
00099 {
00100 if (impl_ && impl_->isValid())
00101 {
00102 TopicManager::instance()->incrementSequence(impl_->topic_);
00103 }
00104 }
00105
00106 void Publisher::shutdown()
00107 {
00108 if (impl_)
00109 {
00110 impl_->unadvertise();
00111 impl_.reset();
00112 }
00113 }
00114
00115 std::string Publisher::getTopic() const
00116 {
00117 if (impl_)
00118 {
00119 return impl_->topic_;
00120 }
00121
00122 return std::string();
00123 }
00124
00125 uint32_t Publisher::getNumSubscribers() const
00126 {
00127 if (impl_ && impl_->isValid())
00128 {
00129 return TopicManager::instance()->getNumSubscribers(impl_->topic_);
00130 }
00131
00132 return 0;
00133 }
00134
00135 }