YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Network.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 <yarp/os/Network.h>
8
11#include <yarp/os/Bottle.h>
12#include <yarp/os/Carriers.h>
13#include <yarp/os/Face.h>
15#include <yarp/os/NameSpace.h>
16#include <yarp/os/NetType.h>
18#include <yarp/os/Port.h>
19#include <yarp/os/Route.h>
20#include <yarp/os/Time.h>
21#include <yarp/os/Vocab.h>
22#include <yarp/os/YarpPlugin.h>
33
34#ifdef YARP_HAS_ACE
35# include <ace/Init_ACE.h>
36# include <ace/config.h>
37// In one the ACE headers there is a definition of "main" for WIN32
38# ifdef main
39# undef main
40# endif
41#endif
42
43#include <cstdio>
44#include <cstdlib>
45#include <mutex>
46#include <string>
47
48using namespace yarp::os::impl;
49using namespace yarp::os;
50
51namespace {
52YARP_OS_LOG_COMPONENT(NETWORK, "yarp.os.Network")
53}
54
55static int __yarp_is_initialized = 0;
56static bool __yarp_auto_init_active = false; // was yarp auto-initialized?
57
58
64{
65public:
82};
84
90
91static bool needsLookup(const Contact& contact)
92{
93 if (!contact.getHost().empty()) {
94 return false;
95 }
96 if (contact.getCarrier() == "topic") {
97 return false;
98 }
99 return true;
100}
101
102static int noteDud(const Contact& src)
103{
104 NameStore* store = getNameSpace().getQueryBypass();
105 if (store != nullptr) {
106 return store->announce(src.getName(), 0);
107 }
108 Bottle cmd;
109 Bottle reply;
110 cmd.addString("announce");
111 cmd.addString(src.getName().c_str());
112 cmd.addInt32(0);
113 ContactStyle style;
114 bool ok = NetworkBase::writeToNameServer(cmd,
115 reply,
116 style);
117 return ok ? 0 : 1;
118}
119
120
121static int enactConnection(const Contact& src,
122 const Contact& dest,
123 const ContactStyle& style,
124 int mode,
125 bool reversed)
126{
128 rpc.admin = true;
129 rpc.quiet = style.quiet;
130 rpc.timeout = style.timeout;
131
133 "enactConnection: SRC %s DST %s using carrier %s, MODE=%d, rev=%d",
134 src.getName().c_str(),
135 dest.getName().c_str(),
136 style.carrier.c_str(),
137 mode,
138 reversed);
139
140 if (style.persistent) {
141 bool ok = false;
142 // we don't talk to the ports, we talk to the nameserver
143 NameSpace& ns = getNameSpace();
144 if (mode == YARP_ENACT_CONNECT) {
145 ok = ns.connectPortToPortPersistently(src, dest, style);
146 } else if (mode == YARP_ENACT_DISCONNECT) {
147 ok = ns.disconnectPortToPortPersistently(src, dest, style);
148 } else {
149 yCError(NETWORK, "Failure: cannot check subscriptions yet");
150 return 1;
151 }
152 if (!ok) {
153 return 1;
154 }
155 if (!style.quiet) {
156 yCInfo(NETWORK, "Success: port-to-port persistent connection added.");
157 }
158 return 0;
159 }
160
161 Bottle cmd;
162 Bottle reply;
163 cmd.addVocab32("list");
164 cmd.addVocab32(reversed ? "in" : "out");
165 cmd.addString(dest.getName().c_str());
166 yCDebug(NETWORK, "asking %s: %s", src.toString().c_str(), cmd.toString().c_str());
167 bool ok = NetworkBase::write(src, cmd, reply, rpc);
168 if (!ok) {
169 noteDud(src);
170 return 1;
171 }
172 if (reply.check("carrier")) {
173 std::string carrier = reply.find("carrier").asString();
174 if (!style.quiet) {
176 "Connection found between %s and %s using carrier %s",
177 src.getName().c_str(),
178 dest.getName().c_str(),
179 carrier.c_str());
180 }
181 if (mode == YARP_ENACT_EXISTS) {
182 return (carrier == style.carrier) ? 0 : 1;
183 }
184
185 // This is either a connect or a disconnect command, but the current
186 // connection is connectionless, the other side will not know that we
187 // are closing the connection and therefore will continue sending data.
188 // Therefore we send an explicit disconnect here.
189 bool currentIsConnectionLess = false;
190 bool currentIsPush = true;
191 if (reply.check("push")) {
192 currentIsPush = reply.find("push").asBool();
193 }
194 if (reply.check("connectionless")) {
195 currentIsConnectionLess = reply.find("connectionless").asBool();
196 }
197 if (currentIsConnectionLess && ((reversed && currentIsPush) || (!reversed && !currentIsPush))) {
198 enactConnection(dest, src, style, YARP_ENACT_DISCONNECT, !reversed);
199 }
200 }
201 if (mode == YARP_ENACT_EXISTS) {
202 return 1;
203 }
204
206
207 // Let's ask the destination to connect/disconnect to the source.
208 // We assume the YARP carrier will reverse the connection if
209 // appropriate when connecting.
210 cmd.clear();
211 reply.clear();
212 cmd.addVocab32(act);
213 Contact c = dest;
214 if (!style.carrier.empty()) {
215 c.setCarrier(style.carrier);
216 }
217 if (mode != YARP_ENACT_DISCONNECT) {
218 cmd.addString(c.toString());
219 } else {
220 cmd.addString(c.getName());
221 }
222
223 Contact c2 = src;
224 if (c2.getPort() <= 0) {
226 }
227
228 yCDebug(NETWORK, "** asking %s: %s", src.toString().c_str(), cmd.toString().c_str());
229 ok = NetworkBase::write(c2, cmd, reply, rpc);
230 if (!ok) {
231 noteDud(src);
232 return 1;
233 }
234 std::string msg;
235 if (reply.get(0).isInt32()) {
236 ok = (reply.get(0).asInt32() == 0);
237 msg = reply.get(1).asString();
238 } else {
239 // older protocol
240 // FIXME Is this protocol still in use?
241 msg = reply.get(0).asString();
242 ok = msg[0] == 'A' || msg[0] == 'R';
243 }
244 if (mode == YARP_ENACT_DISCONNECT && !ok) {
245 msg = "no such connection";
246 }
247 if (mode == YARP_ENACT_CONNECT && !ok) {
248 noteDud(dest);
249 }
250 if (!style.quiet) {
251 if (!ok) {
252 yCError(NETWORK, "%s %s", "Failure:", msg.c_str());
253 } else if (style.verboseOnSuccess) {
254 yCInfo(NETWORK, "%s %s", "Success:", msg.c_str());
255 }
256 }
257 return ok ? 0 : 1;
258}
259
260static std::string collectParams(Contact& c)
261{
262 std::string carrier_name = c.getCarrier();
263 auto pos = carrier_name.find('+');
264 if (pos != std::string::npos) {
265 return carrier_name.substr(pos);
266 }
267 return {};
268}
269
270static std::string extractCarrierNameOnly(const std::string& carrier_name_with_params)
271{
272 return carrier_name_with_params.substr(0, carrier_name_with_params.find('+'));
273}
274
275/*
276
277 Connect two ports, bearing in mind that one of them may not be
278 a regular YARP port.
279
280 Normally, YARP sends a request to the source port asking it to
281 connect to the destination port. But the source port may not
282 be capable of initiating connections, in which case we can
283 request the destination port to connect to the source (this
284 is appropriate for carriers that can reverse the initiative).
285
286 The source or destination could also be topic ports, which are
287 entirely virtual. In that case, we just need to tell the name
288 server, and it will take care of the details.
289
290*/
291
292static bool is_log_port(const std::string& src,
293 const std::string& dest)
294{
295 if (src.find ("/log") != std::string::npos &&
296 dest.find("/log") != std::string::npos &&
297 src.find ("/logger") != std::string::npos &&
298 dest.find("/logger") != std::string::npos)
299 {
300 return true;
301 }
302 return false;
303}
304
305static bool is_stats_port(const std::string& src,
306 const std::string& dest)
307{
308 if (src.find ("/stats_monitor") != std::string::npos &&
309 dest.find("/stats_monitor") != std::string::npos &&
310 src.find ("/yarpConnectionsStats") != std::string::npos &&
311 dest.find("/yarpConnectionsStats") != std::string::npos)
312 {
313 return true;
314 }
315 return false;
316}
317
318static void addStatsMonitorIfNeeded(const std::string& src,
319 const std::string& dest,
320 ContactStyle& style,
321 int mode)
322{
323 // If YARP_CONNECTIONS_STATS_ENABLE is set in the environment, automatically
324 // inject the stats_monitor portmonitor into the carrier string for every new connection
325 if (mode == YARP_ENACT_CONNECT && !style.carrier.empty())
326 {
327 std::string statsEnabled;
328 bool statsEnabledFound = false;
329 statsEnabled = yarp::conf::environment::get_string("YARP_CONNECTIONS_STATS_ENABLE", &statsEnabledFound);
330 if (statsEnabledFound && statsEnabled == "1")
331 {
332 //The stats and the log ports must be skipped
333 if (!is_log_port(src,dest) && !is_stats_port(src,dest))
334 {
335 //bool stat_portmonitor_is_available = true;
338 {
339 //If the connection already uses a portmonitor, we need to concatenate the stats_monitor to the existing portmonitor.
340 if (style.carrier.find("portmonitor") != std::string::npos)
341 {
342 // Find the next available numeric suffix for file/type parameters.
343 // "file." (no suffix) is treated as the base entry already present;
344 // we scan "file1.", "file2.", ... and take the first missing index.
345 int idx = 1;
346 while (style.carrier.find("file" + std::to_string(idx) + ".") != std::string::npos) {
347 ++idx;
348 }
349 const std::string suffix = std::to_string(idx);
350 style.carrier += "+file" + suffix + ".stats_monitor+type" + suffix + ".dll";
351 }
352 // otherwise, we inject the stats_monitor portmonitor into the carrier string
353 else
354 {
355 style.carrier += "+send.portmonitor+file.stats_monitor+type.dll";
356 }
357 }
358 else
359 {
360 yCWarning(NETWORK, "Failure: stats_monitor not available. Using default connection.");
361 }
362 }
363 }
364 }
365}
366
367static int metaConnect(const std::string& src,
368 const std::string& dest,
369 ContactStyle style,
370 int mode)
371{
373 "working on connection %s to %s (%s)",
374 src.c_str(),
375 dest.c_str(),
376 (mode == YARP_ENACT_CONNECT) ? "connect" : ((mode == YARP_ENACT_DISCONNECT) ? "disconnect" : "check"));
377 // check if source name and destination name contain spaces
378 if (dest.find(' ') != std::string::npos || src.find(' ') != std::string::npos) {
380 "Failure: no way to make connection %s->%s, one of the port names contains a space character.",
381 src.c_str(),
382 dest.c_str());
383 return 1;
384 }
385
387 "METACONNECT: src=%s dest=%s style=%s",
388 src.c_str(),
389 dest.c_str(),
390 style.carrier.c_str());
391
392 // get the expressed contacts, without name server input
395
397 "DYNAMIC_SRC: name=%s, carrier=%s",
398 dynamicSrc.getName().c_str(),
399 dynamicSrc.getCarrier().c_str());
401 "DYNAMIC_DST: name=%s, carrier=%s",
402 dynamicDest.getName().c_str(),
403 dynamicDest.getCarrier().c_str());
404
407 "Failure: no way to make connection, invalid source '%s'",
408 dynamicSrc.getName().c_str());
409 return 1;
410 }
413 "Failure: no way to make connection, invalid destination '%s'",
414 dynamicDest.getName().c_str());
415 return 1;
416 }
417
418 bool topical = style.persistent;
419 if (dynamicSrc.getCarrier() == "topic" || dynamicDest.getCarrier() == "topic") {
420 topical = true;
421 }
422
423 bool topicalNeedsLookup = !getNameSpace().connectionHasNameOfEndpoints();
424
425 // fetch completed contacts from name server, if needed
430 if (!staticSrc.isValid()) {
431 if (!style.persistent) {
432 if (!style.quiet) {
434 "Failure: could not find source port %s",
435 src.c_str());
436 }
437 return 1;
438 }
440 }
441 } else {
443 }
444 if (staticSrc.getCarrier().empty()) {
445 staticSrc.setCarrier("tcp");
446 }
447 if (staticDest.getCarrier().empty()) {
448 staticDest.setCarrier("tcp");
449 }
450
453 if (!staticDest.isValid()) {
454 if (!style.persistent) {
455 if (!style.quiet) {
457 "Failure: could not find destination port %s",
458 dest.c_str());
459 }
460 return 1;
461 }
463 }
464 } else {
466 }
467
469 "STATIC_SRC: name=%s, carrier=%s",
470 staticSrc.getName().c_str(),
471 staticSrc.getCarrier().c_str());
473 "STATIC_DST: name=%s, carrier=%s",
474 staticDest.getName().c_str(),
475 staticDest.getCarrier().c_str());
476
477 //DynamicSrc and DynamicDst are the contacts created by connect command
478 //while staticSrc and staticDest are contacts created by querying the server
479 std::string carrierConstraint;
480
481 // see if we can do business with the source port
482 bool srcIsCompetent = false;
483 bool srcIsTopic = false;
484 if (staticSrc.getCarrier() != "topic") {
485 if (!topical) {
486 Carrier* srcCarrier = nullptr;
488 "staticSrc.getCarrier = %s",
489 staticSrc.getCarrier().c_str());
490 if (!staticSrc.getCarrier().empty()) {
492 }
493 if (srcCarrier != nullptr) {
495 "srcCarrier is NOT null; its name is %s",
496 srcCarrier->getName().c_str());
497 std::string srcBootstrap = srcCarrier->getBootstrapCarrierName();
498 if (!srcBootstrap.empty()) {
499
501 "it is competent (bootstrapname is %s), while its name is %s",
502 srcBootstrap.c_str(),
503 srcCarrier->getName().c_str());
504 srcIsCompetent = true;
505 } else {
506 //if the srcCarrier is not competent, (that is it can't perform the starting yarp handshaking)
507 //set the carrier constraint equal to the carrier with which the posrt had been registered.
508 carrierConstraint = staticSrc.getCarrier();
510 "it is NOT competent. its constraint is %s",
511 carrierConstraint.c_str());
512 }
513 delete srcCarrier;
514 srcCarrier = nullptr;
515 }
516 }
517 } else {
518 srcIsTopic = true;
519 }
520
521 // see if we can do business with the destination port
522 bool destIsCompetent = false;
523 bool destIsTopic = false;
524 if (staticDest.getCarrier() != "topic") {
525 if (!topical) {
526 Carrier* destCarrier = nullptr;
528 "staticDest.getCarrier = %s",
529 staticDest.getCarrier().c_str());
530 if (!staticDest.getCarrier().empty()) {
532 }
533 if (destCarrier != nullptr) {
535 "destCarrier is NOT null; its name is %s",
536 destCarrier->getName().c_str());
537 std::string destBootstrap = destCarrier->getBootstrapCarrierName();
538 if (!destBootstrap.empty()) {
540 "it is competent (bootstrapname is %s), while its name is %s",
541 destBootstrap.c_str(),
542 destCarrier->getName().c_str());
543 destIsCompetent = true;
544 } else {
545 //if the destCarrier is not competent, (that is it can't perform the starting yarp handshaking)
546 //set the carrier constraint equal to the carrier with which the posrt had been registered.
547 carrierConstraint = staticDest.getCarrier();
549 "it is NOT competent. its constraint is %s",
550 carrierConstraint.c_str());
551 }
552 delete destCarrier;
553 destCarrier = nullptr;
554 }
555 }
556 } else {
557 destIsTopic = true;
558 }
559
560 if (srcIsTopic || destIsTopic) {
561 Bottle cmd;
562 Bottle reply;
563 NameSpace& ns = getNameSpace();
564
565 bool ok = false;
566 if (srcIsTopic) {
567 if (mode == YARP_ENACT_CONNECT) {
568 ok = ns.connectTopicToPort(staticSrc, staticDest, style);
569 } else if (mode == YARP_ENACT_DISCONNECT) {
571 } else {
572 yCError(NETWORK, "Failure: cannot check subscriptions yet");
573 return 1;
574 }
575 } else {
576 if (mode == YARP_ENACT_CONNECT) {
577 ok = ns.connectPortToTopic(staticSrc, staticDest, style);
578 } else if (mode == YARP_ENACT_DISCONNECT) {
580 } else {
581 yCError(NETWORK, "Failure: cannot check subscriptions yet");
582 return 1;
583 }
584 }
585 if (!ok) {
586 return 1;
587 }
588 if (!style.quiet) {
589 if (style.verboseOnSuccess) {
590 yCInfo(NETWORK, "Success: connection to topic %s.", mode == YARP_ENACT_CONNECT ? "added" : "removed");
591 }
592 }
593 return 0;
594 }
595
597 "dynamicSrc.getCarrier() = %s",
598 dynamicSrc.getCarrier().c_str());
600 "dynamicDest.getCarrier() = %s",
601 dynamicDest.getCarrier().c_str());
603 "staticSrc.getCarrier() = %s",
604 staticSrc.getCarrier().c_str());
606 "staticDest.getCarrier() = %s",
607 staticDest.getCarrier().c_str());
609 "carrierConstraint is %s",
610 carrierConstraint.c_str());
611
613 "style.carrier (1) is %s",
614 style.carrier.c_str());
615
616
617 if (!dynamicSrc.getCarrier().empty()) { //if in connect command the user specified the carrier of src port
618 style.carrier = dynamicSrc.getCarrier();
620 "style.carrier is %s ==> in connect command the user specified the carrier of src port",
621 style.carrier.c_str());
622 }
623
624 if (!dynamicDest.getCarrier().empty()) { //if in connect command the user specified the carrier of dest port or the carrier of the connection
625 style.carrier = dynamicDest.getCarrier();
627 "style.carrier is %s ==> in connect command the user specified the carrier of dest port or the carrier of the connection",
628 style.carrier.c_str());
629 }
630
632 "at the end style style.carrier is %s",
633 style.carrier.c_str());
634
635 //here we'll check if the style carrier and the constraint carrier are equal.
636 //note that in both string may contain params of carrier, so we need to comapare only the name of carrier.
637 if (!style.carrier.empty() && !carrierConstraint.empty()) {
638 //get only carrier name of style.
640
641 //get only carrier name of carrierConstraint.
643
645 yCError(NETWORK, "Failure: conflict between %s and %s", style_carrier_name.c_str(), carrier_constraint_name.c_str());
646 return 1;
647 }
649 "style_carrier_name=%s and carrier_constraint_name=%s are equals!",
650 style_carrier_name.c_str(),
652 }
653 //we are going to choose the carrier of this connection, and we collect parameters specified by user
654 //in order to pass them to the carrier, so it can configure itself.
655 if (!carrierConstraint.empty()) {
657 //if I'm here means that sorce or dest is not competent.
658 //so we need to get parameters of carrier given in connect command.
660 "if I'm here means that source or dest is not competent");
661 std::string c = dynamicSrc.getCarrier();
664 }
665 c = dynamicDest.getCarrier();
668 }
669 }
670 if (style.carrier.empty()) {
671 style.carrier = staticDest.getCarrier();
672 //if I'm here means that both src and dest are copentent and the user didn't specified a carrier in the connect command
674 "if I'm here means that both src and dest are compentent and the user didn't specified a carrier in the connect command");
675 std::string c = dynamicSrc.getCarrier();
678 }
679 }
680
681 if (style.carrier.empty()) {
682 style.carrier = staticSrc.getCarrier();
683 yCTrace(NETWORK, "the chosen style carrier is static src");
684 }
685
686 //now stylecarrier contains the carrier chosen for this connection
687
689 "style_carrier with params =%s",
690 style.carrier.c_str());
691
692 addStatsMonitorIfNeeded(src,dest,style,mode);
693
694 bool connectionIsPush = false;
695 bool connectionIsPull = false;
696 Carrier* connectionCarrier = nullptr;
697 if (style.carrier != "topic") {
699 if (connectionCarrier != nullptr) {
702 }
703 }
704
705 int result = -1;
707 // Classic case.
709 delete connectionCarrier;
710 return enactConnection(staticSrc, c, style, mode, false);
711 }
714 delete connectionCarrier;
715 return enactConnection(staticDest, c, style, mode, true);
716 }
717
718 if (connectionCarrier != nullptr) {
719 if (!connectionIsPull) {
721 result = connectionCarrier->connect(staticSrc, c, style, mode, false);
722 } else {
724 result = connectionCarrier->connect(staticDest, c, style, mode, true);
725 }
726 }
727 if (connectionCarrier != nullptr) {
728 delete connectionCarrier;
729 connectionCarrier = nullptr;
730 }
731 if (result != -1) {
732 if (!style.quiet) {
733 if (result == 0) {
734 if (style.verboseOnSuccess) {
735 yCInfo(NETWORK, "Success: added connection using custom carrier method");
736 }
737 } else {
738 yCError(NETWORK, "Failure: custom carrier method did not work");
739 }
740 }
741 return result;
742 }
743
744 if (mode != YARP_ENACT_DISCONNECT) {
745 yCError(NETWORK, "Failure: no way to make connection %s->%s", src.c_str(), dest.c_str());
746 }
747
748 return 1;
749}
750
751bool NetworkBase::connect(const std::string& src, const std::string& dest, const std::string& carrier, bool quiet)
752{
753 ContactStyle style;
754 style.quiet = quiet;
755 if (!carrier.empty()) {
756 style.carrier = carrier;
757 }
758 return connect(src, dest, style);
759}
760
761bool NetworkBase::connect(const std::string& src,
762 const std::string& dest,
763 const ContactStyle& style)
764{
765 int result = metaConnect(src, dest, style, YARP_ENACT_CONNECT);
766 return result == 0;
767}
768
769bool NetworkBase::disconnect(const std::string& src,
770 const std::string& dest,
771 bool quiet)
772{
773 ContactStyle style;
774 style.quiet = quiet;
775 return disconnect(src, dest, style);
776}
777
778bool NetworkBase::disconnect(const std::string& src,
779 const std::string& dest,
780 const ContactStyle& style)
781{
782 int result = metaConnect(src, dest, style, YARP_ENACT_DISCONNECT);
783 return result == 0;
784}
785
786bool NetworkBase::disconnect(const std::string& src, const std::string& dest, const std::string& carrier, bool quiet)
787{
788 ContactStyle style;
789 style.quiet = quiet;
790 if (!carrier.empty()) {
791 style.carrier = carrier;
792 }
793 return disconnect(src, dest, style);
794}
795
796bool NetworkBase::isConnected(const std::string& src,
797 const std::string& dest,
798 bool quiet)
799{
800 ContactStyle style;
801 style.quiet = quiet;
802 return isConnected(src, dest, style);
803}
804
805bool NetworkBase::isConnected(const std::string& src, const std::string& dest, const std::string& carrier, bool quiet)
806{
807 ContactStyle style;
808 style.quiet = quiet;
809 if (!carrier.empty()) {
810 style.carrier = carrier;
811 }
812 return isConnected(src, dest, style);
813}
814
815bool NetworkBase::exists(const std::string& port, bool quiet, bool checkVer)
816{
817 ContactStyle style;
818 style.quiet = quiet;
819 return exists(port, style, checkVer);
820}
821
822bool NetworkBase::exists(const std::string& port, const ContactStyle& style, bool checkVer)
823{
824 bool silent = style.quiet;
825 Contact address = NetworkBase::queryName(port);
826 if (!address.isValid()) {
827 if (!silent) {
828 yCInfo(NETWORK, "Address of port %s is not valid", port.c_str());
829 }
830 return false;
831 }
832
833 Contact address2(address);
834 if (style.timeout >= 0) {
835 address2.setTimeout((float)style.timeout);
836 }
838
839 if (out == nullptr) {
840 if (!silent) {
841 yCInfo(NETWORK, "Cannot connect to port %s", port.c_str());
842 }
843 return false;
844 }
845 out->close();
846 delete out;
847 out = nullptr;
848
849 if (!checkVer) {
850 return true;
851 }
852
853 ContactStyle style2 = style;
854 style2.admin = true;
855 Bottle cmd("[ver]");
856 Bottle resp;
857 bool ok = NetworkBase::write(Contact(port), cmd, resp, style2);
858 if (!ok) {
859 return false;
860 }
861 if (resp.get(0).toString() != "ver" && resp.get(0).toString() != "dict") {
862 // YARP nameserver responds with a version
863 // ROS nameserver responds with a dictionary of error data
864 // Treat everything else an unknown
865 return false;
866 }
867
868 return true;
869}
870
871
872bool NetworkBase::waitConnection(const std::string& source, const std::string& destination, bool quiet)
873{
874 int ct = 1;
875 while (true) {
876
877 if (ct % 30 == 1) {
878 if (!quiet) {
879 yCInfo(NETWORK, "Waiting for %s->%s...", source.c_str(), destination.c_str());
880 }
881 }
882 ct++;
883
884 int result = NetworkBase::isConnected(source, destination, true) ? 0 : 1;
885 if (result != 0) {
887 } else {
888 return true;
889 }
890 }
891}
892
893
894bool NetworkBase::waitPort(const std::string& target, bool quiet)
895{
896 int ct = 1;
897 while (true) {
898
899 if (ct % 30 == 1) {
900 if (!quiet) {
901 yCInfo(NETWORK, "Waiting for %s...", target.c_str());
902 }
903 }
904 ct++;
905
906 bool result = exists(target, true, false);
907 if (!result) {
909 } else {
910 return true;
911 }
912 }
913}
914
915
916bool NetworkBase::sync(const std::string& port, bool quiet)
917{
918 bool result = waitPort(port, quiet);
919 if (result) {
920 poll(port, true);
921 }
922 return result;
923}
924
925
930
939
944
949
950#if defined(YARP_HAS_ACE)
951namespace {
952class YARP_ACE
953{
954private:
955 YARP_ACE()
956 {
957 ACE::init();
958 }
959
960public:
961 ~YARP_ACE()
962 {
963 ACE::fini();
964 }
965
966 static YARP_ACE& init()
967 {
968 static YARP_ACE ace;
969 return ace;
970 }
971};
972} // namespace
973#endif
974
975
977{
978 YARP_UNUSED(custom);
979 if (__yarp_is_initialized == 0) {
980 // Broken pipes need to be dealt with through other means
981 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
982
983#ifdef YARP_HAS_ACE
984 YARP_ACE::init();
985#endif
986
987 // make sure system is actually able to do things fast
989
990 // MultiNameSpace is a c++11 singleton and need to be initialized
991 // before the first port that is opened and it has to exist until
992 // the last port is closed.
993 getNameSpace();
994
995 // Increment counter before initializing the clock.
997
998 // The network clock open a port, and inside the open of the port
999 // the __yarp_is_initialized counter is checked > 0.
1002 }
1003 } else {
1005 }
1006}
1007
1009{
1010 if (__yarp_is_initialized == 1) {
1011 // The log forwarder needs to be shut down in order to close the
1012 // internal port. The shutdown method will do nothing if the
1013 // LogForwarded was not used.
1015
1018
1019 // reset system timer resolution
1021 }
1022 if (__yarp_is_initialized > 0) {
1024 }
1025}
1026
1028{
1029 std::string clock;
1032 if (!clock.empty()) {
1034 } else {
1036 }
1037 }
1038
1039 switch (clockType) {
1040 case YARP_CLOCK_SYSTEM:
1041 yCDebug(NETWORK, "Using SYSTEM clock");
1043 break;
1044
1045 case YARP_CLOCK_NETWORK:
1046 yCDebug(NETWORK, "Using NETWORK clock");
1048 // check of valid parameter is done inside the call, throws YARP_FAIL in case of error
1050 break;
1051
1052 case YARP_CLOCK_CUSTOM: {
1053 yCDebug(NETWORK, "Using CUSTOM clock");
1054 // check of valid parameter is done inside the call, throws YARP_FAIL in case of error
1056 } break;
1057
1058 default:
1059 yCFatal(NETWORK, "yarpClockInit called with unknown clock type. Quitting");
1060 break;
1061 }
1062}
1063
1064Contact NetworkBase::queryName(const std::string& name)
1065{
1066 yCDebug(NETWORK, "query name %s", name.c_str());
1067 if (getNameServerName() == name) {
1068 yCDebug(NETWORK, "query recognized as name server: %s", name.c_str());
1069 return getNameServerContact();
1070 }
1071 Contact c = c.fromString(name);
1072 if (c.isValid() && c.getPort() > 0) {
1073 return c;
1074 }
1075 return getNameSpace().queryName(name);
1076}
1077
1078
1079Contact NetworkBase::registerName(const std::string& name)
1080{
1081 yCDebug(NETWORK, "register name %s", name.c_str());
1082 return getNameSpace().registerName(name);
1083}
1084
1085
1087{
1088 yCDebug(NETWORK, "register contact %s", contact.toString().c_str());
1089 return getNameSpace().registerContact(contact);
1090}
1091
1092Contact NetworkBase::unregisterName(const std::string& name)
1093{
1094 return getNameSpace().unregisterName(name);
1095}
1096
1097
1099{
1100 return getNameSpace().unregisterContact(contact);
1101}
1102
1103
1104bool NetworkBase::setProperty(const char* name,
1105 const char* key,
1106 const Value& value)
1107{
1108 return getNameSpace().setProperty(name, key, value);
1109}
1110
1111
1112Value* NetworkBase::getProperty(const char* name, const char* key)
1113{
1114 return getNameSpace().getProperty(name, key);
1115}
1116
1117
1119{
1120 return getNameSpace().setLocalMode(flag);
1121}
1122
1124{
1125 NameSpace& ns = getNameSpace();
1126 return ns.localOnly();
1127}
1128
1130{
1131 // could replace with ACE assertions, except should not
1132 // evaporate in release mode
1134}
1135
1136bool NetworkBase::setConnectionQos(const std::string& src, const std::string& dest, const QosStyle& style, bool quiet)
1137{
1138 return setConnectionQos(src, dest, style, style, quiet);
1139}
1140
1141bool NetworkBase::setConnectionQos(const std::string& src, const std::string& dest, const QosStyle& srcStyle, const QosStyle& destStyle, bool quiet)
1142{
1143
1144 //e.g., prop set /portname (sched ((priority 30) (policy 1))) (qos ((tos 0)))
1145 yarp::os::Bottle cmd;
1146 yarp::os::Bottle reply;
1147
1148 // ignore if everything left as default
1149 if (srcStyle.getPacketPriorityAsTOS() != -1 || srcStyle.getThreadPolicy() != -1) {
1150 // set the source Qos
1151 cmd.addString("prop");
1152 cmd.addString("set");
1153 cmd.addString(dest.c_str());
1154 Bottle& sched = cmd.addList();
1155 sched.addString("sched");
1156 Property& sched_prop = sched.addDict();
1157 sched_prop.put("priority", srcStyle.getThreadPriority());
1158 sched_prop.put("policy", srcStyle.getThreadPolicy());
1159 Bottle& qos = cmd.addList();
1160 qos.addString("qos");
1161 Property& qos_prop = qos.addDict();
1162 qos_prop.put("tos", srcStyle.getPacketPriorityAsTOS());
1164 bool ret = write(srcCon, cmd, reply, true, true, 2.0);
1165 if (!ret) {
1166 if (!quiet) {
1167 yCError(NETWORK, "Cannot write to '%s'", src.c_str());
1168 }
1169 return false;
1170 }
1171 if (reply.get(0).asString() != "ok") {
1172 if (!quiet) {
1173 yCError(NETWORK, "Cannot set qos properties of '%s'. (%s)", src.c_str(), reply.toString().c_str());
1174 }
1175 return false;
1176 }
1177 }
1178
1179 // ignore if everything left as default
1180 if (destStyle.getPacketPriorityAsTOS() != -1 || destStyle.getThreadPolicy() != -1) {
1181 // set the destination Qos
1182 cmd.clear();
1183 reply.clear();
1184 cmd.addString("prop");
1185 cmd.addString("set");
1186 cmd.addString(src.c_str());
1187 Bottle& sched2 = cmd.addList();
1188 sched2.addString("sched");
1189 Property& sched_prop2 = sched2.addDict();
1190 sched_prop2.put("priority", destStyle.getThreadPriority());
1191 sched_prop2.put("policy", destStyle.getThreadPolicy());
1192 Bottle& qos2 = cmd.addList();
1193 qos2.addString("qos");
1194 Property& qos_prop2 = qos2.addDict();
1195 qos_prop2.put("tos", destStyle.getPacketPriorityAsTOS());
1197 bool ret = write(destCon, cmd, reply, true, true, 2.0);
1198 if (!ret) {
1199 if (!quiet) {
1200 yCError(NETWORK, "Cannot write to '%s'", dest.c_str());
1201 }
1202 return false;
1203 }
1204 if (reply.get(0).asString() != "ok") {
1205 if (!quiet) {
1206 yCError(NETWORK, "Cannot set qos properties of '%s'. (%s)", dest.c_str(), reply.toString().c_str());
1207 }
1208 return false;
1209 }
1210 }
1211 return true;
1212}
1213
1215{
1216 NameConfig nc;
1217 std::string name = nc.getNamespace();
1218 Bottle msg, reply;
1219 msg.addString("bot");
1220 msg.addString("list");
1221 yCInfo(NETWORK, "Requesting list of ports from name server... ");
1222 NetworkBase::write(name,
1223 msg,
1224 reply);
1225 int ct = reply.size()-1;
1226 yCInfo(NETWORK, "got %d port%s", ct, (ct!=1)?"s":"");
1227
1228 for (size_t i=1; i<reply.size(); i++) {
1229 Bottle *entry = reply.get(i).asList();
1230 if (entry != nullptr) {
1231 std::string port = entry->check("name", Value("")).asString();
1232 if (port!="" && port!="fallback" && port!=name) {
1234 if (c.getCarrier()=="mcast") {
1235 yCInfo(NETWORK, "Skipping mcast port %s...", port.c_str());
1236 } else {
1237 Contact addr = c;
1238 yCInfo(NETWORK, "Testing %s at %s",
1239 port.c_str(),
1240 addr.toURI().c_str());
1241 if (addr.isValid()) {
1242 if (timeout>=0) {
1243 addr.setTimeout((float)timeout);
1244 }
1246 if (out == nullptr) {
1247 yCInfo(NETWORK, "* No response, removing port %s", port.c_str());
1249 } else {
1250 delete out;
1251 }
1252 }
1253 }
1254 } else {
1255 if (port!="") {
1256 yCInfo(NETWORK, "Ignoring %s", port.c_str());
1257 }
1258 }
1259 }
1260 }
1261 yCInfo(NETWORK, "Giving name server a chance to do garbage collection.");
1263 Bottle cmd2("gc"), reply2;
1265 yCInfo(NETWORK, "Name server says: %s", reply2.toString().c_str());
1266
1267 return true;
1268}
1269
1270static bool getPortQos(const std::string& port, const std::string& unit, QosStyle& style, bool quiet)
1271{
1272 // request: "prop get /portname"
1273 // reply : "(sched ((priority 30) (policy 1))) (qos ((priority HIGH)))"
1274 yarp::os::Bottle cmd;
1275 yarp::os::Bottle reply;
1276
1277 // set the source Qos
1278 cmd.addString("prop");
1279 cmd.addString("get");
1280 cmd.addString(unit.c_str());
1282 bool ret = NetworkBase::write(portCon, cmd, reply, true, true, 2.0);
1283 if (!ret) {
1284 if (!quiet) {
1285 yCError(NETWORK, "Cannot write to '%s'", port.c_str());
1286 }
1287 return false;
1288 }
1289 if (reply.size() == 0 || reply.get(0).asString() == "fail") {
1290 if (!quiet) {
1291 yCError(NETWORK, "Cannot get qos properties of '%s'. (%s)", port.c_str(), reply.toString().c_str());
1292 }
1293 return false;
1294 }
1295
1296 Bottle& sched = reply.findGroup("sched");
1297 Bottle* sched_prop = sched.find("sched").asList();
1298 style.setThreadPriority(sched_prop->find("priority").asInt32());
1299 style.setThreadPolicy(sched_prop->find("policy").asInt32());
1300 Bottle& qos = reply.findGroup("qos");
1301 Bottle* qos_prop = qos.find("qos").asList();
1302 style.setPacketPrioritybyTOS(qos_prop->find("tos").asInt32());
1303
1304 return true;
1305}
1306
1307bool NetworkBase::getConnectionQos(const std::string& src, const std::string& dest, QosStyle& srcStyle, QosStyle& destStyle, bool quiet)
1308{
1309 if (!getPortQos(src, dest, srcStyle, quiet)) {
1310 return false;
1311 }
1312 if (!getPortQos(dest, src, destStyle, quiet)) {
1313 return false;
1314 }
1315 return true;
1316}
1317
1318bool NetworkBase::isValidPortName(const std::string& portName)
1319{
1320 if (portName.empty()) {
1321 return false;
1322 }
1323
1324 if (portName == "...") {
1325 return true;
1326 }
1327
1328 if (portName.at(0) != '/') {
1329 return false;
1330 }
1331
1332 if (portName.at(portName.size() - 1) == '/') {
1333 return false;
1334 }
1335
1336 if (portName.find(' ') != std::string::npos) {
1337 return false;
1338 }
1339
1340 return true;
1341}
1342
1343
1344bool NetworkBase::write(const Contact& contact,
1345 PortWriter& cmd,
1346 PortReader& reply,
1347 bool admin,
1348 bool quiet,
1349 double timeout)
1350{
1351 ContactStyle style;
1352 style.admin = admin;
1353 style.quiet = quiet;
1354 style.timeout = timeout;
1355 style.carrier = contact.getCarrier();
1356 return write(contact, cmd, reply, style);
1357}
1358
1359bool NetworkBase::write(const Contact& contact,
1360 PortWriter& cmd,
1361 PortReader& reply,
1362 const ContactStyle& style)
1363{
1364 if (!getNameSpace().serverAllocatesPortNumbers()) {
1365 // switch to more up-to-date method
1366
1367 Port port;
1368 port.setAdminMode(style.admin);
1369 port.openFake("network_write");
1370 Contact ec = contact;
1371 if (!style.carrier.empty()) {
1372 ec.setCarrier(style.carrier);
1373 }
1374 if (!port.addOutput(ec)) {
1375 if (!style.quiet) {
1376 yCError(NETWORK, "Cannot make connection to '%s'", ec.toString().c_str());
1377 }
1378 return false;
1379 }
1380
1381 bool ok = port.write(cmd, reply);
1382 return ok;
1383 }
1384
1385 const char* connectionName = "admin";
1386 std::string name = contact.getName();
1387 const char* targetName = name.c_str(); // use carefully!
1388 Contact address = contact;
1389 if (!address.isValid()) {
1390 address = getNameSpace().queryName(targetName);
1391 }
1392 if (!address.isValid()) {
1393 if (!style.quiet) {
1394 yCError(NETWORK, "cannot find port %s", targetName);
1395 }
1396 return false;
1397 }
1398
1399 if (style.timeout > 0) {
1400 address.setTimeout((float)style.timeout);
1401 }
1402 OutputProtocol* out = Carriers::connect(address);
1403 if (out == nullptr) {
1404 if (!style.quiet) {
1405 yCError(NETWORK, "Cannot connect to port %s", targetName);
1406 }
1407 return false;
1408 }
1409 if (style.timeout > 0) {
1410 out->setTimeout(style.timeout);
1411 }
1412
1413 Route r(connectionName, targetName, (!style.carrier.empty()) ? style.carrier.c_str() : "text_ack");
1414 out->open(r);
1415
1416 PortCommand pc(0, style.admin ? "a" : "d");
1417 BufferedConnectionWriter bw(out->getConnection().isTextMode(),
1418 out->getConnection().isBareMode());
1419 bool ok = true;
1420 if (out->getConnection().canEscape()) {
1421 ok = pc.write(bw);
1422 }
1423 if (!ok) {
1424 if (!style.quiet) {
1425 yCError(NETWORK, "could not write to connection");
1426 }
1427 delete out;
1428 return false;
1429 }
1430 ok = cmd.write(bw);
1431 if (!ok) {
1432 if (!style.quiet) {
1433 yCError(NETWORK, "could not write to connection");
1434 }
1435 delete out;
1436 return false;
1437 }
1438 if (style.expectReply) {
1439 bw.setReplyHandler(reply);
1440 }
1441 out->write(bw);
1442 if (out != nullptr) {
1443 delete out;
1444 out = nullptr;
1445 }
1446 return true;
1447}
1448
1449bool NetworkBase::write(const std::string& port_name,
1450 PortWriter& cmd,
1451 PortReader& reply)
1452{
1453 return write(Contact(port_name), cmd, reply);
1454}
1455
1456bool NetworkBase::isConnected(const std::string& src, const std::string& dest, const ContactStyle& style)
1457{
1458 int result = metaConnect(src, dest, style, YARP_ENACT_EXISTS);
1459 if (result != 0) {
1460 if (!style.quiet) {
1461 yCInfo(NETWORK, "No connection from %s to %s found",
1462 src.c_str(),
1463 dest.c_str());
1464 }
1465 }
1466 return result == 0;
1467}
1468
1469
1471{
1472 NameConfig nc;
1473 std::string name = nc.getNamespace(false);
1474 return name;
1475}
1476
1477
1479{
1480 return getNameSpace().getNameServerContact();
1481}
1482
1483
1484bool NetworkBase::setNameServerName(const std::string& name)
1485{
1486 NameConfig nc;
1487 std::string fname = nc.getConfigFileName(YARP_CONFIG_NAMESPACE_FILENAME);
1488 nc.writeConfig(fname, name + "\n");
1489 nc.getNamespace(true);
1490 getNameSpace().activate(true);
1491 return true;
1492}
1493
1494
1496{
1497 return getNameSpace().checkNetwork();
1498}
1499
1500
1501bool NetworkBase::checkNetwork(double timeout)
1502{
1503 return getNameSpace().checkNetwork(timeout);
1504}
1505
1506
1508{
1509 return __yarp_is_initialized > 0;
1510}
1511
1512#ifndef YARP_NO_DEPRECATED // Since YARP 3.4
1523#endif
1524
1526{
1527 getNameSpace().queryBypass(store);
1528}
1529
1531{
1532 return getNameSpace().getQueryBypass();
1533}
1534
1535namespace {
1536std::mutex& getNetworkMutex()
1537{
1538 static std::mutex mutex;
1539 return mutex;
1540}
1541} // namespace
1542
1544{
1545 getNetworkMutex().lock();
1546}
1547
1549{
1550 getNetworkMutex().unlock();
1551}
1552
1553
1554int NetworkBase::sendMessage(const std::string& port,
1556 bool silent)
1557{
1558 std::string output;
1559 return sendMessage(port, writable, output, silent);
1560}
1561
1562int NetworkBase::sendMessage(const std::string& port,
1564 std::string& output,
1565 bool quiet)
1566{
1567 output = "";
1569 if (!srcAddress.isValid()) {
1570 if (!quiet) {
1571 yCError(NETWORK, "Cannot find port named %s", port.c_str());
1572 }
1573 return 1;
1574 }
1576 if (out == nullptr) {
1577 if (!quiet) {
1578 yCError(NETWORK, "Cannot connect to port named %s at %s", port.c_str(), srcAddress.toURI().c_str());
1579 }
1580 return 1;
1581 }
1582 Route route("admin", port, "text");
1583
1584
1585 bool ok = out->open(route);
1586 if (!ok) {
1587 if (!quiet) {
1588 yCError(NETWORK, "Cannot make connection");
1589 }
1590 delete out;
1591 return 1;
1592 }
1593
1594 BufferedConnectionWriter bw(out->getConnection().isTextMode());
1595 PortCommand disconnect('\0', "q");
1596 bool wok = writable.write(bw);
1597 if (!wok) {
1598 if (!quiet) {
1599 yCError(NETWORK, "Cannot write on connection");
1600 }
1601 delete out;
1602 return 1;
1603 }
1604 if (!disconnect.write(bw)) {
1605 if (!quiet) {
1606 yCError(NETWORK, "Cannot write on connection");
1607 }
1608 delete out;
1609 return 1;
1610 }
1611
1612 out->write(bw);
1613 InputProtocol& ip = out->getInput();
1614 ConnectionReader& con = ip.beginRead();
1615 Bottle b;
1616 b.read(con);
1617 b.read(con);
1618 output = b.toString();
1619 if (!quiet) {
1620 yCInfo(NETWORK, "%s", b.toString().c_str());
1621 }
1622 ip.endRead();
1623 out->close();
1624 delete out;
1625 out = nullptr;
1626
1627 return 0;
1628}
1629
1630int NetworkBase::poll(const std::string& target, bool silent)
1631{
1632 PortCommand pc('\0', "*");
1633 return sendMessage(target, pc, silent);
1634}
1635
1636int NetworkBase::disconnectInput(const std::string& src,
1637 const std::string& dest,
1638 bool silent)
1639{
1640 PortCommand pc('\0', std::string("~") + dest);
1641 return sendMessage(src, pc, silent);
1642}
1643
1645{
1646public:
1650
1652 {
1653 owner = nullptr;
1654 factory = nullptr;
1655 }
1656
1658 Carrier* owner) :
1660 owner(owner)
1661 {
1662 factory->addRef();
1663 car.open(*factory);
1664 }
1665
1667 {
1668 car.close();
1669 if (factory == nullptr) {
1670 return;
1671 }
1672 factory->removeRef();
1673 if (factory->getReferenceCount() <= 0) {
1674 delete factory;
1675 }
1676 factory = nullptr;
1677 }
1678
1680 {
1681 return car.getContent();
1682 }
1683
1684 virtual const Carrier& getContent() const
1685 {
1686 return car.getContent();
1687 }
1688
1689 Carrier* create() const override
1690 {
1691 return owner->create();
1692 }
1693
1694
1695 // Forward yarp::os::Connection methods
1696
1697 bool isValid() const override
1698 {
1699 return car.isValid();
1700 }
1701
1702 bool isTextMode() const override
1703 {
1704 return getContent().isTextMode();
1705 }
1706
1707 bool isBareMode() const override
1708 {
1709 return getContent().isBareMode();
1710 }
1711
1712 bool canEscape() const override
1713 {
1714 return getContent().canEscape();
1715 }
1716
1717 void handleEnvelope(const std::string& envelope) override
1718 {
1719 getContent().handleEnvelope(envelope);
1720 }
1721
1722 bool requireAck() const override
1723 {
1724 return getContent().requireAck();
1725 }
1726
1727 bool supportReply() const override
1728 {
1729 return getContent().supportReply();
1730 }
1731
1732 bool isLocal() const override
1733 {
1734 return getContent().isLocal();
1735 }
1736
1737 bool isPush() const override
1738 {
1739 return getContent().isPush();
1740 }
1741
1742 bool isConnectionless() const override
1743 {
1744 return getContent().isConnectionless();
1745 }
1746
1747 bool isBroadcast() const override
1748 {
1749 return getContent().isBroadcast();
1750 }
1751
1752 bool isActive() const override
1753 {
1754 return getContent().isActive();
1755 }
1756
1757 bool modifiesIncomingData() const override
1758 {
1759 return getContent().modifiesIncomingData();
1760 }
1761
1763 {
1764 return getContent().modifyIncomingData(reader);
1765 }
1766
1768 {
1769 return getContent().acceptIncomingData(reader);
1770 }
1771
1772 bool modifiesOutgoingData() const override
1773 {
1774 return getContent().modifiesOutgoingData();
1775 }
1776
1777 const PortWriter& modifyOutgoingData(const PortWriter& writer) override
1778 {
1779 return getContent().modifyOutgoingData(writer);
1780 }
1781
1782 bool acceptOutgoingData(const PortWriter& writer) override
1783 {
1784 return getContent().acceptOutgoingData(writer);
1785 }
1786
1787 bool modifiesReply() const override
1788 {
1789 return getContent().modifiesReply();
1790 }
1791
1793 {
1794 return getContent().modifyReply(reader);
1795 }
1796
1797 void setCarrierParams(const Property& params) override
1798 {
1799 getContent().setCarrierParams(params);
1800 }
1801
1802 void getCarrierParams(Property& params) const override
1803 {
1804 getContent().getCarrierParams(params);
1805 }
1806
1807 void getHeader(yarp::os::Bytes& header) const override
1808 {
1809 getContent().getHeader(header);
1810 }
1811
1812 void prepareDisconnect() override
1813 {
1814 getContent().prepareDisconnect();
1815 }
1816
1817 std::string getName() const override
1818 {
1819 return getContent().getName();
1820 }
1821
1822
1823 // Forward yarp::os::Carrier methods
1824
1825 bool checkHeader(const yarp::os::Bytes& header) override
1826 {
1827 return getContent().checkHeader(header);
1828 }
1829
1830 void setParameters(const yarp::os::Bytes& header) override
1831 {
1832 getContent().setParameters(header);
1833 }
1834
1835 bool canAccept() const override
1836 {
1837 return getContent().canAccept();
1838 }
1839
1840 bool canOffer() const override
1841 {
1842 return getContent().canOffer();
1843 }
1844
1846 {
1847 return getContent().prepareSend(proto);
1848 }
1849
1851 {
1852 return getContent().sendHeader(proto);
1853 }
1854
1856 {
1857 return getContent().expectReplyToHeader(proto);
1858 }
1859
1860 bool write(ConnectionState& proto, SizedWriter& writer) override
1861 {
1862 return getContent().write(proto, writer);
1863 }
1864
1865 bool reply(ConnectionState& proto, SizedWriter& writer) override
1866 {
1867 return getContent().reply(proto, writer);
1868 }
1869
1871 {
1872 return getContent().expectExtraHeader(proto);
1873 }
1874
1876 {
1877 return getContent().respondToHeader(proto);
1878 }
1879
1881 {
1882 return getContent().expectIndex(proto);
1883 }
1884
1886 {
1887 return getContent().expectSenderSpecifier(proto);
1888 }
1889
1891 {
1892 return getContent().sendAck(proto);
1893 }
1894
1896 {
1897 return getContent().expectAck(proto);
1898 }
1899
1900 std::string toString() const override
1901 {
1902 return getContent().toString();
1903 }
1904
1905 void close() override
1906 {
1907 getContent().close();
1908 }
1909
1910 std::string getBootstrapCarrierName() const override
1911 {
1912 return getContent().getBootstrapCarrierName();
1913 }
1914
1916 const yarp::os::Contact& dest,
1917 const yarp::os::ContactStyle& style,
1918 int mode,
1919 bool reversed) override
1920 {
1921 return getContent().connect(src, dest, style, mode, reversed);
1922 }
1923
1925 {
1926 return getContent().configure(proto);
1927 }
1929 {
1930 return getContent().configureFromProperty(options);
1931 }
1932
1933 yarp::os::Face* createFace() const override
1934 {
1935 return getContent().createFace();
1936 }
1937};
1938
1939
1941{
1942private:
1943 YarpPluginSettings settings;
1944 YarpPlugin<Carrier> plugin;
1945
1946public:
1947 StubCarrier(const char* dll_name, const char* fn_name)
1948 {
1949 settings.setLibraryMethodName(dll_name, fn_name);
1950 init();
1951 }
1952
1953 StubCarrier(const char* name)
1954 {
1955 settings.setPluginName(name);
1956 init();
1957 }
1958
1959 void init()
1960 {
1961 YarpPluginSelector selector;
1962 selector.scan();
1963 settings.setSelector(selector);
1964 if (plugin.open(settings)) {
1965 car.open(*plugin.getFactory());
1966 settings.setLibraryMethodName(plugin.getFactory()->getName(),
1967 settings.getMethodName());
1968 }
1969 }
1970
1972 {
1973 return car.getContent();
1974 }
1975
1976 const Carrier& getContent() const override
1977 {
1978 return car.getContent();
1979 }
1980
1981 Carrier* create() const override
1982 {
1983 auto* ncar = new ForwardingCarrier(plugin.getFactory(), const_cast<StubCarrier*>(this));
1984 if (ncar == nullptr) {
1985 return nullptr;
1986 }
1987 if (!ncar->isValid()) {
1988 delete ncar;
1989 ncar = nullptr;
1990 return nullptr;
1991 }
1992 return ncar;
1993 }
1994
1995 std::string getDllName() const
1996 {
1997 return settings.getLibraryName();
1998 }
1999
2000 std::string getFnName() const
2001 {
2002 return settings.getMethodName();
2003 }
2004};
2005
2006
2007bool NetworkBase::registerCarrier(const char* name, const char* dll)
2008{
2009 StubCarrier* factory = nullptr;
2010 if (dll == nullptr) {
2011 factory = new StubCarrier(name);
2012 if (factory == nullptr) {
2013 return false;
2014 }
2015 } else {
2016 factory = new StubCarrier(dll, name);
2017 }
2018 if (factory == nullptr) {
2019 yCError(NETWORK, "Failed to register carrier");
2020 return false;
2021 }
2022 if (!factory->isValid()) {
2023 if (dll != nullptr) {
2024 yCError(NETWORK, "Failed to find library %s with carrier %s", dll, name);
2025 } else {
2026 yCError(NETWORK, "Failed to find library support for carrier %s", name);
2027 }
2028 delete factory;
2029 factory = nullptr;
2030 return false;
2031 }
2033 return true;
2034}
2035
2036
2038{
2039 bool globalAlloc = getNameSpace().serverAllocatesPortNumbers();
2040 return !globalAlloc;
2041}
2042
2043
2045 bool& scanNeeded,
2046 bool& serverUsed)
2047{
2048 return getNameSpace().detectNameServer(useDetectedServer,
2049 scanNeeded,
2050 serverUsed);
2051}
2052
2054{
2056 if (!nameServerContact.getName().empty()) {
2058 }
2059 nameConfig.fromFile();
2060 nameConfig.setAddress(nameServerContact);
2061 bool result = nameConfig.toFile();
2062 getNameSpace().activate(true);
2063 return result;
2064}
2065
2066
2068 PortReader& reply,
2069 const ContactStyle& style)
2070{
2071 NameStore* store = getNameSpace().getQueryBypass();
2072 if (store != nullptr) {
2073 Contact contact;
2074 return store->process(cmd, reply, contact);
2075 }
2076 return getNameSpace().writeToNameServer(cmd, reply, style);
2077}
2078
2079
2080std::string NetworkBase::getConfigFile(const char* fname)
2081{
2083}
2084
2085
2087{
2088 std::string range = yarp::conf::environment::get_string("YARP_PORT_RANGE");
2089 if (!range.empty()) {
2090 int irange = yarp::conf::numeric::from_string<int>(range);
2091 if (irange != 0) {
2092 return irange;
2093 }
2094 }
2095 return 10000;
2096}
#define YARP_ENACT_DISCONNECT
Definition Carrier.h:13
#define YARP_ENACT_EXISTS
Definition Carrier.h:14
#define YARP_ENACT_CONNECT
Definition Carrier.h:12
#define YARP_CONFIG_NAMESPACE_FILENAME
Definition NameConfig.h:16
static bool is_stats_port(const std::string &src, const std::string &dest)
Definition Network.cpp:305
static int enactConnection(const Contact &src, const Contact &dest, const ContactStyle &style, int mode, bool reversed)
Definition Network.cpp:121
static bool getPortQos(const std::string &port, const std::string &unit, QosStyle &style, bool quiet)
Definition Network.cpp:1270
static void addStatsMonitorIfNeeded(const std::string &src, const std::string &dest, ContactStyle &style, int mode)
Definition Network.cpp:318
static MultiNameSpace & getNameSpace()
Definition Network.cpp:85
static bool is_log_port(const std::string &src, const std::string &dest)
Definition Network.cpp:292
static std::string collectParams(Contact &c)
Definition Network.cpp:260
static int metaConnect(const std::string &src, const std::string &dest, ContactStyle style, int mode)
Definition Network.cpp:367
static bool needsLookup(const Contact &contact)
Definition Network.cpp:91
static int __yarp_is_initialized
Definition Network.cpp:55
static YarpAutoInit yarp_auto_init
destructor is called on shutdown.
Definition Network.cpp:83
static std::string extractCarrierNameOnly(const std::string &carrier_name_with_params)
Definition Network.cpp:270
static int noteDud(const Contact &src)
Definition Network.cpp:102
static bool __yarp_auto_init_active
Definition Network.cpp:56
bool modifiesReply() const override
Check if this carrier modifies outgoing data through the Carrier::modifyReply method.
Definition Network.cpp:1787
void getHeader(yarp::os::Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Definition Network.cpp:1807
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
Definition Network.cpp:1712
bool checkHeader(const yarp::os::Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
Definition Network.cpp:1825
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition Network.cpp:1817
ConnectionReader & modifyIncomingData(ConnectionReader &reader) override
Modify incoming payload data, if appropriate.
Definition Network.cpp:1762
bool sendAck(ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
Definition Network.cpp:1890
bool isTextMode() const override
Check if carrier is textual in nature.
Definition Network.cpp:1702
bool expectAck(ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
Definition Network.cpp:1895
void prepareDisconnect() override
Do cleanup and preparation for the coming disconnect, if necessary.
Definition Network.cpp:1812
bool reply(ConnectionState &proto, SizedWriter &writer) override
Definition Network.cpp:1865
bool isBareMode() const override
Check if carrier excludes type information from payload.
Definition Network.cpp:1707
bool modifiesIncomingData() const override
Check if this carrier modifies incoming data through the Carrier::modifyIncomingData method.
Definition Network.cpp:1757
SharedLibraryClassFactory< Carrier > * factory
Definition Network.cpp:1647
void handleEnvelope(const std::string &envelope) override
Carriers that do not distinguish data from administrative headers (i.e.
Definition Network.cpp:1717
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition Network.cpp:1855
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
Definition Network.cpp:1850
bool isBroadcast() const override
Check if this carrier uses a broadcast mechanism.
Definition Network.cpp:1747
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
Definition Network.cpp:1860
bool modifiesOutgoingData() const override
Check if this carrier modifies outgoing data through the Carrier::modifyOutgoingData method.
Definition Network.cpp:1772
const PortWriter & modifyOutgoingData(const PortWriter &writer) override
Modify outgoing payload data, if appropriate.
Definition Network.cpp:1777
SharedLibraryClass< Carrier > car
Definition Network.cpp:1648
Carrier * create() const override
Factory method.
Definition Network.cpp:1689
bool isPush() const override
Check if carrier is "push" or "pull" style.
Definition Network.cpp:1737
bool canAccept() const override
Check if reading is implemented for this carrier.
Definition Network.cpp:1835
bool configure(ConnectionState &proto) override
Give carrier a shot at looking at how the connection is set up.
Definition Network.cpp:1924
PortReader & modifyReply(PortReader &reader) override
Modify reply payload data, if appropriate.
Definition Network.cpp:1792
bool configureFromProperty(yarp::os::Property &options) override
Definition Network.cpp:1928
bool isLocal() const override
Check if carrier operates within a single process.
Definition Network.cpp:1732
bool acceptIncomingData(ConnectionReader &reader) override
Determine whether incoming data should be accepted.
Definition Network.cpp:1767
virtual const Carrier & getContent() const
Definition Network.cpp:1684
virtual Carrier & getContent()
Definition Network.cpp:1679
ForwardingCarrier(SharedLibraryClassFactory< Carrier > *factory, Carrier *owner)
Definition Network.cpp:1657
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
Definition Network.cpp:1727
std::string toString() const override
Get name of carrier.
Definition Network.cpp:1900
bool canOffer() const override
Check if writing is implemented for this carrier.
Definition Network.cpp:1840
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition Network.cpp:1742
bool prepareSend(ConnectionState &proto) override
Perform any initialization needed before writing on a connection.
Definition Network.cpp:1845
std::string getBootstrapCarrierName() const override
Get the name of the carrier that should be used prior to handshaking, if a port is registered with th...
Definition Network.cpp:1910
void setCarrierParams(const Property &params) override
Configure carrier from port administrative commands.
Definition Network.cpp:1797
~ForwardingCarrier() override
Definition Network.cpp:1666
bool isValid() const override
Check if this object is really a connection, or just an empty placeholder.
Definition Network.cpp:1697
int connect(const yarp::os::Contact &src, const yarp::os::Contact &dest, const yarp::os::ContactStyle &style, int mode, bool reversed) override
Some carrier types may require special connection logic.
Definition Network.cpp:1915
bool acceptOutgoingData(const PortWriter &writer) override
Determine whether outgoing data should be accepted.
Definition Network.cpp:1782
bool expectExtraHeader(ConnectionState &proto) override
Receive any carrier-specific header.
Definition Network.cpp:1870
yarp::os::Face * createFace() const override
Create new Face object that the carrier needs.
Definition Network.cpp:1933
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
Definition Network.cpp:1875
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
Definition Network.cpp:1880
bool isActive() const override
Check if carrier is alive and error free.
Definition Network.cpp:1752
void close() override
Close the carrier.
Definition Network.cpp:1905
bool expectSenderSpecifier(ConnectionState &proto) override
Expect the name of the sending port.
Definition Network.cpp:1885
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition Network.cpp:1830
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition Network.cpp:1722
void getCarrierParams(Property &params) const override
Get carrier configuration and deliver it by port administrative commands.
Definition Network.cpp:1802
std::string getDllName() const
Definition Network.cpp:1995
void init()
Definition Network.cpp:1959
StubCarrier(const char *dll_name, const char *fn_name)
Definition Network.cpp:1947
std::string getFnName() const
Definition Network.cpp:2000
Carrier * create() const override
Factory method.
Definition Network.cpp:1981
StubCarrier(const char *name)
Definition Network.cpp:1953
const Carrier & getContent() const override
Definition Network.cpp:1976
Carrier & getContent() override
Definition Network.cpp:1971
A single-use class to shut down the yarp library if it was initialized automatically.
Definition Network.cpp:64
~YarpAutoInit()
Shut down the yarp library if it was automatically initialized.
Definition Network.cpp:75
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:65
void addVocab32(yarp::conf::vocab32_t x)
Places a 32 bit vocabulary item in the bottle, at the end of the list.
Definition Bottle.cpp:164
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition Bottle.cpp:188
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:257
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition Bottle.cpp:246
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:252
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition Bottle.cpp:299
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition Bottle.cpp:283
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:217
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition Bottle.cpp:293
A mini-server for performing network communication in the background.
std::string getName() const override
Get name of port.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
A simple abstraction for a block of bytes.
Definition Bytes.h:24
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition Carrier.h:44
virtual Carrier * create() const =0
Factory method.
static bool addCarrierPrototype(Carrier *carrier)
Add a new connection type.
Definition Carriers.cpp:303
static Carrier * chooseCarrier(const std::string &name)
Select a carrier by name.
Definition Carriers.cpp:233
static OutputProtocol * connect(const Contact &address)
Initiate a connection to an address.
Definition Carriers.cpp:282
An interface for reading from a network connection.
The basic state of a connection - route, streams in use, etc.
Preferences for how to communicate with a contact.
double timeout
Set a timeout for communication (in units of seconds, fractional seconds allowed).
bool quiet
Suppress all outputs and warnings.
std::string carrier
Request that communication be made using a particular carrier.
bool verboseOnSuccess
Allow output on success.
bool expectReply
Specify whether you expect a reply to a message.
bool admin
Ask recipient to treat message as administrative.
bool persistent
Specify whether a requested connection should be persistent.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
static Contact fromConfig(const Searchable &config)
Factory method.
Definition Contact.cpp:129
bool isValid() const
Checks if a Contact is tagged as valid.
Definition Contact.cpp:298
std::string getName() const
Get the name associated with this Contact.
Definition Contact.cpp:205
std::string toString() const
Get a textual representation of the Contact.
Definition Contact.cpp:303
static Contact fromString(const std::string &txt)
Factory method.
Definition Contact.cpp:139
void setTimeout(float timeout)
Set timeout for this Contact.
Definition Contact.cpp:282
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
The initial point-of-contact with a port.
Definition Face.h:20
The input side of an active connection between two ports.
virtual void endRead()=0
End the current read operation, begin by beginRead().
virtual ConnectionReader & beginRead()=0
Begin a read operation, with bytes read via the returned yarp::os::ConnectionReader object.
@ DebugType
Definition Log.h:93
@ WarningType
Definition Log.h:95
static void setMinimumForwardLevel(LogType level)
Set current minimum forward level (it does nothing if forwarding is not enabled)
Definition Log.cpp:865
static void setMinimumPrintLevel(LogType level)
Set current minimum print level.
Definition Log.cpp:847
An abstract name space for ports.
Definition NameSpace.h:22
virtual bool disconnectTopicFromPort(const Contact &src, const Contact &dest, const ContactStyle &style)=0
Stop subscribing a port to a topic.
virtual bool localOnly() const =0
Check if the NameSpace is only valid for the current process ("local").
virtual bool disconnectPortFromTopic(const Contact &src, const Contact &dest, const ContactStyle &style)=0
Stop publishing a port to a topic.
virtual bool connectPortToPortPersistently(const Contact &src, const Contact &dest, const ContactStyle &style)=0
Connect two ports with persistence.
virtual bool connectTopicToPort(const Contact &src, const Contact &dest, const ContactStyle &style)=0
Subscribe a port to a topic.
virtual bool connectPortToTopic(const Contact &src, const Contact &dest, const ContactStyle &style)=0
Publish a port to a topic.
virtual bool disconnectPortToPortPersistently(const Contact &src, const Contact &dest, const ContactStyle &style)=0
Disconnect two ports, removing any persistence.
Abstract interface for a database of port names.
Definition NameStore.h:19
virtual bool announce(const std::string &name, int activity)=0
virtual bool process(PortWriter &in, PortReader &out, const Contact &source)=0
static bool initialized()
Returns true if YARP has been fully initialized.
Definition Network.cpp:1507
static std::string getNameServerName()
Get the name of the port associated with the nameserver (usually "/root", but this can be overwritten...
Definition Network.cpp:1470
static bool isNetworkInitialized()
Definition Network.cpp:945
static bool getLocalMode()
Get current value of flag "localMode", see setLocalMode function.
Definition Network.cpp:1123
static Contact getNameServerContact()
Get the contact information for the port associated with the nameserver (usually "/root",...
Definition Network.cpp:1478
static bool sync(const std::string &port, bool quiet=true)
Wait for a port to be ready and responsive.
Definition Network.cpp:916
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition Network.cpp:751
static void queryBypass(NameStore *store)
Redirect queries to another source.
Definition Network.cpp:1525
static void yarpClockInit(yarp::os::yarpClockType clockType, Clock *custom=nullptr)
This function specifically initialize the clock In case clockType is one of the valid cases: YARP_CLO...
Definition Network.cpp:1027
static void unlock()
Call post() on a global mutual-exclusion semaphore allocated by YARP.
Definition Network.cpp:1548
static bool registerCarrier(const char *name, const char *dll)
Register a carrier to make available at runtime.
Definition Network.cpp:2007
static Contact unregisterName(const std::string &name)
Removes the registration for a name from the name server.
Definition Network.cpp:1092
static NameStore * getQueryBypass()
Definition Network.cpp:1530
static Contact unregisterContact(const Contact &contact)
Removes the registration for a contact from the name server.
Definition Network.cpp:1098
static bool waitPort(const std::string &target, bool quiet=false)
Delays the system until a specified port is open.
Definition Network.cpp:894
static bool setNameServerName(const std::string &name)
Set the name of the port associated with the nameserver (usually "/root", but this can be overwritten...
Definition Network.cpp:1484
static bool getConnectionQos(const std::string &src, const std::string &dest, QosStyle &srcStyle, QosStyle &destStyle, bool quiet=true)
Gets the Qos preferences of a connection.
Definition Network.cpp:1307
static Contact registerContact(const Contact &contact)
Register contact information with the name server.
Definition Network.cpp:1086
static bool setNameServerContact(Contact &nameServerContact)
Set explicitly the nameserver information.
Definition Network.cpp:2053
static int poll(const std::string &target, bool silent=false)
Sends a 'describe yourself' message to a specified port, in order to receive information about the po...
Definition Network.cpp:1630
static Contact queryName(const std::string &name)
Find out information about a registered name.
Definition Network.cpp:1064
static bool exists(const std::string &port, bool quiet=true, bool checkVer=true)
Check for a port to be ready and responsive.
Definition Network.cpp:815
static void setVerbosity(int verbosity)
Set level of verbosity of YARP messages.
Definition Network.cpp:1513
static void initMinimum()
Basic system initialization, not including plugins.
Definition Network.cpp:940
static int sendMessage(const std::string &port, yarp::os::PortWriter &writable, bool silent=false)
Just a reminder to sendMessage with temporary output parameter that will be discarded.
Definition Network.cpp:1554
static void autoInitMinimum()
Basic system initialization, not including plugins.
Definition Network.cpp:926
static void finiMinimum()
Deinitialization, excluding plugins.
Definition Network.cpp:1008
static Contact detectNameServer(bool useDetectedServer, bool &scanNeeded, bool &serverUsed)
Scan for an available name server.
Definition Network.cpp:2044
static bool setProperty(const char *name, const char *key, const Value &value)
Names registered with the nameserver can have arbitrary key->value properties associated with them.
Definition Network.cpp:1104
static int getDefaultPortRange()
Under normal operation, YARP has a name server that manages a pool of (socket) ports starting at a po...
Definition Network.cpp:2086
static void lock()
Call wait() on a global mutual-exclusion semaphore allocated by YARP.
Definition Network.cpp:1543
static std::string getConfigFile(const char *fname)
Search for a configuration file in YARP's standard config file path.
Definition Network.cpp:2080
static Contact registerName(const std::string &name)
Register a name with the name server.
Definition Network.cpp:1079
static int disconnectInput(const std::string &src, const std::string &dest, bool silent=false)
Sends a disconnection command to the specified port.
Definition Network.cpp:1636
static bool waitConnection(const std::string &source, const std::string &destination, bool quiet=false)
Delays the system until a specified connection is established.
Definition Network.cpp:872
static void assertion(bool shouldBeTrue)
An assertion.
Definition Network.cpp:1129
static bool checkNetwork()
Check if the YARP Network is up and running.
Definition Network.cpp:1495
static bool writeToNameServer(PortWriter &cmd, PortReader &reply, const ContactStyle &style)
Variant write method specialized to name server.
Definition Network.cpp:2067
static bool disconnect(const std::string &src, const std::string &dest, bool quiet)
Request that an output port disconnect from an input port.
Definition Network.cpp:769
static bool localNetworkAllocation()
Check where the name server in use expects processes to allocate their own network resources.
Definition Network.cpp:2037
static Value * getProperty(const char *name, const char *key)
Look up the value associated with a particular key for a named entry registered with the nameserver.
Definition Network.cpp:1112
static bool isValidPortName(const std::string &portName)
Checks that the port has a valid name.
Definition Network.cpp:1318
static bool setConnectionQos(const std::string &src, const std::string &dest, const QosStyle &srcStyle, const QosStyle &destStyle, bool quiet=true)
Adjust the Qos preferences of a connection.
Definition Network.cpp:1141
static bool write(const Contact &contact, PortWriter &cmd, PortReader &reply, bool admin=false, bool quiet=false, double timeout=-1)
Send a single command to a port and await a single response.
Definition Network.cpp:1344
static bool isConnected(const std::string &src, const std::string &dest, bool quiet)
Check if a connection with tcp carrier exists between two ports.
Definition Network.cpp:796
static bool cleanUnresponsivePorts(double timeout=-1)
Clean up unresponsive ports from the name server.
Definition Network.cpp:1214
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition Network.cpp:1118
The output side of an active connection between two ports.
virtual Connection & getConnection()=0
Get the connection whose protocol operations we are managing.
virtual bool open(const Route &route)=0
Start negotiating a carrier, using the given route (this should generally match the name of the sendi...
virtual InputProtocol & getInput()=0
Get an interface for doing read operations on the connection.
virtual void close()=0
Negotiate an end to operations.
virtual bool setTimeout(double timeout)=0
Set the timeout to be used for network operations.
virtual bool write(SizedWriter &writer)=0
Write a message on the connection.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition PortReader.h:24
Interface implemented by all objects that can write themselves to the network, such as Bottle objects...
Definition PortWriter.h:23
virtual bool write(ConnectionWriter &writer) const =0
Write this object to a network connection.
A mini-server for network communication.
Definition Port.h:46
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:395
void setAdminMode(bool adminMode=true)
Turn on/off "admin" mode.
Definition Port.cpp:554
bool addOutput(const std::string &name) override
Add an output connection to the specified port.
Definition Port.cpp:320
bool openFake(const std::string &name)
Start port without making it accessible from the network.
Definition Port.cpp:74
A class for storing options and configuration information.
Definition Property.h:33
Preferences for the port's Quality of Service.
Definition QosStyle.h:23
void setThreadPriority(int priority)
sets the communication thread priority level
Definition QosStyle.h:127
void setPacketPrioritybyTOS(int tos)
sets the packet priority given as TOS value
Definition QosStyle.h:97
void setThreadPolicy(int policy)
sets the communication thread scheduling policy
Definition QosStyle.h:137
Information about a connection between two ports.
Definition Route.h:28
Minimal requirements for an efficient Writer.
Definition SizedWriter.h:32
static void delaySystem(double seconds)
A single value (typically within a Bottle).
Definition Value.h:44
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:192
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:210
virtual Bottle * asList() const
Get list value.
Definition Value.cpp:252
virtual bool isInt32() const
Checks if value is a 32-bit integer.
Definition Value.cpp:132
virtual std::string asString() const
Get string value.
Definition Value.cpp:246
Pick out a set of relevant plugins.
void scan()
Find plugin configuration files, and run [plugin] sections through the select method.
static bool checkPlugin(const std::string &name, const std::string &type={})
Checks if a pluigin of the given type is available.
Collect hints for finding a particular plugin.
bool setSelector(YarpPluginSelector &selector)
Use a selector to find a plugin or plugins.
void setLibraryMethodName(const std::string &dll_name, const std::string &fn_name)
Set the name of the library to load and the method name to use as a factory.
void setPluginName(const std::string &name)
Set the name of the plugin to load.
A helper for creating cached object descriptions.
Small helper class to help deal with legacy YARP configuration files.
Definition NameConfig.h:23
static std::string expandFilename(const char *fname)
Simple Readable and Writable object representing a command to a YARP port.
Definition PortCommand.h:24
DumpObj * factory(Bottle &obj)
Definition main.cpp:81
#define yCInfo(component,...)
#define yCError(component,...)
#define yCAssert(component, x)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define yCFatal(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition environment.h:65
std::int32_t vocab32_t
Definition numeric.h:78
yarpClockType getClockType()
Definition Time.cpp:270
void useSystemClock()
Configure YARP to use system time (this is the default).
Definition Time.cpp:137
bool isClockInitialized()
Check if YARP clock is initialized.
Definition Time.cpp:250
void useNetworkClock(const std::string &clock, const std::string &localPortName="")
Configure YARP to read time from a specified topic.
Definition Time.cpp:177
void useCustomClock(Clock *clock)
Configure YARP clients to use a custom clock source provided by the user.
Definition Time.cpp:220
void startTurboBoost()
For OS where it makes sense sets the scheduler to be called more often.
Definition Time.cpp:91
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
Definition OSTestMsg.cpp:13
yarpClockType
Definition Time.h:25
@ YARP_CLOCK_UNINITIALIZED
Definition Time.h:26
@ YARP_CLOCK_CUSTOM
Definition Time.h:30
@ YARP_CLOCK_SYSTEM
Definition Time.h:28
@ YARP_CLOCK_NETWORK
Definition Time.h:29
@ YARP_CLOCK_DEFAULT
Definition Time.h:27
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition Vocab32.h:27
#define YARP_UNUSED(var)
Definition api.h:162