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