YARP
Yet Another Robot Platform
Network.h
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 #ifndef YARP_OS_NETWORK_H
8 #define YARP_OS_NETWORK_H
9 
10 #include <yarp/os/Contact.h>
11 #include <yarp/os/ContactStyle.h>
12 #include <yarp/os/NameStore.h>
13 #include <yarp/os/Portable.h>
14 #include <yarp/os/Property.h>
15 #include <yarp/os/QosStyle.h>
16 #include <yarp/os/Time.h>
17 #include <yarp/os/Value.h>
18 
19 
20 namespace yarp::os {
21 class ContactStyle;
22 class QosStyle;
23 } // namespace yarp::os
24 
25 // Make plugins in a library available for use
26 #define YARP_DECLARE_PLUGINS(name) extern "C" void add_ ## name ## _plugins();
27 #define YARP_REGISTER_PLUGINS(name) add_ ## name ## _plugins();
28 
29 namespace yarp::os {
30 
38 {
39 public:
44  static void initMinimum();
45 
51  static void initMinimum(yarp::os::yarpClockType clockType,
52  yarp::os::Clock* custom = nullptr);
53 
59  static void autoInitMinimum();
60 
67  static void autoInitMinimum(yarp::os::yarpClockType clockType,
68  yarp::os::Clock* custom = nullptr);
69 
89  static void yarpClockInit(yarp::os::yarpClockType clockType,
90  Clock* custom = nullptr);
91 
95  static void finiMinimum();
96 
97  /*
98  * Return true if network is initialized.
99  * Expect problem if you use things that require a network before
100  * initializing it.
101  */
102  static bool isNetworkInitialized();
103 
112  static bool connect(const std::string& src,
113  const std::string& dest,
114  const std::string& carrier = "",
115  bool quiet = true);
116 
117  // Catch old uses of nullptr for carrier
118  static bool connect(const char* src,
119  const char* dest,
120  const char* carrier,
121  bool quiet = true)
122  {
123  return connect(std::string(src),
124  std::string(dest),
125  std::string((carrier == nullptr) ? "" : carrier),
126  quiet);
127  }
128 
136  static bool connect(const std::string& src,
137  const std::string& dest,
138  const ContactStyle& style);
139 
147  static bool disconnect(const std::string& src,
148  const std::string& dest,
149  bool quiet);
150 
158  static bool disconnect(const std::string& src,
159  const std::string& dest,
160  const ContactStyle& style);
161 
170  static bool disconnect(const std::string& src,
171  const std::string& dest,
172  const std::string& carrier = "",
173  bool quiet = true);
174 
175  // Catch old uses of nullptr for carrier
176  static bool disconnect(const char* src,
177  const char* dest,
178  const char* carrier,
179  bool quiet = true)
180  {
181  return disconnect(std::string(src),
182  std::string(dest),
183  std::string((carrier == nullptr) ? "" : carrier),
184  quiet);
185  }
186 
194  static bool isConnected(const std::string& src,
195  const std::string& dest,
196  bool quiet);
197 
205  static bool isConnected(const std::string& src,
206  const std::string& dest,
207  const ContactStyle& style);
208 
217  static bool isConnected(const std::string& src,
218  const std::string& dest,
219  const std::string& carrier = "",
220  bool quiet = true);
221 
222  // Catch old uses of nullptr for carrier
223  static bool isConnected(const char* src,
224  const char* dest,
225  const char* carrier,
226  bool quiet = true)
227  {
228  return isConnected(std::string(src),
229  std::string(dest),
230  std::string((carrier == nullptr) ? "" : carrier),
231  quiet);
232  }
233 
240  static bool exists(const std::string& port,
241  bool quiet = true,
242  bool checkVer = true);
243 
250  static bool exists(const std::string& port,
251  const ContactStyle& style,
252  bool checkVer = true);
253 
260  static bool sync(const std::string& port,
261  bool quiet = true);
262 
269  static void assertion(bool shouldBeTrue);
270 
281  static Contact queryName(const std::string& name);
282 
291  static Contact registerName(const std::string& name);
292 
301  static Contact registerContact(const Contact& contact);
302 
310  static Contact unregisterName(const std::string& name);
311 
319  static Contact unregisterContact(const Contact& contact);
320 
321 
332  static bool setProperty(const char* name,
333  const char* key,
334  const Value& value);
335 
336 
344  static Value* getProperty(const char* name,
345  const char* key);
346 
347 
354  static std::string getNameServerName();
355 
362  static Contact getNameServerContact();
363 
364 
373  static bool setNameServerName(const std::string& name);
374 
375 
383  static bool setLocalMode(bool flag);
384 
389  static bool getLocalMode();
390 
391 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
406  YARP_DEPRECATED
407  static std::string readString(bool* eof = nullptr);
408 #endif // YARP_NO_DEPRECATED
409 
410 
427  static bool write(const Contact& contact,
428  PortWriter& cmd,
429  PortReader& reply,
430  bool admin = false,
431  bool quiet = false,
432  double timeout = -1);
433 
446  static bool write(const Contact& contact,
447  PortWriter& cmd,
448  PortReader& reply,
449  const ContactStyle& style);
450 
460  static bool writeToNameServer(PortWriter& cmd,
461  PortReader& reply,
462  const ContactStyle& style);
463 
474  static bool write(const std::string& port_name,
475  PortWriter& cmd,
476  PortReader& reply);
477 
478 
487  static bool checkNetwork();
488 
489 
500  static bool checkNetwork(double timeout);
501 
510  static bool initialized();
511 
512 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4
519  YARP_DEPRECATED_MSG("Use LogComponents instead")
520  static void setVerbosity(int verbosity);
521 #endif // YARP_NO_DEPRECATED
522 
528  static void queryBypass(NameStore* store);
529 
530  static NameStore* getQueryBypass();
531 
532 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4
542  YARP_DEPRECATED_MSG("Use yarp::conf::environment::get_string instead")
543  static std::string getEnvironment(const char* key,
544  bool* found = nullptr);
545 
554  YARP_DEPRECATED_MSG("Use yarp::conf::environment::set_string instead")
555  static void setEnvironment(const std::string& key,
556  const std::string& val);
557 
565  YARP_DEPRECATED_MSG("Use yarp::conf::environment::unset instead")
566  static void unsetEnvironment(const std::string& key);
567 #endif // YARP_NO_DEPRECATED
568 
569 
570 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3.0
576  YARP_DEPRECATED_MSG("Use yarp::conf::filesystem::preferred_separator instead")
577  static std::string getDirectorySeparator();
578 
584  YARP_DEPRECATED_MSG("Use yarp::conf::environment::path_separator instead")
585  static std::string getPathSeparator();
586 #endif // YARP_NO_DEPRECATED
587 
596  static bool registerCarrier(const char* name,
597  const char* dll);
598 
602  static void lock();
603 
607  static void unlock();
608 
617  static bool localNetworkAllocation();
618 
630  static Contact detectNameServer(bool useDetectedServer,
631  bool& scanNeeded,
632  bool& serverUsed);
633 
640  static bool setNameServerContact(Contact& nameServerContact);
641 
651  static std::string getConfigFile(const char* fname);
652 
663  static int getDefaultPortRange();
664 
673  static bool setConnectionQos(const std::string& src,
674  const std::string& dest,
675  const QosStyle& srcStyle,
676  const QosStyle& destStyle,
677  bool quiet = true);
678 
686  static bool setConnectionQos(const std::string& src,
687  const std::string& dest,
688  const QosStyle& style,
689  bool quiet = true);
690 
699  static bool getConnectionQos(const std::string& src,
700  const std::string& dest,
701  QosStyle& srcStyle,
702  QosStyle& destStyle,
703  bool quiet = true);
704 
710  static bool isValidPortName(const std::string& portName);
711 
719  static bool waitConnection(const std::string& source,
720  const std::string& destination,
721  bool quiet = false);
722 
729  static bool waitPort(const std::string& target, bool quiet = false);
730 
735  static int sendMessage(const std::string& port,
736  yarp::os::PortWriter& writable,
737  bool silent = false);
738 
747  static int sendMessage(const std::string& port,
748  yarp::os::PortWriter& writable,
749  std::string& output,
750  bool quiet);
751 
759  static int disconnectInput(const std::string& src,
760  const std::string& dest,
761  bool silent = false);
762 
770  static int poll(const std::string& target, bool silent = false);
771 };
772 
780 {
781 public:
789 
803  yarp::os::Clock* custom = nullptr);
804 
808  virtual ~Network();
809 
816  static void init();
817 
822  static void init(yarp::os::yarpClockType clockType,
823  Clock* custom = nullptr);
824 
831  static void fini();
832 };
833 
834 } // namespace yarp::os
835 
836 #endif // YARP_OS_NETWORK_H
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:23
Represents how to reach a part of a YARP network.
Definition: Contact.h:33
Abstract interface for a database of port names.
Definition: NameStore.h:19
Utilities for manipulating the YARP network, excluding initialization and shutdown.
Definition: Network.h:38
static bool isConnected(const char *src, const char *dest, const char *carrier, bool quiet=true)
Definition: Network.h:223
static bool connect(const char *src, const char *dest, const char *carrier, bool quiet=true)
Definition: Network.h:118
static bool disconnect(const char *src, const char *dest, const char *carrier, bool quiet=true)
Definition: Network.h:176
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:780
Network(yarp::os::yarpClockType clockType, yarp::os::Clock *custom=nullptr)
Initialize the YARP network using the specified clock.
static void fini()
Deinitialization.
virtual ~Network()
Destructor.
static void init()
Initialization.
Network()
Constructor.
static void init(yarp::os::yarpClockType clockType, Clock *custom=nullptr)
Initialization.
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
Preferences for the port's Quality of Service.
Definition: QosStyle.h:23
A single value (typically within a Bottle).
Definition: Value.h:43
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2885
void setEnvironment(const std::string &key, const std::string &value)
Set or change an environment variable.
Definition: environment.h:322
bool unset(const std::string &key)
Remove an environment variable.
Definition: environment.h:272
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:307
void unsetEnvironment(const std::string &key)
Remove an environment variable.
Definition: environment.h:336
std::string readString(bool *eof)
Definition: Terminal.cpp:76
An interface to the operating system, including Port based communication.
yarpClockType
Definition: Time.h:25
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1091
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_init_API
Definition: api.h:32
#define YARP_os_API
Definition: api.h:18