7 #ifndef YARP_OS_IMPL_NAMESERVER_H
8 #define YARP_OS_IMPL_NAMESERVER_H
38 virtual std::string
apply(
const std::string& txt,
const Contact& remote) = 0;
60 return registerName(name, address,
"...");
63 Contact registerName(
const std::string& name,
65 const std::string& remote);
69 return registerName(name,
Contact());
72 Contact queryName(
const std::string& name);
74 Contact unregisterName(
const std::string& name);
76 std::string apply(
const std::string& txt,
const Contact& remote)
override;
80 std::string
apply(
const std::string& txt)
90 static std::string textify(
const Contact& address);
95 this->basePort = basePort;
96 mcastRecord.setBasePort(basePort);
107 std::vector<T> reuse;
110 virtual ~ReusableRecord() =
default;
112 virtual T fresh() = 0;
114 void release(
const T& o)
121 if (reuse.size() >= 1) {
122 T result = reuse[reuse.size() - 1];
131 class DisposableNameRecord :
public ReusableRecord<int>
138 DisposableNameRecord()
141 prefix =
"/tmp/port/";
156 bool release(
const std::string& name)
158 if (name.find(prefix) == 0) {
159 std::string num = name.substr(prefix.length());
160 int x = yarp::conf::numeric::from_string<int>(num);
161 ReusableRecord<int>::release(x);
169 class HostRecord :
public ReusableRecord<int>
181 void setBase(
int base)
188 int result = ReusableRecord<int>::getFree();
201 class McastRecord :
public ReusableRecord<int>
217 void setBasePort(
int basePort)
219 this->basePort = basePort;
241 return basePort + last;
244 void releaseAddress(
const char* addr)
247 constexpr
size_t ipv4_size = 4;
248 int ip[] = {224, 3, 1, 1};
250 for (
size_t i = 0; i < ipv4_size; ++i) {
251 ip[i] = yarp::conf::numeric::from_string<int>(ss[i]);
256 int x = v2 * 255 + v1;
266 std::vector<std::string> prop;
278 void add(
const std::string& p)
283 bool check(
const std::string& p)
285 for (
unsigned int i = 0; i < prop.size(); i++) {
293 std::string match(
const std::string& str)
295 std::string base =
"";
296 bool needSpace =
false;
297 for (
unsigned int i = 0; i < prop.size(); i++) {
298 if (prop[i].find(str) == 0) {
311 std::string base =
"";
312 for (
unsigned int i = 0; i < prop.size(); i++) {
327 std::map<std::string, PropertyRecord> propMap;
335 reusablePort =
false;
338 NameRecord(
const NameRecord& alt) :
343 reusablePort =
false;
346 bool isReusablePort()
361 reusablePort =
false;
364 void setAddress(
const Contact& address,
365 bool reusablePort =
false,
366 bool reusableIp =
false)
368 this->address = address;
369 this->reusablePort = reusablePort;
370 this->reusableIp = reusableIp;
379 PropertyRecord* getPR(
const std::string& key,
bool create =
true)
381 std::map<std::string, PropertyRecord>::iterator entry = propMap.find(key);
382 if (entry == propMap.end()) {
386 propMap[key] = PropertyRecord();
387 entry = propMap.find(key);
390 return &(entry->second);
393 void clearProp(
const std::string& key)
398 void addProp(
const std::string& key,
const std::string& val)
400 getPR(key)->add(val);
403 std::string getProp(
const std::string& key)
405 PropertyRecord* rec = getPR(key,
false);
406 if (rec !=
nullptr) {
407 return rec->toString();
412 bool checkProp(
const std::string& key,
const std::string& val)
414 PropertyRecord* rec = getPR(key,
false);
415 if (rec !=
nullptr) {
416 return rec->check(val);
421 std::string matchProp(
const std::string& key,
const std::string& val)
423 PropertyRecord* rec = getPR(key,
false);
424 if (rec !=
nullptr) {
425 return rec->match(val);
432 std::string cmdRegister(
int argc,
char* argv[]);
433 std::string cmdQuery(
int argc,
char* argv[]);
434 std::string cmdUnregister(
int argc,
char* argv[]);
435 std::string cmdAnnounce(
int argc,
char* argv[]);
436 std::string cmdHelp(
int argc,
char* argv[]);
437 std::string cmdSet(
int argc,
char* argv[]);
438 std::string cmdGet(
int argc,
char* argv[]);
439 std::string cmdCheck(
int argc,
char* argv[]);
440 std::string cmdMatch(
int argc,
char* argv[]);
441 std::string cmdList(
int argc,
char* argv[]);
442 std::string cmdRoute(
int argc,
char* argv[]);
443 std::string cmdGarbageCollect(
int argc,
char* argv[]);
444 std::string cmdBot(
int argc,
char* argv[]);
453 std::map<std::string, NameRecord> nameMap;
454 std::map<std::string, HostRecord> hostMap;
456 McastRecord mcastRecord;
457 DisposableNameRecord tmpNames;
459 NameRecord* getNameRecord(
const std::string& name,
bool create);
461 NameRecord& getNameRecord(
const std::string& name)
463 NameRecord* result = getNameRecord(name,
true);
468 HostRecord* getHostRecord(
const std::string& name,
bool create);
470 HostRecord& getHostRecord(
const std::string& name)
472 HostRecord* result = getHostRecord(name,
true);
477 Dispatcher<NameServer, std::string> dispatcher;
478 Dispatcher<NameServer, yarp::os::Bottle> ndispatcher;
481 std::string terminate(
const std::string& str);
const yarp::os::LogComponent & NAMESERVER()
A simple collection of objects that can be described and transmitted in a portable way.
Stub for a YARP2-conforming name server.
virtual std::string apply(const std::string &txt, const Contact &remote)=0
virtual ~NameServerStub()=default
Implementation of a YARP2-conforming name server.
std::string apply(const std::string &txt)
virtual ~NameServer()=default
virtual void onEvent(yarp::os::Bottle &event)
Contact registerName(const std::string &name, const Contact &address)
Contact registerName(const std::string &name)
void setBasePort(int basePort)
std::string toString(const T &value)
convert an arbitrary type to string.
#define yCInfo(component,...)
#define yCAssert(component, x)
#define yCTrace(component,...)
#define YARP_DECLARE_LOG_COMPONENT(name)
std::string to_string(IntegerType x)
ContainerT split(const typename ContainerT::value_type &s, std::basic_regex< typename ContainerT::value_type::value_type > regex)
Utility to split a string by a separator, into a vector of strings.
The main, catch-all namespace for YARP.