YARP
Yet Another Robot Platform
YarpNameSpace.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_OS_YARPNAMESPACE_H
7 #define YARP_OS_YARPNAMESPACE_H
8 
9 #include <yarp/os/NameSpace.h>
10 
11 #include <cstdio>
12 
13 namespace yarp {
14 namespace os {
15 
17 {
18 public:
19  YarpNameSpace(const Contact& contact);
20 
21  virtual ~YarpNameSpace();
22 
23  Contact getNameServerContact() const override
24  {
25  return contact;
26  }
27 
28  Contact queryName(const std::string& name) override;
29 
30  Contact registerName(const std::string& name) override;
31 
32  Contact registerContact(const Contact& contact) override;
33 
34  Contact unregisterName(const std::string& name) override;
35 
36  Contact unregisterContact(const Contact& contact) override;
37 
38  virtual bool setProperty(const std::string& name,
39  const std::string& key,
40  const Value& value) override;
41 
42  virtual Value* getProperty(const std::string& name,
43  const std::string& key) override;
44 
45  virtual bool connectPortToTopic(const Contact& src,
46  const Contact& dest,
47  const ContactStyle& style) override
48  {
49  return connectTopic("subscribe", false, true, src, dest, style);
50  }
51 
52  virtual bool connectTopicToPort(const Contact& src,
53  const Contact& dest,
54  const ContactStyle& style) override
55  {
56  return connectTopic("subscribe", true, false, src, dest, style);
57  }
58 
59  virtual bool disconnectPortFromTopic(const Contact& src,
60  const Contact& dest,
61  const ContactStyle& style) override
62  {
63  return connectTopic("unsubscribe", false, true, src, dest, style);
64  }
65 
66  virtual bool disconnectTopicFromPort(const Contact& src,
67  const Contact& dest,
68  const ContactStyle& style) override
69  {
70  return connectTopic("unsubscribe", true, false, src, dest, style);
71  }
72 
73  virtual bool connectPortToPortPersistently(const Contact& src,
74  const Contact& dest,
75  const ContactStyle& style) override
76  {
77  return connectTopic("subscribe", false, false, src, dest, style);
78  }
79 
80  virtual bool disconnectPortToPortPersistently(const Contact& src,
81  const Contact& dest,
82  const ContactStyle& style) override
83  {
84  return connectTopic("unsubscribe", false, false, src, dest, style);
85  }
86 
87  virtual bool connectTopic(const std::string& dir,
88  bool srcIsTopic,
89  bool destIsTopic,
90  const Contact& src,
91  const Contact& dest,
92  const ContactStyle& style)
93  {
94  YARP_UNUSED(srcIsTopic);
95  Contact dynamicSrc = src;
96  Contact dynamicDest = dest;
97  Bottle cmd, reply;
98  cmd.addString(dir.c_str());
99  if (style.carrier != "") {
100  if (!destIsTopic) {
101  dynamicDest.setCarrier(style.carrier);
102  } else {
103  dynamicSrc.setCarrier(style.carrier);
104  }
105  }
106  cmd.addString(dynamicSrc.toString().c_str());
107  cmd.addString(dynamicDest.toString().c_str());
108  if (style.persistent) {
109  switch (style.persistenceType) {
111  cmd.addString("from");
112  break;
114  cmd.addString("to");
115  break;
116  default:
117  break;
118  }
119  }
120  bool ok = false;
122  ok = NetworkBase::write(getNameServerContact(), cmd, reply);
123  } else {
124  ContactStyle style;
125  ok = NetworkBase::writeToNameServer(cmd, reply, style);
126  }
127  bool fail = (reply.get(0).toString() == "fail") || !ok;
128  if (fail) {
129  if (!style.quiet) {
130  fprintf(stderr, "Failure: name server did not accept connection to topic.\n");
131  }
132  }
133  return !fail;
134  }
135 
136  bool localOnly() const override
137  {
138  return false;
139  }
140 
141  bool usesCentralServer() const override
142  {
143  return true;
144  }
145 
146  bool serverAllocatesPortNumbers() const override
147  {
148  return true;
149  }
150 
151  bool connectionHasNameOfEndpoints() const override
152  {
153  return true;
154  }
155 
156  virtual Contact detectNameServer(bool useDetectedServer,
157  bool& scanNeeded,
158  bool& serverUsed) override;
159 
160  virtual bool writeToNameServer(PortWriter& cmd,
161  PortReader& reply,
162  const ContactStyle& style) override;
163 
164 private:
165  void* system_resource;
166  Contact contact;
167 };
168 
170 {
171 public:
174  {
175  }
176 
177  bool localOnly() const override
178  {
179  return true;
180  }
181 
182  Contact getNameServerContact() const override
183  {
184  return Contact("/root");
185  }
186 };
187 
188 } // namespace os
189 } // namespace yarp
190 
191 #endif // YARP_OS_YARPNAMESPACE_H
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:170
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:24
PersistenceType persistenceType
Specify kind of persistence to use.
Definition: ContactStyle.h:81
bool quiet
Suppress all outputs and warnings.
Definition: ContactStyle.h:36
std::string carrier
Request that communication be made using a particular carrier.
Definition: ContactStyle.h:53
bool persistent
Specify whether a requested connection should be persistent.
Definition: ContactStyle.h:63
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
void setCarrier(const std::string &carrier)
Set the carrier to use for this Contact.
Definition: Contact.cpp:255
std::string toString() const
Get a textual representation of the Contact.
Definition: Contact.cpp:303
An abstract name space for ports.
Definition: NameSpace.h:23
static NameStore * getQueryBypass()
Definition: Network.cpp:1412
static bool writeToNameServer(PortWriter &cmd, PortReader &reply, const ContactStyle &style)
Variant write method specialized to name server.
Definition: Network.cpp:1983
static bool write(const Contact &contact, PortWriter &cmd, PortReader &reply, bool admin=false, bool quiet=false, double timeout=-1)
Send a single command to a port and await a single response.
Definition: Network.cpp:1226
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:25
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:24
A single value (typically within a Bottle).
Definition: Value.h:45
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:356
Contact getNameServerContact() const override
Get an address for a name server that manages the name space, if available.
bool localOnly() const override
Check if the NameSpace is only valid for the current process ("local").
virtual bool disconnectPortFromTopic(const Contact &src, const Contact &dest, const ContactStyle &style) override
Stop publishing a port to a topic.
Definition: YarpNameSpace.h:59
bool usesCentralServer() const override
Check if a central server is involved in managing the NameSpace.
virtual bool connectTopic(const std::string &dir, bool srcIsTopic, bool destIsTopic, const Contact &src, const Contact &dest, const ContactStyle &style)
Definition: YarpNameSpace.h:87
virtual bool connectPortToPortPersistently(const Contact &src, const Contact &dest, const ContactStyle &style) override
Connect two ports with persistence.
Definition: YarpNameSpace.h:73
bool localOnly() const override
Check if the NameSpace is only valid for the current process ("local").
Contact getNameServerContact() const override
Get an address for a name server that manages the name space, if available.
Definition: YarpNameSpace.h:23
virtual bool disconnectTopicFromPort(const Contact &src, const Contact &dest, const ContactStyle &style) override
Stop subscribing a port to a topic.
Definition: YarpNameSpace.h:66
bool connectionHasNameOfEndpoints() const override
When connections are made involving ports managed by this NameSpace do the ports involved end up know...
virtual bool connectPortToTopic(const Contact &src, const Contact &dest, const ContactStyle &style) override
Publish a port to a topic.
Definition: YarpNameSpace.h:45
virtual bool connectTopicToPort(const Contact &src, const Contact &dest, const ContactStyle &style) override
Subscribe a port to a topic.
Definition: YarpNameSpace.h:52
bool serverAllocatesPortNumbers() const override
Check if a central server is responsible for allocating port numbers, or if this should be left up to...
virtual bool disconnectPortToPortPersistently(const Contact &src, const Contact &dest, const ContactStyle &style) override
Disconnect two ports, removing any persistence.
Definition: YarpNameSpace.h:80
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_UNUSED(var)
Definition: api.h:162
#define YARP_os_API
Definition: api.h:18