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 #ifdef _MSC_VER
35  #ifdef snprintf
36  #undef snprintf
37  #endif
38  #define snprintf _snprintf_s
39 #endif
40 
41 namespace ros
42 {
43 
44 namespace names
45 {
46 void init(const M_string& remappings);
47 }
48 
49 namespace this_node
50 {
51 
52 // collect all static variables into a singleton to ensure proper destruction order
53 class ThisNode
54 {
55  std::string name_;
56  std::string namespace_;
57 
58  ThisNode() : name_("empty") {}
59 
60 public:
61  static ThisNode& instance()
62  {
63  static ThisNode singleton;
64  return singleton;
65  }
66 
67  const std::string& getName() const
68  {
69  return name_;
70  }
71 
72  const std::string& getNamespace() const
73  {
74  return namespace_;
75  }
76 
77  void init(const std::string& name, const M_string& remappings, uint32_t options);
78 };
79 
80 
81 const std::string& getName()
82 {
83  return ThisNode::instance().getName();
84 }
85 
86 const std::string& getNamespace()
87 {
88  return ThisNode::instance().getNamespace();
89 }
90 
92 {
93  TopicManager::instance()->getAdvertisedTopics(topics);
94 }
95 
97 {
98  TopicManager::instance()->getSubscribedTopics(topics);
99 }
100 
101 void init(const std::string& name, const M_string& remappings, uint32_t options)
102 {
103  ThisNode::instance().init(name, remappings, options);
104 }
105 
106 void ThisNode::init(const std::string& name, const M_string& remappings, uint32_t options)
107 {
108  char *ns_env = NULL;
109 #ifdef _MSC_VER
110  _dupenv_s(&ns_env, NULL, "ROS_NAMESPACE");
111 #else
112  ns_env = getenv("ROS_NAMESPACE");
113 #endif
114 
115  if (ns_env)
116  {
117  namespace_ = ns_env;
118 #ifdef _MSC_VER
119  free(ns_env);
120 #endif
121  }
122 
123  if (name.empty()) {
124  throw InvalidNameException("The node name must not be empty");
125  }
126 
127  name_ = name;
128 
129  bool disable_anon = false;
130  M_string::const_iterator it = remappings.find("__name");
131  if (it != remappings.end())
132  {
133  name_ = it->second;
134  disable_anon = true;
135  }
136 
137  it = remappings.find("__ns");
138  if (it != remappings.end())
139  {
140  namespace_ = it->second;
141  }
142 
143  namespace_ = names::clean(namespace_);
144  if (namespace_.empty() || (namespace_[0] != '/'))
145  {
146  namespace_ = "/" + namespace_;
147  }
148 
149  std::string error;
150  if (!names::validate(namespace_, error))
151  {
152  std::stringstream ss;
153  ss << "Namespace [" << namespace_ << "] is invalid: " << error;
154  throw InvalidNameException(ss.str());
155  }
156 
157  // names must be initialized here, because it requires the namespace to already be known so that it can properly resolve names.
158  // It must be done before we resolve g_name, because otherwise the name will not get remapped.
159  names::init(remappings);
160 
161  if (name_.find("/") != std::string::npos)
162  {
163  throw InvalidNodeNameException(name_, "node names cannot contain /");
164  }
165  if (name_.find("~") != std::string::npos)
166  {
167  throw InvalidNodeNameException(name_, "node names cannot contain ~");
168  }
169 
170  name_ = names::resolve(namespace_, name_);
171 
172  if (options & init_options::AnonymousName && !disable_anon)
173  {
174  char buf[200];
175  snprintf(buf, sizeof(buf), "_%llu", (unsigned long long)WallTime::now().toNSec());
176  name_ += buf;
177  }
178 
179  ros::console::setFixedFilterToken("node", name_);
180 }
181 
182 } // namespace this_node
183 
184 } // 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
const std::string & getName() const
Definition: this_node.cpp:67
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROS initialization function.
Definition: init.cpp:470
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:81
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:61
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:86
ROSCPP_DECL void getSubscribedTopics(V_string &topics)
Get the list of topics subscribed to by this node.
Definition: this_node.cpp:96
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:91
const std::string & getNamespace() const
Definition: this_node.cpp:72
static const TopicManagerPtr & instance()


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Feb 3 2019 03:29:54