|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
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),
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.
std::string apply(const std::string &txt, const Contact &remote) override
std::string toString() const override
Gives a human-readable textual representation of the bottle.
void clear()
Empties the bottle of any objects it contains.
static NameStore * getQueryBypass()
bool setContact(const yarp::os::Contact &contact)
size_type size() const
Gets the number of elements in the bottle.
bool didSave()
Check whether the name client saved the address of the name server.
The output side of an active connection between two ports.
Abstract interface for a database of port names.
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.
virtual OutputStream & getOutputStream()=0
Access the output stream associated with the connection.
static NameClient & getNameClient()
Get an instance of the name client.
virtual void write(char ch)
Write a single byte to the stream.
Contact queryName(const std::string &name)
Look up the address of a named port.
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a variable from the environment.
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.
virtual ~NameClient()
Destructor.
static bool writeToNameServer(PortWriter &cmd, PortReader &reply, const ContactStyle &style)
Variant write method specialized to name server.
Contact registerName(const std::string &name)
Register a port with a given name.
Contact probe(const std::string &cmd)
Send a message to the name server, and interpret the result as an address.
static yarp::os::Bottle getIpsAsBottle()
NameStore * getQueryBypass()
void fromString(const std::string &text)
Initializes bottle from a string.
int getpid()
Portable wrapper for the getppid() function.
virtual InputStream & getInputStream()=0
Access the input stream associated with the connection.
void queryBypass(NameStore *store)
OutputProtocol * write(const Contact &address) override
Try to reach out and talk to someone.
Contact unregisterName(const std::string &name)
Register disassociation of name from port.
Communicating with a port via TCP.
static int getDefaultPortRange()
Under normal operation, YARP has a name server that manages a pool of (socket) ports starting at a po...
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void setAddress(const Contact &address)
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 ...
Implementation of a YARP2-conforming name server.
virtual std::string asString() const
Get string value.
static NameClient * create()
bool didScan()
Check whether the name client scanned for the address of the name server.
virtual void flush()
Make sure all pending write operations are finished.
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
virtual Contact query(const std::string &name)=0
bool updateAddress()
Force the name client to reread the cached location of the name server.
static Bottle listCarriers()
void addString(const char *str)
Places a string in the bottle, at the end of the list.
A simple abstraction for a block of bytes.
virtual bool isOk() const =0
Check if the connection is valid and can be used.
virtual void close()=0
Negotiate an end to operations.
#define yCAssert(component, x)
bool toFile(bool clean=false)
#define yCError(component,...)
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Client for YARP name server.
#define yCInfo(component,...)
An interface to the operating system, including Port based communication.
#define yCDebug(component,...)
void setSave(bool allow=true)
Control whether the name client can save the address of the name server in a cache file.
bool fromFile(const char *ns=nullptr)
Contact getAddress()
The address of the name server.
yarp::os::Nodes & getNodes()
#define yCTrace(component,...)
static Contact extractAddress(const std::string &txt)
Extract an address from its text representation.
A single value (typically within a Bottle).
Small helper class to help deal with legacy YARP configuration files.
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
void setFakeMode(bool fake=true)
For testing, the nameclient can be set to use a "fake" name server rather than communicating with an ...
void append(const Bottle &alt)
Append the content of the given bottle to the current list.