YARP
Yet Another Robot Platform
XmlRpcCarrier.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_XMLRPC_CARRIER_XMLRPCCARRIER_H
10 #define YARP_XMLRPC_CARRIER_XMLRPCCARRIER_H
11 
12 #include <yarp/os/Carrier.h>
14 #include "XmlRpcStream.h"
15 
39  public yarp::os::Carrier
40 {
41 private:
42  bool firstRound;
43  bool sender;
44  yarp::os::Contact host;
45  std::string http;
46  bool interpretRos;
47 public:
49  firstRound(true),
50  sender(false),
51  interpretRos(false)
52  {
53  }
54 
55  Carrier *create() const override
56  {
57  return new XmlRpcCarrier();
58  }
59 
60  std::string getName() const override
61  {
62  return "xmlrpc";
63  }
64 
65  bool isConnectionless() const override
66  {
67  return false;
68  }
69 
70  bool canAccept() const override
71  {
72  return true;
73  }
74 
75  bool canOffer() const override
76  {
77  return true;
78  }
79 
80  bool isTextMode() const override
81  {
82  return true;
83  }
84 
85  bool canEscape() const override
86  {
87  return true;
88  }
89 
90  bool requireAck() const override
91  {
92  return false;
93  }
94 
95  bool supportReply() const override
96  {
97  return true;
98  }
99 
100  bool isLocal() const override
101  {
102  return false;
103  }
104 
105  std::string toString() const override
106  {
107  return "xmlrpc_carrier";
108  }
109 
110  void getHeader(yarp::os::Bytes& header) const override
111  {
112  const char *target = "POST /RP";
113  for (size_t i=0; i<8 && i<header.length(); i++) {
114  header.get()[i] = target[i];
115  }
116  }
117 
118  bool checkHeader(const yarp::os::Bytes& header) override
119  {
120  if (header.length()!=8) {
121  return false;
122  }
123  const char *target = "POST /";
124  for (int i=0; i<6; i++) {
125  if (header.get()[i] != target[i]) {
126  return false;
127  }
128  }
129  return true;
130  }
131 
132  void setParameters(const yarp::os::Bytes& header) override
133  {
134  // no parameters - no carrier variants
135  }
136 
137 
138  // Now, the initial hand-shaking
139 
141  {
142  // nothing special to do
143  return true;
144  }
145 
146  bool sendHeader(yarp::os::ConnectionState& proto) override;
147 
148  bool expectSenderSpecifier(yarp::os::ConnectionState& proto) override;
149 
151  {
152  // interpret any extra header information sent - optional
153  return true;
154  }
155 
156  bool respondToHeader(yarp::os::ConnectionState& proto) override;
157 
159  {
160  sender = true;
161  XmlRpcStream *stream = new XmlRpcStream(proto.giveStreams(),sender,
162  interpretRos);
163  if (stream == nullptr) {
164  return false;
165  }
166  proto.takeStreams(stream);
167  return true;
168  }
169 
170  bool isActive() const override
171  {
172  return true;
173  }
174 
175 
176  // Payload time!
177 
178  bool write(yarp::os::ConnectionState& proto, yarp::os::SizedWriter& writer) override;
179 
180  bool reply(yarp::os::ConnectionState& proto, yarp::os::SizedWriter& writer) override;
181 
183  {
184  return true;
185  }
186 
188  {
189  return true;
190  }
191 
192  bool sendAck(yarp::os::ConnectionState& proto) override
193  {
194  return true;
195  }
196 
197  bool expectAck(yarp::os::ConnectionState& proto) override
198  {
199  return true;
200  }
201 
202  std::string getBootstrapCarrierName() const override
203  {
204  return {};
205  }
206 
207 private:
208  bool shouldInterpretRosMessages(yarp::os::ConnectionState& proto);
209 };
210 
211 #endif // YARP_XMLRPC_CARRIER_XMLRPCCARRIER_H
XmlRpcCarrier::expectExtraHeader
bool expectExtraHeader(yarp::os::ConnectionState &proto) override
Receive any carrier-specific header.
Definition: XmlRpcCarrier.h:150
XmlRpcCarrier::respondToHeader
bool respondToHeader(yarp::os::ConnectionState &proto) override
Respond to the header.
Definition: XmlRpcCarrier.cpp:219
XmlRpcCarrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: XmlRpcCarrier.h:60
XmlRpcCarrier::create
Carrier * create() const override
Factory method.
Definition: XmlRpcCarrier.h:55
XmlRpcStream
Definition: XmlRpcStream.h:25
XmlRpcCarrier::getBootstrapCarrierName
std::string getBootstrapCarrierName() const override
Get the name of the carrier that should be used prior to handshaking, if a port is registered with th...
Definition: XmlRpcCarrier.h:202
XmlRpcCarrier::isConnectionless
bool isConnectionless() const override
Check if this carrier is connectionless (like udp, mcast) or connection based (like tcp).
Definition: XmlRpcCarrier.h:65
yarp::os::Carrier
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
XmlRpcCarrier::isTextMode
bool isTextMode() const override
Check if carrier is textual in nature.
Definition: XmlRpcCarrier.h:80
yarp::os::ConnectionState::giveStreams
virtual TwoWayStream * giveStreams()=0
Take ownership of the streams associated with the connection.
XmlRpcCarrier::expectSenderSpecifier
bool expectSenderSpecifier(yarp::os::ConnectionState &proto) override
Expect the name of the sending port.
Definition: XmlRpcCarrier.cpp:83
XmlRpcCarrier::setParameters
void setParameters(const yarp::os::Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: XmlRpcCarrier.h:132
XmlRpcCarrier::supportReply
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
Definition: XmlRpcCarrier.h:95
XmlRpcCarrier::canAccept
bool canAccept() const override
Check if reading is implemented for this carrier.
Definition: XmlRpcCarrier.h:70
XmlRpcCarrier::checkHeader
bool checkHeader(const yarp::os::Bytes &header) override
Given the first 8 bytes received on a connection, decide if this is the right carrier type to use for...
Definition: XmlRpcCarrier.h:118
XmlRpcCarrier::isActive
bool isActive() const override
Check if carrier is alive and error free.
Definition: XmlRpcCarrier.h:170
XmlRpcStream.h
yarp::os::ConnectionState::takeStreams
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
ConnectionState.h
yarp::os::Bytes::get
const char * get() const
Definition: Bytes.cpp:30
yarp::os::Bytes::length
size_t length() const
Definition: Bytes.cpp:25
XmlRpcCarrier::XmlRpcCarrier
XmlRpcCarrier()
Definition: XmlRpcCarrier.h:48
XmlRpcCarrier::expectReplyToHeader
bool expectReplyToHeader(yarp::os::ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: XmlRpcCarrier.h:158
XmlRpcCarrier::expectIndex
bool expectIndex(yarp::os::ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
Definition: XmlRpcCarrier.h:187
XmlRpcCarrier::toString
std::string toString() const override
Get name of carrier.
Definition: XmlRpcCarrier.h:105
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
XmlRpcCarrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: XmlRpcCarrier.h:90
XmlRpcCarrier
This carrier enables XML/RPC message transmission.
Definition: XmlRpcCarrier.h:40
XmlRpcCarrier::sendAck
bool sendAck(yarp::os::ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
Definition: XmlRpcCarrier.h:192
yarp::os::ConnectionState
The basic state of a connection - route, streams in use, etc.
Definition: ConnectionState.h:31
XmlRpcCarrier::canEscape
bool canEscape() const override
Check if carrier can encode administrative messages, as opposed to just user data.
Definition: XmlRpcCarrier.h:85
XmlRpcCarrier::write
bool write(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Write a message.
Definition: XmlRpcCarrier.cpp:91
Carrier.h
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
XmlRpcCarrier::reply
bool reply(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer) override
Definition: XmlRpcCarrier.cpp:158
XmlRpcCarrier::prepareSend
bool prepareSend(yarp::os::ConnectionState &proto) override
Perform any initialization needed before writing on a connection.
Definition: XmlRpcCarrier.h:140
XmlRpcCarrier::expectAck
bool expectAck(yarp::os::ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
Definition: XmlRpcCarrier.h:197
XmlRpcCarrier::sendHeader
bool sendHeader(yarp::os::ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
Definition: XmlRpcCarrier.cpp:199
XmlRpcCarrier::isLocal
bool isLocal() const override
Check if carrier operates within a single process.
Definition: XmlRpcCarrier.h:100
XmlRpcCarrier::sendIndex
virtual bool sendIndex(yarp::os::ConnectionState &proto, yarp::os::SizedWriter &writer)
Definition: XmlRpcCarrier.h:182
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
XmlRpcCarrier::canOffer
bool canOffer() const override
Check if writing is implemented for this carrier.
Definition: XmlRpcCarrier.h:75
XmlRpcCarrier::getHeader
void getHeader(yarp::os::Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Definition: XmlRpcCarrier.h:110