YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
17using namespace yarp::os::impl;
18using namespace yarp::os;
19
20namespace {
21YARP_OS_LOG_COMPONENT(FALLBACKNAMECLIENT, "yarp.os.impl.FallbackNameClient")
22} // namespace
23
25{
26 NameConfig nc;
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
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}
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
std::string toURI(bool includeCarrier=true) const
Get a representation of the Contact as a URI.
Definition Contact.cpp:313
std::string readLine(const char terminal='\n', bool *success=nullptr)
Read a block of text terminated with a specific marker (or EOF).
virtual void writeLine(const char *data, int len)
Write some text followed by a line feed.
static void delaySystem(double seconds)
A stream abstraction for datagram communication.
void interrupt() override
Interrupt the stream.
void close() override
Terminate the stream.
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.
bool isOk() const override
Check if the stream is ok or in an error state.
A client for the FallbackNameServer class.
static Contact extractAddress(const std::string &txt)
Extract an address from its text representation.
Small helper class to help deal with legacy YARP configuration files.
Definition NameConfig.h:23
std::string getNamespace(bool refresh=false)
int join(double seconds=-1)
#define yCInfo(component,...)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.