roscpp_plugin_provider.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Dirk Thomas, TU Darmstadt
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  *   * Redistributions of source code must retain the above copyright
00010  *     notice, this list of conditions and the following disclaimer.
00011  *   * Redistributions in binary form must reproduce the above
00012  *     copyright notice, this list of conditions and the following
00013  *     disclaimer in the documentation and/or other materials provided
00014  *     with the distribution.
00015  *   * Neither the name of the TU Darmstadt nor the names of its
00016  *     contributors may be used to endorse or promote products derived
00017  *     from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00027  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00029  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #include "roscpp_plugin_provider.h"
00034 
00035 #include "nodelet_plugin_provider.h"
00036 #include <rqt_gui_cpp/plugin.h>
00037 
00038 #include <qt_gui_cpp/plugin_provider.h>
00039 
00040 #include <nodelet/nodelet.h>
00041 #include <pluginlib/class_list_macros.h>
00042 #include <ros/ros.h>
00043 
00044 #include <stdexcept>
00045 #include <sys/types.h>
00046 #include <unistd.h>
00047 #include <iostream>
00048 
00049 namespace rqt_gui_cpp {
00050 
00051 RosCppPluginProvider::RosCppPluginProvider()
00052   : qt_gui_cpp::CompositePluginProvider()
00053   , node_initialized_(false)
00054 {
00055   QList<PluginProvider*> plugin_providers;
00056   plugin_providers.append(new NodeletPluginProvider("rqt_gui", "rqt_gui_cpp::Plugin"));
00057   set_plugin_providers(plugin_providers);
00058 }
00059 
00060 RosCppPluginProvider::~RosCppPluginProvider()
00061 {
00062   if (ros::isStarted())
00063   {
00064     ros::shutdown();
00065   }
00066 }
00067 
00068 void* RosCppPluginProvider::load(const QString& plugin_id, qt_gui_cpp::PluginContext* plugin_context)
00069 {
00070   init_node();
00071   return qt_gui_cpp::CompositePluginProvider::load(plugin_id, plugin_context);
00072 }
00073 
00074 qt_gui_cpp::Plugin* RosCppPluginProvider::load_plugin(const QString& plugin_id, qt_gui_cpp::PluginContext* plugin_context)
00075 {
00076   init_node();
00077   return qt_gui_cpp::CompositePluginProvider::load_plugin(plugin_id, plugin_context);
00078 }
00079 
00080 void RosCppPluginProvider::init_node()
00081 {
00082   // initialize ROS node once
00083   if (!node_initialized_)
00084   {
00085     int argc = 0;
00086     char** argv = 0;
00087     std::stringstream name;
00088     name << "rqt_gui_cpp_node_";
00089     name << getpid();
00090     qDebug("RosCppPluginProvider::init_node() initialize ROS node '%s'", name.str().c_str());
00091     ros::init(argc, argv, name.str().c_str(), ros::init_options::NoSigintHandler);
00092     if (!ros::master::check())
00093     {
00094       throw std::runtime_error("RosCppPluginProvider::init_node() could not find ROS master");
00095     }
00096     ros::start();
00097     node_initialized_ = true;
00098   }
00099 }
00100 
00101 }
00102 
00103 PLUGINLIB_DECLARE_CLASS(rqt_gui_cpp, RosCppPluginProvider, rqt_gui_cpp::RosCppPluginProvider, qt_gui_cpp::PluginProvider)


rqt_gui_cpp
Author(s): Dirk Thomas
autogenerated on Fri Jan 3 2014 11:54:08