58 return registerName(name, address,
"...");
61 Contact registerName(
const std::string& name,
63 const std::string& remote);
67 return registerName(name,
Contact());
70 Contact queryName(
const std::string& name);
72 Contact unregisterName(
const std::string& name);
74 std::string apply(
const std::string& txt,
const Contact& remote)
override;
78 std::string
apply(
const std::string& txt)
88 static std::string textify(
const Contact& address);
93 this->basePort = basePort;
94 mcastRecord.setBasePort(basePort);
105 std::vector<T> reuse;
108 virtual ~ReusableRecord() =
default;
110 virtual T fresh() = 0;
112 void release(
const T&
o)
119 if (reuse.size() >= 1) {
120 T result = reuse[reuse.size() - 1];
129 class DisposableNameRecord :
public ReusableRecord<int>
136 DisposableNameRecord()
139 prefix =
"/tmp/port/";
154 bool release(
const std::string& name)
156 if (name.find(prefix) == 0) {
157 std::string num = name.substr(prefix.length());
158 int x = yarp::conf::numeric::from_string<int>(num);
159 ReusableRecord<int>::release(x);
167 class HostRecord :
public ReusableRecord<int>
179 void setBase(
int base)
186 int result = ReusableRecord<int>::getFree();
199 class McastRecord :
public ReusableRecord<int>
215 void setBasePort(
int basePort)
217 this->basePort = basePort;
239 return basePort + last;
242 void releaseAddress(
const char* addr)
245 constexpr size_t ipv4_size = 4;
246 int ip[] = {224, 3, 1, 1};
248 for (
size_t i = 0; i < ipv4_size; ++i) {
249 ip[i] = yarp::conf::numeric::from_string<int>(ss[i]);
254 int x = v2 * 255 + v1;
264 std::vector<std::string> prop;
276 void add(
const std::string& p)
281 bool check(
const std::string& p)
283 for (
unsigned int i = 0; i < prop.size(); i++) {
291 std::string match(
const std::string& str)
293 std::string base =
"";
294 bool needSpace =
false;
295 for (
unsigned int i = 0; i < prop.size(); i++) {
296 if (prop[i].find(str) == 0) {
309 std::string base =
"";
310 for (
unsigned int i = 0; i < prop.size(); i++) {
325 std::map<std::string, PropertyRecord> propMap;
333 reusablePort =
false;
336 NameRecord(
const NameRecord& alt) :
341 reusablePort =
false;
344 bool isReusablePort()
359 reusablePort =
false;
362 void setAddress(
const Contact& address,
363 bool reusablePort =
false,
364 bool reusableIp =
false)
366 this->address = address;
367 this->reusablePort = reusablePort;
368 this->reusableIp = reusableIp;
377 PropertyRecord* getPR(
const std::string& key,
bool create =
true)
379 std::map<std::string, PropertyRecord>::iterator entry = propMap.find(key);
380 if (entry == propMap.end()) {
384 propMap[key] = PropertyRecord();
385 entry = propMap.find(key);
388 return &(entry->second);
391 void clearProp(
const std::string& key)
396 void addProp(
const std::string& key,
const std::string& val)
398 getPR(key)->add(val);
401 std::string getProp(
const std::string& key)
403 PropertyRecord* rec = getPR(key,
false);
404 if (rec !=
nullptr) {
405 return rec->toString();
410 bool checkProp(
const std::string& key,
const std::string& val)
412 PropertyRecord* rec = getPR(key,
false);
413 if (rec !=
nullptr) {
414 return rec->check(val);
419 std::string matchProp(
const std::string& key,
const std::string& val)
421 PropertyRecord* rec = getPR(key,
false);
422 if (rec !=
nullptr) {
423 return rec->match(val);
430 std::string cmdRegister(
int argc,
char* argv[]);
431 std::string cmdQuery(
int argc,
char* argv[]);
432 std::string cmdUnregister(
int argc,
char* argv[]);
433 std::string cmdAnnounce(
int argc,
char* argv[]);
434 std::string cmdHelp(
int argc,
char* argv[]);
435 std::string cmdSet(
int argc,
char* argv[]);
436 std::string cmdGet(
int argc,
char* argv[]);
437 std::string cmdCheck(
int argc,
char* argv[]);
438 std::string cmdMatch(
int argc,
char* argv[]);
439 std::string cmdList(
int argc,
char* argv[]);
440 std::string cmdRoute(
int argc,
char* argv[]);
441 std::string cmdGarbageCollect(
int argc,
char* argv[]);
442 std::string cmdBot(
int argc,
char* argv[]);
451 std::map<std::string, NameRecord> nameMap;
452 std::map<std::string, HostRecord> hostMap;
454 McastRecord mcastRecord;
455 DisposableNameRecord tmpNames;
457 NameRecord* getNameRecord(
const std::string& name,
bool create);
459 NameRecord& getNameRecord(
const std::string& name)
461 NameRecord* result = getNameRecord(name,
true);
466 HostRecord* getHostRecord(
const std::string& name,
bool create);
468 HostRecord& getHostRecord(
const std::string& name)
470 HostRecord* result = getHostRecord(name,
true);
475 Dispatcher<NameServer, std::string> dispatcher;
476 Dispatcher<NameServer, yarp::os::Bottle> ndispatcher;
479 std::string terminate(
const std::string& str);
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.