roscpp_initializer.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2012, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
00036 
00037 #include "moveit/py_bindings_tools/roscpp_initializer.h"
00038 #include "moveit/py_bindings_tools/py_conversions.h"
00039 #include <boost/thread.hpp>
00040 #include <boost/scoped_ptr.hpp>
00041 #include <ros/ros.h>
00042 
00043 static std::vector<std::string>& ROScppArgs()
00044 {
00045   static std::vector<std::string> args;
00046   return args;
00047 }
00048 
00049 static std::string& ROScppNodeName()
00050 {
00051   static std::string node_name("moveit_python_wrappers");
00052   return node_name;
00053 }
00054 
00055 void moveit::py_bindings_tools::roscpp_set_arguments(const std::string& node_name, boost::python::list& argv)
00056 {
00057   ROScppNodeName() = node_name;
00058   ROScppArgs() = stringFromList(argv);
00059 }
00060 
00061 namespace
00062 {
00063 struct InitProxy
00064 {
00065   InitProxy()
00066   {
00067     const std::vector<std::string>& args = ROScppArgs();
00068     int fake_argc = args.size();
00069     char** fake_argv = new char*[args.size()];
00070     for (std::size_t i = 0; i < args.size(); ++i)
00071       fake_argv[i] = strdup(args[i].c_str());
00072 
00073     ros::init(fake_argc, fake_argv, ROScppNodeName(),
00074               ros::init_options::AnonymousName | ros::init_options::NoSigintHandler);
00075     for (int i = 0; i < fake_argc; ++i)
00076       delete[] fake_argv[i];
00077     delete[] fake_argv;
00078   }
00079 
00080   ~InitProxy()
00081   {
00082     if (ros::isInitialized() && !ros::isShuttingDown())
00083       ros::shutdown();
00084   }
00085 };
00086 }
00087 
00088 static void roscpp_init_or_stop(bool init)
00089 {
00090   // ensure we do not accidentally initialize ROS multiple times per process
00091   static boost::mutex lock;
00092   boost::mutex::scoped_lock slock(lock);
00093 
00094   // once per process, we start a spinner
00095   static bool once = true;
00096   static boost::scoped_ptr<InitProxy> proxy;
00097   static boost::scoped_ptr<ros::AsyncSpinner> spinner;
00098 
00099   // initialize only once
00100   if (once && init)
00101   {
00102     once = false;
00103 
00104     // if ROS (cpp) is not initialized, we initialize it
00105     if (!ros::isInitialized())
00106     {
00107       proxy.reset(new InitProxy());
00108       spinner.reset(new ros::AsyncSpinner(1));
00109       spinner->start();
00110     }
00111   }
00112 
00113   // shutdown if needed
00114   if (!init)
00115   {
00116     once = false;
00117     proxy.reset();
00118     spinner.reset();
00119   }
00120 }
00121 
00122 void moveit::py_bindings_tools::roscpp_init()
00123 {
00124   roscpp_init_or_stop(true);
00125 }
00126 
00127 void moveit::py_bindings_tools::roscpp_init(const std::string& node_name, boost::python::list& argv)
00128 {
00129   roscpp_set_arguments(node_name, argv);
00130   roscpp_init();
00131 }
00132 
00133 void moveit::py_bindings_tools::roscpp_init(boost::python::list& argv)
00134 {
00135   ROScppArgs() = stringFromList(argv);
00136   roscpp_init();
00137 }
00138 
00139 void moveit::py_bindings_tools::roscpp_shutdown()
00140 {
00141   roscpp_init_or_stop(false);
00142 }
00143 
00144 moveit::py_bindings_tools::ROScppInitializer::ROScppInitializer()
00145 {
00146   roscpp_init();
00147 }
00148 
00149 moveit::py_bindings_tools::ROScppInitializer::ROScppInitializer(boost::python::list& argv)
00150 {
00151   roscpp_init(argv);
00152 }
00153 
00154 moveit::py_bindings_tools::ROScppInitializer::ROScppInitializer(const std::string& node_name, boost::python::list& argv)
00155 {
00156   roscpp_init(node_name, argv);
00157 }


planning_interface
Author(s): Ioan Sucan
autogenerated on Wed Jun 19 2019 19:24:53