YARP
Yet Another Robot Platform
BootstrapServer.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/DummyConnector.h>
16 
18 
20 
21 using namespace yarp::os;
22 using namespace yarp::name;
23 using namespace yarp::os::impl;
24 
32 private:
33  FallbackNameServer *fallback;
35 public:
37  fallback = new FallbackNameServer(*this);
38  if (fallback==nullptr) {
39  fprintf(stderr,"Cannot allocate ServerAdapter\n");
40  ::exit(1);
41  }
42  }
43 
45  if (fallback!=nullptr) delete fallback;
46  fallback = nullptr;
47  }
48 
49  std::string apply(const std::string& txt, const Contact& remote) override {
50  DummyConnector con, con2;
51  con.setTextMode(true);
52  ConnectionWriter& writer = con.getWriter();
53  writer.appendText(txt.c_str());
54  bool ok = handler.apply(con.getReader(),&(con2.getWriter()));
55  std::string result;
56  if (ok) {
57  result = con2.getReader().expectText();
58  }
59  printf("ASKED %s, GAVE %s\n", txt.c_str(), result.c_str());
60  return result;
61  }
62 
63  bool start() {
64  return fallback->start();
65  }
66 
67  bool stop() {
68  fallback->close();
69  return true;
70  }
71 };
72 
73 BootstrapServer::BootstrapServer(NameService& owner) {
74 
76  if (implementation==nullptr) {
77  fprintf(stderr,"Cannot allocate ServerAdapter\n");
78  ::exit(1);
79  }
80 }
81 
82 BootstrapServer::~BootstrapServer() {
83  if (implementation!=nullptr) {
85  implementation = nullptr;
86  }
87 
88 }
89 
90 bool BootstrapServer::start() {
91  if (implementation!=nullptr) {
92  return ((BootstrapServerAdapter*)implementation)->start();
93  }
94  return false;
95 }
96 
97 bool BootstrapServer::stop() {
98  if (implementation!=nullptr) {
99  return ((BootstrapServerAdapter*)implementation)->stop();
100  }
101  return false;
102 }
103 
104 
105 bool BootstrapServer::configFileBootstrap(yarp::os::Contact& contact,
106  bool configFileRequired,
107  bool mayEditConfigFile) {
108  Contact suggest = contact;
109 
110  // see what address is lying around
111  Contact prev;
112  NameConfig conf;
113  if (conf.fromFile()) {
114  prev = conf.getAddress();
115  } else if (configFileRequired) {
116  fprintf(stderr,"Could not read configuration file %s\n",
117  conf.getConfigFileName().c_str());
118  return false;
119  }
120 
121  // merge
122  if (prev.isValid()) {
123  if (suggest.getHost() == "...") {
124  suggest.setHost(prev.getHost());
125  }
126  if (suggest.getCarrier() == "...") {
127  suggest.setCarrier(prev.getCarrier());
128  }
129  if (suggest.getPort() == 0) {
130  suggest.setPort(prev.getPort());
131  }
132  }
133 
134  if (suggest.getRegName() == "...") {
135  suggest.setName(conf.getNamespace());
136  }
137 
138  // still something not set?
139  if (suggest.getPort() == 0) {
140  suggest.setPort(10000);
141  }
142  if (suggest.getHost() == "...") {
143  // should get my IP
144  suggest.setHost(conf.getHostName());
145  }
146 
147  if (!configFileRequired) {
148  // finally, should make sure IP is local, and if not, correct it
149  if (!conf.isLocalName(suggest.getHost())) {
150  fprintf(stderr,"Overriding non-local address for name server\n");
151  suggest.setHost(conf.getHostName());
152  } else {
153  // Let's just check we're not a loopback
154  std::string betterHost = conf.getHostName(false,suggest.getHost());
155  if (betterHost!=suggest.getHost()) {
156  fprintf(stderr,"Overriding loopback address for name server\n");
157  suggest.setHost(betterHost);
158  }
159  }
160  }
161 
162  bool changed = false;
163  if (prev.isValid()) {
164  changed = (prev.getHost() != suggest.getHost()) ||
165  (prev.getPort() != suggest.getPort()) ||
166  (conf.getMode() != "yarp" && conf.getMode() != "");
167  }
168  if (changed && !mayEditConfigFile) {
169  fprintf(stderr,"PROBLEM: need to change settings in %s\n",
170  conf.getConfigFileName().c_str());
171  fprintf(stderr," Current settings: host %s port %d family %s\n",
172  prev.getHost().c_str(), prev.getPort(),
173  (conf.getMode()=="")?"yarp":conf.getMode().c_str());
174  fprintf(stderr," Desired settings: host %s port %d family %s\n",
175  suggest.getHost().c_str(), suggest.getPort(), "yarp");
176  fprintf(stderr,"Please specify '--write' if it is ok to overwrite current settings, or\n");
177  fprintf(stderr,"Please specify '--read' to use the current settings, or\n");
178  fprintf(stderr,"delete %s\n", conf.getConfigFileName().c_str());
179  return false;
180  }
181  bool shouldSave = changed || !prev.isValid();
182 
183  if (shouldSave) {
184  // and save
185  conf.setAddress(suggest);
186  if (!conf.toFile()) {
187  fprintf(stderr,"Could not save configuration file %s\n",
188  conf.getConfigFileName().c_str());
189  }
190  }
191 
192  contact = suggest;
193  return true;
194 }
195 
196 
197 Contact BootstrapServer::where() {
198  Contact addr = FallbackNameServer::getAddress();
199  addr.setName("fallback");
200  return addr;
201 }
202 
203 //#endif
yarp::os::DummyConnector
A dummy connection to test yarp::os::Portable implementations.
Definition: DummyConnector.h:35
yarp::os::impl::NameConfig::getConfigFileName
std::string getConfigFileName(const char *stem=nullptr, const char *ns=nullptr)
Definition: NameConfig.cpp:120
yarp::os::impl::ThreadImpl::start
virtual bool start()
Definition: ThreadImpl.cpp:187
yarp::os::impl::NameConfig::isLocalName
static bool isLocalName(const std::string &name)
Definition: NameConfig.cpp:317
BootstrapServerAdapter::~BootstrapServerAdapter
virtual ~BootstrapServerAdapter()
Definition: BootstrapServer.cpp:44
yarp::os::Contact::setName
void setName(const std::string &name)
Set the name associated with this Contact.
Definition: Contact.cpp:225
yarp::os::impl::NameConfig::getHostName
static std::string getHostName(bool prefer_loopback=false, const std::string &seed="")
Definition: NameConfig.cpp:207
yarp::os::impl::FallbackNameServer
Multi-cast server, for last resort information sharing about name information – when config files are...
Definition: FallbackNameServer.h:28
yarp::os::DummyConnector::getReader
ConnectionReader & getReader(ConnectionWriter *replyWriter=nullptr)
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
Definition: DummyConnector.cpp:117
yarp::os::Contact::getRegName
std::string getRegName() const
Get the name associated with this Contact.
Definition: Contact.cpp:220
yarp::os::impl::NameConfig::getNamespace
std::string getNamespace(bool refresh=false)
Definition: NameConfig.cpp:440
BootstrapServerAdapter::stop
bool stop()
Definition: BootstrapServer.cpp:67
yarp::os::impl::NameConfig::getAddress
Contact getAddress()
Definition: NameConfig.cpp:185
BootstrapServerAdapter::apply
std::string apply(const std::string &txt, const Contact &remote) override
Definition: BootstrapServer.cpp:49
BootstrapServerAdapter
Adapt YARP multicast server to use a different NameService.
Definition: BootstrapServer.cpp:31
BootstrapServer.h
yarp::os::impl::NameConfig::setAddress
void setAddress(const Contact &address)
Definition: NameConfig.cpp:429
yarp::os::Contact::getPort
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition: Contact.cpp:242
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
FallbackNameServer.h
handler
static void handler(int sig)
Definition: RFModule.cpp:244
DummyConnector.h
yarp::os::DummyConnector::getWriter
ConnectionWriter & getWriter()
Get the dummy ConnectionWriter loaded with whatever was written the ConnectionWriter since it was las...
Definition: DummyConnector.cpp:112
yarp::os::Contact::getCarrier
std::string getCarrier() const
Get the carrier associated with this Contact for socket communication.
Definition: Contact.cpp:253
yarp::os::Contact::setPort
void setPort(int port)
Set the port number to be the input parameter.
Definition: Contact.cpp:247
NameConfig.h
yarp::os::Contact::setCarrier
void setCarrier(const std::string &carrier)
Set the carrier to use for this Contact.
Definition: Contact.cpp:258
system.h
NameServer.h
BootstrapServerAdapter::start
bool start()
Definition: BootstrapServer.cpp:63
yarp::os::impl::NameConfig::toFile
bool toFile(bool clean=false)
Definition: NameConfig.cpp:170
yarp::os::impl::NameConfig::getMode
std::string getMode()
Definition: NameConfig.h:58
yarp::name::NameService
Abstract interface for a name server operator.
Definition: NameService.h:39
yarp::name
Classes for constructing name servers.
Definition: BootstrapServer.h:18
BootstrapServerAdapter::BootstrapServerAdapter
BootstrapServerAdapter(NameService &owner)
Definition: BootstrapServer.cpp:36
yarp::os::DummyConnector::setTextMode
void setTextMode(bool textmode)
Set the textMode of the dummy connection.
Definition: DummyConnector.cpp:102
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::ConnectionReader::expectText
virtual std::string expectText(const char terminatingChar='\n')=0
Read some text from the network connection.
yarp::os::Contact::isValid
bool isValid() const
Checks if a Contact is tagged as valid.
Definition: Contact.cpp:301
yarp::os::Contact::getHost
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition: Contact.cpp:231
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::impl::NameConfig::fromFile
bool fromFile(const char *ns=nullptr)
Definition: NameConfig.cpp:157
implementation
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
yarp::name::NameServerConnectionHandler
Manage a single connection to the name server.
Definition: NameServerConnectionHandler.h:40
yarp::os::impl::NameServerStub
Stub for a YARP2-conforming name server.
Definition: NameServer.h:36
yarp::os::impl::FallbackNameServer::close
void close() override
Definition: FallbackNameServer.cpp:66
NameServerConnectionHandler.h
yarp::os::Contact::setHost
void setHost(const std::string &hostname)
Set the host name to be the input parameter.
Definition: Contact.cpp:236
yarp::os::impl::NameConfig
Small helper class to help deal with legacy YARP configuration files.
Definition: NameConfig.h:28
yarp::os::impl
The components from which ports and connections are built.
yarp::os::ConnectionWriter::appendText
virtual void appendText(const std::string &str, const char terminate='\n')=0
Send a terminated string to the network connection.