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
30using yarp::os::Value;
36
38
39namespace {
40YARP_SERVERSQL_LOG_COMPONENT(NAMESERVERCONTAINER, "yarp.serversql.impl.NameServerContainer")
41} // namespace
42
43
48
50{
51 return contact;
52}
53
55{
56 Network::registerContact(c);
57 subscriber.welcome(c.getName(),1);
58}
59
61{
62 std::string dbDefault = ":memory:";
63 std::string subdbDefault = ":memory:";
64
65 if (options.check("memory")) {
66 yCWarning(NAMESERVERCONTAINER, "The --memory option was given, but that is now a default. Continuing.");
67 }
68
69 std::string dbFilename = options.check("portdb",
70 Value(dbDefault)).asString();
71 std::string subdbFilename = options.check("subdb",
72 Value(subdbDefault)).asString();
73
74 std::string ip = options.check("ip",Value("...")).asString();
75 int sock = options.check("socket",Value(Network::getDefaultPortRange())).asInt32();
76 bool cautious = options.check("cautious");
77
78 yCInfo(NAMESERVERCONTAINER, "Using port database: %s", dbFilename.c_str());
79 yCInfo(NAMESERVERCONTAINER, "Using subscription database: %s", subdbFilename.c_str());
80 if (dbFilename!=":memory:" || subdbFilename!=":memory:") {
81 yCInfo(NAMESERVERCONTAINER, "If you ever need to clear the name server's state, just delete those files.");
82 }
83 yCInfo(NAMESERVERCONTAINER, "IP address: %s", (ip=="...")?"default":ip.c_str());
84 yCInfo(NAMESERVERCONTAINER, "Port number: %d", sock);
85
86 bool reset = false;
87 if (options.check("ip")||options.check("socket")) {
88 yCInfo(NAMESERVERCONTAINER, "Database needs to be reset, IP or port number set.");
89 reset = true;
90 }
91
92 TripleSource *pmem = db.open(dbFilename.c_str(),cautious,reset);
93 if (pmem == nullptr) {
94 yCError(NAMESERVERCONTAINER, "Aborting, ports database failed to open.");
95 return false;
96 }
97
98 if (!subscriber.open(subdbFilename)) {
99 yCError(NAMESERVERCONTAINER, "Aborting, subscription database failed to open.");
100 return false;
101 }
102
103 contact = Contact("...", "tcp", ip, sock);
104
105 if (!options.check("local")) {
106 if (!BootstrapServer::configFileBootstrap(contact,
107 options.check("read"),
108 options.check("write"))) {
109 yCError(NAMESERVERCONTAINER, "Aborting.\n");
110 return false;
111 }
112 }
113
114 config.minPortNumber = contact.getPort() + MIN_PORT_NUMBER_OFFSET;
115 config.maxPortNumber = contact.getPort() + MAX_PORT_NUMBER_OFFSET;
116 alloc.open(pmem,config);
117 ns.open(pmem,&alloc,contact);
118 NetworkBase::queryBypass(&ns);
119 subscriber.setStore(ns);
120 ns.setSubscriber(&subscriber);
121 style.configure(options);
122 combo1.open(subscriber,style);
123 open(combo1,ns);
124 return true;
125}
#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
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:718
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:33
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)