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