YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
NameServiceOnTriples.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
7#include <cstdlib>
8
9#include <yarp/os/Vocab.h>
11#include <yarp/os/Network.h>
12#include <yarp/os/Time.h>
13#include <yarp/os/SystemClock.h>
17
18using namespace yarp::os;
19using namespace yarp::serversql::impl;
20
21namespace {
22YARP_SERVERSQL_LOG_COMPONENT(NAMESERVICEONTRIPLES, "yarp.serversql.impl.NameServiceOnTriples")
23} // namespace
24
25
26Contact NameServiceOnTriples::query(const std::string& portName,
28 const std::string& prefix,
29 bool nested)
30{
31 if (!nested) {
32 lock();
33 }
34 Triple t;
35 t.setNameValue("port",portName.c_str());
36 int result = act.mem.find(t, nullptr);
37 TripleContext context;
38 context.setRid(result);
39 if (result!=-1) {
40 std::string host;
41 if (!std::string(prefix).empty()) {
42 printf("LOOKING AT IPS FOR %s\n", prefix.c_str());
43 t.setNameValue("ips","*");
44 std::list<Triple> lst = act.mem.query(t,&context);
45 for (auto& it : lst) {
46 printf("LOOKING AT IPS %s\n", it.value.c_str());
47 if (it.value.find(prefix)==0) {
48 host = it.value;
49 break;
50 }
51 }
52 }
53 if (host.empty()) {
54 t.setNameValue("host","*");
55 std::list<Triple> lst = act.mem.query(t,&context);
56 if (!lst.empty()) {
57 host = lst.begin()->value;
58 }
59 }
60 if (host.empty()) {
61 host = "localhost";
62 }
63 t.setNameValue("socket","*");
64 std::list<Triple> lst = act.mem.query(t,&context);
65 int sock = 10000;
66 if (!lst.empty()) {
67 sock = atoi(lst.begin()->value.c_str());
68 }
69 t.setNameValue("carrier","*");
70 std::string carrier = "tcp";
71 lst = act.mem.query(t,&context);
72 if (!lst.empty()) {
73 carrier = lst.begin()->value;
74 }
75 t.setNameValue("type","*");
76 std::string typ = "*";
77 lst = act.mem.query(t,&context);
78 if (!lst.empty()) {
79 typ = lst.begin()->value;
80 }
81 if (!nested) {
82 unlock();
83 }
84 Contact result = Contact(portName, carrier, host, sock);
85 if (!typ.empty() && typ!="*") {
87 nc.fromString(result.getName());
88 nc.setTypeName(typ);
89 result.setNestedContact(nc);
90 }
91 return result;
92 }
93 if (!nested) {
94 unlock();
95 }
96 return Contact();
97}
98
99
101{
102 Contact check = Contact::fromString(port);
103 if (!check.getHost().empty()) {
104 return check;
105 }
106 Bottle cmd;
107 Bottle reply;
108 Bottle event;
109 Contact remote;
110 TripleSource& mem = *db;
111 NameTripleState act(cmd,reply,event,remote,mem);
112 return query(port,act,"");
113}
114
115
117{
118 std::string port = act.cmd.get(1).asString();
119
120 ParseName parser;
121 parser.apply(port);
122 port = parser.getPortName();
123
124 /*
125 // port names may be prefixed - sort that out
126 std::string base = port;
127 std::string pat = "";
128 if (base.find("/net=") == 0 || base.find("/NET=") == 0) {
129 int patStart = 5;
130 int patEnd = base.find('/',patStart);
131 if (patEnd>=patStart) {
132 pat = base.substr(patStart,patEnd-patStart);
133 base = base.substr(patEnd);
134 }
135 port = base;
136 }
137 */
138
139 if (act.reply.size()==0 && !act.bottleMode) {
140 act.reply.addString("old");
141 }
142 Bottle& q=(act.bottleMode&&!act.nestedMode)?
143 act.reply :
144 act.reply.addList();
145 Contact c = query(port, act, parser.getNetworkChoice(), nested);
146 std::string host = c.getHost();
147 std::string carrier = c.getCarrier();
148 int sock = c.getPort();
149 if (c.isValid()) {
150 if (!act.bottleMode) {
151 q.addString("registration");
152 q.addString("name");
153 q.addString(port);
154 q.addString("ip");
155 q.addString(host);
156 q.addString("port");
157 q.addInt32(sock);
158 q.addString("type");
159 q.addString(carrier);
160 } else {
162 bname.addString("name");
163 bname.addString(port);
164 Bottle bip;
165 bip.addString("ip");
166 bip.addString(host);
167 Bottle bnum;
168 bnum.addString("port_number");
169 bnum.addInt32(sock);
171 bcarrier.addString("carrier");
172 bcarrier.addString(carrier);
173 q.addString("port");
174 q.addList() = bname;
175 q.addList() = bip;
176 q.addList() = bnum;
177 q.addList() = bcarrier;
178 }
179 } else {
180 if (act.bottleMode) {
182 bstate.addString("error");
183 bstate.addInt32(-2);
184 bstate.addString("port not known");
185 q.addString("port");
186 q.addList() = bstate;
187 }
188 }
189 return true;
190}
191
193{
194 std::string port = act.cmd.get(1).asString();
195
196 lock();
197 Triple t;
198 t.setNameValue("port",port.c_str());
199 int result = act.mem.find(t, nullptr);
200 unlock();
201
202 if (result!=-1) {
203 // Hmm, we already have a registration.
204 // This could be fine - maybe program crashed or was abruptly
205 // terminated. Classically, that is what YARP would assume.
206 // Now, let us try checking instead to see if there is a port
207 // alive at the registered address. Note that this can lead
208 // to delays...
209#if 0
210 Contact c = query(port.c_str());
211 if (c.isValid()) {
212 if (gonePublic) {
213 printf(" ? checking prior registration, to avoid accidental collision\n");
214 Bottle cmd("[ver]"), reply;
215 double timeout = 3.0;
216 double pre = SystemClock::nowSystem();
217 bool ok = Network::write(c,cmd,reply,true,true,timeout);
218 double post = SystemClock::nowSystem();
219 if (post-pre>timeout-1) {
220 ok = false;
221 }
222 if (ok) {
223 printf(" ? prior registration seems to be live! Denying new registration.\n");
224 /*
225 printf("Got a live one! %s - said %s\n",
226 port.c_str(),
227 reply.toString().c_str());
228 */
229 // oops! there is a live port!
230 // give back a blank query
231 act.cmd.fromString("query");
232 return cmdQuery(act);
233 } else {
234 printf(" ! prior registration seems to be no longer valid, good!\n");
235 }
236 }
237 }
238#endif
240 act.reply.clear();
241 }
242
243 act.reply.addString("old");
244
245 size_t at = 2;
246 int sock = -1;
247 std::string carrier = "...";
248 std::string machine = "...";
249 std::string typ = "*";
250 if (act.cmd.size()>at) {
251 carrier = act.cmd.get(at).asString();
252 at++;
253 }
254 if (carrier=="...") {
255 carrier = "tcp";
256 }
257 if (act.cmd.size()>at) {
258 machine = act.cmd.get(at).asString();
259 at++;
260 }
261 if (machine == "...") {
262 if (carrier=="topic") {
263 machine = serverContact.getHost();
264 } else if (carrier!="mcast") {
265 std::string remote = act.remote.getHost();
266 if (remote.empty() || remote == "...") {
267 //fprintf(stderr,"Not detecting real remote machine name, guessing local\n");
268 machine = "localhost";
269 } else {
270 machine = remote;
271 }
272 }
273 }
274 if (act.cmd.size()>at) {
275 sock = act.cmd.get(at).asInt32();
276 at++;
277 } else {
278 if (carrier=="topic") {
279 sock = serverContact.getPort();
280 }
281 }
282 if (act.cmd.size()>at) {
283 typ = act.cmd.get(at).asString();
284 at++;
285 }
286 lock();
287 if (port=="..." || (port.length()>0 && port[0]=='=')) {
288 Contact c(port, carrier, machine, sock);
289 c = alloc->completePortName(c);
290 if (port =="...") {
291 port = c.getName();
292 } else {
293 port = c.getName() + port;
294 }
295 }
296 t.setNameValue("port",port.c_str());
297 act.mem.remove_query(t, nullptr);
298 act.mem.insert(t, nullptr);
299 result = act.mem.find(t, nullptr);
300 TripleContext context;
301 context.setRid(result);
302 t.setNameValue("carrier",carrier.c_str());
303 act.mem.update(t,&context);
304 char buf[100];
305 Contact c(port, carrier, machine, sock);
306 c = alloc->completeSocket(c);
307 sock = c.getPort();
308 machine = c.getHost();
309 t.setNameValue("host",machine.c_str());
310 act.mem.update(t,&context);
311 sprintf(buf,"%d",sock);
312 t.setNameValue("socket",buf);
313 act.mem.update(t,&context);
314 if (typ!="*") {
315 t.setNameValue("type",typ.c_str());
316 act.mem.update(t,&context);
317 }
318 // now, query to report that it worked
319 act.mem.reset();
320 act.cmd.clear();
321 act.cmd.addString("query");
322 act.cmd.addString(port);
323
324 if (carrier!="mcast") {
325 Bottle& event = act.event.addList();
326 event.addVocab32("add");
327 event.addString(port);
328 }
329 unlock();
330
331 return cmdQuery(act);
332}
333
334
335bool NameServiceOnTriples::announce(const std::string& name, int activity)
336{
337 if (subscriber != nullptr && gonePublic) {
338 subscriber->welcome(name,activity);
339 }
340 return true;
341}
342
344{
345 std::string port = act.cmd.get(1).asString();
346 //printf(" - unregister %s\n", port.c_str());
347 announce(port,-1);
348 lock();
349 Contact contact = query(port,act,"",true);
350 alloc->freePortResources(contact);
351 act.reply.addString("old");
352 Triple t;
353 t.setNameValue("port",port.c_str());
354 int result = act.mem.find(t, nullptr);
355 TripleContext context;
356 context.setRid(result);
357 if (result!=-1) {
358 t.setNameValue("owns","*");
359 std::list<Triple> lst = act.mem.query(t,&context);
360 unlock();
361 for (auto& it : lst) {
362 act.cmd.clear();
363 act.cmd.addString("unregister");
364 act.cmd.addString(it.value.c_str());
366 }
367 lock();
368 t.setNsNameValue("*","*","*");
369 act.mem.remove_query(t,&context);
370
371 t.setNameValue("port",port.c_str());
372 act.mem.remove_query(t, nullptr);
373 // now, query to report that there is nothing there
374
375 if (contact.getCarrier()!="mcast") {
376 Bottle& event = act.event.addList();
377 event.addVocab32("del");
378 event.addString(port);
379 }
380 }
381
382 act.mem.reset();
383 unlock();
384
385 return cmdQuery(act);
386}
387
389{ // this is a combination of cmdList and cmdCheck codes
390 if (!act.bottleMode)
391 {
392 act.reply.addString("old");
393 } else
394 {
395 act.reply.addString("ports");
396 }
397 lock();
398 Triple t;
399 t.setNameValue("port","*");
400
401 // obtain all ports names
402 std::list<Triple> lst = act.mem.query(t, nullptr);
403 act.nestedMode = true;
404
405 for (auto& it : lst)
406 { // check yarprun property for each port
407 std::string port = it.value;
408 act.mem.reset();
409
410 Triple t;
411 t.setNameValue("port",port.c_str());
412 int rid = act.mem.find(t, nullptr);
413 if (rid == -1) {
414 unlock();
415 return false;
416 }
417
418 // find all triples with yarprun = true for the specified RID (at most one)
419 TripleContext context;
420 context.setRid(rid);
421 t.setNameValue("yarprun","true");
422 std::list<Triple> lst = act.mem.query(t,&context);
423
424 if (!lst.empty())
425 { // if the port is a runner, do a classic query to build the reply with complete information about the port
426 act.cmd.clear();
427 act.cmd.addString("query");
428 act.cmd.addString(port);
429 act.mem.reset();
430 cmdQuery(act, true);
431 }
432 }
433 unlock();
434 return true;
435}
436
438{
439 if (!act.bottleMode) {
440 act.reply.addString("old");
441 } else {
442 act.reply.addString("ports");
443 }
444 lock();
445 Triple t;
446 t.setNameValue("port","*");
447 std::string prefix;
448 if (act.cmd.size()>1) {
449 prefix = act.cmd.get(1).asString();
450 }
451 std::list<Triple> lst = act.mem.query(t, nullptr);
452 act.nestedMode = true;
453 for (auto& it : lst) {
454 if (prefix.empty()) {
455 act.cmd.clear();
456 act.cmd.addString("query");
457 act.cmd.addString(it.value.c_str());
458 act.mem.reset();
459 cmdQuery(act,true);
460 } else {
461 std::string iname = it.value;
462 if (iname.find(prefix)==0) {
463 if (iname==prefix || iname[prefix.length()]=='/' ||
464 prefix[prefix.length()-1]=='/') {
465 act.cmd.clear();
466 act.cmd.addString("query");
467 act.cmd.addString(iname);
468 act.mem.reset();
469 cmdQuery(act,true);
470 }
471 }
472 }
473 }
474 unlock();
475 return true;
476}
477
478
480{
481 lock();
482 if (!act.bottleMode) {
483 act.reply.addString("old");
484 }
485 std::string port = act.cmd.get(1).asString();
486 std::string key = act.cmd.get(2).toString();
487 int at = 3;
488 int n = act.cmd.size() - at;
489 Triple t;
490 t.setNameValue("port", port.c_str());
491 int result = act.mem.find(t, nullptr);
492 if (result==-1) {
493 unlock();
494 return false;
495 }
496 TripleContext context;
497 context.setRid(result);
498 t.setNameValue(key.c_str(),"*");
499 act.mem.remove_query(t,&context);
500 for (int i=0; i<n; i++) {
501 t.setNameValue(key.c_str(),act.cmd.get(at).toString().c_str());
502 at++;
503 act.mem.insert(t,&context);
504 }
505 act.mem.reset();
506 act.cmd.clear();
507 act.cmd.addString("get");
508 act.cmd.addString(port);
509 act.cmd.addString(key);
510 unlock();
511 return cmdGet(act);
512}
513
514
516{
517 lock();
518 if (!act.bottleMode) {
519 if (act.reply.size()==0) {
520 act.reply.addString("old");
521 }
522 }
523 std::string port = act.cmd.get(1).asString();
524 std::string key = act.cmd.get(2).toString();
525 Triple t;
526 t.setNameValue("port",port.c_str());
527 int result = act.mem.find(t, nullptr);
528 if (result==-1) {
529 unlock();
530 return false;
531 }
532 TripleContext context;
533 context.setRid(result);
534 t.setNameValue(key.c_str(),"*");
535 std::list<Triple> lst = act.mem.query(t,&context);
536 Bottle& q = (act.bottleMode?act.reply:act.reply.addList());
537 if (!act.bottleMode) {
538 q.addString("port");
539 q.addString(port);
540 q.addString("property");
541 q.addString(key);
542 q.addString("=");
543 for (auto& it : lst) {
544 q.addString(it.value.c_str());
545 }
546 } else {
547 for (auto& it : lst) {
548 Value v;
549 v.fromString(it.value.c_str());
550 q.add(v);
551 }
552 }
553 unlock();
554 return true;
555}
556
557
559{
560 lock();
561 if (act.reply.size()==0) {
562 act.reply.addString("old");
563 }
564 std::string port = act.cmd.get(1).asString();
565 std::string key = act.cmd.get(2).toString();
566 std::string val = act.cmd.get(3).toString();
567 Triple t;
568 t.setNameValue("port",port.c_str());
569 int result = act.mem.find(t, nullptr);
570 if (result==-1) {
571 unlock();
572 return false;
573 }
574 TripleContext context;
575 context.setRid(result);
576 t.setNameValue(key.c_str(),"*");
577 std::list<Triple> lst = act.mem.query(t,&context);
578 Bottle& q = act.reply.addList();
579 q.addString("port");
580 q.addString(port);
581 q.addString("property");
582 q.addString(key);
583 q.addString("value");
584 q.addString(val);
585 q.addString("present");
586 std::string present = "false";
587 for (auto& it : lst) {
588 if (val == it.value) {
589 present = "true";
590 }
591 }
593 unlock();
594 return true;
595}
596
597
599{
600 if (act.reply.size()==0) {
601 act.reply.addString("old");
602 }
603 std::string port1 = act.cmd.get(1).asString();
604 std::string port2 = act.cmd.get(2).asString();
605 Bottle& q = act.reply.addList();
606 q.addString("port");
607 q.addString(port1);
608 q.addString("route");
609 q.addString(port2);
610 q.addString("=");
611 q.addString(std::string("tcp:/") + port2);
612 return true;
613}
614
616{
617 act.reply.addString("old");
618 Bottle& q = act.reply.addList();
619 // nothing needed
620 q.addString("garbage collection done.");
621 return true;
622}
623
624
626{
627 Bottle& bot = act.reply;
628 if (!act.bottleMode) {
629 bot.addString("old");
630 bot.addString("Here are some ways to use the name server:");
631 }
632 bot.addString("+ help");
633 bot.addString("+ list [$prefix]");
634 bot.addString("+ register $portname");
635 bot.addString("+ register $portname $carrier $ipAddress $portNumber");
636 bot.addString(" (if you want a field set automatically, write '...')");
637 bot.addString("+ unregister $portname");
638 bot.addString("+ query $portname");
639 bot.addString("+ set $portname $property $value");
640 bot.addString("+ get $portname $property");
641 bot.addString("+ check $portname $property");
642 bot.addString("+ route $port1 $port2");
643 bot.addString("+ runners");
644 bot.addString(" (to get a list of the yarprun ports)");
645 return true;
646}
647
649 yarp::os::Bottle& reply,
650 yarp::os::Bottle& event,
651 const yarp::os::Contact& remote)
652{
653 std::string key = cmd.get(0).toString();
654 std::string prefix = " * ";
655
656 access.wait();
657 if (key=="register") {
658 lastRegister = cmd.get(1).asString();
659 } else if (key=="set") {
660 if (cmd.get(1).asString()==lastRegister) {
661 prefix = " + ";
662 }
663 } else {
664 lastRegister = "";
665 }
666 if (!silent) {
667 yCInfo(NAMESERVICEONTRIPLES, "%s%s", prefix.c_str(), cmd.toString().c_str());
668 }
669 access.post();
670
671 TripleSource& mem = *db;
672 //mem.begin();
673 mem.reset();
674 reply.clear();
675 NameTripleState act(cmd,reply,event,remote,mem);
676
677 if (cmd.check("format")) {
678 if (cmd.find("format").asString()=="json") {
679 act.bottleMode = true;
680 }
681 }
682
683 if (key == "NAME_SERVER") {
684 cmd = cmd.tail();
685 key = cmd.get(0).asString();
686 }
687 if (key == "bot") {
688 act.bottleMode = true;
689 cmd = cmd.tail();
690 key = cmd.get(0).asString();
691 }
692
693 if (key=="register") {
694 return cmdRegister(act);
695 }
696 if (key=="unregister") {
697 return cmdUnregister(act);
698 }
699 if (key=="query") {
700 return cmdQuery(act);
701 }
702 if (key=="list") {
703 return cmdList(act);
704 }
705 if (key=="runners") {
706 return cmdListRunners(act);
707 }
708 if (key=="set") {
709 return cmdSet(act);
710 }
711 if (key=="get") {
712 return cmdGet(act);
713 }
714 if (key=="check") {
715 return cmdCheck(act);
716 }
717 if (key=="route") {
718 return cmdRoute(act);
719 }
720 if (key=="gc") {
721 return cmdGc(act);
722 }
723 if (key=="help") {
724 return cmdHelp(act);
725 }
726
727 // not understood
728 act.reply.addString("old");
729
730 //mem.end();
731
732 return true;
733}
734
735
736
738{
739 mutex.lock();
740 db->begin(nullptr);
741}
742
744{
745 db->end(nullptr);
746 mutex.unlock();
747}
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition Bottle.cpp:310
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition Bottle.cpp:188
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:247
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition Bottle.cpp:278
Bottle tail() const
Get all but the first element of a bottle.
Definition Bottle.cpp:362
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:176
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:212
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition Bottle.cpp:288
A mini-server for performing network communication in the background.
std::string getName() const override
Get name of port.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
void setNestedContact(const yarp::os::NestedContact &nestedContact)
Sets the NestedContact containing extra information for this Contact.
Definition Contact.cpp:266
std::string getName() const
Get the name associated with this Contact.
Definition Contact.cpp:205
static Contact fromString(const std::string &txt)
Factory method.
Definition Contact.cpp:139
int getPort() const
Get the port number associated with this Contact for socket communication.
Definition Contact.cpp:239
std::string getCarrier() const
Get the carrier associated with this Contact for socket communication.
Definition Contact.cpp:250
std::string getHost() const
Get the host name associated with this Contact for socket communication.
Definition Contact.cpp:228
A placeholder for rich contact information.
void wait()
Decrement the counter, even if we must wait to do that.
Definition Semaphore.cpp:96
void post()
Increment the counter.
static double nowSystem()
A single value (typically within a Bottle).
Definition Value.h:44
std::string toString() const override
Return a standard text representation of the content of the object.
Definition Value.cpp:347
void fromString(const char *str)
Set value to correspond to a textual representation.
Definition Value.cpp:342
virtual std::string asString() const
Get string value.
Definition Value.cpp:240
virtual yarp::os::Contact completePortName(const yarp::os::Contact &c)=0
virtual yarp::os::Contact completeSocket(const yarp::os::Contact &c)=0
virtual bool freePortResources(const yarp::os::Contact &c)=0
bool cmdQuery(NameTripleState &act, bool nested=false)
bool apply(yarp::os::Bottle &cmd, yarp::os::Bottle &reply, yarp::os::Bottle &event, const yarp::os::Contact &remote) override
bool announce(const std::string &name, int activity) override
yarp::os::Contact query(const std::string &portName, NameTripleState &act, const std::string &prefix, bool nested=false)
State information for a single name server operation on a database.
void apply(const std::string &str)
Definition ParseName.cpp:13
virtual bool welcome(const std::string &port, int activity)=0
Side information for controlling access to triples.
Abstract view of a database as a collection of triples.
virtual void end(TripleContext *context)=0
virtual void begin(TripleContext *context)=0
The basic unit of data the name server works with.
Definition Triple.h:23
void setNsNameValue(const char *ns, const char *name, const char *value)
Definition Triple.h:116
void setNameValue(const char *name, const char *value)
Definition Triple.h:123
#define yCInfo(component,...)
#define YARP_SERVERSQL_LOG_COMPONENT(name, name_string)
An interface to the operating system, including Port based communication.