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