YARP
Yet Another Robot Platform
FallbackNameClient.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <yarp/conf/system.h>
8 
9 #include <yarp/os/NetType.h>
10 #include <yarp/os/Time.h>
16 
17 using namespace yarp::os::impl;
18 using namespace yarp::os;
19 
20 namespace {
21 YARP_OS_LOG_COMPONENT(FALLBACKNAMECLIENT, "yarp.os.impl.FallbackNameClient")
22 } // namespace
23 
25 {
26  NameConfig nc;
28  DgramTwoWayStream send;
29  send.join(call, true);
30  listen.join(call, false);
31  if (!listen.isOk()) {
32  yCError(FALLBACKNAMECLIENT, "Multicast not available");
33  return;
34  }
35  std::string msg = std::string("NAME_SERVER query ") + nc.getNamespace();
36  send.beginPacket();
37  send.writeLine(msg.c_str(), (int)msg.length());
38  send.flush();
39  send.endPacket();
40  for (int i = 0; i < 5; i++) {
41  listen.beginPacket();
42  std::string txt = listen.readLine();
43  listen.endPacket();
44  if (closed) {
45  return;
46  }
47  yCDebug(FALLBACKNAMECLIENT, "Fallback name client got %s", txt.c_str());
48  if (txt.find("registration ") == 0) {
49  address = NameClient::extractAddress(txt);
50  yCInfo(FALLBACKNAMECLIENT, "Received address %s", address.toURI().c_str());
51  return;
52  }
53  }
54 }
55 
56 
58 {
59  if (!closed) {
60  closed = true;
61  listen.interrupt();
62  listen.close();
63  join();
64  }
65 }
66 
67 
69 {
70  return address;
71 }
72 
73 
75 {
76  int tries = 3;
77  for (int k = 0; k < tries; k++) {
78 
79  FallbackNameClient seeker;
80 
81  yCInfo(FALLBACKNAMECLIENT, "Polling for name server (using multicast), try %d of max %d", k + 1, tries);
82 
83  seeker.start();
85  if (seeker.getAddress().isValid()) {
86  return seeker.getAddress();
87  }
88  int len = 20;
89  for (int i0 = 0; i0 < len; i0++) {
90  fprintf(stderr, "++");
91  }
92  fprintf(stderr, "\n");
93 
94  for (int i = 0; i < len; i++) {
96  fprintf(stderr, "++");
97  if (seeker.getAddress().isValid()) {
98  fprintf(stderr, "\n");
99  return seeker.getAddress();
100  }
101  }
102  fprintf(stderr, "\n");
103  yCInfo(FALLBACKNAMECLIENT, "No response to search for server");
104  seeker.close();
105  seeker.join();
106  }
107  return Contact();
108 }
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
bool isValid() const
Checks if a Contact is tagged as valid.
Definition: Contact.cpp:298
virtual void writeLine(const char *data, int len)
Write some text followed by a line feed.
static void delaySystem(double seconds)
Definition: SystemClock.cpp:29
A stream abstraction for datagram communication.
void endPacket() override
Mark the end of a logical packet (see beginPacket).
virtual bool join(const Contact &group, bool sender, const Contact &ipLocal)
void beginPacket() override
Mark the beginning of a logical packet.
void flush() override
Make sure all pending write operations are finished.
A client for the FallbackNameServer class.
static Contact extractAddress(const std::string &txt)
Extract an address from its text representation.
Definition: NameClient.cpp:270
Small helper class to help deal with legacy YARP configuration files.
Definition: NameConfig.h:25
std::string getNamespace(bool refresh=false)
Definition: NameConfig.cpp:441
int join(double seconds=-1)
Definition: ThreadImpl.cpp:120
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCDebug(component,...)
Definition: LogComponent.h:109
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:35
ContainerT::value_type join(typename ContainerT::const_iterator begin, typename ContainerT::const_iterator end, const typename ContainerT::value_type &separator=", ")
Utility to join the elements in a container to a single string separated by a separator.
Definition: string.h:93
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.