10 #ifndef YARP_OS_IMPL_NAMESERVER_H
11 #define YARP_OS_IMPL_NAMESERVER_H
39 virtual std::string
apply(
const std::string& txt,
const Contact& remote) = 0;
61 return registerName(name, address,
"...");
64 Contact registerName(
const std::string& name,
66 const std::string& remote);
70 return registerName(name,
Contact());
73 Contact queryName(
const std::string& name);
75 Contact unregisterName(
const std::string& name);
77 std::string apply(
const std::string& txt,
const Contact& remote)
override;
81 std::string
apply(
const std::string& txt)
91 static std::string textify(
const Contact& address);
96 this->basePort = basePort;
97 mcastRecord.setBasePort(basePort);
108 std::vector<T> reuse;
111 virtual ~ReusableRecord() =
default;
113 virtual T fresh() = 0;
115 void release(
const T& o)
122 if (reuse.size() >= 1) {
123 T result = reuse[reuse.size() - 1];
132 class DisposableNameRecord :
public ReusableRecord<int>
139 DisposableNameRecord()
142 prefix =
"/tmp/port/";
157 bool release(
const std::string& name)
159 if (name.find(prefix) == 0) {
160 std::string num = name.substr(prefix.length());
162 ReusableRecord<int>::release(x);
170 class HostRecord :
public ReusableRecord<int>
182 void setBase(
int base)
189 int result = ReusableRecord<int>::getFree();
202 class McastRecord :
public ReusableRecord<int>
218 void setBasePort(
int basePort)
220 this->basePort = basePort;
242 return basePort + last;
245 void releaseAddress(
const char* addr)
247 SplitString ss(addr,
'.');
248 int ip[] = {224, 3, 1, 1};
250 for (
int i = 0; i < 4; i++) {
255 int x = v2 * 255 + v1;
265 std::vector<std::string> prop;
277 void add(
const std::string& p)
282 bool check(
const std::string& p)
284 for (
unsigned int i = 0; i < prop.size(); i++) {
292 std::string match(
const std::string& str)
294 std::string base =
"";
295 bool needSpace =
false;
296 for (
unsigned int i = 0; i < prop.size(); i++) {
297 if (prop[i].find(str) == 0) {
310 std::string base =
"";
311 for (
unsigned int i = 0; i < prop.size(); i++) {
326 std::map<std::string, PropertyRecord> propMap;
334 reusablePort =
false;
337 NameRecord(
const NameRecord& alt) :
342 reusablePort =
false;
345 bool isReusablePort()
360 reusablePort =
false;
363 void setAddress(
const Contact& address,
364 bool reusablePort =
false,
365 bool reusableIp =
false)
367 this->address = address;
368 this->reusablePort = reusablePort;
369 this->reusableIp = reusableIp;
378 PropertyRecord* getPR(
const std::string& key,
bool create =
true)
380 std::map<std::string, PropertyRecord>::iterator entry = propMap.find(key);
381 if (entry == propMap.end()) {
385 propMap[key] = PropertyRecord();
386 entry = propMap.find(key);
389 return &(entry->second);
392 void clearProp(
const std::string& key)
397 void addProp(
const std::string& key,
const std::string& val)
399 getPR(key)->add(val);
402 std::string getProp(
const std::string& key)
404 PropertyRecord* rec = getPR(key,
false);
405 if (rec !=
nullptr) {
406 return rec->toString();
411 bool checkProp(
const std::string& key,
const std::string& val)
413 PropertyRecord* rec = getPR(key,
false);
414 if (rec !=
nullptr) {
415 return rec->check(val);
420 std::string matchProp(
const std::string& key,
const std::string& val)
422 PropertyRecord* rec = getPR(key,
false);
423 if (rec !=
nullptr) {
424 return rec->match(val);
431 std::string cmdRegister(
int argc,
char* argv[]);
432 std::string cmdQuery(
int argc,
char* argv[]);
433 std::string cmdUnregister(
int argc,
char* argv[]);
434 std::string cmdAnnounce(
int argc,
char* argv[]);
435 std::string cmdHelp(
int argc,
char* argv[]);
436 std::string cmdSet(
int argc,
char* argv[]);
437 std::string cmdGet(
int argc,
char* argv[]);
438 std::string cmdCheck(
int argc,
char* argv[]);
439 std::string cmdMatch(
int argc,
char* argv[]);
440 std::string cmdList(
int argc,
char* argv[]);
441 std::string cmdRoute(
int argc,
char* argv[]);
442 std::string cmdGarbageCollect(
int argc,
char* argv[]);
443 std::string cmdBot(
int argc,
char* argv[]);
452 std::map<std::string, NameRecord> nameMap;
453 std::map<std::string, HostRecord> hostMap;
455 McastRecord mcastRecord;
456 DisposableNameRecord tmpNames;
458 NameRecord* getNameRecord(
const std::string& name,
bool create);
460 NameRecord& getNameRecord(
const std::string& name)
462 NameRecord* result = getNameRecord(name,
true);
467 HostRecord* getHostRecord(
const std::string& name,
bool create);
469 HostRecord& getHostRecord(
const std::string& name)
471 HostRecord* result = getHostRecord(name,
true);
476 Dispatcher<NameServer, std::string> dispatcher;
477 Dispatcher<NameServer, yarp::os::Bottle> ndispatcher;
480 std::string terminate(
const std::string& str);
492 #endif // YARP_OS_IMPL_NAMESERVER_H