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