YARP
Yet Another Robot Platform
NameServerContainer.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 
8 
10 
11 #include <yarp/os/Carriers.h>
12 #include <yarp/os/Network.h>
13 #include <yarp/os/RosNameSpace.h>
14 #include <yarp/os/Value.h>
17 
18 #include <string>
19 
20 // For a yarp server running on the port 10000
21 // * the first port assigned will be 10002
22 // * the last port assigned will be 19999
23 #define MIN_PORT_NUMBER_OFFSET 2
24 #define MAX_PORT_NUMBER_OFFSET 9999
25 
26 
27 using yarp::os::Contact;
31 using yarp::os::Network;
32 using yarp::os::Value;
33 using yarp::os::Carriers;
39 
41 
42 namespace {
43 YARP_SERVERSQL_LOG_COMPONENT(NAMESERVERCONTAINER, "yarp.serversql.impl.NameServerContainer")
44 } // namespace
45 
46 
47 NameServerContainer::~NameServerContainer()
48 {
49  subscriber.clear();
50  if (space) {
51  delete space;
52  space = nullptr;
53  }
54 }
55 
57 {
58  return contact;
59 }
60 
62 {
63  if (!space) {
64  return Contact();
65  }
66  return space->getNameServerContact();
67 }
68 
70 {
71  Network::registerContact(c);
72  subscriber.welcome(c.getName(),1);
73 }
74 
76 {
77  std::string dbDefault = ":memory:";
78  std::string subdbDefault = ":memory:";
79 
80  if (options.check("memory")) {
81  yCWarning(NAMESERVERCONTAINER, "The --memory option was given, but that is now a default. Continuing.");
82  }
83 
84  std::string dbFilename = options.check("portdb",
85  Value(dbDefault)).asString();
86  std::string subdbFilename = options.check("subdb",
87  Value(subdbDefault)).asString();
88 
89  std::string ip = options.check("ip",Value("...")).asString();
90  int sock = options.check("socket",Value(Network::getDefaultPortRange())).asInt32();
91  bool cautious = options.check("cautious");
92 
93  yCInfo(NAMESERVERCONTAINER, "Using port database: %s", dbFilename.c_str());
94  yCInfo(NAMESERVERCONTAINER, "Using subscription database: %s", subdbFilename.c_str());
95  if (dbFilename!=":memory:" || subdbFilename!=":memory:") {
96  yCInfo(NAMESERVERCONTAINER, "If you ever need to clear the name server's state, just delete those files.");
97  }
98  yCInfo(NAMESERVERCONTAINER, "IP address: %s", (ip=="...")?"default":ip.c_str());
99  yCInfo(NAMESERVERCONTAINER, "Port number: %d", sock);
100 
101  bool reset = false;
102  if (options.check("ip")||options.check("socket")) {
103  yCError(NAMESERVERCONTAINER, "Database needs to be reset, IP or port number set.");
104  reset = true;
105  }
106 
107  TripleSource *pmem = db.open(dbFilename.c_str(),cautious,reset);
108  if (pmem == nullptr) {
109  yCError(NAMESERVERCONTAINER, "Aborting, ports database failed to open.");
110  return false;
111  }
112 
113  if (!subscriber.open(subdbFilename)) {
114  yCError(NAMESERVERCONTAINER, "Aborting, subscription database failed to open.");
115  return false;
116  }
117 
118  contact = Contact("...", "tcp", ip, sock);
119 
120  if (!options.check("local")) {
121  if (!BootstrapServer::configFileBootstrap(contact,
122  options.check("read"),
123  options.check("write"))) {
124  yCError(NAMESERVERCONTAINER, "Aborting.\n");
125  return false;
126  }
127  }
128 
129  if (options.check("ros") || yarp::conf::environment::get_string("YARP_USE_ROS")!="") {
131  std::string lstStr(lst.toString());
132  if (lstStr.find("rossrv") == std::string::npos ||
133  lstStr.find("tcpros") == std::string::npos ||
134  lstStr.find("xmlrpc") == std::string::npos) {
135  yCError(NAMESERVERCONTAINER, "Missing one or more required carriers ");
136  yCError(NAMESERVERCONTAINER, "for yarpserver --ros (rossrv, tcpros, xmlrpc).\n");
137  yCError(NAMESERVERCONTAINER, "Run 'yarp connect --list-carriers' to see carriers on your machine\n");
138  yCError(NAMESERVERCONTAINER, "Aborting.\n");
139  return false;
140  }
141  std::string addr = yarp::conf::environment::get_string("ROS_MASTER_URI");
142  Contact c = Contact::fromString(addr);
143  if (c.isValid()) {
144  c.setCarrier("xmlrpc");
145  c.setName("/ros");
146  space = new RosNameSpace(c);
147  subscriber.setDelegate(space);
148  ns.setDelegate(space);
149  yCInfo(NAMESERVERCONTAINER, "Using ROS with ROS_MASTER_URI=%s\n", addr.c_str());
150  } else {
151  yCFatal(NAMESERVERCONTAINER, "Cannot find ROS, check ROS_MASTER_URI (currently '%s')\n", addr.c_str());
152  }
153  }
154 
155  config.minPortNumber = contact.getPort() + MIN_PORT_NUMBER_OFFSET;
156  config.maxPortNumber = contact.getPort() + MAX_PORT_NUMBER_OFFSET;
157  alloc.open(pmem,config);
158  ns.open(pmem,&alloc,contact);
159  NetworkBase::queryBypass(&ns);
160  subscriber.setStore(ns);
161  ns.setSubscriber(&subscriber);
162  style.configure(options);
163  combo1.open(subscriber,style);
164  open(combo1,ns);
165  return true;
166 }
#define MAX_PORT_NUMBER_OFFSET
#define MIN_PORT_NUMBER_OFFSET
Multicast and file-based mechanisms for finding the name server.
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
Collection of carriers, a singleton.
Definition: Carriers.h:30
static Bottle listCarriers()
Definition: Carriers.cpp:317
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
bool isValid() const
Checks if a Contact is tagged as valid.
Definition: Contact.cpp:298
std::string getName() const
Get the name associated with this Contact.
Definition: Contact.cpp:205
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
An abstract name space for ports.
Definition: NameSpace.h:23
virtual Contact getNameServerContact() const =0
Get an address for a name server that manages the name space, if available.
Utilities for manipulating the YARP network, excluding initialization and shutdown.
Definition: Network.h:41
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:783
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
A single value (typically within a Bottle).
Definition: Value.h:45
void open(TripleSource *db, const AllocatorConfig &config)
Compose two name services into one.
void open(NameService &ns1, NameService &ns2)
void open(NameService &ns1, NameService &ns2)
void preregister(const yarp::os::Contact &c)
An implementation of name service operators on a triple store.
void setDelegate(yarp::os::NameSpace *delegate)
void open(TripleSource *db, Allocator *alloc, const yarp::os::Contact &serverContact)
bool configure(const yarp::os::Searchable &options)
bool open(const std::string &filename, bool fresh=false)
bool welcome(const std::string &port, int activity) override
void setDelegate(yarp::os::NameSpace *delegate)
Definition: Subscriber.h:103
void setStore(yarp::os::NameStore &store)
Definition: Subscriber.h:36
TripleSource * open(const char *filename, bool cautious=false, bool fresh=false)
Abstract view of a database as a collection of triples.
Definition: TripleSource.h:41
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCWarning(component,...)
Definition: LogComponent.h:143
#define yCFatal(component,...)
Definition: LogComponent.h:165
#define YARP_SERVERSQL_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:34
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:68