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 
44 {
45  Impl()
46  : unsubscribed_(false)
47  {
48  }
49 
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 
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  }
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 
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
ROSCPP_DECL std::string clean(const std::string &name)
#define ROS_WARN(...)
static std::string getLookupName(const std::string &transport_type)
Return the lookup name of the SubscriberPlugin associated with a specific transport identifier...
boost::shared_ptr< SubscriberPlugin > subscriber_
Definition: subscriber.cpp:70
std::string getTransport() const
Returns the name of the transport being used.
Definition: subscriber.cpp:126
std::string getTopic() const
Returns the base image topic.
Definition: subscriber.cpp:114
An exception class thrown when image_transport is unable to load a requested transport.
Definition: exception.h:54
uint32_t getNumPublishers() const
Returns the number of publishers this subscriber is connected to.
Definition: subscriber.cpp:120
void shutdown()
Unsubscribe the callback associated with this Subscriber.
Definition: subscriber.cpp:132
Stores transport settings for an image topic subscription.
const std::string & getTransport() const


image_transport
Author(s): Patrick Mihelich
autogenerated on Sat Apr 4 2020 03:14:58