YARP
Yet Another Robot Platform
NameserCarrier.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 
10 #include <yarp/os/Route.h>
11 #include <yarp/os/SizedWriter.h>
12 
13 #include <string>
14 
15 using namespace yarp::os;
16 using namespace yarp::os::impl;
17 
19  delegate(delegate),
20  pendingRead(""),
21  swallowRead("VER ")
22 {
23 }
24 
26 {
27  if (delegate != nullptr) {
28  delete delegate;
29  delegate = nullptr;
30  }
31 }
32 
34 {
35  return *this;
36 }
37 
39 {
40  return delegate->getOutputStream();
41 }
42 
44 {
45  return delegate->getLocalAddress();
46 }
47 
49 {
50  return delegate->getRemoteAddress();
51 }
52 
54 {
55  return delegate->isOk();
56 }
57 
59 {
60  delegate->reset();
61 }
62 
64 {
65  delegate->close();
66 }
67 
69 {
70  delegate->beginPacket();
71 }
72 
74 {
75  delegate->endPacket();
76 }
77 
79 {
80  // assume it is ok for name_ser to go byte-by-byte
81  // since this protocol will be phased out
82  if (b.length() <= 0) {
83  return 0;
84  }
85  Bytes tmp(b.get(), 1);
86  while (swallowRead.length() > 0) {
87  yarp::conf::ssize_t r = delegate->getInputStream().read(tmp);
88  if (r <= 0) {
89  return r;
90  }
91  swallowRead = swallowRead.substr(1, swallowRead.length() - 1);
92  }
93  if (pendingRead.length() > 0) {
94  b.get()[0] = pendingRead[0];
95  pendingRead = pendingRead.substr(1, pendingRead.length() - 1);
96  return 1;
97  }
98  yarp::conf::ssize_t r = delegate->getInputStream().read(tmp);
99  if (r <= 0) {
100  return r;
101  }
102  if (tmp.get()[0] == '\n') {
103  pendingRead = "";
104  swallowRead = "NAME_SERVER ";
105  }
106  return r;
107 }
108 
109 
111 {
112  firstSend = true;
113 }
114 
116 {
117  return "name_ser";
118 }
119 
121 {
122  return "NAME_SER";
123 }
124 
126 {
127  return new NameserCarrier();
128 }
129 
131 {
132  if (header.length() == 8) {
133  std::string target = getSpecifierName();
134  for (int i = 0; i < 8; i++) {
135  if (!(target[i] == header.get()[i])) {
136  return false;
137  }
138  }
139  return true;
140  }
141  return false;
142 }
143 
145 {
146  if (header.length() == 8) {
147  std::string target = getSpecifierName();
148  for (int i = 0; i < 8; i++) {
149  header.get()[i] = target[i];
150  }
151  }
152 }
153 
154 
156 {
157  return false;
158 }
159 
161 {
162  return true;
163 }
164 
166 {
167  return true;
168 }
169 
171 {
172  return false;
173 }
174 
176 {
177  std::string target = getSpecifierName();
178  yarp::os::Bytes b((char*)target.c_str(), 8);
179  proto.os().write(b);
180  proto.os().flush();
181  return proto.os().isOk();
182 }
183 
185 {
186  Route route = proto.getRoute();
187  route.setFromName("anon");
188  proto.setRoute(route);
189  return true;
190 }
191 
193 {
194  YARP_UNUSED(proto);
195  return true;
196 }
197 
199 {
200  YARP_UNUSED(proto);
201  return true;
202 }
203 
205 {
206  YARP_UNUSED(proto);
207  return true;
208 }
209 
211 {
212  // I am the receiver
213  auto* stream = new NameserTwoWayStream(proto.giveStreams());
214  proto.takeStreams(stream);
215  return true;
216 }
217 
219 {
220  YARP_UNUSED(proto);
221  // I am the sender
222  return true;
223 }
224 
226 {
227  std::string target = firstSend ? "VER " : "NAME_SERVER ";
228  Bytes b((char*)target.c_str(), target.length());
229  proto.os().write(b);
230  proto.os().flush();
231  std::string txt;
232  // ancient nameserver can't deal with quotes
233  for (size_t i = 0; i < writer.length(); i++) {
234  for (size_t j = 0; j < writer.length(i); j++) {
235  char ch = writer.data(i)[j];
236  if (ch != '\"') {
237  txt += ch;
238  }
239  }
240  }
241  Bytes b2((char*)txt.c_str(), txt.length());
242  proto.os().write(b2);
243  proto.os().flush();
244  firstSend = false;
245  return proto.os().isOk();
246 }
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
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:45
The basic state of a connection - route, streams in use, etc.
virtual const Route & getRoute() const =0
Get the route associated with this connection.
virtual TwoWayStream * giveStreams()=0
Take ownership of the streams associated with the connection.
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
OutputStream & os()
Shorthand for getOutputStream()
virtual void setRoute(const Route &route)=0
Set the route associated with this connection.
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:26
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:20
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:22
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:29
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:33
virtual size_t length() const =0
virtual const char * data(size_t index) const =0
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:26
Communicating between two ports via a variant plain-text protocol originally designed for the yarp na...
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Carrier * create() const override
Factory method.
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
bool expectSenderSpecifier(ConnectionState &proto) override
Expect the name of the sending port.
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 sendAck(ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
std::string getSpecifierName() const
bool expectAck(ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
bool isTextMode() const override
Check if carrier is textual in nature.
Communicating between two ports via a variant plain-text protocol originally designed for the yarp na...
void reset() override
Reset the stream.
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
InputStream & getInputStream() override
Get an InputStream to read from.
NameserTwoWayStream(TwoWayStream *delegate)
void endPacket() override
Mark the end of a logical packet (see beginPacket).
bool isOk() const override
Check if the stream is ok or in an error state.
void close() override
Terminate the stream.
void beginPacket() override
Mark the beginning of a logical packet.
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
::ssize_t ssize_t
Definition: numeric.h:86
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