YARP
Yet Another Robot Platform
YarpNameSpace.cpp
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 
10 
11 #include <yarp/os/DummyConnector.h>
12 #include <yarp/os/Log.h>
16 
17 #include <cstdio>
18 
19 using namespace yarp::os;
20 using namespace yarp::os::impl;
21 
22 #define HELPER(x) (*((NameClient*)((x)->system_resource)))
23 
24 namespace {
25 YARP_OS_LOG_COMPONENT(YARPNAMESPACE, "yarp.os.YarpNameSpace")
26 }
27 
29 {
30  system_resource = NameClient::create();
31  yAssert(system_resource != nullptr);
32  HELPER(this).setContact(contact);
33  this->contact = contact;
34 }
35 
37 {
38  if (system_resource != nullptr) {
39  delete &HELPER(this);
40  system_resource = nullptr;
41  }
42 }
43 
44 Contact YarpNameSpace::queryName(const std::string& name)
45 {
46  NameClient& nic = HELPER(this);
47  return nic.queryName(name);
48 }
49 
50 
51 Contact YarpNameSpace::registerName(const std::string& name)
52 {
53  return registerContact(Contact(name));
54 }
55 
57 {
58  NameClient& nic = HELPER(this);
59  yCDebug(YARPNAMESPACE, "Registering contact: %s", contact.toURI().c_str());
60  Contact address = nic.registerName(contact.getName(), contact);
61  yCDebug(YARPNAMESPACE, "Registered address: %s", address.toURI().c_str());
62 
63  if (address.isValid()) {
64  NestedContact nc;
65  nc.fromString(address.getRegName());
66  std::string cat = nc.getCategory();
67  if (!nc.getNestedName().empty()) {
68  //bool service = (cat.find("1") != std::string::npos);
69  bool publish = (cat.find('+') != std::string::npos);
70  bool subscribe = (cat.find('-') != std::string::npos);
71  ContactStyle style;
72  Contact c1(nc.getFullName());
73  Contact c2(std::string("topic:/") + nc.getNestedName());
74  if (subscribe) {
76  connectPortToTopic(c2, c1, style);
77  }
78  if (publish) {
80  connectPortToTopic(c1, c2, style);
81  }
82  }
83  }
84  return address;
85 }
86 
87 Contact YarpNameSpace::unregisterName(const std::string& name)
88 {
89  NestedContact nc;
90  nc.fromString(name);
91  std::string cat = nc.getCategory();
92  if (!nc.getNestedName().empty()) {
93  //bool service = (cat.find("1") != std::string::npos);
94  bool publish = (cat.find('+') != std::string::npos);
95  bool subscribe = (cat.find('-') != std::string::npos);
96  ContactStyle style;
97  Contact c1(nc.getFullName());
98  Contact c2(std::string("topic:/") + nc.getNestedName());
99  if (subscribe) {
100  disconnectPortFromTopic(c2, c1, style);
101  }
102  if (publish) {
103  disconnectPortFromTopic(c1, c2, style);
104  }
105  }
106  NameClient& nic = HELPER(this);
107  return nic.unregisterName(name);
108 }
109 
111 {
112  NameClient& nic = HELPER(this);
113  return nic.unregisterName(contact.getName());
114 }
115 
116 
117 bool YarpNameSpace::setProperty(const std::string& name, const std::string& key, const Value& value)
118 {
119  Bottle command;
120  command.addString("bot");
121  command.addString("set");
122  command.addString(name);
123  command.addString(key);
124  command.add(value);
125  Bottle reply;
126  NameClient& nic = HELPER(this);
127  nic.send(command, reply);
128  return reply.size() > 0;
129 }
130 
131 Value* YarpNameSpace::getProperty(const std::string& name, const std::string& key)
132 {
133  Bottle command;
134  command.addString("bot");
135  command.addString("get");
136  command.addString(name);
137  command.addString(key);
138  Bottle reply;
139  NameClient& nic = HELPER(this);
140  nic.send(command, reply);
141  return Value::makeValue(reply.toString());
142 }
143 
145  bool& scanNeeded,
146  bool& serverUsed)
147 {
148  NameConfig nc;
149  NameClient& nic = HELPER(this);
150  nic.setFakeMode(false);
151  nic.updateAddress();
152  nic.setScan();
153  if (useDetectedServer) {
154  nic.setSave();
155  }
156  nic.send("ping", false);
157  scanNeeded = nic.didScan();
158  serverUsed = nic.didSave();
159 
160  Contact c = nic.getAddress();
161  c.setName(nc.getNamespace());
162  //Contact c = nic.getAddress().toContact();
163  // if (scanNeeded) {
164  // Address addr = nic.getAddress();
165  // c.setSocket("tcp", addr.getName().c_str(), addr.getPort());
167  //c.setName(nc.getNamespace().c_str());
168  return c;
169 }
170 
171 
173  PortReader& reply,
174  const ContactStyle& style)
175 {
176  Contact srv = getNameServerContact();
177  std::string cmd0 = "NAME_SERVER";
178 
179  DummyConnector con0;
180  cmd.write(con0.getWriter());
181  Bottle in;
182  in.read(con0.getReader());
183  for (size_t i = 0; i < in.size(); i++) {
184  cmd0 += " ";
185  cmd0 += in.get(i).toString();
186  }
187  NameClient& nic = HELPER(this);
188  std::string result = nic.send(cmd0, true, style);
189  Bottle reply2;
190  reply2.addString(result.c_str());
191  DummyConnector con;
192  reply2.write(con.getWriter());
193  reply.read(con.getReader());
194  return !result.empty();
195 }
yarp::os::DummyConnector
A dummy connection to test yarp::os::Portable implementations.
Definition: DummyConnector.h:35
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
yarp::os::PortReader::read
virtual bool read(ConnectionReader &reader)=0
Read this object from a network connection.
yarp::os::YarpNameSpace::queryName
Contact queryName(const std::string &name) override
Map from port name to contact information.
Definition: YarpNameSpace.cpp:44
yarp::os::ContactStyle
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:27
yarp::os::NestedContact
A placeholder for rich contact information.
Definition: NestedContact.h:27
yarp::os::Bottle::size
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
yarp::os::impl::NameClient::didSave
bool didSave()
Check whether the name client saved the address of the name server.
Definition: NameClient.cpp:450
yarp::os::impl::NameClient::send
std::string send(const std::string &cmd, bool multi=true, const ContactStyle &style=ContactStyle())
Send a text message to the nameserver, and return the result.
Definition: NameClient.cpp:302
yarp::os::Contact::getName
std::string getName() const
Get the name associated with this Contact.
Definition: Contact.cpp:208
yarp::os::impl::NameClient::queryName
Contact queryName(const std::string &name)
Look up the address of a named port.
Definition: NameClient.cpp:146
yarp::os::impl::NameClient::registerName
Contact registerName(const std::string &name)
Register a port with a given name.
Definition: NameClient.cpp:166
yarp::os::Contact::setName
void setName(const std::string &name)
Set the name associated with this Contact.
Definition: Contact.cpp:225
yarp::os::ContactStyle::persistenceType
PersistenceType persistenceType
Specify kind of persistence to use.
Definition: ContactStyle.h:84
HELPER
#define HELPER(x)
Definition: YarpNameSpace.cpp:22
yarp::os::YarpNameSpace::~YarpNameSpace
virtual ~YarpNameSpace()
Definition: YarpNameSpace.cpp:36
yarp::os::YarpNameSpace::registerName
Contact registerName(const std::string &name) override
Record contact information to tie to a port name.
Definition: YarpNameSpace.cpp:51
LogComponent.h
yarp::os::DummyConnector::getReader
ConnectionReader & getReader(ConnectionWriter *replyWriter=nullptr)
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
Definition: DummyConnector.cpp:117
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::registerContact
Contact registerContact(const Contact &contact) override
Record contact information (should include a port name).
Definition: YarpNameSpace.cpp:56
yarp::os::Contact::getRegName
std::string getRegName() const
Get the name associated with this Contact.
Definition: Contact.cpp:220
yarp::os::YarpNameSpace::detectNameServer
virtual Contact detectNameServer(bool useDetectedServer, bool &scanNeeded, bool &serverUsed) override
Find a name server for this NameSpace, if applicable.
Definition: YarpNameSpace.cpp:144
yarp::os::impl::NameConfig::getNamespace
std::string getNamespace(bool refresh=false)
Definition: NameConfig.cpp:440
yarp::os::impl::NameClient::unregisterName
Contact unregisterName(const std::string &name)
Register disassociation of name from port.
Definition: NameClient.cpp:260
yarp::os::NestedContact::getCategory
std::string getCategory() const
Definition: NestedContact.cpp:194
yarp::os::ContactStyle::END_WITH_FROM_PORT
@ END_WITH_FROM_PORT
Definition: ContactStyle.h:75
yarp::os::Contact::toURI
std::string toURI(bool includeCarrier=true) const
Get a representation of the Contact as a URI.
Definition: Contact.cpp:316
Log.h
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::Bottle::write
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:233
yarp::os::impl::NameClient::setScan
void setScan(bool allow=true)
Control whether the name client should scan for the name server if the cached connection information ...
Definition: NameClient.cpp:435
DummyConnector.h
yarp::os::DummyConnector::getWriter
ConnectionWriter & getWriter()
Get the dummy ConnectionWriter loaded with whatever was written the ConnectionWriter since it was las...
Definition: DummyConnector.cpp:112
yarp::os::impl::NameClient::create
static NameClient * create()
Definition: NameClient.cpp:134
yarp::os::YarpNameSpace::setProperty
virtual bool setProperty(const std::string &name, const std::string &key, const Value &value) override
Associate a key/value pair with a named port.
Definition: YarpNameSpace.cpp:117
yarp::os::impl::NameClient::didScan
bool didScan()
Check whether the name client scanned for the address of the name server.
Definition: NameClient.cpp:445
yarp::os::YarpNameSpace::getProperty
virtual Value * getProperty(const std::string &name, const std::string &key) override
Get the value of a named key from a named port.
Definition: YarpNameSpace.cpp:131
NameClient.h
yarp::os::impl::NameClient::updateAddress
bool updateAddress()
Force the name client to reread the cached location of the name server.
Definition: NameClient.cpp:455
NameConfig.h
yarp::os::PortWriter::write
virtual bool write(ConnectionWriter &writer) const =0
Write this object to a network connection.
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::writeToNameServer
virtual bool writeToNameServer(PortWriter &cmd, PortReader &reply, const ContactStyle &style) override
Write a message to a name server for this NameSpace, if applicable.
Definition: YarpNameSpace.cpp:172
yarp::os::Value::makeValue
static Value * makeValue(const std::string &txt)
Create a Value from a text description.
Definition: Value.cpp:465
yarp::os::impl::NameClient
Client for YARP name server.
Definition: NameClient.h:35
yarp::os::NestedContact::fromString
bool fromString(const std::string &nFullName)
Definition: NestedContact.cpp:156
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
yarp::os::Contact::isValid
bool isValid() const
Checks if a Contact is tagged as valid.
Definition: Contact.cpp:301
cat
void cat(Vector &a, const Vector &b)
Definition: JoypadControlServer.cpp:29
yarp::os::impl::NameClient::setSave
void setSave(bool allow=true)
Control whether the name client can save the address of the name server in a cache file.
Definition: NameClient.cpp:440
yarp::os::Bottle::read
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:243
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::impl::NameClient::getAddress
Contact getAddress()
The address of the name server.
Definition: NameClient.cpp:140
yarp::os::YarpNameSpace::unregisterName
Contact unregisterName(const std::string &name) override
Disassociate contact information from a port name.
Definition: YarpNameSpace.cpp:87
yarp::os::Bottle::add
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition: Bottle.cpp:339
yarp::os::Value::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:359
YarpNameSpace.h
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::os::impl::NameConfig
Small helper class to help deal with legacy YARP configuration files.
Definition: NameConfig.h:28
yAssert
#define yAssert(x)
Definition: Log.h:297
YARP_OS_LOG_COMPONENT
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
yarp::os::impl
The components from which ports and connections are built.
yarp::os::ContactStyle::END_WITH_TO_PORT
@ END_WITH_TO_PORT
Definition: ContactStyle.h:76
yarp::os::NestedContact::getNestedName
std::string getNestedName() const
Definition: NestedContact.cpp:189
yarp::os::impl::NameClient::setFakeMode
void setFakeMode(bool fake=true)
For testing, the nameclient can be set to use a "fake" name server rather than communicating with an ...
Definition: NameClient.cpp:425
yarp::os::YarpNameSpace::YarpNameSpace
YarpNameSpace(const Contact &contact)
Definition: YarpNameSpace.cpp:28
yarp::os::YarpNameSpace::unregisterContact
Contact unregisterContact(const Contact &contact) override
Disassociate contact information (should include a port name).
Definition: YarpNameSpace.cpp:110
yarp::os::NestedContact::getFullName
std::string getFullName() const
Definition: NestedContact.cpp:179