41 static constexpr
size_t MAX_ARG_CT =20;
42 static constexpr
size_t MAX_ARG_LEN = 256;
45 const char* argv[MAX_ARG_CT];
46 char buf[MAX_ARG_CT][MAX_ARG_LEN];
52 for (
auto& i : argv) {
57 Params(
const char* command)
67 const char* get(
int idx)
72 void apply(
const char* command)
77 for (i = 0; i < strlen(command) + 1; i++) {
78 if (at < MAX_ARG_CT) {
80 if (ch >= 32 || ch ==
'\0' || ch ==
'\n') {
81 if (ch ==
' ' || ch ==
'\n') {
84 if (sub_at < MAX_ARG_LEN) {
97 for (i = 0; i < MAX_ARG_CT; i++) {
99 buf[i][MAX_ARG_LEN - 1] =
'\0';
108 NameClient::NameClient() :
112 allowSaveScan(false),
114 reportSaveScan(false),
120 NameClient::~NameClient()
122 if (fakeServer !=
nullptr) {
124 fakeServer =
nullptr;
148 size_t i1 = name.find(
':');
149 if (i1 != std::string::npos) {
156 if (altStore !=
nullptr) {
161 std::string q(
"NAME_SERVER query ");
183 if (suggest.
isValid() || !prefix.empty() || typ !=
"*") {
189 if (!suggest.
getHost().empty()) {
192 if (!prefix.empty()) {
194 for (
size_t i = 0; i < ips.
size(); i++) {
196 if (ip.find(prefix) == 0) {
202 cmd.
addString((!prefix.empty()) ? prefix :
"...");
228 std::string cmdOffers =
"set /port offers ";
230 for (
size_t i = 0; i < lst.
size(); i++) {
231 cmdOffers.append(
" ").append(lst.
get(i).
asString());
262 std::string q(
"NAME_SERVER unregister ");
269 std::string result =
send(cmd);
275 Params p(txt.c_str());
278 if (std::string(p.get(0)) ==
"registration") {
279 const char* regName = p.get(2);
280 const char* ip = p.get(4);
281 int port = atoi(p.get(6));
282 const char* carrier = p.get(8);
283 return Contact(regName, carrier, ip, port);
291 if (bot.
size() >= 9) {
304 yCTrace(NAMECLIENT,
"*** OLD YARP command %s", cmd.c_str());
321 bool retried =
false;
333 yCDebug(NAMECLIENT,
"sending to nameserver: %s", cmd.c_str());
336 yCDebug(NAMECLIENT,
"fake mode nameserver");
344 ip = face.
write(server);
349 yCInfo(NAMECLIENT,
"No connection to nameserver");
351 yCInfo(NAMECLIENT,
"*** try running: yarp detect ***");
356 yCInfo(NAMECLIENT,
"no connection to nameserver, scanning mcast");
364 reportSaveScan =
true;
371 ip = face.
write(server);
373 yCError(NAMECLIENT,
"no connection to nameserver, scanning mcast");
380 std::string cmdn = cmd +
"\n";
381 Bytes b((
char*)cmdn.c_str(), cmdn.length());
392 if (line.length() > 1) {
393 if (line[0] ==
'*' || line[0] ==
'[') {
397 result += line +
"\n";
401 yCDebug(NAMECLIENT,
"<<< received from nameserver: %s", result.c_str());
402 }
while (retry && !retried);
416 yCDebug(NAMECLIENT,
"fake mode nameserver");
442 allowSaveScan = allow;
452 return reportSaveScan;
501 if (fakeServer ==
nullptr) {
504 yCAssert(NAMECLIENT, fakeServer !=
nullptr);
508 void NameClient::setup()
510 static std::mutex mutex;
512 if ((!fake) && (!isSetup)) {
514 yCError(NAMECLIENT,
"Cannot find name server");
517 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 getEnvironment(const char *key, bool *found=nullptr)
Read a variable from the environment.
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.