YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
13namespace yarp::os {
14
16{
17public:
18 YarpNameSpace(const Contact& contact);
19
20 virtual ~YarpNameSpace();
21
23 {
24 return contact;
25 }
26
27 Contact queryName(const std::string& name) override;
28
29 Contact registerName(const std::string& name) override;
30
31 Contact registerContact(const Contact& contact) override;
32
33 Contact unregisterName(const std::string& name) override;
34
35 Contact unregisterContact(const Contact& contact) override;
36
37 virtual bool setProperty(const std::string& name,
38 const std::string& key,
39 const Value& value) override;
40
41 virtual Value* getProperty(const std::string& name,
42 const std::string& key) override;
43
44 virtual bool connectPortToTopic(const Contact& src,
45 const Contact& dest,
46 const ContactStyle& style) override
47 {
48 return connectTopic("subscribe", false, true, src, dest, style);
49 }
50
51 virtual bool connectTopicToPort(const Contact& src,
52 const Contact& dest,
53 const ContactStyle& style) override
54 {
55 return connectTopic("subscribe", true, false, src, dest, style);
56 }
57
58 virtual bool disconnectPortFromTopic(const Contact& src,
59 const Contact& dest,
60 const ContactStyle& style) override
61 {
62 return connectTopic("unsubscribe", false, true, src, dest, style);
63 }
64
65 virtual bool disconnectTopicFromPort(const Contact& src,
66 const Contact& dest,
67 const ContactStyle& style) override
68 {
69 return connectTopic("unsubscribe", true, false, src, dest, style);
70 }
71
72 virtual bool connectPortToPortPersistently(const Contact& src,
73 const Contact& dest,
74 const ContactStyle& style) override
75 {
76 return connectTopic("subscribe", false, false, src, dest, style);
77 }
78
80 const Contact& dest,
81 const ContactStyle& style) override
82 {
83 return connectTopic("unsubscribe", false, false, src, dest, style);
84 }
85
86 virtual bool connectTopic(const std::string& dir,
87 bool srcIsTopic,
88 bool destIsTopic,
89 const Contact& src,
90 const Contact& dest,
91 const ContactStyle& style)
92 {
94 Contact dynamicSrc = src;
95 Contact dynamicDest = dest;
96 Bottle cmd, reply;
97 cmd.addString(dir.c_str());
98 if (style.carrier != "") {
99 if (!destIsTopic) {
100 dynamicDest.setCarrier(style.carrier);
101 } else {
102 dynamicSrc.setCarrier(style.carrier);
103 }
104 }
105 cmd.addString(dynamicSrc.toString().c_str());
106 cmd.addString(dynamicDest.toString().c_str());
107 if (style.persistent) {
108 switch (style.persistenceType) {
109 case ContactStyle::END_WITH_FROM_PORT:
110 cmd.addString("from");
111 break;
112 case ContactStyle::END_WITH_TO_PORT:
113 cmd.addString("to");
114 break;
115 default:
116 break;
117 }
118 }
119 bool ok = false;
120 if (!NetworkBase::getQueryBypass()) {
121 ok = NetworkBase::write(getNameServerContact(), cmd, reply);
122 } else {
123 ContactStyle style;
124 ok = NetworkBase::writeToNameServer(cmd, reply, style);
125 }
126 bool fail = (reply.get(0).toString() == "fail") || !ok;
127 if (fail) {
128 if (!style.quiet) {
129 fprintf(stderr, "Failure: name server did not accept connection to topic.\n");
130 }
131 }
132 return !fail;
133 }
134
135 bool localOnly() const override
136 {
137 return false;
138 }
139
140 bool usesCentralServer() const override
141 {
142 return true;
143 }
144
145 bool serverAllocatesPortNumbers() const override
146 {
147 return true;
148 }
149
150 bool connectionHasNameOfEndpoints() const override
151 {
152 return true;
153 }
154
155 virtual Contact detectNameServer(bool useDetectedServer,
156 bool& scanNeeded,
157 bool& serverUsed) override;
158
159 virtual bool writeToNameServer(PortWriter& cmd,
160 PortReader& reply,
161 const ContactStyle& style) override;
162
163private:
164 void* system_resource;
165 Contact contact;
166};
167
169{
170public:
175
176 bool localOnly() const override
177 {
178 return true;
179 }
180
182 {
183 return Contact("/root");
184 }
185};
186
187} // namespace yarp::os
188
189#endif // YARP_OS_YARPNAMESPACE_H
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
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
A mini-server for performing network communication in the background.
Preferences for how to communicate with a contact.
PersistenceType persistenceType
Specify kind of persistence to use.
bool quiet
Suppress all outputs and warnings.
std::string carrier
Request that communication be made using a particular carrier.
bool persistent
Specify whether a requested connection should be persistent.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
An abstract name space for ports.
Definition NameSpace.h:22
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition PortWriter.h:23
A single value (typically within a Bottle).
Definition Value.h:43
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.
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)
virtual bool connectPortToPortPersistently(const Contact &src, const Contact &dest, const ContactStyle &style) override
Connect two ports with persistence.
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.
virtual bool disconnectTopicFromPort(const Contact &src, const Contact &dest, const ContactStyle &style) override
Stop subscribing a port to a topic.
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.
virtual bool connectTopicToPort(const Contact &src, const Contact &dest, const ContactStyle &style) override
Subscribe a port to a topic.
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.
An interface to the operating system, including Port based communication.
#define YARP_UNUSED(var)
Definition api.h:162
#define YARP_os_API
Definition api.h:18