YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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/Value.h>
16
17#include <string>
18
19// For a yarp server running on the port 10000
20// * the first port assigned will be 10002
21// * the last port assigned will be 19999
22#define MIN_PORT_NUMBER_OFFSET 2
23#define MAX_PORT_NUMBER_OFFSET 9999
24
25
31using yarp::os::Value;
37
39
40namespace {
41YARP_SERVERSQL_LOG_COMPONENT(NAMESERVERCONTAINER, "yarp.serversql.impl.NameServerContainer")
42} // namespace
43
44
46{
47 subscriber.clear();
48 if (space) {
49 delete space;
50 space = nullptr;
51 }
52}
53
55{
56 return contact;
57}
58
60{
61 if (!space) {
62 return Contact();
63 }
64 return space->getNameServerContact();
65}
66
68{
69 Network::registerContact(c);
70 subscriber.welcome(c.getName(),1);
71}
72
74{
75 std::string dbDefault = ":memory:";
76 std::string subdbDefault = ":memory:";
77
78 if (options.check("memory")) {
79 yCWarning(NAMESERVERCONTAINER, "The --memory option was given, but that is now a default. Continuing.");
80 }
81
82 std::string dbFilename = options.check("portdb",
83 Value(dbDefault)).asString();
84 std::string subdbFilename = options.check("subdb",
85 Value(subdbDefault)).asString();
86
87 std::string ip = options.check("ip",Value("...")).asString();
88 int sock = options.check("socket",Value(Network::getDefaultPortRange())).asInt32();
89 bool cautious = options.check("cautious");
90
91 yCInfo(NAMESERVERCONTAINER, "Using port database: %s", dbFilename.c_str());
92 yCInfo(NAMESERVERCONTAINER, "Using subscription database: %s", subdbFilename.c_str());
93 if (dbFilename!=":memory:" || subdbFilename!=":memory:") {
94 yCInfo(NAMESERVERCONTAINER, "If you ever need to clear the name server's state, just delete those files.");
95 }
96 yCInfo(NAMESERVERCONTAINER, "IP address: %s", (ip=="...")?"default":ip.c_str());
97 yCInfo(NAMESERVERCONTAINER, "Port number: %d", sock);
98
99 bool reset = false;
100 if (options.check("ip")||options.check("socket")) {
101 yCInfo(NAMESERVERCONTAINER, "Database needs to be reset, IP or port number set.");
102 reset = true;
103 }
104
105 TripleSource *pmem = db.open(dbFilename.c_str(),cautious,reset);
106 if (pmem == nullptr) {
107 yCError(NAMESERVERCONTAINER, "Aborting, ports database failed to open.");
108 return false;
109 }
110
111 if (!subscriber.open(subdbFilename)) {
112 yCError(NAMESERVERCONTAINER, "Aborting, subscription database failed to open.");
113 return false;
114 }
115
116 contact = Contact("...", "tcp", ip, sock);
117
118 if (!options.check("local")) {
119 if (!BootstrapServer::configFileBootstrap(contact,
120 options.check("read"),
121 options.check("write"))) {
122 yCError(NAMESERVERCONTAINER, "Aborting.\n");
123 return false;
124 }
125 }
126
127 config.minPortNumber = contact.getPort() + MIN_PORT_NUMBER_OFFSET;
128 config.maxPortNumber = contact.getPort() + MAX_PORT_NUMBER_OFFSET;
129 alloc.open(pmem,config);
130 ns.open(pmem,&alloc,contact);
131 NetworkBase::queryBypass(&ns);
132 subscriber.setStore(ns);
133 ns.setSubscriber(&subscriber);
134 style.configure(options);
135 combo1.open(subscriber,style);
136 open(combo1,ns);
137 return true;
138}
#define MAX_PORT_NUMBER_OFFSET
#define MIN_PORT_NUMBER_OFFSET
Multicast and file-based mechanisms for finding the name server.
Collection of carriers, a singleton.
Definition Carriers.h:29
Represents how to reach a part of a YARP network.
Definition Contact.h:33
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
An abstract name space for ports.
Definition NameSpace.h:22
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:38
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:706
A base class for nested structures that can be searched.
Definition Searchable.h:31
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:44
void open(TripleSource *db, const AllocatorConfig &config)
Compose two name services into one.
void open(NameService &ns1, NameService &ns2)
bool open(yarp::os::Searchable &options)
void preregister(const yarp::os::Contact &c)
An implementation of name service operators on a triple store.
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 setStore(yarp::os::NameStore &store)
Definition Subscriber.h:34
TripleSource * open(const char *filename, bool cautious=false, bool fresh=false)
Abstract view of a database as a collection of triples.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_SERVERSQL_LOG_COMPONENT(name, name_string)