YARP
Yet Another Robot Platform
TcpRosStream.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 TCPROSSTREAM_INC
10 #define TCPROSSTREAM_INC
11 
12 #include <yarp/os/InputStream.h>
13 #include <yarp/os/OutputStream.h>
14 #include <yarp/os/TwoWayStream.h>
15 #include <yarp/os/ManagedBytes.h>
16 
19 
20 #include <string>
21 #include <map>
22 
23 class TcpRosStream :
25  public yarp::os::InputStream,
27 {
28 private:
29  yarp::os::TwoWayStream *delegate;
30  int raw;
33  char *cursor;
34  int remaining;
35  int phase;
36  bool expectTwiddle;
38  std::string kind;
40  bool initiative;
41  bool setInitiative;
42 public:
43  TcpRosStream(TwoWayStream *delegate,
44  bool sender,
45  bool reply,
46  bool service,
47  int raw,
48  const char *kind) :
49  delegate(delegate),
50  raw(raw),
51  header(yarp::wire_rep_utils::BlobNetworkHeader{0,0,0}),
52  cursor(nullptr),
53  remaining(0),
54  phase(0),
55  expectTwiddle(service && sender),
56  kind(kind),
57  twiddlerReader(delegate->getInputStream(), twiddler),
58  initiative(false),
59  setInitiative(false)
60  {
61  updateKind(kind,sender,reply);
62  }
63 
64  virtual ~TcpRosStream() {
65  if (delegate!=NULL) {
66  delete delegate;
67  delegate = NULL;
68  }
69  }
70 
71  yarp::os::InputStream& getInputStream() override { return *this; }
72  yarp::os::OutputStream& getOutputStream() override { return *this; }
73 
74 
75  const yarp::os::Contact& getLocalAddress() const override {
76  return delegate->getLocalAddress();
77  }
78 
79  const yarp::os::Contact& getRemoteAddress() const override {
80  return delegate->getRemoteAddress();
81  }
82 
83  bool isOk() const override {
84  return delegate->isOk();
85  }
86 
87  void reset() override {
88  delegate->reset();
89  }
90 
91  void close() override {
92  delegate->close();
93  }
94 
95  void beginPacket() override {
96  twiddlerReader.reset();
97  delegate->beginPacket();
98  }
99 
100  void endPacket() override {
101  delegate->endPacket();
102  }
103 
105  void write(const yarp::os::Bytes& b) override;
106 
109 
110  void interrupt() override {
111  delegate->getInputStream().interrupt();
112  }
113 
114  void updateKind(const char *kind, bool sender, bool reply);
115 
116  static std::map<std::string, std::string> rosToKind();
117  static std::string rosToKind(const char *rosname);
118  static bool configureTwiddler(yarp::wire_rep_utils::WireTwiddler& twiddler, const char *txt, const char *prompt, bool sender, bool reply);
119 };
120 
121 #endif
yarp::os::TwoWayStream::getLocalAddress
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
yarp::os::TwoWayStream
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:29
WireTwiddler.h
TcpRosStream::reset
void reset() override
Reset the stream.
Definition: TcpRosStream.h:87
BlobNetworkHeader.h
TcpRosStream::TcpRosStream
TcpRosStream(TwoWayStream *delegate, bool sender, bool reply, bool service, int raw, const char *kind)
Definition: TcpRosStream.h:43
yarp::os::OutputStream::write
virtual void write(char ch)
Write a single byte to the stream.
Definition: OutputStream.cpp:17
TcpRosStream::rosToKind
static std::map< std::string, std::string > rosToKind()
yarp::os::OutputStream
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
yarp::os::InputStream::interrupt
virtual void interrupt()
Interrupt the stream.
Definition: InputStream.cpp:45
OutputStream.h
TcpRosStream::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: TcpRosStream.h:83
TcpRosStream::updateKind
void updateKind(const char *kind, bool sender, bool reply)
TcpRosStream::configureTwiddler
static bool configureTwiddler(yarp::wire_rep_utils::WireTwiddler &twiddler, const char *txt, const char *prompt, bool sender, bool reply)
yarp::os::TwoWayStream::reset
virtual void reset()=0
Reset the stream.
TwoWayStream.h
yarp::wire_rep_utils::BlobNetworkHeader
Definition: BlobNetworkHeader.h:22
TcpRosStream
Definition: TcpRosStream.h:27
TcpRosStream::getRemoteAddress
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: TcpRosStream.h:79
ManagedBytes.h
yarp::os::ManagedBytes
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:25
yarp::os::TwoWayStream::close
virtual void close()=0
Terminate the stream.
yarp::os::TwoWayStream::endPacket
virtual void endPacket()=0
Mark the end of a logical packet (see beginPacket).
TcpRosStream::beginPacket
void beginPacket() override
Mark the beginning of a logical packet.
Definition: TcpRosStream.h:95
yarp::conf::ssize_t
::ssize_t ssize_t
Definition: numeric.h:60
yarp::os::TwoWayStream::getRemoteAddress
virtual const Contact & getRemoteAddress() const =0
Get the address of the remote side of the stream.
TcpRosStream::write
void write(const yarp::os::Bytes &b) override
Write a block of bytes to the stream.
Definition: TcpRosStream.cpp:141
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
TcpRosStream::getLocalAddress
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: TcpRosStream.h:75
TcpRosStream::endPacket
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: TcpRosStream.h:100
TcpRosStream::getInputStream
yarp::os::InputStream & getInputStream() override
Get an InputStream to read from.
Definition: TcpRosStream.h:71
yarp::os::TwoWayStream::beginPacket
virtual void beginPacket()=0
Mark the beginning of a logical packet.
yarp::wire_rep_utils::WireTwiddler
Definition: WireTwiddler.h:72
yarp::os::InputStream::read
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
yarp::wire_rep_utils::WireTwiddlerReader::reset
void reset()
Definition: WireTwiddler.h:167
TcpRosStream::interrupt
void interrupt() override
Interrupt the stream.
Definition: TcpRosStream.h:110
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
TcpRosStream::close
void close() override
Terminate the stream.
Definition: TcpRosStream.h:91
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::TwoWayStream::isOk
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
InputStream.h
yarp::os::TwoWayStream::getInputStream
virtual InputStream & getInputStream()=0
Get an InputStream to read from.
yarp::os::InputStream
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
yarp::wire_rep_utils::WireTwiddlerReader
Definition: WireTwiddler.h:143
TcpRosStream::~TcpRosStream
virtual ~TcpRosStream()
Definition: TcpRosStream.h:64
TcpRosStream::rosToKind
static std::string rosToKind(const char *rosname)
TcpRosStream::getOutputStream
yarp::os::OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: TcpRosStream.h:72