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