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 namespace ros
00035 {
00036
00037 namespace names
00038 {
00039 void init(const M_string& remappings);
00040 }
00041
00042 namespace this_node
00043 {
00044
00045 std::string g_name = "empty";
00046 std::string g_namespace;
00047
00048 const std::string& getName()
00049 {
00050 return g_name;
00051 }
00052
00053 const std::string& getNamespace()
00054 {
00055 return g_namespace;
00056 }
00057
00058 void getAdvertisedTopics(V_string& topics)
00059 {
00060 TopicManager::instance()->getAdvertisedTopics(topics);
00061 }
00062
00063 void getSubscribedTopics(V_string& topics)
00064 {
00065 TopicManager::instance()->getSubscribedTopics(topics);
00066 }
00067
00068 void init(const std::string& name, const M_string& remappings, uint32_t options)
00069 {
00070 char *ns_env = getenv("ROS_NAMESPACE");
00071 if (ns_env)
00072 {
00073 g_namespace = ns_env;
00074 }
00075
00076 g_name = name;
00077
00078 bool disable_anon = false;
00079 M_string::const_iterator it = remappings.find("__name");
00080 if (it != remappings.end())
00081 {
00082 g_name = it->second;
00083 disable_anon = true;
00084 }
00085
00086 it = remappings.find("__ns");
00087 if (it != remappings.end())
00088 {
00089 g_namespace = it->second;
00090 }
00091
00092 if (g_namespace.empty())
00093 {
00094 g_namespace = "/";
00095 }
00096
00097 std::string error;
00098 if (!names::validate(g_namespace, error))
00099 {
00100 std::stringstream ss;
00101 ss << "Namespace [" << g_namespace << "] is invalid: " << error;
00102 throw InvalidNameException(ss.str());
00103 }
00104
00105
00106
00107 names::init(remappings);
00108
00109 if (g_name.find("/") != std::string::npos)
00110 {
00111 throw InvalidNodeNameException(g_name, "node names cannot contain /");
00112 }
00113 if (g_name.find("~") != std::string::npos)
00114 {
00115 throw InvalidNodeNameException(g_name, "node names cannot contain ~");
00116 }
00117
00118 g_name = names::resolve(g_namespace, g_name);
00119
00120 if (options & init_options::AnonymousName && !disable_anon)
00121 {
00122 char buf[200];
00123 snprintf(buf, sizeof(buf), "_%llu", (unsigned long long)WallTime::now().toNSec());
00124 g_name += buf;
00125 }
00126
00127 ros::console::setFixedFilterToken("node", g_name);
00128 }
00129
00130 }
00131
00132 }