this_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <cstdio>
29 #include "ros/this_node.h"
30 #include "ros/names.h"
31 #include "ros/topic_manager.h"
32 #include "ros/init.h"
33 
34 namespace ros
35 {
36 
37 namespace names
38 {
39 void init(const M_string& remappings);
40 }
41 
42 namespace this_node
43 {
44 
45 // collect all static variables into a singleton to ensure proper destruction order
46 class ThisNode
47 {
48  std::string name_;
49  std::string namespace_;
50 
51  ThisNode() : name_("empty") {}
52 
53 public:
54  static ThisNode& instance()
55  {
56  static ThisNode singleton;
57  return singleton;
58  }
59 
60  const std::string& getName() const
61  {
62  return name_;
63  }
64 
65  const std::string& getNamespace() const
66  {
67  return namespace_;
68  }
69 
70  void init(const std::string& name, const M_string& remappings, uint32_t options);
71 };
72 
73 
74 const std::string& getName()
75 {
76  return ThisNode::instance().getName();
77 }
78 
79 const std::string& getNamespace()
80 {
82 }
83 
85 {
86  TopicManager::instance()->getAdvertisedTopics(topics);
87 }
88 
90 {
91  TopicManager::instance()->getSubscribedTopics(topics);
92 }
93 
94 void init(const std::string& name, const M_string& remappings, uint32_t options)
95 {
96  ThisNode::instance().init(name, remappings, options);
97 }
98 
99 void ThisNode::init(const std::string& name, const M_string& remappings, uint32_t options)
100 {
101  char *ns_env = NULL;
102 #ifdef _MSC_VER
103  _dupenv_s(&ns_env, NULL, "ROS_NAMESPACE");
104 #else
105  ns_env = getenv("ROS_NAMESPACE");
106 #endif
107 
108  if (ns_env)
109  {
110  namespace_ = ns_env;
111 #ifdef _MSC_VER
112  free(ns_env);
113 #endif
114  }
115 
116  if (name.empty()) {
117  throw InvalidNameException("The node name must not be empty");
118  }
119 
120  name_ = name;
121 
122  bool disable_anon = false;
123  M_string::const_iterator it = remappings.find("__name");
124  if (it != remappings.end())
125  {
126  name_ = it->second;
127  disable_anon = true;
128  }
129 
130  it = remappings.find("__ns");
131  if (it != remappings.end())
132  {
133  namespace_ = it->second;
134  }
135 
137  if (namespace_.empty() || (namespace_[0] != '/'))
138  {
139  namespace_ = "/" + namespace_;
140  }
141 
142  std::string error;
143  if (!names::validate(namespace_, error))
144  {
145  std::stringstream ss;
146  ss << "Namespace [" << namespace_ << "] is invalid: " << error;
147  throw InvalidNameException(ss.str());
148  }
149 
150  // names must be initialized here, because it requires the namespace to already be known so that it can properly resolve names.
151  // It must be done before we resolve g_name, because otherwise the name will not get remapped.
152  names::init(remappings);
153 
154  if (name_.find("/") != std::string::npos)
155  {
156  throw InvalidNodeNameException(name_, "node names cannot contain /");
157  }
158  if (name_.find("~") != std::string::npos)
159  {
160  throw InvalidNodeNameException(name_, "node names cannot contain ~");
161  }
162 
164 
165  if (options & init_options::AnonymousName && !disable_anon)
166  {
167  char buf[200];
168  std::snprintf(buf, sizeof(buf), "_%llu", (unsigned long long)WallTime::now().toNSec());
169  name_ += buf;
170  }
171 
173 }
174 
175 } // namespace this_node
176 
177 } // namespace ros
ros::init_options::AnonymousName
@ AnonymousName
Anonymize the node name. Adds a random number to the end of your node's name, to make it unique.
Definition: init.h:59
this_node.h
ros::this_node::getNamespace
const ROSCPP_DECL std::string & getNamespace()
Returns the namespace of the current node.
Definition: this_node.cpp:79
ros::this_node::ThisNode
Definition: this_node.cpp:46
ros
ros::this_node::ThisNode::init
void init(const std::string &name, const M_string &remappings, uint32_t options)
Definition: this_node.cpp:99
ros::names::clean
ROSCPP_DECL std::string clean(const std::string &name)
Cleans a graph resource name: removes double slashes, trailing slash.
Definition: names.cpp:99
ros::names::validate
ROSCPP_DECL bool validate(const std::string &name, std::string &error)
Validate a name against the name spec.
Definition: names.cpp:66
ros::names::resolve
ROSCPP_DECL std::string resolve(const std::string &name, bool remap=true)
Resolve a graph resource name into a fully qualified graph resource name.
Definition: names.cpp:136
ros::names::init
void init(const M_string &remappings)
Definition: names.cpp:187
topic_manager.h
ros::this_node::ThisNode::instance
static ThisNode & instance()
Definition: this_node.cpp:54
ros::this_node::ThisNode::name_
std::string name_
Definition: this_node.cpp:48
ros::this_node::init
void init(const std::string &names, const M_string &remappings, uint32_t options)
Definition: this_node.cpp:94
init.h
ros::this_node::ThisNode::getNamespace
const std::string & getNamespace() const
Definition: this_node.cpp:65
ros::WallTime::now
static WallTime now()
ros::InvalidNodeNameException
Thrown when an invalid node name is specified to ros::init()
Definition: exceptions.h:39
ros::console::setFixedFilterToken
ROSCONSOLE_DECL void setFixedFilterToken(const std::string &key, const std::string &val)
ros::this_node::getName
const ROSCPP_DECL std::string & getName()
Returns the name of the current node.
Definition: this_node.cpp:74
ros::TopicManager::instance
static const TopicManagerPtr & instance()
Definition: topic_manager.cpp:56
ros::this_node::getAdvertisedTopics
ROSCPP_DECL void getAdvertisedTopics(V_string &topics)
Get the list of topics advertised by this node.
Definition: this_node.cpp:84
ros::InvalidNameException
Thrown when an invalid graph resource name is specified to any roscpp function.
Definition: exceptions.h:51
ros::V_string
std::vector< std::string > V_string
ros::this_node::getSubscribedTopics
ROSCPP_DECL void getSubscribedTopics(V_string &topics)
Get the list of topics subscribed to by this node.
Definition: this_node.cpp:89
names.h
ros::this_node::ThisNode::namespace_
std::string namespace_
Definition: this_node.cpp:49
ros::this_node::ThisNode::ThisNode
ThisNode()
Definition: this_node.cpp:51
ros::this_node::ThisNode::getName
const std::string & getName() const
Definition: this_node.cpp:60
M_string
std::map< std::string, std::string > M_string


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:36