33 std::string bytes_to_string(
const Bytes& header)
36 for (
size_t i = 0; i < header.
length(); i++) {
41 for (
size_t i = 0; i < header.
length(); i++) {
42 char ch = header.
get()[i];
54 #ifndef DOXYGEN_SHOULD_SKIP_THIS
59 static std::mutex mutex;
61 std::vector<Carrier*> delegates;
63 Carrier* chooseCarrier(
const std::string& name,
64 bool load_if_needed =
true,
65 bool return_template =
false);
67 bool load_if_needed =
true);
69 static bool matchCarrier(
const Bytes& header,
Bottle& code);
71 static bool scanForCarrier(
const Bytes& header);
76 std::mutex Carriers::Private::mutex{};
78 Carrier* Carriers::Private::chooseCarrier(
const std::string& name,
82 auto pos = name.find(
'+');
83 if (pos != std::string::npos) {
84 return chooseCarrier(name.substr(0, pos), load_if_needed, return_template);
87 for (
auto& delegate : delegates) {
90 if (!return_template) {
102 return Carriers::Private::chooseCarrier(name,
false);
107 "Could not find carrier \"%s\"",
108 (!name.empty()) ? name.c_str() :
"[bytes]");
113 Carrier* Carriers::Private::chooseCarrier(
const Bytes& header,
116 for (
auto& delegate : delegates) {
123 if (load_if_needed) {
124 if (scanForCarrier(header)) {
126 return Carriers::Private::chooseCarrier(header,
false);
131 "Could not find carrier for a connection starting with: %s",
132 bytes_to_string(header).c_str());
138 bool Carriers::Private::matchCarrier(
const Bytes& header,
Bottle& code)
143 for (
size_t i = 0; i < code.
size() && !done; i++) {
148 if (header.
length() <= at) {
153 if (j != header.
get()[at]) {
167 bool Carriers::Private::checkForCarrier(
const Bytes& header,
Searchable& group)
170 if (code.
size() == 0) {
173 if (matchCarrier(header, code)) {
182 bool Carriers::Private::scanForCarrier(
const Bytes& header)
184 yCDebug(CARRIERS,
"Scanning for a carrier by header.");
188 for (
size_t i = 0; i < lst.
size(); i++) {
189 if (checkForCarrier(header, lst.
get(i))) {
196 bool Carriers::Private::select(
Searchable& options)
198 return options.
check(
"type",
Value(
"none")).asString() ==
"carrier";
204 Carriers::Carriers() :
210 mPriv->delegates.emplace_back(
new TcpCarrier());
211 mPriv->delegates.emplace_back(
new TcpCarrier(
false));
213 mPriv->delegates.emplace_back(
new UdpCarrier());
215 mPriv->delegates.emplace_back(
new TextCarrier(
true));
218 Carriers::~Carriers()
226 for (
auto& delegate : mPriv->delegates) {
230 mPriv->delegates.clear();
252 Face* face =
nullptr;
273 bool ok = face->
open(address);
305 getInstance().mPriv->delegates.emplace_back(carrier);
320 std::lock_guard<std::mutex> guard(Private::mutex);
325 std::vector<Carrier*>& delegates = instance.mPriv->delegates;
326 for (
auto& delegate : delegates) {
332 instance.mPriv->scan();
333 Bottle plugins = instance.mPriv->getSelectedPlugins();
334 for (
size_t i = 0; i < plugins.
size(); i++) {
336 std::string name = options.
check(
"name",
Value(
"untitled")).asString();
337 if (done.
check(name)) {
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Bottle tail() const
Get all but the first element of a bottle.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
A simple abstraction for a block of bytes.
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
virtual Carrier * create() const =0
Factory method.
virtual yarp::os::Face * createFace() const
Create new Face object that the carrier needs.
virtual bool checkHeader(const Bytes &header)=0
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
Collection of carriers, a singleton.
static Bottle listCarriers()
static Face * listen(const Contact &address)
Create a "proto-carrier" interface object that waits for incoming connections prior to a carrier bein...
static Carrier * getCarrierTemplate(const std::string &name)
Get template for carrier.
static bool addCarrierPrototype(Carrier *carrier)
Add a new connection type.
static Carrier * chooseCarrier(const std::string &name)
Select a carrier by name.
static Carriers & getInstance()
static OutputProtocol * connect(const Contact &address)
Initiate a connection to an address.
void clear()
Remove all carriers.
virtual std::string getName() const =0
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
The initial point-of-contact with a port.
virtual bool open(const Contact &address)=0
Start listening to the given address.
virtual OutputProtocol * write(const Contact &address)=0
Try to reach out and talk to someone.
static bool registerCarrier(const char *name, const char *dll)
Register a carrier to make available at runtime.
The output side of an active connection between two ports.
A class for storing options and configuration information.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
A base class for nested structures that can be searched.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
A wrapper for a named factory method in a named shared library.
std::string getName() const
Get the name associated with this factory.
A single value (typically within a Bottle).
virtual bool isString() const
Checks if value is a string.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
virtual std::string asString() const
Get string value.
Pick out a set of relevant plugins.
Bottle getSelectedPlugins() const
void scan()
Find plugin configuration files, and run [plugin] sections through the select method.
Collect hints for finding a particular plugin.
bool setSelector(YarpPluginSelector &selector)
Use a selector to find a plugin or plugins.
bool open(SharedLibraryFactory &factory)
Initialize a factory object based on the hints available.
bool readFromSearchable(Searchable &options, const std::string &name)
Configure settings from a configuration file or other searchable object.
A dummy Face for testing purposes.
A carrier for communicating locally within a process.
Communicating between two ports via MCAST.
Communicating between two ports via a variant plain-text protocol originally designed for the yarp na...
Communicating between two ports via TCP.
Communicating with a port via TCP.
Communicating between two ports via a plain-text protocol.
Communicating between two ports via UDP.
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
std::string to_string(IntegerType x)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.