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>
15 
16 #include <cstdio>
17 
18 using namespace yarp::os;
19 using namespace yarp::os::impl;
20 
21 #define HELPER(x) (*((NameClient*)((x)->system_resource)))
22 
24 {
25  system_resource = NameClient::create();
26  yAssert(system_resource != nullptr);
27  HELPER(this).setContact(contact);
28  this->contact = contact;
29 }
30 
32 {
33  if (system_resource != nullptr) {
34  delete &HELPER(this);
35  system_resource = nullptr;
36  }
37 }
38 
39 Contact YarpNameSpace::queryName(const std::string& name)
40 {
41  NameClient& nic = HELPER(this);
42  return nic.queryName(name);
43 }
44 
45 
46 Contact YarpNameSpace::registerName(const std::string& name)
47 {
48  return registerContact(Contact(name));
49 }
50 
52 {
53  NameClient& nic = HELPER(this);
54  Contact address = nic.registerName(contact.getName(), contact);
55  if (address.isValid()) {
56  NestedContact nc;
57  nc.fromString(address.getRegName());
58  std::string cat = nc.getCategory();
59  if (!nc.getNestedName().empty()) {
60  //bool service = (cat.find("1") != std::string::npos);
61  bool publish = (cat.find('+') != std::string::npos);
62  bool subscribe = (cat.find('-') != std::string::npos);
63  ContactStyle style;
64  Contact c1(nc.getFullName());
65  Contact c2(std::string("topic:/") + nc.getNestedName());
66  if (subscribe) {
68  connectPortToTopic(c2, c1, style);
69  }
70  if (publish) {
72  connectPortToTopic(c1, c2, style);
73  }
74  }
75  }
76  return address;
77 }
78 
79 Contact YarpNameSpace::unregisterName(const std::string& name)
80 {
81  NestedContact nc;
82  nc.fromString(name);
83  std::string cat = nc.getCategory();
84  if (!nc.getNestedName().empty()) {
85  //bool service = (cat.find("1") != std::string::npos);
86  bool publish = (cat.find('+') != std::string::npos);
87  bool subscribe = (cat.find('-') != std::string::npos);
88  ContactStyle style;
89  Contact c1(nc.getFullName());
90  Contact c2(std::string("topic:/") + nc.getNestedName());
91  if (subscribe) {
92  disconnectPortFromTopic(c2, c1, style);
93  }
94  if (publish) {
95  disconnectPortFromTopic(c1, c2, style);
96  }
97  }
98  NameClient& nic = HELPER(this);
99  return nic.unregisterName(name);
100 }
101 
103 {
104  NameClient& nic = HELPER(this);
105  return nic.unregisterName(contact.getName());
106 }
107 
108 
109 bool YarpNameSpace::setProperty(const std::string& name, const std::string& key, const Value& value)
110 {
111  Bottle command;
112  command.addString("bot");
113  command.addString("set");
114  command.addString(name);
115  command.addString(key);
116  command.add(value);
117  Bottle reply;
118  NameClient& nic = HELPER(this);
119  nic.send(command, reply);
120  return reply.size() > 0;
121 }
122 
123 Value* YarpNameSpace::getProperty(const std::string& name, const std::string& key)
124 {
125  Bottle command;
126  command.addString("bot");
127  command.addString("get");
128  command.addString(name);
129  command.addString(key);
130  Bottle reply;
131  NameClient& nic = HELPER(this);
132  nic.send(command, reply);
133  return Value::makeValue(reply.toString());
134 }
135 
137  bool& scanNeeded,
138  bool& serverUsed)
139 {
140  NameConfig nc;
141  NameClient& nic = HELPER(this);
142  nic.setFakeMode(false);
143  nic.updateAddress();
144  nic.setScan();
145  if (useDetectedServer) {
146  nic.setSave();
147  }
148  nic.send("ping", false);
149  scanNeeded = nic.didScan();
150  serverUsed = nic.didSave();
151 
152  Contact c = nic.getAddress();
153  c.setName(nc.getNamespace());
154  //Contact c = nic.getAddress().toContact();
155  // if (scanNeeded) {
156  // Address addr = nic.getAddress();
157  // c.setSocket("tcp", addr.getName().c_str(), addr.getPort());
159  //c.setName(nc.getNamespace().c_str());
160  return c;
161 }
162 
163 
165  PortReader& reply,
166  const ContactStyle& style)
167 {
168  Contact srv = getNameServerContact();
169  std::string cmd0 = "NAME_SERVER";
170 
171  DummyConnector con0;
172  cmd.write(con0.getWriter());
173  Bottle in;
174  in.read(con0.getReader());
175  for (size_t i = 0; i < in.size(); i++) {
176  cmd0 += " ";
177  cmd0 += in.get(i).toString();
178  }
179  NameClient& nic = HELPER(this);
180  std::string result = nic.send(cmd0, true, style);
181  Bottle reply2;
182  reply2.addString(result.c_str());
183  DummyConnector con;
184  reply2.write(con.getWriter());
185  reply.read(con.getReader());
186  return !result.empty();
187 }
yarp::os::DummyConnector
A dummy connection to test yarp::os::Portable implementations.
Definition: DummyConnector.h:38
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:72
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:39
yarp::os::ContactStyle
Definition: ContactStyle.h:26
yarp::os::NestedContact
A placeholder for rich contact information.
Definition: NestedContact.h:26
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:447
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:299
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::DummyConnector::getReader
ConnectionReader & getReader()
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
Definition: DummyConnector.cpp:113
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:21
yarp::os::YarpNameSpace::~YarpNameSpace
virtual ~YarpNameSpace()
Definition: YarpNameSpace.cpp:31
yarp::os::YarpNameSpace::registerName
Contact registerName(const std::string &name) override
Record contact information to tie to a port name.
Definition: YarpNameSpace.cpp:46
yarp::os::PortWriter
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition: PortWriter.h:26
yarp::os::YarpNameSpace::registerContact
Contact registerContact(const Contact &contact) override
Record contact information (should include a port name).
Definition: YarpNameSpace.cpp:51
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:136
yarp::os::impl::NameConfig::getNamespace
std::string getNamespace(bool refresh=false)
Definition: NameConfig.cpp:436
yarp::os::impl::NameClient::unregisterName
Contact unregisterName(const std::string &name)
Register disassociation of name from port.
Definition: NameClient.cpp:257
yarp::os::NestedContact::getCategory
std::string getCategory() const
Definition: NestedContact.cpp:177
yarp::os::ContactStyle::END_WITH_FROM_PORT
@ END_WITH_FROM_PORT
Definition: ContactStyle.h:75
Log.h
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:27
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:432
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:108
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:109
yarp::os::impl::NameClient::didScan
bool didScan()
Check whether the name client scanned for the address of the name server.
Definition: NameClient.cpp:442
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:123
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:452
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:164
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:34
yarp::os::NestedContact::fromString
bool fromString(const std::string &nFullName)
Definition: NestedContact.cpp:142
yarp::os
Definition: AbstractCarrier.h:17
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:437
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:38
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:79
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:46
yarp::os::impl::NameConfig
Small helper class to help deal with legacy YARP configuration files.
Definition: NameConfig.h:27
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::os::impl
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:172
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:422
yarp::os::YarpNameSpace::YarpNameSpace
YarpNameSpace(const Contact &contact)
Definition: YarpNameSpace.cpp:23
yarp::os::YarpNameSpace::unregisterContact
Contact unregisterContact(const Contact &contact) override
Disassociate contact information (should include a port name).
Definition: YarpNameSpace.cpp:102
yarp::os::NestedContact::getFullName
std::string getFullName() const
Definition: NestedContact.cpp:162