YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
18using namespace yarp::os;
19using namespace yarp::name;
20using namespace yarp::os::impl;
21
29private:
30 FallbackNameServer *fallback;
32public:
33 BootstrapServerAdapter(NameService& owner) : handler(&owner) {
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 {
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
73
74 implementation = new BootstrapServerAdapter(owner);
75 if (implementation==nullptr) {
76 fprintf(stderr,"Cannot allocate ServerAdapter\n");
77 ::exit(1);
78 }
79}
80
82 if (implementation!=nullptr) {
83 delete ((BootstrapServerAdapter*)implementation);
84 implementation = nullptr;
85 }
86
87}
88
90 if (implementation!=nullptr) {
91 return ((BootstrapServerAdapter*)implementation)->start();
92 }
93 return false;
94}
95
97 if (implementation!=nullptr) {
98 return ((BootstrapServerAdapter*)implementation)->stop();
99 }
100 return false;
101}
102
103
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
198 addr.setName("fallback");
199 return addr;
200}
201
202//#endif
Adapt YARP multicast server to use a different NameService.
BootstrapServerAdapter(NameService &owner)
std::string apply(const std::string &txt, const Contact &remote) override
static bool configFileBootstrap(yarp::os::Contact &contact, bool configFileRequired, bool mayEditConfigFile)
BootstrapServer(NameService &owner)
Manage a single connection to the name server.
virtual bool apply(yarp::os::ConnectionReader &reader, yarp::os::ConnectionWriter *writer, bool lock=true)
Abstract interface for a name server operator.
Definition NameService.h:24
A mini-server for performing network communication in the background.
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:33
bool isValid() const
Checks if a Contact is tagged as valid.
Definition Contact.cpp:298
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition Contact.cpp:239
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:23
std::string getNamespace(bool refresh=false)
static std::string getHostName(bool prefer_loopback=false, const std::string &seed="")
static bool isLocalName(const std::string &name)
bool fromFile(const char *ns=nullptr)
std::string getConfigFileName(const char *stem=nullptr, const char *ns=nullptr)
void setAddress(const Contact &address)
bool toFile(bool clean=false)
Stub for a YARP2-conforming name server.
Definition NameServer.h:33
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.