YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
TextCarrier.cpp
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
8
11#include <yarp/os/Route.h>
13
14using namespace yarp::os;
15using namespace yarp::os::impl;
16
17namespace {
18YARP_OS_LOG_COMPONENT(TEXTCARRIER, "yarp.os.impl.TextCarrier")
19} // namespace
20
22{
23 this->ackVariant = ackVariant;
24}
25
27{
28 if (ackVariant) {
29 return "text_ack";
30 }
31 return "text";
32}
33
35{
36 if (ackVariant) {
37 return "CONNACK ";
38 }
39 return "CONNECT ";
40}
41
43{
44 if (header.length() == 8) {
45 std::string target = getSpecifierName();
46 for (int i = 0; i < 8; i++) {
47 if (!(target[i] == header.get()[i])) {
48 return false;
49 }
50 }
51 return true;
52 }
53 return false;
54}
55
57{
58 if (header.length() == 8) {
59 std::string target = getSpecifierName();
60 for (int i = 0; i < 8; i++) {
61 header.get()[i] = target[i];
62 }
63 }
64}
65
67{
68 return new TextCarrier(ackVariant);
69}
70
72{
73 return ackVariant;
74}
75
77{
78 return true;
79}
80
81
83{
84 return requireAck();
85}
86
88{
89 std::string target = getSpecifierName();
90 yarp::os::Bytes b((char*)target.c_str(), 8);
91 proto.os().write(b);
92 std::string from = proto.getSenderSpecifier();
93 yarp::os::Bytes b2((char*)from.c_str(), from.length());
94 proto.os().write(b2);
95 proto.os().write('\r');
96 proto.os().write('\n');
97 proto.os().flush();
98 return proto.os().isOk();
99}
100
102{
103 if (ackVariant) {
104 // expect and ignore welcome line
105 std::string result = proto.is().readLine();
106 }
107 return true;
108}
109
111{
112 yCDebug(TEXTCARRIER, "TextCarrier::expectSenderSpecifier");
113 Route route = proto.getRoute();
114 route.setFromName(proto.is().readLine());
115 proto.setRoute(route);
116 return true;
117}
118
120{
121 YARP_UNUSED(proto);
122 YARP_UNUSED(writer);
123 return true;
124}
125
127{
128 YARP_UNUSED(proto);
129 return true;
130}
131
133{
134 if (ackVariant) {
135 std::string from = "<ACK>\r\n";
136 Bytes b2((char*)from.c_str(), from.length());
137 proto.os().write(b2);
138 proto.os().flush();
139 }
140 return proto.os().isOk();
141}
142
144{
145 if (ackVariant) {
146 // expect and ignore acknowledgement
147 std::string result = proto.is().readLine();
148 }
149 return true;
150}
151
153{
154 std::string from = "Welcome ";
155 from += proto.getRoute().getFromName();
156 from += "\r\n";
157 yarp::os::Bytes b2((char*)from.c_str(), from.length());
158 proto.os().write(b2);
159 proto.os().flush();
160 return proto.os().isOk();
161}
A mini-server for performing network communication in the background.
A simple abstraction for a block of bytes.
Definition Bytes.h:24
size_t length() const
Definition Bytes.cpp:22
const char * get() const
Definition Bytes.cpp:27
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition Carrier.h:44
The basic state of a connection - route, streams in use, etc.
OutputStream & os()
Shorthand for getOutputStream()
virtual const Route & getRoute() const =0
Get the route associated with this connection.
virtual std::string getSenderSpecifier() const =0
Extract a name for the sender, if the connection type supports that.
InputStream & is()
Shorthand for getInputStream()
virtual void setRoute(const Route &route)=0
Set the route associated with this connection.
std::string readLine(const char terminal='\n', bool *success=nullptr)
Read a block of text terminated with a specific marker (or EOF).
virtual void flush()
Make sure all pending write operations are finished.
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.
Information about a connection between two ports.
Definition Route.h:28
void setFromName(const std::string &fromName)
Set the source of the route.
Definition Route.cpp:98
Minimal requirements for an efficient Writer.
Definition SizedWriter.h:32
Communicating between two ports via a plain-text protocol.
Definition TextCarrier.h:18
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
bool sendAck(ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
bool expectAck(ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
bool sendIndex(ConnectionState &proto, SizedWriter &writer) override
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
bool isTextMode() const override
Check if carrier is textual in nature.
Carrier * create() const override
Factory method.
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
bool checkHeader(const Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
bool expectSenderSpecifier(ConnectionState &proto) override
Expect the name of the sending port.
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
TextCarrier(bool ackVariant=false)
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
virtual std::string getSpecifierName() const
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
#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.
#define YARP_UNUSED(var)
Definition api.h:162