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;
164 size_t start = carrier.find(
'+');
165 if (start != std::string::npos) {
167 for (
size_t i = start + 1; i < (
size_t)carrier.length(); i++) {
168 char ch = carrier[i];
171 }
else if (ch ==
'.') {
197 if (delegate ==
nullptr) {
198 return nullConnection;
220 bool ok = expectHeader();
225 return respondToHeader();
233 if (delegate ==
nullptr) {
238 bool ok = sendHeader();
243 return expectReplyToHeader();
288 return !(!
checkStreams() || recv_delegate_fail || send_delegate_fail);
301 this->writer = &writer;
305 bool ok = delegate->
write(*
this, writer);
308 if (
reply !=
nullptr) {
320 this->writer =
nullptr;
328 delegate->
reply(*
this, writer);
329 pendingReply =
false;
351 if (delegate !=
nullptr) {
400 if (recv_delegate ==
nullptr) {
401 return nullConnection;
403 return *recv_delegate;
427 if (send_delegate ==
nullptr) {
428 return nullConnection;
430 return *send_delegate;
434bool Protocol::getRecvDelegate()
437 if (recv_delegate !=
nullptr) {
440 if (!need_recv_delegate) {
443 if (recv_delegate_fail) {
448 std::string tag = b.find(
"recv").asString();
450 if (recv_delegate ==
nullptr) {
451 fprintf(
stderr,
"Need carrier \"%s\", but cannot find it.\n", tag.c_str());
452 recv_delegate_fail =
true;
457 fprintf(
stderr,
"Carrier \"%s\" does not modify incoming data as expected.\n", tag.c_str());
458 recv_delegate_fail =
true;
464 fprintf(
stderr,
"Carrier \"%s\" could not configure the send delegate.\n", tag.c_str());
465 recv_delegate_fail =
true;
473bool Protocol::getSendDelegate()
476 if (send_delegate !=
nullptr) {
479 if (!need_send_delegate) {
482 if (send_delegate_fail) {
487 std::string tag = b.find(
"send").asString();
489 if (send_delegate ==
nullptr) {
490 fprintf(
stderr,
"Need carrier \"%s\", but cannot find it.\n", tag.c_str());
491 send_delegate_fail =
true;
496 fprintf(
stderr,
"Carrier \"%s\" does not modify outgoing data as expected.\n", tag.c_str());
497 send_delegate_fail =
true;
503 fprintf(
stderr,
"Carrier \"%s\" could not configure the send delegate.\n", tag.c_str());
504 send_delegate_fail =
true;
512bool Protocol::respondToHeader()
524bool Protocol::expectAck()
534void Protocol::closeHelper()
541 if (delegate !=
nullptr) {
546 if (recv_delegate !=
nullptr) {
547 recv_delegate->
close();
548 delete recv_delegate;
549 recv_delegate =
nullptr;
551 if (send_delegate !=
nullptr) {
552 send_delegate->
close();
553 delete send_delegate;
554 send_delegate =
nullptr;
559bool Protocol::sendAck()
563 if (delegate ==
nullptr) {
575bool Protocol::expectIndex()
588 if (delegate !=
nullptr) {
598 if (ref !=
nullptr) {
619 if (delegate ==
nullptr) {
621 if (delegate !=
nullptr) {
624 fprintf(
stderr,
"Carrier \"%s\" cannot be used this way, try \"tcp+recv.%s\" instead.\n", carrierName.c_str(), carrierName.c_str());
631 fprintf(
stderr,
"Carrier \"%s\" could not be configured.\n", carrierName.c_str());
641bool Protocol::expectHeader()
650 bool ok = expectProtocolSpecifier();
654 ok = expectSenderSpecifier();
664bool Protocol::expectProtocolSpecifier()
677 if ((
size_t)len != header.length()) {
682 if (delegate !=
nullptr) {
689 if (delegate ==
nullptr) {
691 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";
697 if (delegate ==
nullptr) {
709bool Protocol::expectSenderSpecifier()
716bool Protocol::sendHeader()
723bool Protocol::expectReplyToHeader()
730bool 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.
void interruptInputStream()
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.