YARP
Yet Another Robot Platform
FallbackNameServer.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 
11 
12 #include <yarp/conf/system.h>
13 
14 #include <yarp/os/Bytes.h>
15 #include <yarp/os/NetType.h>
19 
20 using namespace yarp::os::impl;
21 using namespace yarp::os;
22 
23 namespace {
24 YARP_OS_LOG_COMPONENT(FALLBACKNAMESERVER, "yarp.os.impl.FallbackNameServer")
25 } // namespace
26 
27 
29 {
30  return Contact("fallback",
31  "mcast",
32  "224.2.1.1",
34 }
35 
36 
38 {
39  DgramTwoWayStream send;
40  send.join(getAddress(), true);
41  listen.join(getAddress(), false);
42 
43  yCDebug(FALLBACKNAMESERVER, "Fallback server running");
44  while (listen.isOk() && send.isOk() && !closed) {
45  yCDebug(FALLBACKNAMESERVER, "Fallback server waiting");
46  std::string msg;
47  listen.beginPacket();
48  msg = listen.readLine();
49  listen.endPacket();
50  yCDebug(FALLBACKNAMESERVER, "Fallback server got something");
51  if (listen.isOk() && !closed) {
52  yCDebug(FALLBACKNAMESERVER, "Fallback server got %s", msg.c_str());
53  if (msg.find("NAME_SERVER ") == 0) {
54  Contact addr;
55  std::string result = owner.apply(msg, addr);
56  send.beginPacket();
57  send.writeLine(result.c_str(), (int)result.length());
58  send.flush();
59  send.endPacket();
60  }
61  }
62  }
63 }
64 
65 
67 {
68  closed = true;
69  listen.interrupt();
70 }
yarp::os::impl::DgramTwoWayStream::endPacket
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: DgramTwoWayStream.cpp:947
yarp::os::impl::FallbackNameServer::getAddress
static Contact getAddress()
Definition: FallbackNameServer.cpp:28
yarp::os::impl::DgramTwoWayStream::join
virtual bool join(const Contact &group, bool sender, const Contact &ipLocal)
Definition: DgramTwoWayStream.cpp:486
LogComponent.h
NetType.h
yarp::os::NetworkBase::getDefaultPortRange
static int getDefaultPortRange()
Under normal operation, YARP has a name server that manages a pool of (socket) ports starting at a po...
Definition: Network.cpp:2005
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::impl::DgramTwoWayStream::beginPacket
void beginPacket() override
Mark the beginning of a logical packet.
Definition: DgramTwoWayStream.cpp:941
yarp::os::impl::DgramTwoWayStream
A stream abstraction for datagram communication.
Definition: DgramTwoWayStream.h:40
system.h
NameServer.h
DgramTwoWayStream.h
yarp::os::impl::FallbackNameServer::run
void run() override
Definition: FallbackNameServer.cpp:37
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
Bytes.h
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::impl::FallbackNameServer::close
void close() override
Definition: FallbackNameServer.cpp:66
yarp::os::impl::DgramTwoWayStream::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: DgramTwoWayStream.cpp:926
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