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