YARP
Yet Another Robot Platform
HumanCarrier.h
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 
10 #include <yarp/os/Carrier.h>
11 #include <yarp/os/Route.h>
12 #include <yarp/os/SizedWriter.h>
13 #include "HumanStream.h"
14 
15 class HumanCarrier : public Carrier {
16 public:
17 
18  // First, the easy bits...
19 
20  Carrier *create() const override {
21  return new HumanCarrier();
22  }
23 
24  std::string getName() const override {
25  return "human";
26  }
27 
28  bool isConnectionless() const override {
29  return true;
30  }
31 
32  bool canAccept() const override {
33  return true;
34  }
35 
36  bool canOffer() const override {
37  return true;
38  }
39 
40  bool isTextMode() const override {
41  // let's be text mode, for human-friendliness
42  return true;
43  }
44 
45  bool canEscape() const override {
46  return true;
47  }
48 
49  bool requireAck() const override {
50  return true;
51  }
52 
53  bool supportReply() const override {
54  return true;
55  }
56 
57  bool isLocal() const override {
58  return false;
59  }
60 
61  std::string toString() const override {
62  return "humans are handy";
63  }
64 
65  void getHeader(Bytes& header) const override {
66  const char *target = "HUMANITY";
67  for (size_t i=0; i<8 && i<header.length(); i++) {
68  header.get()[i] = target[i];
69  }
70  }
71 
72  bool checkHeader(const Bytes& header) override {
73  if (header.length()!=8) {
74  return false;
75  }
76  const char *target = "HUMANITY";
77  for (size_t i=0; i<8; i++) {
78  if (header.get()[i] != target[i]) {
79  return false;
80  }
81  }
82  return true;
83  }
84 
85  void setParameters(const Bytes& header) override {
86  // no parameters - no carrier variants
87  }
88 
89 
90  // Now, the initial hand-shaking
91 
92  bool prepareSend(ConnectionState& proto) override {
93  // nothing special to do
94  return true;
95  }
96 
97  bool sendHeader(ConnectionState& proto) override;
98 
99  bool expectSenderSpecifier(ConnectionState& proto) override {
100  // interpret everything that sendHeader wrote
101  Route route = proto.getRoute();
102  route.setFromName(proto.is().readLine());
103  proto.setRoute(route);
104  return proto.is().isOk();
105  }
106 
107  bool expectExtraHeader(ConnectionState& proto) override {
108  // interpret any extra header information sent - optional
109  return true;
110  }
111 
112  bool respondToHeader(ConnectionState& proto) override {
113  // SWITCH TO NEW STREAM TYPE
114  HumanStream *stream = new HumanStream();
115  if (stream==NULL) { return false; }
116  proto.takeStreams(stream);
117  return true;
118  }
119 
120  bool expectReplyToHeader(ConnectionState& proto) override {
121  // SWITCH TO NEW STREAM TYPE
122  HumanStream *stream = new HumanStream();
123  if (stream==NULL) { return false; }
124  proto.takeStreams(stream);
125  return true;
126  }
127 
128  bool isActive() const override {
129  return true;
130  }
131 
132 
133  // Payload time!
134 
135  bool write(ConnectionState& proto, SizedWriter& writer) override {
136  bool ok = sendIndex(proto,writer);
137  if (!ok) return false;
138  writer.write(proto.os());
139  return proto.os().isOk();
140  }
141 
142  virtual bool sendIndex(ConnectionState& proto,SizedWriter& writer) {
143  std::string prefix = "human says ";
144  Bytes b2((char*)prefix.c_str(),prefix.length());
145  proto.os().write(b2);
146  return true;
147  }
148 
149  bool expectIndex(ConnectionState& proto) override {
150  std::string prefix = "human says ";
151  std::string compare = prefix;
152  Bytes b2((char*)prefix.c_str(),prefix.length());
153  proto.is().read(b2);
154  bool ok = proto.is().isOk() && (prefix==compare);
155  if (!ok) std::cout << "YOU DID NOT SAY 'human says '" << std::endl;
156  return ok;
157  }
158 
159  // Acknowledgements, we don't do them
160 
161  bool sendAck(ConnectionState& proto) override {
162  std::string prefix = "computers rule!\r\n";
163  Bytes b2((char*)prefix.c_str(),prefix.length());
164  proto.os().write(b2);
165  return true;
166  }
167 
168  bool expectAck(ConnectionState& proto) override {
169  std::string prefix = "computers rule!\r\n";
170  std::string compare = prefix;
171  Bytes b2((char*)prefix.c_str(),prefix.length());
172  proto.is().read(b2);
173  bool ok = proto.is().isOk() && (prefix==compare);
174  if (!ok) std::cout << "YOU DID NOT SAY 'computers rule!'" << std::endl;
175  return ok;
176  }
177 
178 };
HumanCarrier::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: HumanCarrier.h:72
HumanCarrier::sendAck
bool sendAck(ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
Definition: HumanCarrier.h:161
HumanCarrier::canAccept
bool canAccept() const override
Check if reading is implemented for this carrier.
Definition: HumanCarrier.h:32
HumanCarrier::expectSenderSpecifier
bool expectSenderSpecifier(ConnectionState &proto) override
Expect the name of the sending port.
Definition: HumanCarrier.h:99
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
HumanStream.h
HumanCarrier
Definition: HumanCarrier.h:15
HumanCarrier::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: HumanCarrier.h:65
yarp::os::SizedWriter::write
virtual void write(OutputStream &os)
Definition: SizedWriter.cpp:19
yarp::os::Route
Information about a connection between two ports.
Definition: Route.h:32
HumanCarrier::sendIndex
virtual bool sendIndex(ConnectionState &proto, SizedWriter &writer)
Definition: HumanCarrier.h:142
HumanCarrier::canEscape
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
Definition: HumanCarrier.h:45
HumanCarrier::write
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
Definition: HumanCarrier.h:135
HumanCarrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: HumanCarrier.h:49
HumanCarrier::setParameters
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: HumanCarrier.h:85
yarp::os::ConnectionState::takeStreams
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
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.
HumanStream
Definition: HumanStream.h:26
Route.h
HumanCarrier::create
Carrier * create() const override
Factory method.
Definition: HumanCarrier.h:20
HumanCarrier::supportReply
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
Definition: HumanCarrier.h:53
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.
HumanCarrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: HumanCarrier.h:24
yarp::os::Bytes::length
size_t length() const
Definition: Bytes.cpp:25
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
HumanCarrier::isConnectionless
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: HumanCarrier.h:28
HumanCarrier::expectIndex
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
Definition: HumanCarrier.h:149
yarp::os::InputStream::isOk
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
HumanCarrier::expectExtraHeader
bool expectExtraHeader(ConnectionState &proto) override
Receive any carrier-specific header.
Definition: HumanCarrier.h:107
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.
HumanCarrier::isActive
bool isActive() const override
Check if carrier is alive and error free.
Definition: HumanCarrier.h:128
Carrier.h
yarp::os::InputStream::read
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
HumanCarrier::respondToHeader
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
Definition: HumanCarrier.h:112
yarp::os::Route::setFromName
void setFromName(const std::string &fromName)
Set the source of the route.
Definition: Route.cpp:101
HumanCarrier::sendHeader
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
Definition: HumanCarrier.cpp:13
yarp::os::ConnectionState::is
InputStream & is()
Shorthand for getInputStream()
Definition: ConnectionState.h:125
HumanCarrier::isLocal
bool isLocal() const override
Check if carrier operates within a single process.
Definition: HumanCarrier.h:57
HumanCarrier::canOffer
bool canOffer() const override
Check if writing is implemented for this carrier.
Definition: HumanCarrier.h:36
HumanCarrier::toString
std::string toString() const override
Get name of carrier.
Definition: HumanCarrier.h:61
HumanCarrier::isTextMode
bool isTextMode() const override
Check if carrier is textual in nature.
Definition: HumanCarrier.h:40
SizedWriter.h
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
HumanCarrier::expectReplyToHeader
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: HumanCarrier.h:120
HumanCarrier::expectAck
bool expectAck(ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
Definition: HumanCarrier.h:168
HumanCarrier::prepareSend
bool prepareSend(ConnectionState &proto) override
Perform any initialization needed before writing on a connection.
Definition: HumanCarrier.h:92