nodelet_plugin_provider.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Dirk Thomas, TU Darmstadt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution.
15  * * Neither the name of the TU Darmstadt nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
34 
35 #include "roscpp_plugin_provider.h"
36 
37 #include <ros/callback_queue.h>
38 #include <ros/ros.h>
39 
40 #include <stdexcept>
41 #include <boost/bind.hpp>
42 
43 namespace rqt_gui_cpp {
44 
45 NodeletPluginProvider::NodeletPluginProvider(const QString& export_tag, const QString& base_class_type)
46  : qt_gui_cpp::RosPluginlibPluginProvider<rqt_gui_cpp::Plugin>(export_tag, base_class_type)
47  , loader_(0)
48  , ros_spin_thread_(0)
49 {}
50 
52 {
53  if (loader_)
54  {
55  delete loader_;
56  }
57 }
58 
59 void NodeletPluginProvider::unload(void* instance)
60 {
61  qDebug("NodeletPluginProvider::unload()");
62  if (!instances_.contains(instance))
63  {
64  qCritical("NodeletPluginProvider::unload() instance not found");
65  return;
66  }
67 
68  QString nodelet_name = instances_[instance];
69  bool unloaded = loader_->unload(nodelet_name.toStdString());
70  if (!unloaded)
71  {
72  qCritical("NodeletPluginProvider::unload() '%s' failed", nodelet_name.toStdString().c_str());
73  }
74 
75  // stop and garbage ros spin thread if not needed anymore
76  if (loader_->listLoadedNodelets().empty())
77  {
78  shutdown();
79  }
80 
82 }
83 
85 {
86  if (ros_spin_thread_ != 0)
87  {
88  ros_spin_thread_->abort = true;
89  ros_spin_thread_->wait();
90  ros_spin_thread_->deleteLater();
91  ros_spin_thread_ = 0;
92  }
93 }
94 
96 {
97  // initialize nodelet Loader once
98  if (loader_ == 0)
99  {
100  loader_ = new nodelet::Loader(boost::bind(&NodeletPluginProvider::create_instance, this, _1));
101  }
102 
103  // spawn ros spin thread
104  if (ros_spin_thread_ == 0)
105  {
106  ros_spin_thread_ = new RosSpinThread(this);
107  ros_spin_thread_->start();
108  }
109 }
110 
112 {
113  init_loader();
114 
115  nodelet::M_string remappings;
116  nodelet::V_string my_argv;
117  std::string nodelet_name = lookup_name + "_" + QString::number(plugin_context->serialNumber()).toStdString();
118  instance_.reset();
119  qDebug("NodeletPluginProvider::create_plugin() load %s", lookup_name.c_str());
120  bool loaded = loader_->load(nodelet_name, lookup_name, remappings, my_argv);
121  if (loaded)
122  {
123  qDebug("NodeletPluginProvider::create_plugin() loaded");
124  instances_[&*instance_] = nodelet_name.c_str();
125  }
127  instance_.reset();
128  return instance;
129 }
130 
132 {
134  return instance_;
135 }
136 
137 void NodeletPluginProvider::init_plugin(const QString& plugin_id, qt_gui_cpp::PluginContext* plugin_context, qt_gui_cpp::Plugin* plugin)
138 {
139  qDebug("NodeletPluginProvider::init_plugin()");
140 
141  rqt_gui_cpp::Plugin* nodelet = dynamic_cast<rqt_gui_cpp::Plugin*>(plugin);
142  if (!nodelet)
143  {
144  throw std::runtime_error("plugin is not a nodelet");
145  }
146 
148 }
149 
151  : QThread(parent)
152  , abort(false)
153 {}
154 
156 {}
157 
159 {
160  while (!abort)
161  {
163  }
164 }
165 
166 }
bool load(const std::string &name, const std::string &type, const M_string &remappings, const V_string &my_argv)
CallOneResult callOne()
std::vector< std::string > listLoadedNodelets()
virtual boost::shared_ptr< T > create_plugin(const std::string &lookup_name, PluginContext *=0)
bool unload(const std::string &name)
virtual void init_plugin(const QString &, PluginContext *plugin_context, Plugin *plugin)
boost::shared_ptr< nodelet::Nodelet > create_instance(const std::string &lookup_name)
NodeletPluginProvider(const QString &export_tag, const QString &base_class_type)
ROSCPP_DECL CallbackQueue * getGlobalCallbackQueue()
boost::shared_ptr< rqt_gui_cpp::Plugin > instance_
virtual boost::shared_ptr< Plugin > create_plugin(const std::string &lookup_name, qt_gui_cpp::PluginContext *plugin_context)
virtual void init_plugin(const QString &plugin_id, qt_gui_cpp::PluginContext *plugin_context, qt_gui_cpp::Plugin *plugin)
virtual void unload(void *instance)
std::vector< std::string > V_string
std::map< std::string, std::string > M_string


rqt_gui_cpp
Author(s): Dirk Thomas
autogenerated on Sat Mar 16 2019 02:54:14