this_node.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009, Willow Garage, Inc.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *   * Redistributions of source code must retain the above copyright notice,
00007  *     this list of conditions and the following disclaimer.
00008  *   * Redistributions in binary form must reproduce the above copyright
00009  *     notice, this list of conditions and the following disclaimer in the
00010  *     documentation and/or other materials provided with the distribution.
00011  *   * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
00012  *     contributors may be used to endorse or promote products derived from
00013  *     this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #include <cstdio>
00029 #include "ros/this_node.h"
00030 #include "ros/names.h"
00031 #include "ros/topic_manager.h"
00032 #include "ros/init.h"
00033 
00034 #ifdef _MSC_VER
00035   #ifdef snprintf
00036     #undef snprintf
00037   #endif
00038   #define snprintf _snprintf_s
00039 #endif
00040 
00041 namespace ros
00042 {
00043 
00044 namespace names
00045 {
00046 void init(const M_string& remappings);
00047 }
00048 
00049 namespace this_node
00050 {
00051 
00052 std::string g_name = "empty";
00053 std::string g_namespace;
00054 
00055 const std::string& getName()
00056 {
00057   return g_name;
00058 }
00059 
00060 const std::string& getNamespace()
00061 {
00062   return g_namespace;
00063 }
00064 
00065 void getAdvertisedTopics(V_string& topics)
00066 {
00067   TopicManager::instance()->getAdvertisedTopics(topics);
00068 }
00069 
00070 void getSubscribedTopics(V_string& topics)
00071 {
00072   TopicManager::instance()->getSubscribedTopics(topics);
00073 }
00074 
00075 void init(const std::string& name, const M_string& remappings, uint32_t options)
00076 {
00077   char *ns_env = NULL;
00078 #ifdef _MSC_VER
00079   _dupenv_s(&ns_env, NULL, "ROS_NAMESPACE");
00080 #else
00081   ns_env = getenv("ROS_NAMESPACE");
00082 #endif
00083 
00084   if (ns_env)
00085   {
00086     g_namespace = ns_env;
00087 #ifdef _MSC_VER
00088     free(ns_env);
00089 #endif
00090   }
00091 
00092   g_name = name;
00093 
00094   bool disable_anon = false;
00095   M_string::const_iterator it = remappings.find("__name");
00096   if (it != remappings.end())
00097   {
00098     g_name = it->second;
00099     disable_anon = true;
00100   }
00101 
00102   it = remappings.find("__ns");
00103   if (it != remappings.end())
00104   {
00105     g_namespace = it->second;
00106   }
00107 
00108   if (g_namespace.empty())
00109   {
00110     g_namespace = "/";
00111   }
00112 
00113   g_namespace = (g_namespace == "/")
00114     ? std::string("/") 
00115     : ("/" + g_namespace)
00116     ;
00117 
00118 
00119   std::string error;
00120   if (!names::validate(g_namespace, error))
00121   {
00122     std::stringstream ss;
00123     ss << "Namespace [" << g_namespace << "] is invalid: " << error;
00124     throw InvalidNameException(ss.str());
00125   }
00126 
00127   // names must be initialized here, because it requires the namespace to already be known so that it can properly resolve names.
00128   // It must be done before we resolve g_name, because otherwise the name will not get remapped.
00129   names::init(remappings);
00130 
00131   if (g_name.find("/") != std::string::npos)
00132   {
00133     throw InvalidNodeNameException(g_name, "node names cannot contain /");
00134   }
00135   if (g_name.find("~") != std::string::npos)
00136   {
00137     throw InvalidNodeNameException(g_name, "node names cannot contain ~");
00138   }
00139 
00140   g_name = names::resolve(g_namespace, g_name);
00141 
00142   if (options & init_options::AnonymousName && !disable_anon)
00143   {
00144     char buf[200];
00145     snprintf(buf, sizeof(buf), "_%llu", (unsigned long long)WallTime::now().toNSec());
00146     g_name += buf;
00147   }
00148 
00149   ros::console::setFixedFilterToken("node", g_name);
00150 }
00151 
00152 } // namespace this_node
00153 
00154 } // namespace ros


roscpp
Author(s): Morgan Quigley mquigley@cs.stanford.edu, Josh Faust jfaust@willowgarage.com, Brian Gerkey gerkey@willowgarage.com, Troy Straszheim straszheim@willowgarage.com
autogenerated on Sat Dec 28 2013 17:35:52