YARP
Yet Another Robot Platform
os/buffered_port.cpp

This example shows how to communicate between a pair of buffered ports.

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <yarp/os/Bottle.h>
#include <yarp/os/Time.h>
#include <stdio.h>
using namespace yarp::os;
int main() {
// Initialize YARP - some OSes need network and time service initialization
// Work locally - don't rely on name server (just for this example).
// If you have a YARP name server running, you can remove this line.
// Create two ports that we'll be using to transmit "Bottle" objects.
// The ports are buffered, so that sending and receiving can happen
// in the background.
// we will want to read every message, with no skipping of "old" messages
// when new ones come in
in.setStrict();
// Name the ports
in.open("/in");
out.open("/out");
// Connect the ports so that anything written from /out arrives to /in
Network::connect("/out","/in");
// Send one "Bottle" object. The port is responsible for creating
// and reusing/destroying that object, since it needs to be sure
// it exists until communication to all recipients (just one in
// this case) is complete.
Bottle& outBot1 = out.prepare(); // Get the object
outBot1.fromString("hello world"); // Set it up the way we want
printf("Writing bottle 1 (%s)\n", outBot1.toString().c_str());
out.write(); // Now send it on its way
// Send another "Bottle" object
Bottle& outBot2 = out.prepare();
outBot2.fromString("2 3 5 7 11");
printf("Writing bottle 2 (%s)\n", outBot2.toString().c_str());
out.writeStrict(); // writeStrict() will wait for any
// previous communication to finish;
// write() would skip sending if
// there was something being sent
// Read the first object
Bottle *inBot1 = in.read();
printf("Bottle 1 is: %s\n", inBot1->toString().c_str());
// Read the second object
Bottle *inBot2 = in.read();
printf("Bottle 2 is: %s\n", inBot2->toString().c_str());
return 0;
}
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:72
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
Network.h
yarp::os::BufferedPort::read
T * read(bool shouldWait=true) override
Read an available object from the port.
Definition: BufferedPort-inl.h:154
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:259
yarp::os::Bottle::fromString
void fromString(const std::string &text)
Initializes bottle from a string.
Definition: Bottle.cpp:207
yarp::os::BufferedPort::setStrict
void setStrict(bool strict=true) override
Call this to strictly keep all messages, or allow old ones to be quietly dropped.
Definition: BufferedPort-inl.h:147
yarp::os::BufferedPort::prepare
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
Definition: BufferedPort-inl.h:114
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:63
yarp::os::BufferedPort::writeStrict
void writeStrict()
Write the current object being returned by BufferedPort::prepare, waiting until any previous sends ar...
Definition: BufferedPort-inl.h:135
yarp::os::NetworkBase::connect
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:684
yarp::os::BufferedPort::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: BufferedPort-inl.h:41
BufferedPort.h
yarp::os::NetworkBase::setLocalMode
static bool setLocalMode(bool flag)
Chooses whether communication is process-local.
Definition: Network.cpp:1052
yarp::os::BufferedPort::write
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
Definition: BufferedPort-inl.h:126
yarp::os
Definition: AbstractCarrier.h:17
yarp::os::Network
Definition: Network.h:781
yarp
Definition: numeric.h:47
Time.h
Bottle.h