38 static constexpr
size_t MAX_ARG_CT =20;
39 static constexpr
size_t MAX_ARG_LEN = 256;
42 const char* argv[MAX_ARG_CT];
43 char buf[MAX_ARG_CT][MAX_ARG_LEN];
49 for (
auto& i : argv) {
54 Params(
const char* command)
64 const char* get(
int idx)
69 void apply(
const char* command)
74 for (i = 0; i < strlen(command) + 1; i++) {
75 if (at < MAX_ARG_CT) {
77 if (ch >= 32 || ch ==
'\0' || ch ==
'\n') {
78 if (ch ==
' ' || ch ==
'\n') {
81 if (sub_at < MAX_ARG_LEN) {
94 for (i = 0; i < MAX_ARG_CT; i++) {
96 buf[i][MAX_ARG_LEN - 1] =
'\0';
105 NameClient::NameClient() :
109 allowSaveScan(false),
111 reportSaveScan(false),
117 NameClient::~NameClient()
119 if (fakeServer !=
nullptr) {
121 fakeServer =
nullptr;
145 size_t i1 = name.find(
':');
146 if (i1 != std::string::npos) {
153 if (altStore !=
nullptr) {
158 std::string q(
"NAME_SERVER query ");
180 if (suggest.
isValid() || !prefix.empty() || typ !=
"*") {
186 if (!suggest.
getHost().empty()) {
189 if (!prefix.empty()) {
191 for (
size_t i = 0; i < ips.
size(); i++) {
193 if (ip.find(prefix) == 0) {
199 cmd.
addString((!prefix.empty()) ? prefix :
"...");
225 std::string cmdOffers =
"set /port offers ";
227 for (
size_t i = 0; i < lst.
size(); i++) {
228 cmdOffers.append(
" ").append(lst.
get(i).
asString());
259 std::string q(
"NAME_SERVER unregister ");
266 std::string result =
send(cmd);
272 Params p(txt.c_str());
275 if (std::string(p.get(0)) ==
"registration") {
276 const char* regName = p.get(2);
277 const char* ip = p.get(4);
278 int port = atoi(p.get(6));
279 const char* carrier = p.get(8);
280 return Contact(regName, carrier, ip, port);
288 if (bot.
size() >= 9) {
301 yCTrace(NAMECLIENT,
"*** OLD YARP command %s", cmd.c_str());
318 bool retried =
false;
330 yCDebug(NAMECLIENT,
"sending to nameserver: %s", cmd.c_str());
333 yCDebug(NAMECLIENT,
"fake mode nameserver");
341 ip = face.
write(server);
346 yCInfo(NAMECLIENT,
"No connection to nameserver");
348 yCInfo(NAMECLIENT,
"*** try running: yarp detect ***");
353 yCInfo(NAMECLIENT,
"no connection to nameserver, scanning mcast");
361 reportSaveScan =
true;
368 ip = face.
write(server);
370 yCError(NAMECLIENT,
"no connection to nameserver, scanning mcast");
377 std::string cmdn = cmd +
"\n";
378 Bytes b((
char*)cmdn.c_str(), cmdn.length());
389 if (line.length() > 1) {
390 if (line[0] ==
'*' || line[0] ==
'[') {
394 result += line +
"\n";
398 yCDebug(NAMECLIENT,
"<<< received from nameserver: %s", result.c_str());
399 }
while (retry && !retried);
413 yCDebug(NAMECLIENT,
"fake mode nameserver");
439 allowSaveScan = allow;
449 return reportSaveScan;
498 if (fakeServer ==
nullptr) {
501 yCAssert(NAMECLIENT, fakeServer !=
nullptr);
505 void NameClient::setup()
507 static std::mutex mutex;
509 if ((!fake) && (!isSetup)) {
511 yCError(NAMECLIENT,
"Cannot find name server");
514 yCDebug(NAMECLIENT,
"name server address is %s", address.
toURI().c_str());
A simple collection of objects that can be described and transmitted in a portable way.
void fromString(const std::string &text)
Initializes bottle from a string.
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void clear()
Empties the bottle of any objects it contains.
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
std::string toString() const override
Gives a human-readable textual representation of the bottle.
A simple abstraction for a block of bytes.
static Bottle listCarriers()
Abstract interface for a database of port names.
virtual Contact query(const std::string &name)=0
static NameStore * getQueryBypass()
static int getDefaultPortRange()
Under normal operation, YARP has a name server that manages a pool of (socket) ports starting at a po...
static bool writeToNameServer(PortWriter &cmd, PortReader &reply, const ContactStyle &style)
Variant write method specialized to name server.
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.
The output side of an active connection between two ports.
virtual OutputStream & getOutputStream()=0
Access the output stream associated with the connection.
virtual InputStream & getInputStream()=0
Access the input stream associated with the connection.
virtual void close()=0
Negotiate an end to operations.
virtual bool isOk() const =0
Check if the connection is valid and can be used.
virtual void flush()
Make sure all pending write operations are finished.
virtual void write(char ch)
Write a single byte to the stream.
A single value (typically within a Bottle).
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual std::string asString() const
Get string value.
Client for YARP name server.
Contact unregisterName(const std::string &name)
Register disassociation of name from port.
static NameClient & getNameClient()
Get an instance of the name client.
bool didScan()
Check whether the name client scanned for the address of the name server.
yarp::os::Nodes & getNodes()
static NameClient * create()
Contact probe(const std::string &cmd)
Send a message to the name server, and interpret the result as an address.
static Contact extractAddress(const std::string &txt)
Extract an address from its text representation.
bool didSave()
Check whether the name client saved the address of the name server.
void queryBypass(NameStore *store)
Contact getAddress()
The address of the name server.
bool updateAddress()
Force the name client to reread the cached location of the name server.
bool setContact(const yarp::os::Contact &contact)
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.
Contact queryName(const std::string &name)
Look up the address of a named port.
NameStore * getQueryBypass()
void setFakeMode(bool fake=true)
For testing, the nameclient can be set to use a "fake" name server rather than communicating with an ...
bool isFakeMode() const
Check whether a fake name server is being used.
void setScan(bool allow=true)
Control whether the name client should scan for the name server if the cached connection information ...
Contact registerName(const std::string &name)
Register a port with a given name.
void setSave(bool allow=true)
Control whether the name client can save the address of the name server in a cache file.
Small helper class to help deal with legacy YARP configuration files.
bool fromFile(const char *ns=nullptr)
static yarp::os::Bottle getIpsAsBottle()
void setAddress(const Contact &address)
bool toFile(bool clean=false)
Implementation of a YARP2-conforming name server.
std::string apply(const std::string &txt, const Contact &remote) override
Communicating with a port via TCP.
OutputProtocol * write(const Contact &address) override
Try to reach out and talk to someone.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCAssert(component, x)
#define yCTrace(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
int getpid()
Portable wrapper for the getppid() function.