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 the 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/shared_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 
00064 struct InitProxy
00065 {
00066   InitProxy()
00067   {
00068     const std::vector<std::string> &args = ROScppArgs();
00069     int fake_argc = args.size();
00070     char **fake_argv = new char*[args.size()];
00071     for (std::size_t i = 0 ; i < args.size() ; ++i)
00072       fake_argv[i] = strdup(args[i].c_str());
00073 
00074     ros::init(fake_argc, fake_argv, ROScppNodeName(), 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 
00089 static void roscpp_init_or_stop(bool init)
00090 {
00091   // ensure we do not accidentally initialize ROS multiple times per process
00092   static boost::mutex lock;
00093   boost::mutex::scoped_lock slock(lock);
00094 
00095   // once per process, we start a spinner
00096   static bool once = true;
00097   static boost::scoped_ptr<InitProxy> proxy;
00098   static boost::scoped_ptr<ros::AsyncSpinner> spinner;
00099 
00100   // initialize only once
00101   if (once && init)
00102   {
00103     once = false;
00104 
00105     // if ROS (cpp) is not initialized, we initialize it
00106     if (!ros::isInitialized())
00107     {
00108       proxy.reset(new InitProxy());
00109       spinner.reset(new ros::AsyncSpinner(1));
00110       spinner->start();
00111     }
00112   }
00113 
00114   // shutdown if needed
00115   if (!init)
00116   {
00117     once = false;
00118     proxy.reset();
00119     spinner.reset();
00120   }
00121 }
00122 
00123 void moveit::py_bindings_tools::roscpp_init()
00124 {
00125   roscpp_init_or_stop(true);
00126 }
00127 
00128 void moveit::py_bindings_tools::roscpp_init(const std::string &node_name, boost::python::list &argv)
00129 {
00130   roscpp_set_arguments(node_name, argv);
00131   roscpp_init();
00132 }
00133 
00134 void moveit::py_bindings_tools::roscpp_init(boost::python::list &argv)
00135 {
00136   ROScppArgs() = stringFromList(argv);
00137   roscpp_init();
00138 }
00139 
00140 void moveit::py_bindings_tools::roscpp_shutdown()
00141 {
00142   roscpp_init_or_stop(false);
00143 }
00144 
00145 moveit::py_bindings_tools::ROScppInitializer::ROScppInitializer()
00146 {
00147   roscpp_init();
00148 }
00149 
00150 moveit::py_bindings_tools::ROScppInitializer::ROScppInitializer(boost::python::list &argv)
00151 {
00152   roscpp_init(argv);
00153 }
00154 
00155 moveit::py_bindings_tools::ROScppInitializer::ROScppInitializer(const std::string &node_name, boost::python::list &argv)
00156 {
00157   roscpp_init(node_name, argv);
00158 }


planning_interface
Author(s): Ioan Sucan
autogenerated on Mon Oct 6 2014 02:33:28