YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeTwoWayStream.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#ifndef YARP_OS_IMPL_FAKETWOWAYSTREAM_H
8#define YARP_OS_IMPL_FAKETWOWAYSTREAM_H
9
13
14namespace yarp::os::impl {
15
20 public TwoWayStream
21{
22public:
25 {
26 this->out.owner = this;
27 this->target = target;
28 }
29
31 {
32 this->target = ⌖
33 }
34
36 {
37 return in;
38 }
39
41 {
42 return in;
43 }
44
46 {
47 return out;
48 }
49
50 const Contact& getLocalAddress() const override
51 {
52 return local;
53 }
54
55 const Contact& getRemoteAddress() const override
56 {
57 return remote;
58 }
59
60 void close() override
61 {
62 in.close();
63 out.close();
64 }
65
66 virtual void apply(const Bytes& b)
67 {
68 if (target != nullptr) {
69 target->add(b);
70 }
71 }
72
73 void addInputText(const std::string& str)
74 {
75 in.add(str);
76 }
77
78 std::string getOutputText() const
79 {
80 return out.toString();
81 }
82
83 std::string getInputText() const
84 {
85 return in.toString();
86 }
87
88 bool isOk() const override
89 {
90 return true;
91 }
92
93 void reset() override
94 {
95 }
96
97 void beginPacket() override
98 {
99 }
100
101 void endPacket() override
102 {
103 }
104
105private:
106 class ActiveStringOutputStream : public StringOutputStream
107 {
108 public:
109 ActiveStringOutputStream() :
110 owner(nullptr)
111 {
112 }
113
115 void write(const Bytes& b) override
116 {
118 if (owner) {
119 owner->apply(b);
120 }
121 }
122
123 FakeTwoWayStream* owner;
124 };
125
126 StringInputStream in;
127 ActiveStringOutputStream out;
128 Contact local;
129 Contact remote;
130 StringInputStream* target;
131};
132
133} // namespace yarp::os::impl
134
135#endif // YARP_OS_IMPL_FAKETWOWAYSTREAM_H
A simple abstraction for a block of bytes.
Definition Bytes.h:24
Represents how to reach a part of a YARP network.
Definition Contact.h:33
Simple specification of the minimum functions needed from input streams.
Definition InputStream.h:25
Simple specification of the minimum functions needed from output streams.
virtual void write(char ch)
Write a single byte to the stream.
An InputStream that reads from a string.
void add(const std::string &txt)
virtual std::string toString() const
void close() override
Terminate the stream.
An OutputStream that produces a string.
void write(const Bytes &b) override
Write a block of bytes to the stream.
A stream which can be asked to perform bidirectional communication.
A dummy two way stream for testing purposes.
void beginPacket() override
Mark the beginning of a logical packet.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
void close() override
Terminate the stream.
virtual void apply(const Bytes &b)
FakeTwoWayStream(StringInputStream *target=nullptr)
bool isOk() const override
Check if the stream is ok or in an error state.
void setTarget(StringInputStream &target)
InputStream & getInputStream() override
Get an InputStream to read from.
virtual StringInputStream & getStringInputStream()
void addInputText(const std::string &str)
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
void reset() override
Reset the stream.
void endPacket() override
Mark the end of a logical packet (see beginPacket).
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
The components from which ports and connections are built.