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