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 {
81  return ThisNode::instance().getNamespace();
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 
136  namespace_ = names::clean(namespace_);
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 
163  name_ = names::resolve(namespace_, name_);
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 
172  ros::console::setFixedFilterToken("node", name_);
173 }
174 
175 } // namespace this_node
176 
177 } // namespace ros
Anonymize the node name. Adds a random number to the end of your node&#39;s name, to make it unique...
Definition: init.h:59
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROS initialization function.
Definition: init.cpp:486
ROSCONSOLE_DECL void setFixedFilterToken(const std::string &key, const std::string &val)
ROSCPP_DECL const std::string & getName()
Returns the name of the current node.
Definition: this_node.cpp:74
ROSCPP_DECL bool validate(const std::string &name, std::string &error)
Validate a name against the name spec.
Definition: names.cpp:66
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
ROSCPP_DECL std::string clean(const std::string &name)
Cleans a graph resource name: removes double slashes, trailing slash.
Definition: names.cpp:99
static ThisNode & instance()
Definition: this_node.cpp:54
std::map< std::string, std::string > M_string
void init(const M_string &remappings)
Definition: names.cpp:187
std::vector< std::string > V_string
ROSCPP_DECL const std::string & getNamespace()
Returns the namespace of the current node.
Definition: this_node.cpp:79
const std::string & getNamespace() const
Definition: this_node.cpp:65
ROSCPP_DECL void getSubscribedTopics(V_string &topics)
Get the list of topics subscribed to by this node.
Definition: this_node.cpp:89
static WallTime now()
Thrown when an invalid graph resource name is specified to any roscpp function.
Definition: exceptions.h:51
Thrown when an invalid node name is specified to ros::init()
Definition: exceptions.h:39
ROSCPP_DECL void getAdvertisedTopics(V_string &topics)
Get the list of topics advertised by this node.
Definition: this_node.cpp:84
const std::string & getName() const
Definition: this_node.cpp:60
static const TopicManagerPtr & instance()


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:33:27