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 }
nodelet::V_string
std::vector< std::string > V_string
rqt_gui_cpp::NodeletPluginProvider::RosSpinThread::~RosSpinThread
virtual ~RosSpinThread()
Definition: nodelet_plugin_provider.cpp:155
boost::shared_ptr
nodelet::M_string
std::map< std::string, std::string > M_string
qt_gui_cpp::PluginContext
rqt_gui_cpp::NodeletPluginProvider::shutdown
virtual void shutdown()
Definition: nodelet_plugin_provider.cpp:84
ros.h
qt_gui_cpp::Plugin
qt_gui_cpp::RosPluginlibPluginProvider::create_plugin
virtual boost::shared_ptr< T > create_plugin(const std::string &lookup_name, PluginContext *=0)
nodelet::Loader
nodelet::Loader::load
bool load(const std::string &name, const std::string &type, const M_string &remappings, const V_string &my_argv)
nodelet_plugin_provider.h
rqt_gui_cpp::NodeletPluginProvider::RosSpinThread::RosSpinThread
RosSpinThread(QObject *parent=0)
Definition: nodelet_plugin_provider.cpp:150
rqt_gui_cpp::NodeletPluginProvider::ros_spin_thread_
RosSpinThread * ros_spin_thread_
Definition: nodelet_plugin_provider.h:91
rqt_gui_cpp::Plugin
Definition: plugin.h:49
rqt_gui_cpp
Definition: plugin.h:42
rqt_gui_cpp::NodeletPluginProvider::create_instance
boost::shared_ptr< nodelet::Nodelet > create_instance(const std::string &lookup_name)
Definition: nodelet_plugin_provider.cpp:131
qt_gui_cpp::RosPluginlibPluginProvider::init_plugin
virtual void init_plugin(const QString &, PluginContext *plugin_context, Plugin *plugin)
rqt_gui_cpp::NodeletPluginProvider::RosSpinThread
Definition: nodelet_plugin_provider.h:81
rqt_gui_cpp::NodeletPluginProvider::init_plugin
virtual void init_plugin(const QString &plugin_id, qt_gui_cpp::PluginContext *plugin_context, qt_gui_cpp::Plugin *plugin)
Definition: nodelet_plugin_provider.cpp:137
rqt_gui_cpp::NodeletPluginProvider::unload
virtual void unload(void *instance)
Definition: nodelet_plugin_provider.cpp:59
rqt_gui_cpp::NodeletPluginProvider::RosSpinThread::abort
bool abort
Definition: nodelet_plugin_provider.h:88
rqt_gui_cpp::NodeletPluginProvider::RosSpinThread::run
void run()
Definition: nodelet_plugin_provider.cpp:158
rqt_gui_cpp::NodeletPluginProvider::~NodeletPluginProvider
virtual ~NodeletPluginProvider()
Definition: nodelet_plugin_provider.cpp:51
qt_gui_cpp::PluginContext::serialNumber
int serialNumber() const
callback_queue.h
rqt_gui_cpp::NodeletPluginProvider::NodeletPluginProvider
NodeletPluginProvider(const QString &export_tag, const QString &base_class_type)
Definition: nodelet_plugin_provider.cpp:45
qt_gui_cpp
nodelet
rqt_gui_cpp::NodeletPluginProvider::init_loader
void init_loader()
Definition: nodelet_plugin_provider.cpp:95
roscpp_plugin_provider.h
rqt_gui_cpp::NodeletPluginProvider::instances_
QMap< void *, QString > instances_
Definition: nodelet_plugin_provider.h:79
qt_gui_cpp::RosPluginlibPluginProvider::unload
virtual void unload(void *instance)
ros::CallbackQueue::callOne
CallOneResult callOne()
ros::WallDuration
nodelet::Loader::listLoadedNodelets
std::vector< std::string > listLoadedNodelets()
nodelet::Loader::unload
bool unload(const std::string &name)
rqt_gui_cpp::NodeletPluginProvider::instance_
boost::shared_ptr< rqt_gui_cpp::Plugin > instance_
Definition: nodelet_plugin_provider.h:77
rqt_gui_cpp::NodeletPluginProvider::loader_
nodelet::Loader * loader_
Definition: nodelet_plugin_provider.h:75
rqt_gui_cpp::NodeletPluginProvider::create_plugin
virtual boost::shared_ptr< Plugin > create_plugin(const std::string &lookup_name, qt_gui_cpp::PluginContext *plugin_context)
Definition: nodelet_plugin_provider.cpp:111
ros::getGlobalCallbackQueue
ROSCPP_DECL CallbackQueue * getGlobalCallbackQueue()


rqt_gui_cpp
Author(s): Dirk Thomas, Michael Jeronimo
autogenerated on Fri Jul 12 2024 02:31:10