Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00128
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 }
00153
00154 }
roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Thu Jun 6 2019 21:10:05