subscriber.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2009, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
37 #include <ros/names.h>
38 #include <pluginlib/class_loader.h>
39 #include <boost/scoped_ptr.hpp>
40 
41 namespace image_transport {
42 
43 struct Subscriber::Impl
44 {
45  Impl()
46  : unsubscribed_(false)
47  {
48  }
49 
50  ~Impl()
51  {
52  shutdown();
53  }
54 
55  bool isValid() const
56  {
57  return !unsubscribed_;
58  }
59 
60  void shutdown()
61  {
62  if (!unsubscribed_) {
63  unsubscribed_ = true;
64  if (subscriber_)
65  subscriber_->shutdown();
66  }
67  }
68 
71  bool unsubscribed_;
72  //double constructed_;
73 };
74 
75 Subscriber::Subscriber(ros::NodeHandle& nh, const std::string& base_topic, uint32_t queue_size,
76  const boost::function<void(const sensor_msgs::ImageConstPtr&)>& callback,
77  const ros::VoidPtr& tracked_object, const TransportHints& transport_hints,
78  const SubLoaderPtr& loader)
79  : impl_(new Impl)
80 {
81  // Load the plugin for the chosen transport.
82  std::string lookup_name = SubscriberPlugin::getLookupName(transport_hints.getTransport());
83  try {
84  impl_->subscriber_ = loader->createInstance(lookup_name);
85  }
86  catch (pluginlib::PluginlibException& e) {
87  throw TransportLoadException(transport_hints.getTransport(), e.what());
88  }
89  // Hang on to the class loader so our shared library doesn't disappear from under us.
90  impl_->loader_ = loader;
91 
92  // Try to catch if user passed in a transport-specific topic as base_topic.
93  std::string clean_topic = ros::names::clean(base_topic);
94  size_t found = clean_topic.rfind('/');
95  if (found != std::string::npos) {
96  std::string transport = clean_topic.substr(found+1);
97  std::string plugin_name = SubscriberPlugin::getLookupName(transport);
98  std::vector<std::string> plugins = loader->getDeclaredClasses();
99  if (std::find(plugins.begin(), plugins.end(), plugin_name) != plugins.end()) {
100  std::string real_base_topic = clean_topic.substr(0, found);
101  ROS_WARN("[image_transport] It looks like you are trying to subscribe directly to a "
102  "transport-specific image topic '%s', in which case you will likely get a connection "
103  "error. Try subscribing to the base topic '%s' instead with parameter ~image_transport "
104  "set to '%s' (on the command line, _image_transport:=%s). "
105  "See http://ros.org/wiki/image_transport for details.",
106  clean_topic.c_str(), real_base_topic.c_str(), transport.c_str(), transport.c_str());
107  }
108  }
109 
110  // Tell plugin to subscribe.
111  impl_->subscriber_->subscribe(nh, base_topic, queue_size, callback, tracked_object, transport_hints);
112 }
113 
114 std::string Subscriber::getTopic() const
115 {
116  if (impl_) return impl_->subscriber_->getTopic();
117  return std::string();
118 }
119 
120 uint32_t Subscriber::getNumPublishers() const
121 {
122  if (impl_) return impl_->subscriber_->getNumPublishers();
123  return 0;
124 }
125 
126 std::string Subscriber::getTransport() const
127 {
128  if (impl_) return impl_->subscriber_->getTransportName();
129  return std::string();
130 }
131 
133 {
134  if (impl_) impl_->shutdown();
135 }
136 
137 Subscriber::operator void*() const
138 {
139  return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0;
140 }
141 
142 } //namespace image_transport
boost::shared_ptr
image_transport::Subscriber::Impl::shutdown
void shutdown()
Definition: subscriber.cpp:124
image_transport::TransportHints::getTransport
const std::string & getTransport() const
Definition: transport_hints.h:135
image_transport::SubscriberPlugin::getLookupName
static std::string getLookupName(const std::string &transport_type)
Return the lookup name of the SubscriberPlugin associated with a specific transport identifier.
Definition: subscriber_plugin.h:189
ros::names::clean
ROSCPP_DECL std::string clean(const std::string &name)
image_transport::Subscriber::getNumPublishers
uint32_t getNumPublishers() const
Returns the number of publishers this subscriber is connected to.
Definition: subscriber.cpp:152
image_transport::Subscriber::getTransport
std::string getTransport() const
Returns the name of the transport being used.
Definition: subscriber.cpp:158
subscriber_plugin.h
subscriber.h
image_transport::Subscriber::Impl::Impl
Impl()
Definition: subscriber.cpp:109
image_transport::Subscriber::Impl
Definition: subscriber.cpp:75
image_transport::TransportLoadException
An exception class thrown when image_transport is unable to load a requested transport.
Definition: exception.h:86
image_transport::Subscriber::Impl::~Impl
~Impl()
Definition: subscriber.cpp:114
ROS_WARN
#define ROS_WARN(...)
image_transport::Subscriber::Impl::loader_
SubLoaderPtr loader_
Definition: subscriber.cpp:133
image_transport::Subscriber::Impl::unsubscribed_
bool unsubscribed_
Definition: subscriber.cpp:135
image_transport::Subscriber::impl_
ImplPtr impl_
Definition: subscriber.h:169
names.h
image_transport::Subscriber::Impl::subscriber_
boost::shared_ptr< SubscriberPlugin > subscriber_
Definition: subscriber.cpp:134
image_transport::SubLoaderPtr
boost::shared_ptr< SubLoader > SubLoaderPtr
Definition: loader_fwds.h:53
image_transport
Definition: camera_common.h:41
image_transport::Subscriber::Subscriber
Subscriber()
Definition: subscriber.h:129
image_transport::TransportHints
Stores transport settings for an image topic subscription.
Definition: transport_hints.h:77
image_transport::Subscriber::getTopic
std::string getTopic() const
Returns the base image topic.
Definition: subscriber.cpp:146
ros::NodeHandle
image_transport::Subscriber::Impl::isValid
bool isValid() const
Definition: subscriber.cpp:119
image_transport::Subscriber::shutdown
void shutdown()
Unsubscribe the callback associated with this Subscriber.
Definition: subscriber.cpp:164


image_transport
Author(s): Patrick Mihelich
autogenerated on Sun Apr 5 2020 03:15:28