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
static void handler(int sig)
Definition: RFModule.cpp:244
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
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:39
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:39
void setCarrier(const std::string &carrier)
Set the carrier to use for this Contact.
Definition: Contact.cpp:258
void setHost(const std::string &hostname)
Set the host name to be the input parameter.
Definition: Contact.cpp:236
bool isValid() const
Checks if a Contact is tagged as valid.
Definition: Contact.cpp:301
std::string getRegName() const
Get the name associated with this Contact.
Definition: Contact.cpp:220
void setPort(int port)
Set the port number to be the input parameter.
Definition: Contact.cpp:247
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition: Contact.cpp:242
void setName(const std::string &name)
Set the name associated with this Contact.
Definition: Contact.cpp:225
std::string getCarrier() const
Get the carrier associated with this Contact for socket communication.
Definition: Contact.cpp:253
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition: Contact.cpp:231
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()
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:28
std::string getNamespace(bool refresh=false)
Definition: NameConfig.cpp:440
static std::string getHostName(bool prefer_loopback=false, const std::string &seed="")
Definition: NameConfig.cpp:207
static bool isLocalName(const std::string &name)
Definition: NameConfig.cpp:317
bool fromFile(const char *ns=nullptr)
Definition: NameConfig.cpp:157
std::string getConfigFileName(const char *stem=nullptr, const char *ns=nullptr)
Definition: NameConfig.cpp:120
void setAddress(const Contact &address)
Definition: NameConfig.cpp:429
bool toFile(bool clean=false)
Definition: NameConfig.cpp:170
Stub for a YARP2-conforming name server.
Definition: NameServer.h:36
Classes for constructing name servers.
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.