35 recv_delegate(nullptr),
36 send_delegate(nullptr),
37 need_recv_delegate(
false),
38 need_send_delegate(
false),
39 recv_delegate_fail(
false),
40 send_delegate_fail(
false),
41 route(
"null",
"null",
"tcp"),
72 if (from.find(
' ') != std::string::npos) {
76 for (
size_t i = 1; i < b.
size(); i++) {
82 carrier.append(
"+").append(v.
toString());
93 if (recv_delegate ==
nullptr) {
95 if (b.
check(
"recv")) {
96 need_recv_delegate =
true;
101 if (send_delegate ==
nullptr) {
103 if (b.
check(
"send")) {
104 need_send_delegate =
true;
159 size_t start = carrier.find(
'+');
160 if (start != std::string::npos) {
162 for (
size_t i = start + 1; i < (
size_t)carrier.length(); i++) {
163 char ch = carrier[i];
166 }
else if (ch ==
'.') {
192 if (delegate ==
nullptr) {
193 return nullConnection;
215 bool ok = expectHeader();
220 return respondToHeader();
228 if (delegate ==
nullptr) {
233 bool ok = sendHeader();
238 return expectReplyToHeader();
283 return !(!
checkStreams() || recv_delegate_fail || send_delegate_fail);
296 this->writer = &writer;
300 bool ok = delegate->
write(*
this, writer);
303 if (
reply !=
nullptr) {
315 this->writer =
nullptr;
323 delegate->
reply(*
this, writer);
324 pendingReply =
false;
346 if (delegate !=
nullptr) {
395 if (recv_delegate ==
nullptr) {
396 return nullConnection;
398 return *recv_delegate;
422 if (send_delegate ==
nullptr) {
423 return nullConnection;
425 return *send_delegate;
429bool Protocol::getRecvDelegate()
432 if (recv_delegate !=
nullptr) {
435 if (!need_recv_delegate) {
438 if (recv_delegate_fail) {
443 std::string tag = b.find(
"recv").asString();
445 if (recv_delegate ==
nullptr) {
446 fprintf(
stderr,
"Need carrier \"%s\", but cannot find it.\n", tag.c_str());
447 recv_delegate_fail =
true;
452 fprintf(
stderr,
"Carrier \"%s\" does not modify incoming data as expected.\n", tag.c_str());
453 recv_delegate_fail =
true;
459 fprintf(
stderr,
"Carrier \"%s\" could not configure the send delegate.\n", tag.c_str());
460 recv_delegate_fail =
true;
468bool Protocol::getSendDelegate()
471 if (send_delegate !=
nullptr) {
474 if (!need_send_delegate) {
477 if (send_delegate_fail) {
482 std::string tag = b.find(
"send").asString();
484 if (send_delegate ==
nullptr) {
485 fprintf(
stderr,
"Need carrier \"%s\", but cannot find it.\n", tag.c_str());
486 send_delegate_fail =
true;
491 fprintf(
stderr,
"Carrier \"%s\" does not modify outgoing data as expected.\n", tag.c_str());
492 send_delegate_fail =
true;
498 fprintf(
stderr,
"Carrier \"%s\" could not configure the send delegate.\n", tag.c_str());
499 send_delegate_fail =
true;
507bool Protocol::respondToHeader()
519bool Protocol::expectAck()
529void Protocol::closeHelper()
536 if (delegate !=
nullptr) {
541 if (recv_delegate !=
nullptr) {
542 recv_delegate->
close();
543 delete recv_delegate;
544 recv_delegate =
nullptr;
546 if (send_delegate !=
nullptr) {
547 send_delegate->
close();
548 delete send_delegate;
549 send_delegate =
nullptr;
554bool Protocol::sendAck()
558 if (delegate ==
nullptr) {
570bool Protocol::expectIndex()
583 if (delegate !=
nullptr) {
593 if (ref !=
nullptr) {
614 if (delegate ==
nullptr) {
616 if (delegate !=
nullptr) {
619 fprintf(
stderr,
"Carrier \"%s\" cannot be used this way, try \"tcp+recv.%s\" instead.\n", carrierName.c_str(), carrierName.c_str());
626 fprintf(
stderr,
"Carrier \"%s\" could not be configured.\n", carrierName.c_str());
636bool Protocol::expectHeader()
645 bool ok = expectProtocolSpecifier();
649 ok = expectSenderSpecifier();
659bool Protocol::expectProtocolSpecifier()
672 if ((
size_t)len != header.length()) {
677 if (delegate !=
nullptr) {
684 if (delegate ==
nullptr) {
686 std::string msg =
"* Error. Protocol not found.\r\n* Hello. You appear to be trying to communicate with a YARP Port.\r\n* The first 8 bytes sent to a YARP Port are critical for identifying the\r\n* protocol you wish to speak.\r\n* The first 8 bytes you sent were not associated with any particular protocol.\r\n* If you are a human, try typing \"CONNECT foo\" followed by a <RETURN>.\r\n* The 8 bytes \"CONNECT \" correspond to a simple text-mode protocol.\r\n* Goodbye.\r\n";
692 if (delegate ==
nullptr) {
704bool Protocol::expectSenderSpecifier()
711bool Protocol::sendHeader()
718bool Protocol::expectReplyToHeader()
725bool Protocol::respondToIndex()
std::string toString(const T &value)
convert an arbitrary type to string.
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.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
A mini-server for performing network communication in the background.
T * read(bool shouldWait=true) override
Read an available object from the port.
A simple abstraction for a block of bytes.
bool supportReply() const override=0
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
virtual bool write(ConnectionState &proto, SizedWriter &writer)=0
Write a message.
virtual bool configure(ConnectionState &proto)
Give carrier a shot at looking at how the connection is set up.
virtual bool respondToHeader(ConnectionState &proto)=0
Respond to the header.
bool requireAck() const override=0
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
bool isTextMode() const override=0
Check if carrier is textual in nature.
virtual bool expectSenderSpecifier(ConnectionState &proto)=0
Expect the name of the sending port.
virtual bool sendHeader(ConnectionState &proto)=0
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
virtual void close()
Close the carrier.
virtual void setParameters(const Bytes &header)=0
Configure this carrier based on the first 8 bytes of the connection.
virtual bool expectExtraHeader(ConnectionState &proto)=0
Receive any carrier-specific header.
virtual bool expectIndex(ConnectionState &proto)=0
Expect a message header, if there is one for this carrier.
virtual bool expectAck(ConnectionState &proto)=0
Receive an acknowledgement, if expected for this carrier.
bool modifiesIncomingData() const override
Check if this carrier modifies incoming data through the Carrier::modifyIncomingData method.
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...
virtual bool reply(ConnectionState &proto, SizedWriter &writer)
virtual bool expectReplyToHeader(ConnectionState &proto)=0
Process reply to header, if one is expected for this carrier.
virtual bool prepareSend(ConnectionState &proto)=0
Perform any initialization needed before writing on a connection.
virtual bool sendAck(ConnectionState &proto)=0
Send an acknowledgement, if needed for this carrier.
bool modifiesOutgoingData() const override
Check if this carrier modifies outgoing data through the Carrier::modifyOutgoingData method.
static Carrier * chooseCarrier(const std::string &name)
Select a carrier by name.
An interface for reading from a network connection.
OutputStream & os()
Shorthand for getOutputStream()
InputStream & is()
Shorthand for getInputStream()
A controller for an individual connection.
virtual bool isBareMode() const
Check if carrier excludes type information from payload.
virtual std::string getName() const =0
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
The output side of an active connection between two ports.
Simple specification of the minimum functions needed from output streams.
virtual void flush()
Make sure all pending write operations are finished.
virtual bool setWriteTimeout(double timeout)
Set activity timeout.
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
virtual void write(char ch)
Write a single byte to the stream.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
This is a base class for objects that can be both read from and be written to the YARP network.
Information about a connection between two ports.
const std::string & getCarrierName() const
Get the carrier type of the route.
void setToName(const std::string &toName)
Set the destination of the route.
const std::string & getFromName() const
Get the source of the route.
void setFromName(const std::string &fromName)
Set the source of the route.
void setCarrierName(const std::string &carrierName)
Set the carrier type of the route.
virtual void takeStream(TwoWayStream *stream)
Wrap the supplied stream.
bool isOk() const override
Check if the stream is ok or in an error state.
virtual TwoWayStream * giveStream()
Removes the wrapped stream and returns it.
InputStream & getInputStream() override
Get an InputStream to read from.
void close() override
Terminate the stream.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
Minimal requirements for an efficient Writer.
virtual void stopWrite() const =0
Call when all writing is finished.
virtual PortReader * getReplyHandler()=0
A stream which can be asked to perform bidirectional communication.
virtual void endPacket()=0
Mark the end of a logical packet (see beginPacket).
virtual void beginPacket()=0
Mark the beginning of a logical packet.
A single value (typically within a Bottle).
virtual Bottle * asList() const
Get list value.
std::string toString() const override
Return a standard text representation of the content of the object.
yarp::os::ConnectionReader & beginRead() override
Begin a read operation, with bytes read via the returned yarp::os::ConnectionReader object.
std::string getSenderSpecifier() const override
Extract a name for the sender, if the connection type supports that.
bool isOk() const override
Check if the connection is valid and can be used.
OutputStream & getOutputStream() override
Access the output stream associated with the connection.
TwoWayStream * giveStreams() override
Take ownership of the streams associated with the connection.
void attachPort(yarp::os::Contactable *port) override
Set the port to be associated with the connection.
void endRead() override
End the current read operation, begin by beginRead().
bool setTimeout(double timeout) override
Set the timeout to be used for network operations.
Contactable * getContactable() const override
Get the port associated with the connection.
void rename(const Route &route) override
Relabel the route after the fact (e.g.
bool open(const std::string &name) override
Start negotiating a carrier, using the given name as our own if a name is needed (this should general...
void suppressReply() override
Make sure that any attempt to send a reply to input will be denied.
bool checkStreams() const override
Check whether streams are in a good state.
Connection & getReceiver() override
It is possible to chain a basic connection with a modifier.
bool isReplying() const override
const std::string & getEnvelope() const override
Read the envelope associated with the current message.
void beginWrite() override
Notify connection that we intend to write to it.
TwoWayStream & getStreams() override
Access the streams associated with the connection.
~Protocol() override
Destructor.
void setEnvelope(const std::string &str) override
Set the envelope that will be attached to the next message.
bool write(SizedWriter &writer) override
Write a message on the connection.
void takeStreams(TwoWayStream *streams) override
Provide streams to be used with the connection.
void interrupt() override
InputStream & getInputStream() override
Access the input stream associated with the connection.
void setReference(yarp::os::Portable *ref) override
Give a direct pointer to an object being sent on the connection.
void setRemainingLength(int len) override
Tell the connection that the given number of bytes are left to be read.
Connection & getConnection() override
Get the connection whose protocol operations we are managing.
OutputProtocol & getOutput() override
Get an interface for doing write operations on the connection.
void close() override
Negotiate an end to operations.
Connection & getSender() override
It is possible to chain a basic connection with a modifier.
void reply(SizedWriter &writer) override
Reply to a message we have just read.
InputProtocol & getInput() override
Get an interface for doing read operations on the connection.
Protocol(TwoWayStream *stream)
Constructor.
const Route & getRoute() const override
void setRoute(const Route &route) override
Set the route associated with this connection.
yarp::os::Contact getRemoteContact() const override
Gets information about who is supplying the data being read, if that information is available.
void setProtocol(Protocol *protocol)
void flushWriter() override
void reset(yarp::os::InputStream &in, TwoWayStream *str, const Route &route, size_t len, bool textMode, bool bareMode=false)
virtual void setReference(yarp::os::Portable *obj)
#define yCInfo(component,...)
#define yCAssert(component, x)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.